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  //- Implicit construct for a const reference to an object.
124  inline constexpr tmp(const T& obj) noexcept;
125 
126  //- Move construct, transferring ownership.
127  // Does not affect ref-count
128  inline tmp(tmp<T>&& rhs) noexcept;
129 
130  //- Move construct, transferring ownership.
131  // Does not affect ref-count
132  // \note Non-standard definition - should be non-const
133  inline tmp(const tmp<T>&& rhs) noexcept;
134 
135  //- Copy construct, incrementing ref-count of managed pointer.
136  // \note Non-standard definition - should be non-const
137  inline tmp(const tmp<T>& rhs);
138 
139  //- Copy/move construct. Optionally reusing ref-counted pointer.
140  inline tmp(const tmp<T>& rhs, bool reuse);
141 
142  //- No copy construct from autoPtr,
143  //- also avoids implicit cast to object or pointer
144  tmp(const autoPtr<T>&) = delete;
145 
146  //- No copy construct from refPtr,
147  //- also avoids implicit cast to object
148  tmp(const refPtr<T>&) = delete;
149 
150  //- Move construct from autoPtr, transferring ownership.
151  inline explicit tmp(autoPtr<T>&& rhs) noexcept;
152 
153 
154  //- Destructor: deletes managed pointer when the ref-count is 0
155  inline ~tmp() noexcept;
156 
157 
158  // Factory Methods
159 
160  //- Construct tmp with forwarding arguments
161  // \param args list of arguments with which an instance of T
162  // will be constructed.
163  //
164  // \note Similar to std::make_shared, but the overload for
165  // array types is not disabled.
166  template<class... Args>
167  static tmp<T> New(Args&&... args)
168  {
169  return tmp<T>(new T(std::forward<Args>(args)...));
170  }
171 
172  //- Construct tmp from derived type with forwarding arguments
173  // \param args list of arguments with which an instance of U
174  // will be constructed.
175  //
176  // \note Similar to New but for derived types.
177  // Future check std::enable_if + std::is_convertible ?
178  template<class U, class... Args>
179  static tmp<T> NewFrom(Args&&... args)
180  {
181  return tmp<T>(new U(std::forward<Args>(args)...));
182  }
183 
184 
185  // Static Member Functions
186 
187  //- The type-name, constructed from type-name of T
188  inline static word typeName();
189 
190 
191  // Query
192 
193  //- True if pointer/reference is non-null.
194  bool good() const noexcept { return bool(ptr_); }
195 
196  //- If the stored/referenced content is const
197  inline bool is_const() const noexcept;
198 
199  //- True if this is a managed pointer (not a reference)
200  inline bool is_pointer() const noexcept;
201 
202  //- True if this is a reference (not a pointer)
203  inline bool is_reference() const noexcept;
204 
205  //- True if this is a non-null managed pointer with a unique ref-count
206  inline bool movable() const noexcept;
207 
208 
209  // Access
210 
211  //- Return pointer without nullptr checking.
212  T* get() noexcept { return ptr_; }
213 
214  //- Return const pointer without nullptr checking.
215  const T* get() const noexcept { return ptr_; }
216 
217  //- Return const reference to the object or to the contents
218  //- of a (non-null) managed pointer.
219  // Fatal for a null managed pointer
220  inline const T& cref() const;
222  //- Return non-const reference to the contents of a non-null
223  //- managed pointer.
224  // Fatal for a null managed pointer or if the object is const.
225  inline T& ref() const;
226 
227  //- Return non-const reference to the object or to the contents
228  //- of a (non-null) managed pointer, with an additional const_cast.
229  // Fatal for a null pointer.
230  T& constCast() const { return const_cast<T&>(cref()); }
231 
232 
233  // Edit
234 
235  //- Return managed pointer for reuse, or clone() the object reference.
236  inline T* ptr() const;
237 
238  //- If object pointer points to valid object:
239  //- delete object and set pointer to nullptr
240  inline void clear() const noexcept;
241 
242  //- Use specified protection against moving for the managed pointer.
243  //- No-op for references.
244  inline void protect(bool on) noexcept;
245 
246  //- Clear existing and transfer ownership.
247  inline void reset(tmp<T>&& other) noexcept;
248 
249  //- Delete managed temporary object and set to new given pointer.
250  inline void reset(T* p = nullptr) noexcept;
251 
252  //- Avoid inadvertent casting (to object or pointer)
253  void reset(const autoPtr<T>&) = delete;
254 
255  //- Avoid inadvertent casting (to object)
256  void reset(const refPtr<T>&) = delete;
257 
258  //- Reset with emplace construction.
259  //- Return reference to the new content.
260  template<class... Args>
261  inline T& emplace(Args&&... args);
262 
263  //- Clear existing and set (const) reference from other
264  inline void cref(const tmp<T>& other) noexcept;
265 
266  //- Clear existing and set (const) reference
267  inline void cref(const T& obj) noexcept;
269  //- Clear existing and set (const) reference to pointer content.
270  // The pointer can be null, which is handled like a clear().
271  inline void cref(const T* p) noexcept;
272 
273  //- Avoid inadvertent casting (to object or pointer)
274  void cref(const autoPtr<T>&) = delete;
275 
276  //- Avoid inadvertent casting (to object)
277  void cref(const refPtr<T>&) = delete;
278 
279  //- Clear existing and set to (non-const) reference
280  inline void ref(T& obj) noexcept;
281 
282  //- Clear existing and set (non-const) reference to pointer content.
283  // The pointer can be null, which is handled like a clear().
284  inline void ref(T* p) noexcept;
285 
286  //- Avoid inadvertent casting (to object or pointer)
287  void ref(const autoPtr<T>&) = delete;
288 
289  //- Avoid inadvertent casting (to object)
290  void ref(const refPtr<T>&) = delete;
291 
292  //- Swaps the managed object with other.
293  inline void swap(tmp<T>& other) noexcept;
294 
295 
296  // Member Operators
298  // Note: no 'operator*()' types since there are large chunks
299  // of code that use tmp<Field> and Field interchangeable
300  // Eg, \code (a * b) - and the '*' would be misinterpreted
301 
302  //- Dereferences (const) pointer to the managed object.
303  // Fatal for a null managed pointer.
304  inline const T* operator->() const;
305 
306  //- Dereferences (non-const) pointer to the managed object.
307  // Fatal for a null managed pointer or if the object is const.
308  inline T* operator->();
309 
310  //- Return const reference to the object - same as cref() method.
311  const T& operator()() const { return cref(); }
312 
313 
314  // Casting
315 
316  //- True if pointer/reference is non-null. Same as good()
317  explicit operator bool() const noexcept { return bool(ptr_); }
318 
319  //- Cast to underlying data type, using the cref() method.
320  operator const T&() const { return cref(); }
321 
322 
323  // Assignment
324 
325  //- Transfer ownership of the managed pointer.
326  // Fatal for a null managed pointer or if the object is const.
327  inline void operator=(const tmp<T>& other);
328 
329  //- Clear existing and transfer ownership.
330  inline void operator=(tmp<T>&& other) noexcept;
331 
332  //- Take ownership of the pointer.
333  // Fatal for a null pointer, or when the pointer is non-unique.
334  inline void operator=(T* p);
335 
336  //- Reset via assignment from literal nullptr
337  void operator=(std::nullptr_t) noexcept { reset(nullptr); }
338 
339 
340  // Housekeeping
341 
342  //- Identical to good(), or bool operator
343  bool valid() const noexcept { return bool(ptr_); }
344 
345  //- Identical to is_pointer(). Prefer is_pointer() or movable().
346  //
347  // \deprecated(2020-07) - prefer is_pointer() or movable()
348  bool isTmp() const noexcept { return is_pointer(); }
349 
350  //- Deprecated(2020-07) True if a null managed pointer
351  //
352  // \deprecated(2020-07) - use bool operator
353  FOAM_DEPRECATED_FOR(2020-07, "bool operator")
354  bool empty() const noexcept { return !ptr_; }
355 };
356 
357 
358 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
359 
360 // Does not need std::swap or Foam::Swap() specialization
361 // since tmp is MoveConstructible and MoveAssignable
362 
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 } // End namespace Foam
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 #include "tmpI.H"
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 
374 #endif
375 
376 // ************************************************************************* //
bool isTmp() const noexcept
Identical to is_pointer(). Prefer is_pointer() or movable().
Definition: tmp.H:480
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:472
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:214
T & ref() const
Return non-const reference to the contents of a non-null managed pointer.
Definition: tmpI.H:235
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: tmpI.H:221
bool empty() const noexcept
Deprecated(2020-07) True if a null managed pointer.
Definition: tmp.H:489
void protect(bool on) noexcept
Use specified protection against moving for the managed pointer. No-op for references.
Definition: tmpI.H:307
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:268
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition: tmp.H:206
static tmp< T > NewFrom(Args &&... args)
Construct tmp from derived type with forwarding arguments.
Definition: tmp.H:221
void swap(tmp< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpI.H:418
bool is_reference() const noexcept
True if this is a reference (not a pointer)
Definition: tmpI.H:206
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:112
U
Definition: pEqn.H:72
void operator=(const tmp< T > &other)
Transfer ownership of the managed pointer.
Definition: tmpI.H:470
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: tmpI.H:198
bool good() const noexcept
True if pointer/reference is non-null.
Definition: tmp.H:240
void clear() const noexcept
If object pointer points to valid object: delete object and set pointer to nullptr.
Definition: tmpI.H:289
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:297
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:256
volScalarField & p
A class for managing temporary objects.
Definition: HashPtrTable.H:50
T & emplace(Args &&... args)
Reset with emplace construction. Return reference to the new content.
Definition: tmpI.H:357
Foam::argList args(argc, argv)
bool is_const() const noexcept
If the stored/referenced content is const.
Definition: tmpI.H:191
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:338
Namespace for OpenFOAM.