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