tmp.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::tmp
29 
30 Description
31  A class for managing temporary objects.
32 
33  This is a combination of std::shared_ptr (with intrusive ref-counting)
34  and a shared_ptr without ref-counting and null deleter.
35  This allows the tmp to double as a pointer management and an indirect
36  pointer to externally allocated objects.
37 
38 SourceFiles
39  tmpI.H
40 
41 See also
42  Foam::autoPtr
43  Foam::refCount
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef tmp_H
48 #define tmp_H
49 
50 #include "refCount.H"
51 #include "word.H"
52 #include <utility>
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 /*---------------------------------------------------------------------------*\
60  Class tmp Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class T>
64 class tmp
65 {
66  // Private Data
67 
68  //- Object types
69  enum refType
70  {
71  PTR,
72  CREF
73  };
74 
75  //- The managed pointer or the address of const-reference object
76  mutable T* ptr_;
77 
78  //- The type (managed pointer | const-reference object)
79  mutable refType type_;
80 
81 
82  // Private Member Operators
83 
84  //- Increment the ref-count for a managed pointer
85  inline void operator++();
86 
87 
88 public:
89 
90  // STL type definitions
91 
92  //- Type of object being managed or referenced
93  typedef T element_type;
94 
95  //- Pointer to type of object being managed or referenced
96  typedef T* pointer;
97 
98 
99  //- Reference counter class
100  typedef Foam::refCount refCount;
101 
102 
103  // Factory Methods
104 
105  //- Construct tmp of T with forwarding arguments
106  // \param args list of arguments with which an instance of T
107  // will be constructed.
108  //
109  // \note Similar to std::make_shared, but the overload for
110  // array types is not disabled.
111  template<class... Args>
112  inline static tmp<T> New(Args&&... args);
113 
114  //- Construct tmp from derived type with forwarding arguments
115  // \param args list of arguments with which an instance of U
116  // will be constructed.
117  //
118  // \note Similar to New but for derived types
119  template<class U, class... Args>
120  inline static tmp<T> NewFrom(Args&&... args);
121 
122 
123  // Constructors
124 
125  //- Construct with no managed pointer.
126  inline constexpr tmp() noexcept;
127 
128  //- Construct with no managed pointer.
129  inline constexpr tmp(std::nullptr_t) noexcept;
130 
131  //- Construct, taking ownership of the pointer.
132  inline explicit tmp(T* p);
133 
134  //- Construct for a const reference to an object.
135  inline tmp(const T& obj) noexcept;
136 
137  //- Move construct, transferring ownership.
138  // Does not affect ref-count
139  inline tmp(tmp<T>&& t) noexcept;
140 
141  //- Move construct, transferring ownership.
142  // Does not affect ref-count
143  // \note Non-standard definition - should be non-const
144  inline tmp(const tmp<T>&& t) noexcept;
145 
146  //- Copy construct, incrementing ref-count of managed pointer.
147  // \note Non-standard definition - should be non-const
148  inline tmp(const tmp<T>& t);
149 
150  //- Copy construct. Optionally reusing ref-counted pointer.
151  inline tmp(const tmp<T>& t, bool reuse);
152 
153 
154  //- Destructor: deletes managed pointer when the ref-count is 0
155  inline ~tmp();
156 
157 
158  // Member Functions
159 
160  // Query
161 
162  //- True if this is a managed pointer (not a const reference)
163  inline bool isTmp() const noexcept;
164 
165  //- True if this is a non-null managed pointer
166  inline bool empty() const noexcept;
167 
168  //- True if this is a non-null managed pointer,
169  //- or is a const object reference
170  inline bool valid() const noexcept;
171 
172  //- True if this is a non-null managed pointer with a unique ref-count
173  inline bool movable() const noexcept;
174 
175  //- Return type-name of the tmp, constructed from type-name of T
176  inline word typeName() const;
177 
178 
179  // Access
180 
181  //- Return pointer without nullptr checking.
182  inline T* get() noexcept;
183 
184  //- Return const pointer without nullptr checking.
185  inline const T* get() const noexcept;
186 
187  //- Return the const object reference or a const reference to the
188  //- contents of a non-null managed pointer.
189  // Fatal for a null managed pointer
190  inline const T& cref() const;
191 
192  //- Return non-const reference to the contents of a non-null
193  //- managed pointer.
194  // Fatal for a null managed pointer or if the object is const.
195  inline T& ref() const;
196 
197  //- Non-const dereference, even if the object is const.
198  // This is similar to ref(), but applies a const_cast to access
199  // const objects.
200  // Fatal for a null managed pointer.
201  inline T& constCast() const;
202 
203 
204  // Edit
205 
206  //- Return managed pointer for reuse, or clone() the const object
207  //- reference.
208  inline T* ptr() const;
209 
210  //- If object pointer points to valid object:
211  //- delete object and set pointer to nullptr
212  inline void clear() const noexcept;
213 
214  //- Release ownership of managed temporary object.
215  // After this call no object is managed.
216  inline void reset() noexcept;
217 
218  //- Delete managed temporary object and set to new given pointer
219  inline void reset(T* p) noexcept;
220 
221  //- Clear existing and transfer ownership.
222  inline void reset(tmp<T>&& other) noexcept;
223 
224  //- Delete managed temporary object and set to const reference
225  inline void cref(const T& obj) noexcept;
226 
227  //- Swaps the managed object with other.
228  inline void swap(tmp<T>& other) noexcept;
229 
230 
231  // Member Operators
232 
233  //- Return const reference to the object.
234  // Identical to cref() method.
235  inline const T& operator()() const;
236 
237  //- Cast to underlying data type, using the cref() method.
238  inline operator const T&() const;
239 
240  //- Dereferences (const) pointer to the managed object.
241  // Fatal for a null managed pointer.
242  inline const T* operator->() const;
243 
244  //- Dereferences (non-const) pointer to the managed object.
245  // Fatal for a null managed pointer or if the object is const.
246  inline T* operator->();
247 
248  //- Is non-null managed pointer or const object reference : valid()
249  explicit inline operator bool() const noexcept;
250 
251  //- Take ownership of the pointer.
252  // Fatal for a null pointer, or when the pointer is non-unique.
253  inline void operator=(T* p);
254 
255  //- Transfer ownership of the managed pointer.
256  // Fatal for a null managed pointer or if the object is const.
257  inline void operator=(const tmp<T>& t);
258 
259  //- Clear existing and transfer ownership.
260  inline void operator=(tmp<T>&& other) noexcept;
261 
262 
263  // Housekeeping
264 
265  //- No assignment from literal nullptr.
266  // Consistent with run-time check for nullptr on assignment.
267  void operator=(std::nullptr_t) = delete;
268 };
269 
270 
271 // Global Functions
272 
273 //- Specializes the Swap algorithm for tmp.
274 // Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
275 template<class T>
276 void Swap(tmp<T>& lhs, tmp<T>& rhs)
277 {
278  lhs.swap(rhs);
279 }
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace Foam
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 
288 #include "tmpI.H"
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 
292 #endif
293 
294 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::tmp::constCast
T & constCast() const
Non-const dereference, even if the object is const.
Definition: tmpI.H:282
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmp::clear
void clear() const noexcept
Definition: tmpI.H:325
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::refCount
Reference counter for various OpenFOAM components.
Definition: refCount.H:50
Foam::tmp::empty
bool empty() const noexcept
True if this is a non-null managed pointer.
Definition: tmpI.H:199
Foam::tmp::element_type
T element_type
Type of object being managed or referenced.
Definition: tmp.H:92
Foam::tmp::isTmp
bool isTmp() const noexcept
True if this is a managed pointer (not a const reference)
Definition: tmpI.H:192
Foam::tmp::NewFrom
static tmp< T > NewFrom(Args &&... args)
Construct tmp from derived type with forwarding arguments.
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::tmp::reset
void reset() noexcept
Release ownership of managed temporary object.
Definition: tmpI.H:343
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:258
Foam::tmp::get
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmpI.H:227
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::tmp::pointer
T * pointer
Pointer to type of object being managed or referenced.
Definition: tmp.H:95
Foam::tmp::typeName
word typeName() const
Return type-name of the tmp, constructed from type-name of T.
Definition: tmpI.H:220
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::tmp::ptr
T * ptr() const
Definition: tmpI.H:296
U
U
Definition: pEqn.H:72
Foam::tmp::swap
void swap(tmp< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpI.H:387
Foam::tmp::New
static tmp< T > New(Args &&... args)
Construct tmp of T with forwarding arguments.
Foam::tmp::tmp
constexpr tmp() noexcept
Construct with no managed pointer.
Definition: tmpI.H:70
word.H
Foam::tmp::valid
bool valid() const noexcept
Definition: tmpI.H:206
tmpI.H
args
Foam::argList args(argc, argv)
refCount.H
Foam::tmp::refCount
Foam::refCount refCount
Reference counter class.
Definition: tmp.H:99
Foam::tmp::cref
const T & cref() const
Definition: tmpI.H:241
Foam::tmp::movable
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition: tmpI.H:213