1 /* Temp_* classes declarations.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_Temp_defs_hh
25 #define PPL_Temp_defs_hh 1
26 
27 #include "meta_programming.hh"
28 #include "Slow_Copy.hh"
29 
30 namespace Parma_Polyhedra_Library {
31 
32 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
33 //! A pool of temporary items of type \p T.
34 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
35 template <typename T>
36 class Temp_Item {
37 public:
38   //! Obtains a reference to a temporary item.
39   static Temp_Item& obtain();
40 
41   //! Releases the temporary item \p p.
42   static void release(Temp_Item& p);
43 
44   //! Returns a reference to the encapsulated item.
45   T& item();
46 
47 private:
48   //! The encapsulated item.
49   T item_;
50 
51   //! Pointer to the next item in the free list.
52   Temp_Item* next;
53 
54   class Free_List {
55   public:
56     Free_List();
57     ~Free_List();
58     Temp_Item* head_ptr;
59   private:
60     Free_List(const Free_List&); // Not implemented.
61     Free_List& operator=(const Free_List&); // Not implemented.
62   }; // class Free_List
63 
64   friend class Free_List;
65 
66   //! Head of the free list.
67   static Temp_Item*& free_list_ref();
68 
69   //! Default constructor.
70   Temp_Item();
71 
72   //! Copy constructor: private and intentionally not implemented.
73   Temp_Item(const Temp_Item&);
74 
75   //! Assignment operator: private and intentionally not implemented.
76   Temp_Item& operator=(const Temp_Item&);
77 };
78 
79 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
80 //! An holder for a reference to a temporary object.
81 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
82 template <typename T>
83 class Temp_Reference_Holder {
84 public:
85   //! Constructs an holder holding a dirty temp.
86   Temp_Reference_Holder();
87 
88   //! Destructor.
89   ~Temp_Reference_Holder();
90 
91   //! Returns a reference to the held item.
92   T& item();
93 
94 private:
95   //! Copy constructor: private and intentionally not implemented.
96   Temp_Reference_Holder(const Temp_Reference_Holder&);
97 
98   //! Assignment operator: private and intentionally not implemented.
99   Temp_Reference_Holder& operator=(const Temp_Reference_Holder&);
100 
101   //! The held item, encapsulated.
102   Temp_Item<T>& held;
103 };
104 
105 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
106 //! An (fake) holder for the value of a temporary object.
107 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
108 template <typename T>
109 class Temp_Value_Holder {
110 public:
111   //! Constructs a fake holder.
112   Temp_Value_Holder();
113 
114   //! Returns the value of the held item.
115   T& item();
116 
117 private:
118   //! Copy constructor: private and intentionally not implemented.
119   Temp_Value_Holder(const Temp_Value_Holder&);
120 
121   //! Assignment operator: private and intentionally not implemented.
122   Temp_Value_Holder& operator=(const Temp_Value_Holder&);
123 
124   //! The held item.
125   T item_;
126 };
127 
128 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
129 //! A structure for the efficient handling of temporaries.
130 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
131 template <typename T, typename Enable = void>
132 class Dirty_Temp;
133 
134 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
135 //! Specialization for the handling of temporaries with a free list.
136 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
137 template <typename T>
138 class Dirty_Temp<T, typename Enable_If<Slow_Copy<T>::value>::type>
139   : public Temp_Reference_Holder<T> {
140 };
141 
142 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
143 //! Specialization for the handling of temporaries with local variables.
144 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
145 template <typename T>
146 class Dirty_Temp<T, typename Enable_If<!Slow_Copy<T>::value>::type>
147   : public Temp_Value_Holder<T> {
148 };
149 
150 } // namespace Parma_Polyhedra_Library
151 
152 #include "Temp_inlines.hh"
153 #include "Temp_templates.hh"
154 
155 #endif // !defined(PPL_Temp_defs_hh)
156