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