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-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::tmp
29
30Description
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
38SourceFiles
39 tmpI.H
40
41See also
42 Foam::autoPtr
43 Foam::refPtr
44 Foam::refCount
45
46\*---------------------------------------------------------------------------*/
47
48#ifndef Foam_tmp_H
49#define Foam_tmp_H
50
51#include "refCount.H"
52#include "word.H"
53#include "stdFoam.H"
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56
57namespace Foam
58{
59
60/*---------------------------------------------------------------------------*\
61 Class tmp Declaration
62\*---------------------------------------------------------------------------*/
63
64template<class T>
65class tmp
66{
67 // Private Data
68
69 //- The stored reference type
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 Functions
85
86 //- Increment the ref-count for a managed pointer
87 //- and check that it is not oversubscribed
88 inline void incrCount();
89
90
91public:
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 //- Construct with no managed pointer.
129 inline constexpr tmp() noexcept;
130
131 //- Construct with no managed pointer (literal nullptr).
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 //- True if pointer/reference is non-null.
170 bool good() const noexcept { return bool(ptr_); }
171
172 //- If the stored/referenced content is const
173 bool is_const() const noexcept { return type_ == CREF; }
174
175 //- True if this is a managed pointer (not a reference)
176 bool is_pointer() const noexcept { return type_ == PTR; }
177
178 //- True if this is a non-null managed pointer with a unique ref-count
179 inline bool movable() const noexcept;
180
181
182 // Access
183
184 //- Return pointer without nullptr checking.
185 T* get() noexcept { return ptr_; }
186
187 //- Return const pointer without nullptr checking.
188 const T* get() const noexcept { return ptr_; }
189
190 //- Return const reference to the object or to the contents
191 //- of a (non-null) managed pointer.
192 // Fatal for a null managed pointer
193 inline const T& cref() const;
194
195 //- Return non-const reference to the contents of a non-null
196 //- managed pointer.
197 // Fatal for a null managed pointer or if the object is const.
198 inline T& ref() const;
199
200 //- Return non-const reference to the object or to the contents
201 //- of a (non-null) managed pointer, with an additional const_cast.
202 // Fatal for a null pointer.
203 inline T& constCast() const;
204
205
206 // Edit
207
208 //- Return managed pointer for reuse, or clone() the object reference.
209 inline T* ptr() const;
210
211 //- If object pointer points to valid object:
212 //- delete object and set pointer to nullptr
213 inline void clear() const noexcept;
214
215
216 //- Clear existing and transfer ownership.
217 inline void reset(tmp<T>&& other) noexcept;
218
219 //- Delete managed temporary object and set to new given pointer
220 inline void reset(T* p = nullptr) noexcept;
221
222
223 //- Clear existing and set (const) reference from other
224 inline void cref(const tmp<T>& other) noexcept;
225
226 //- Clear existing and set (const) reference
227 inline void cref(const T& obj) noexcept;
228
229 //- Clear existing and set (const) reference to pointer content.
230 // The pointer can be null, which is handled like a clear().
231 inline void cref(const T* p) noexcept;
232
233
234 //- Clear existing and set to (non-const) reference
235 inline void ref(T& obj) noexcept;
236
237 //- Clear existing and set (non-const) reference to pointer content.
238 // The pointer can be null, which is handled like a clear().
239 inline void ref(T* p) noexcept;
240
241
242 //- Swaps the managed object with other.
243 inline void swap(tmp<T>& other) noexcept;
244
245
246 // Member Operators
247
248 // Note: no 'operator*()' types since there are large chunks
249 // of code that use tmp<Field> and Field interchangeable
250 // Eg, \code (a * b) - and the '*' would be misinterpreted
251
252 //- Dereferences (const) pointer to the managed object.
253 // Fatal for a null managed pointer.
254 inline const T* operator->() const;
255
256 //- Dereferences (non-const) pointer to the managed object.
257 // Fatal for a null managed pointer or if the object is const.
258 inline T* operator->();
259
260 //- Return const reference to the object - same as cref() method.
261 const T& operator()() const { return cref(); }
262
263
264 // Casting
265
266 //- True if pointer/reference is non-null. Same as good()
267 explicit operator bool() const noexcept { return bool(ptr_); }
268
269 //- Cast to underlying data type, using the cref() method.
270 operator const T&() const { return cref(); }
271
272
273 // Assignment
274
275 //- Transfer ownership of the managed pointer.
276 // Fatal for a null managed pointer or if the object is const.
277 inline void operator=(const tmp<T>& other);
278
279 //- Clear existing and transfer ownership.
280 inline void operator=(tmp<T>&& other) noexcept;
281
282 //- Take ownership of the pointer.
283 // Fatal for a null pointer, or when the pointer is non-unique.
284 inline void operator=(T* p);
285
286 //- Reset via assignment from literal nullptr
287 inline void operator=(std::nullptr_t) noexcept;
288
289
290 // Housekeeping
291
292 //- Identical to good(), or bool operator
293 bool valid() const noexcept { return bool(ptr_); }
294
295 //- Identical to is_pointer()
296 bool isTmp() const noexcept { return type_ == PTR; }
297
298 //- Deprecated(2020-07) True if a null managed pointer
299 //
300 // \deprecated(2020-07) - use bool operator
301 FOAM_DEPRECATED_FOR(2020-07, "bool operator")
302 bool empty() const noexcept { return !ptr_; }
303};
304
305
306// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307
308// Global Functions
309
310//- Specialized Swap algorithm for tmp.
311// Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
312template<class T>
313void Swap(tmp<T>& lhs, tmp<T>& rhs)
314{
315 lhs.swap(rhs);
316}
317
318
319// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320
321} // End namespace Foam
322
323// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
324
325#include "tmpI.H"
326
327// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328
329#endif
330
331// ************************************************************************* //
Reference counter for various OpenFOAM components.
Definition: refCount.H:51
A class for managing temporary objects.
Definition: tmp.H:65
void swap(tmp< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpI.H:381
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference)
Definition: tmp.H:175
static word typeName()
The type-name, constructed from type-name of T.
Definition: tmpI.H:53
const T & cref() const
Definition: tmpI.H:213
T & constCast() const
Definition: tmpI.H:248
bool empty() const noexcept
Deprecated(2020-07) True if a null managed pointer.
Definition: tmp.H:301
Foam::refCount refCount
Reference counter class.
Definition: tmp.H:102
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:184
bool good() const noexcept
True if pointer/reference is non-null.
Definition: tmp.H:169
T * pointer
Pointer to type of object being managed or referenced.
Definition: tmp.H:98
const T * get() const noexcept
Return const pointer without nullptr checking.
Definition: tmp.H:187
void clear() const noexcept
Definition: tmpI.H:287
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition: tmpI.H:206
void operator=(const tmp< T > &other)
Transfer ownership of the managed pointer.
Definition: tmpI.H:433
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: tmp.H:292
void reset(tmp< T > &&other) noexcept
Clear existing and transfer ownership.
Definition: tmpI.H:314
bool isTmp() const noexcept
Identical to is_pointer()
Definition: tmp.H:295
bool is_const() const noexcept
If the stored/referenced content is const.
Definition: tmp.H:172
static tmp< T > New(Args &&... args)
Construct tmp of T with forwarding arguments.
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
T element_type
Type of object being managed or referenced.
Definition: tmp.H:95
static tmp< T > NewFrom(Args &&... args)
Construct tmp from derived type with forwarding arguments.
T & ref() const
Definition: tmpI.H:227
constexpr tmp() noexcept
Construct with no managed pointer.
Definition: tmpI.H:78
A class for handling words, derived from Foam::string.
Definition: word.H:68
U
Definition: pEqn.H:72
volScalarField & p
const volScalarField & T
bool
Definition: EEqn.H:20
Namespace for OpenFOAM.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:408
const direction noexcept
Definition: Scalar.H:223
Foam::argList args(argc, argv)
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:52