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-2022 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::refPtr
44  Foam::refCount
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef Foam_tmp_H
49 #define Foam_tmp_H
50 
51 #include "autoPtr.H"
52 #include "refCount.H"
53 #include "word.H"
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 // Forward Declarations
61 template<class T> class refPtr;
62 
63 /*---------------------------------------------------------------------------*\
64  Class tmp Declaration
65 \*---------------------------------------------------------------------------*/
66 
67 template<class T>
68 class tmp
69 {
70  // Private Data
71 
72  //- The stored reference type
73  enum refType
74  {
75  PTR,
76  CREF,
77  REF
78  };
79 
80  //- The managed pointer or address of the object (reference)
81  mutable T* ptr_;
82 
83  //- The type (managed pointer | object reference)
84  mutable refType type_;
85 
86 
87  // Private Member Functions
88 
89  //- Increment the ref-count for a managed pointer
90  //- and check that it is not oversubscribed
91  inline void incrCount();
92 
93 
94 public:
95 
96  // STL type definitions
97 
98  //- Type of object being managed or referenced
99  typedef T element_type;
100 
101  //- Pointer to type of object being managed or referenced
102  typedef T* pointer;
103 
105  //- Reference counter class
106  typedef Foam::refCount refCount;
107 
108 
109  // Constructors
110 
111  //- Construct with no managed pointer.
112  inline constexpr tmp() noexcept;
113 
114  //- Implicit construct from literal nullptr: no managed pointer
115  inline constexpr tmp(std::nullptr_t) noexcept;
116 
117  //- Construct, taking ownership of the pointer.
118  inline explicit tmp(T* p);
119 
120  //- Implicit construct for a const reference to an object.
121  inline constexpr tmp(const T& obj) noexcept;
122 
123  //- Move construct, transferring ownership.
124  // Does not affect ref-count
125  inline tmp(tmp<T>&& rhs) noexcept;
126 
127  //- Move construct, transferring ownership.
128  // Does not affect ref-count
129  // \note Non-standard definition - should be non-const
130  inline tmp(const tmp<T>&& rhs) noexcept;
131 
132  //- Copy construct, incrementing ref-count of managed pointer.
133  // \note Non-standard definition - should be non-const
134  inline tmp(const tmp<T>& rhs);
135 
136  //- Copy/move construct. Optionally reusing ref-counted pointer.
137  inline tmp(const tmp<T>& rhs, bool reuse);
138 
139  //- No copy construct from autoPtr,
140  //- also avoids implicit cast to object or pointer
141  tmp(const autoPtr<T>&) = delete;
142 
143  //- No copy construct from refPtr,
144  //- also avoids implicit cast to object
145  tmp(const refPtr<T>&) = delete;
146 
147  //- Move construct from autoPtr, transferring ownership.
148  inline explicit tmp(autoPtr<T>&& rhs) noexcept;
149 
150 
151  //- Destructor: deletes managed pointer when the ref-count is 0
152  inline ~tmp() noexcept;
153 
154 
155  // Factory Methods
156 
157  //- Construct tmp with forwarding arguments
158  // \param args list of arguments with which an instance of T
159  // will be constructed.
160  //
161  // \note Similar to std::make_shared, but the overload for
162  // array types is not disabled.
163  template<class... Args>
164  static tmp<T> New(Args&&... args)
165  {
166  return tmp<T>(new T(std::forward<Args>(args)...));
167  }
168 
169  //- Construct tmp from derived type with forwarding arguments
170  // \param args list of arguments with which an instance of U
171  // will be constructed.
172  //
173  // \note Similar to New but for derived types.
174  // Future check std::enable_if + std::is_convertible ?
175  template<class U, class... Args>
176  static tmp<T> NewFrom(Args&&... args)
177  {
178  return tmp<T>(new U(std::forward<Args>(args)...));
179  }
180 
181 
182  // Static Member Functions
183 
184  //- The type-name, constructed from type-name of T
185  inline static word typeName();
186 
187 
188  // Query
189 
190  //- True if pointer/reference is non-null.
191  bool good() const noexcept { return bool(ptr_); }
192 
193  //- If the stored/referenced content is const
194  bool is_const() const noexcept { return (type_ == CREF); }
195 
196  //- True if this is a managed pointer (not a reference)
197  bool is_pointer() const noexcept { return (type_ == PTR); }
198 
199  //- True if this is a reference (not a pointer)
200  bool is_reference() const noexcept { return (type_ != PTR); }
201 
202  //- True if this is a non-null managed pointer with a unique ref-count
203  inline bool movable() const noexcept;
204 
205 
206  // Access
207 
208  //- Return pointer without nullptr checking.
209  T* get() noexcept { return ptr_; }
210 
211  //- Return const pointer without nullptr checking.
212  const T* get() const noexcept { return ptr_; }
213 
214  //- Return const reference to the object or to the contents
215  //- of a (non-null) managed pointer.
216  // Fatal for a null managed pointer
217  inline const T& cref() const;
219  //- Return non-const reference to the contents of a non-null
220  //- managed pointer.
221  // Fatal for a null managed pointer or if the object is const.
222  inline T& ref() const;
223 
224  //- Return non-const reference to the object or to the contents
225  //- of a (non-null) managed pointer, with an additional const_cast.
226  // Fatal for a null pointer.
227  T& constCast() const { return const_cast<T&>(cref()); }
228 
229 
230  // Edit
231 
232  //- Return managed pointer for reuse, or clone() the object reference.
233  inline T* ptr() const;
234 
235  //- If object pointer points to valid object:
236  //- delete object and set pointer to nullptr
237  inline void clear() const noexcept;
238 
239  //- Clear existing and transfer ownership.
240  inline void reset(tmp<T>&& other) noexcept;
241 
242  //- Delete managed temporary object and set to new given pointer
243  inline void reset(T* p = nullptr) noexcept;
244 
245  //- Avoid inadvertent casting (to object or pointer)
246  void reset(const autoPtr<T>&) = delete;
248  //- Avoid inadvertent casting (to object)
249  void reset(const refPtr<T>&) = delete;
250 
251  //- Clear existing and set (const) reference from other
252  inline void cref(const tmp<T>& other) noexcept;
253 
254  //- Clear existing and set (const) reference
255  inline void cref(const T& obj) noexcept;
256 
257  //- Clear existing and set (const) reference to pointer content.
258  // The pointer can be null, which is handled like a clear().
259  inline void cref(const T* p) noexcept;
260 
261  //- Avoid inadvertent casting (to object or pointer)
262  void cref(const autoPtr<T>&) = delete;
263 
264  //- Avoid inadvertent casting (to object)
265  void cref(const refPtr<T>&) = delete;
266 
267  //- Clear existing and set to (non-const) reference
268  inline void ref(T& obj) noexcept;
269 
270  //- Clear existing and set (non-const) reference to pointer content.
271  // The pointer can be null, which is handled like a clear().
272  inline void ref(T* p) noexcept;
273 
274  //- Avoid inadvertent casting (to object or pointer)
275  void ref(const autoPtr<T>&) = delete;
276 
277  //- Avoid inadvertent casting (to object)
278  void ref(const refPtr<T>&) = delete;
279 
280  //- Swaps the managed object with other.
281  inline void swap(tmp<T>& other) noexcept;
282 
283 
284  // Member Operators
285 
286  // Note: no 'operator*()' types since there are large chunks
287  // of code that use tmp<Field> and Field interchangeable
288  // Eg, \code (a * b) - and the '*' would be misinterpreted
289 
290  //- Dereferences (const) pointer to the managed object.
291  // Fatal for a null managed pointer.
292  inline const T* operator->() const;
293 
294  //- Dereferences (non-const) pointer to the managed object.
295  // Fatal for a null managed pointer or if the object is const.
296  inline T* operator->();
297 
298  //- Return const reference to the object - same as cref() method.
299  const T& operator()() const { return cref(); }
300 
301 
302  // Casting
303 
304  //- True if pointer/reference is non-null. Same as good()
305  explicit operator bool() const noexcept { return bool(ptr_); }
306 
307  //- Cast to underlying data type, using the cref() method.
308  operator const T&() const { return cref(); }
309 
310 
311  // Assignment
312 
313  //- Transfer ownership of the managed pointer.
314  // Fatal for a null managed pointer or if the object is const.
315  inline void operator=(const tmp<T>& other);
316 
317  //- Clear existing and transfer ownership.
318  inline void operator=(tmp<T>&& other) noexcept;
319 
320  //- Take ownership of the pointer.
321  // Fatal for a null pointer, or when the pointer is non-unique.
322  inline void operator=(T* p);
323 
324  //- Reset via assignment from literal nullptr
325  void operator=(std::nullptr_t) noexcept { reset(nullptr); }
326 
327 
328  // Housekeeping
329 
330  //- Identical to good(), or bool operator
331  bool valid() const noexcept { return bool(ptr_); }
332 
333  //- Identical to is_pointer()
334  bool isTmp() const noexcept { return type_ == PTR; }
335 
336  //- Deprecated(2020-07) True if a null managed pointer
337  //
338  // \deprecated(2020-07) - use bool operator
339  FOAM_DEPRECATED_FOR(2020-07, "bool operator")
340  bool empty() const noexcept { return !ptr_; }
341 };
342 
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 // Global Functions
347 
348 //- Specializes the Swap algorithm for tmp (swaps pointers and types).
349 template<class T>
350 void Swap(tmp<T>& lhs, tmp<T>& rhs)
351 {
352  lhs.swap(rhs);
353 }
354 
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 } // End namespace Foam
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 #include "tmpI.H"
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 #endif
367 
368 // ************************************************************************* //
bool isTmp() const noexcept
Identical to is_pointer()
Definition: tmp.H:461
static word typeName()
The type-name, constructed from type-name of T.
Definition: tmpI.H:45
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: tmp.H:456
Foam::refCount refCount
Reference counter class.
Definition: tmp.H:115
constexpr tmp() noexcept
Construct with no managed pointer.
Definition: tmpI.H:54
Reference counter for various OpenFOAM components.
Definition: refCount.H:44
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition: tmpI.H:189
T & ref() const
Return non-const reference to the contents of a non-null managed pointer.
Definition: tmpI.H:210
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: tmpI.H:196
bool empty() const noexcept
Deprecated(2020-07) True if a null managed pointer.
Definition: tmp.H:470
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:265
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition: tmp.H:203
static tmp< T > NewFrom(Args &&... args)
Construct tmp from derived type with forwarding arguments.
Definition: tmp.H:218
void swap(tmp< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpI.H:359
bool is_reference() const noexcept
True if this is a reference (not a pointer)
Definition: tmp.H:252
const direction noexcept
Definition: Scalar.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
T * pointer
Pointer to type of object being managed or referenced.
Definition: tmp.H:109
U
Definition: pEqn.H:72
void operator=(const tmp< T > &other)
Transfer ownership of the managed pointer.
Definition: tmpI.H:411
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:647
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: tmp.H:247
bool good() const noexcept
True if pointer/reference is non-null.
Definition: tmp.H:237
void clear() const noexcept
If object pointer points to valid object: delete object and set pointer to nullptr.
Definition: tmpI.H:263
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
T & constCast() const
Return non-const reference to the object or to the contents of a (non-null) managed pointer...
Definition: tmp.H:294
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:231
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
bool is_const() const noexcept
If the stored/referenced content is const.
Definition: tmp.H:242
T element_type
Type of object being managed or referenced.
Definition: tmp.H:104
void reset(tmp< T > &&other) noexcept
Clear existing and transfer ownership.
Definition: tmpI.H:290
Namespace for OpenFOAM.