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