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-2020 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 refPtr_H
43 #define 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  //- Object types
63  enum refType
64  {
65  PTR,
66  CREF,
67  REF
68  };
69 
70  //- The managed pointer or address of the object (reference)
71  mutable T* ptr_;
72 
73  //- The type (managed pointer | object reference)
74  mutable refType type_;
75 
76 
77 public:
78 
79  // STL type definitions
80 
81  //- Type of object being managed or referenced
82  typedef T element_type;
83 
84  //- Pointer to type of object being managed or referenced
85  typedef T* pointer;
86 
87 
88  //- Null reference counter class
90 
91 
92  // Factory Methods
93 
94  //- Construct refPtr of T with forwarding arguments
95  // \param args list of arguments with which an instance of T
96  // will be constructed.
97  //
98  // \note Similar to std::make_shared, but the overload for
99  // array types is not disabled.
100  template<class... Args>
101  inline static refPtr<T> New(Args&&... args);
102 
103  //- Construct refPtr from derived type with forwarding arguments
104  // \param args list of arguments with which an instance of U
105  // will be constructed.
106  //
107  // \note Similar to New but for derived types
108  template<class U, class... Args>
109  inline static refPtr<T> NewFrom(Args&&... args);
110 
111 
112  // Constructors
113 
114  //- Default construct, no managed pointer.
115  inline constexpr refPtr() noexcept;
116 
117  //- Construct with no managed pointer.
118  inline constexpr refPtr(std::nullptr_t) noexcept;
119 
120  //- Construct, taking ownership of the pointer.
121  inline explicit constexpr refPtr(T* p) noexcept;
122 
123  //- Move construct from autoPtr, transferring ownership.
124  inline explicit refPtr(autoPtr<T>&& ptr) noexcept;
125 
126  //- Move construct from unique_ptr, transferring ownership.
127  inline explicit refPtr(std::unique_ptr<T>&& ptr) noexcept;
128 
129  //- Construct for a const reference to an object.
130  inline constexpr refPtr(const T& obj) noexcept;
131 
132  //- Move construct, transferring ownership.
133  inline refPtr(refPtr<T>&& rhs) noexcept;
134 
135  //- Copy construct
136  inline refPtr(const refPtr<T>& rhs);
137 
138  //- Copy/move construct. Optionally reusing pointer.
139  inline refPtr(const refPtr<T>& rhs, bool reuse);
140 
141 
142  //- Destructor: deletes managed pointer
143  inline ~refPtr();
144 
145 
146  // Member Functions
147 
148  //- The type-name, constructed from type-name of T
149  inline static word typeName();
150 
151 
152  // Query
153 
154  //- Deprecated(2020-07) True if a null managed pointer
155  //
156  // \deprecated(2020-07) - use bool operator
157  FOAM_DEPRECATED_FOR(2020-07, "bool operator")
158  bool empty() const noexcept { return !ptr_; }
159 
160  //- True for non-null pointer/reference
161  bool valid() const noexcept { return ptr_; }
162 
163  //- True if this is a managed pointer (not a reference)
164  bool is_pointer() const noexcept { return type_ == PTR; }
165 
166  //- Identical to is_pointer()
167  bool isTmp() const noexcept { return type_ == PTR; }
168 
169  //- True if this is a non-null managed pointer
170  inline bool movable() const noexcept;
171 
172 
173  // Access
174 
175  //- Return pointer without nullptr checking.
176  T* get() noexcept { return ptr_; }
177 
178  //- Return const pointer without nullptr checking.
179  const T* get() const noexcept { return ptr_; }
180 
181  //- Return const reference to the object or to the contents
182  //- of a (non-null) managed pointer.
183  // Fatal for a null managed pointer
184  inline const T& cref() const;
185 
186  //- Return non-const reference to the contents of a non-null
187  //- managed pointer.
188  // Fatal for a null managed pointer or if the object is const.
189  inline T& ref() const;
190 
191  //- Return non-const reference to the object or to the contents
192  //- of a (non-null) managed pointer, with an additional const_cast.
193  // Fatal for a null managed pointer.
194  inline T& constCast() const;
195 
196 
197  // Edit
198 
199  //- Return managed pointer for reuse, or clone() the object reference.
200  inline T* ptr() const;
201 
202  //- If object pointer points to valid object:
203  //- delete object and set pointer to nullptr
204  inline void clear() const noexcept;
205 
206  //- Delete managed temporary object and set to new given pointer
207  inline void reset(T* p = nullptr) noexcept;
208 
209  //- Clear existing and transfer ownership from autoPtr.
210  void reset(autoPtr<T>&& other) noexcept { reset(other.release()); }
211 
212  //- Clear existing and transfer ownership from unique_ptr
213  void reset(std::unique_ptr<T>&& other) { reset(other.release()); }
214 
215  //- Clear existing and transfer ownership.
216  inline void reset(refPtr<T>&& other) noexcept;
217 
218  //- Delete managed temporary object and set to (const) reference
219  inline void cref(const T& obj) noexcept;
220 
221  //- Delete managed temporary object and set to (non-const) reference
222  inline void ref(T& obj) noexcept;
223 
224  //- Swaps the managed object with other.
225  inline void swap(refPtr<T>& other) noexcept;
226 
227 
228  // Member Operators
229 
230  //- Dereferences (const) pointer to the managed object.
231  // Fatal for a null managed pointer.
232  inline const T* operator->() const;
233 
234  //- Dereferences (non-const) pointer to the managed object.
235  // Fatal for a null managed pointer or if the object is const.
236  inline T* operator->();
237 
238  //- Return const reference to the object - same as cref() method.
239  const T& operator()() const { return cref(); }
240 
241  //- Cast to underlying data type, using the cref() method.
242  operator const T&() const { return cref(); }
243 
244  //- Non-null pointer/reference : valid()
245  explicit operator bool() const noexcept { return ptr_; }
246 
247  //- Transfer ownership of the managed pointer.
248  // Fatal for a null managed pointer or if the object is const.
249  inline void operator=(const refPtr<T>& other);
250 
251  //- Clear existing and transfer ownership.
252  inline void operator=(refPtr<T>&& other) noexcept;
253 
254  //- Take ownership of the pointer.
255  // Fatal for a null pointer
256  inline void operator=(T* p);
257 
258  //- Reset via assignment from literal nullptr
259  inline void operator=(std::nullptr_t) noexcept;
260 
261  //- Conversion to tmp, releases pointer or shallow-copies reference
262  inline operator tmp<T>();
263 };
264 
265 
266 // Global Functions
267 
268 //- Specialized Swap algorithm for refPtr.
269 // Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
270 template<class T>
271 void Swap(refPtr<T>& lhs, refPtr<T>& rhs)
272 {
273  lhs.swap(rhs);
274 }
275 
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 } // End namespace Foam
280 
281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282 
283 #include "refPtrI.H"
284 
285 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
286 
287 #endif
288 
289 // ************************************************************************* //
Foam::refPtr::New
static refPtr< T > New(Args &&... args)
Construct refPtr of T with forwarding arguments.
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::refPtr::swap
void swap(refPtr< T > &other) noexcept
Swaps the managed object with other.
Definition: refPtrI.H:313
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::refPtr::is_pointer
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: refPtr.H:163
Foam::refPtr::reset
void reset(std::unique_ptr< T > &&other)
Clear existing and transfer ownership from unique_ptr.
Definition: refPtr.H:212
Foam::refPtr::get
T * get() noexcept
Return pointer without nullptr checking.
Definition: refPtr.H:175
Foam::refPtr::element_type
T element_type
Type of object being managed or referenced.
Definition: refPtr.H:81
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:913
Foam::refPtr::get
const T * get() const noexcept
Return const pointer without nullptr checking.
Definition: refPtr.H:178
Foam::refPtr::valid
bool valid() const noexcept
True for non-null pointer/reference.
Definition: refPtr.H:160
Foam::refPtr::clear
void clear() const noexcept
Definition: refPtrI.H:258
Foam::refPtr::cref
const T & cref() const
Definition: refPtrI.H:187
Foam::refPtr::constCast
T & constCast() const
Definition: refPtrI.H:228
Foam::refPtr::reset
void reset(autoPtr< T > &&other) noexcept
Clear existing and transfer ownership from autoPtr.
Definition: refPtr.H:209
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::refPtr::movable
bool movable() const noexcept
True if this is a non-null managed pointer.
Definition: refPtrI.H:180
Foam::refPtr::isTmp
bool isTmp() const noexcept
Identical to is_pointer()
Definition: refPtr.H:166
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
refPtrI.H
Foam::refPtr::refCount
Foam::refCount::zero refCount
Null reference counter class.
Definition: refPtr.H:88
Foam::refPtr::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:235
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
U
U
Definition: pEqn.H:72
Foam::refCount::zero
A non-counting (dummy) refCount.
Definition: refCount.H:59
tmp.H
Foam::refPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed temporary object and set to new given pointer.
Definition: refPtrI.H:269
Foam::refPtr::operator()
const T & operator()() const
Return const reference to the object - same as cref() method.
Definition: refPtr.H:238
Foam::refPtr::empty
bool empty() const noexcept
Deprecated(2020-07) True if a null managed pointer.
Definition: refPtr.H:157
Foam::refPtr::refPtr
constexpr refPtr() noexcept
Default construct, no managed pointer.
Definition: refPtrI.H:60
bool
bool
Definition: EEqn.H:20
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
Foam::refPtr::NewFrom
static refPtr< T > NewFrom(Args &&... args)
Construct refPtr from derived type with forwarding arguments.
Foam::refPtr::pointer
T * pointer
Pointer to type of object being managed or referenced.
Definition: refPtr.H:84
Foam::refPtr::operator=
void operator=(const refPtr< T > &other)
Transfer ownership of the managed pointer.
Definition: refPtrI.H:371
Foam::refPtr::ref
T & ref() const
Definition: refPtrI.H:204
args
Foam::argList args(argc, argv)
Foam::refPtr::operator->
const T * operator->() const
Dereferences (const) pointer to the managed object.
Definition: refPtrI.H:330
Foam::refPtr::typeName
static word typeName()
The type-name, constructed from type-name of T.
Definition: refPtrI.H:51
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
autoPtr.H