Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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 In contrast to std::shared_ptr, only a limited number of tmp items
38 will ever share a pointer.
39
40SourceFiles
41 tmpI.H
42
43See 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
59namespace Foam
60{
61
62namespace Expression
63{
64template<class GeoField> class GeometricFieldConstTmpWrap;
65template<class T> class ListConstTmpWrap;
66}
67
68// Forward Declarations
69template<class T> class refPtr;
70template<class T> class tmp;
71template<class Type, template<class> class PatchField, class GeoMesh>
72class GeometricField;
73
74/*---------------------------------------------------------------------------*\
75 Class tmp Declaration
76\*---------------------------------------------------------------------------*/
77
78template<class T>
79class tmp
80{
81 // Private Data
82
83 //- The stored reference type
84 enum refType
85 {
86 PTR,
87 CACHE_PTR,
88 REF_Types,
89 CREF,
90 REF
91 };
92
93 //- The managed pointer or address of the object (reference)
94 mutable T* ptr_;
95
96 //- The type (managed pointer | object reference)
97 mutable refType type_;
98
99
100 // Private Member Functions
101
102 //- Fatal if ref-count of a managed pointer is oversubscribed
103 inline void checkUseCount() const;
104
105
106public:
107
108 // STL type definitions
109
110 //- Type of object being managed or referenced
111 typedef T element_type;
112
113 //- Pointer to type of object being managed or referenced
114 typedef T* pointer;
115
117 //- Reference counter class
118 typedef Foam::refCount refCount;
119
120
121 // Constructors
122
123 //- Construct with no managed pointer.
124 inline constexpr tmp() noexcept;
125
126 //- Implicit construct from literal nullptr: no managed pointer
127 inline constexpr tmp(std::nullptr_t) noexcept;
128
129 //- Construct, taking ownership of the pointer.
130 inline explicit tmp(T* p);
131
132 //- Implicit construct for a const reference to an object.
133 inline constexpr tmp(const T& obj) noexcept;
134
135 //- Move construct, transferring ownership.
136 // Does not affect ref-count
137 inline tmp(tmp<T>&& rhs) noexcept;
138
139 //- Move construct, transferring ownership.
140 // Does not affect ref-count
141 // \note Non-standard definition - should be non-const
142 inline tmp(const tmp<T>&& rhs) noexcept;
143
144 //- Copy construct, incrementing ref-count of managed pointer.
145 // \note Non-standard definition - should be non-const
146 inline tmp(const tmp<T>& rhs);
147
148 //- Copy/move construct. Optionally reusing ref-counted pointer.
149 inline tmp(const tmp<T>& rhs, bool reuse);
150
151 //- No copy construct from autoPtr,
152 //- also avoids implicit cast to object or pointer
153 tmp(const autoPtr<T>&) = delete;
154
155 //- No copy construct from refPtr,
156 //- also avoids implicit cast to object
157 tmp(const refPtr<T>&) = delete;
158
159 //- Move construct from autoPtr, transferring ownership.
160 inline explicit tmp(autoPtr<T>&& rhs) noexcept;
161
162
163 //- Destructor: deletes managed pointer when the ref-count is 0
164 inline ~tmp() noexcept;
165
166
167 // Factory Methods
168
169 //- Construct tmp with forwarding arguments
170 // \param args list of arguments with which an instance of T
171 // will be constructed.
172 //
173 // \note Similar to std::make_shared, but the overload for
174 // array types is not disabled.
175 template<class... Args>
176 static tmp<T> New(Args&&... args)
177 {
178 return tmp<T>(new T(std::forward<Args>(args)...));
179 }
180
181 //- Construct tmp from derived type with forwarding arguments
182 // \param args list of arguments with which an instance of U
183 // will be constructed.
184 //
185 // \note Similar to New but for derived types.
186 // Future check std::enable_if + std::is_convertible ?
187 template<class U, class... Args>
188 static tmp<T> NewFrom(Args&&... args)
190 return tmp<T>(new U(std::forward<Args>(args)...));
191 }
192
193
194 // Static Member Functions
195
196 //- The type-name, constructed from type-name of T
197 inline static word typeName();
198
199
200 // Query
201
202 //- True if pointer/reference is non-null.
203 bool good() const noexcept { return bool(ptr_); }
204
205 //- If the stored/referenced content is const
206 inline bool is_const() const noexcept;
207
208 //- True if this is a managed pointer (not a reference)
209 inline bool is_pointer() const noexcept;
210
211 //- True if this is a reference (not a pointer)
212 inline bool is_reference() const noexcept;
213
214 //- True if this is a non-null managed pointer with a unique ref-count
215 inline bool movable() const noexcept;
216
217
218 // Access
219
220 //- Return pointer without nullptr checking.
221 T* get() noexcept { return ptr_; }
222
223 //- Return const pointer without nullptr checking.
224 const T* get() const noexcept { return ptr_; }
225
226 //- Return const reference to the object or to the contents
227 //- of a (non-null) managed pointer.
228 // Fatal for a null managed pointer
229 inline const T& cref() const;
231 //- Return non-const reference to the contents of a non-null
232 //- managed pointer.
233 // Fatal for a null managed pointer or if the object is const.
234 inline T& ref() const;
235
236 //- Return non-const reference to the object or to the contents
237 //- of a (non-null) managed pointer, with an additional const_cast.
238 // Fatal for a null pointer.
239 T& constCast() const { return const_cast<T&>(cref()); }
240
241
242 // Edit
243
244 //- Return managed pointer for reuse, or clone() the object reference.
245 inline T* ptr() const;
246
247 //- If object pointer points to valid object:
248 //- delete object and set pointer to nullptr
249 inline void clear() const noexcept;
250
251 //- Use specified protection against moving for the managed pointer.
252 //- No-op for references.
253 inline void protect(bool on) noexcept;
254
255 //- Clear existing and transfer ownership.
256 inline void reset(tmp<T>&& other) noexcept;
257
258 //- Delete managed temporary object and set to new given pointer.
259 inline void reset(T* p = nullptr) noexcept;
260
261 //- Avoid inadvertent casting (to object or pointer)
262 void reset(const autoPtr<T>&) = delete;
263
264 //- Avoid inadvertent casting (to object)
265 void reset(const refPtr<T>&) = delete;
266
267 //- Reset with emplace construction.
268 //- Return reference to the new content.
269 template<class... Args>
270 inline T& emplace(Args&&... args);
271
272 //- Clear existing and set (const) reference from other
273 inline void cref(const tmp<T>& other) noexcept;
274
275 //- Clear existing and set (const) reference
276 inline void cref(const T& obj) noexcept;
278 //- Clear existing and set (const) reference to pointer content.
279 // The pointer can be null, which is handled like a clear().
280 inline void cref(const T* p) noexcept;
281
282 //- Avoid inadvertent casting (to object or pointer)
283 void cref(const autoPtr<T>&) = delete;
284
285 //- Avoid inadvertent casting (to object)
286 void cref(const refPtr<T>&) = delete;
287
288 //- Clear existing and set to (non-const) reference
289 inline void ref(T& obj) noexcept;
290
291 //- Clear existing and set (non-const) reference to pointer content.
292 // The pointer can be null, which is handled like a clear().
293 inline void ref(T* p) noexcept;
294
295 //- Avoid inadvertent casting (to object or pointer)
296 void ref(const autoPtr<T>&) = delete;
297
298 //- Avoid inadvertent casting (to object)
299 void ref(const refPtr<T>&) = delete;
300
301 //- Swaps the managed object with other.
302 inline void swap(tmp<T>& other) noexcept;
303
304
305 // Member Operators
307 // Note: no 'operator*()' types since there are large chunks
308 // of code that use tmp<Field> and Field interchangeable
309 // Eg, \code (a * b) - and the '*' would be misinterpreted
310
311 //- Dereferences (const) pointer to the managed object.
312 // Fatal for a null managed pointer.
313 inline const T* operator->() const;
314
315 //- Dereferences (non-const) pointer to the managed object.
316 // Fatal for a null managed pointer or if the object is const.
317 inline T* operator->();
318
319 //- Return const reference to the object - same as cref() method.
320 const T& operator()() const { return cref(); }
321
322
323 // Casting
324
325 //- True if pointer/reference is non-null. Same as good()
326 explicit operator bool() const noexcept { return bool(ptr_); }
327
328 //- Cast to underlying data type, using the cref() method.
329 operator const T&() const { return cref(); }
330
331
332 // Assignment
333
334 //- Transfer ownership of the managed pointer.
335 // Fatal for a null managed pointer or if the object is const.
336 inline void operator=(const tmp<T>& other);
337
338 //- Clear existing and transfer ownership.
339 inline void operator=(tmp<T>&& other) noexcept;
340
341 //- Take ownership of the pointer.
342 // Fatal for a null pointer, or when the pointer is non-unique.
343 inline void operator=(T* p);
344
345 //- Reset via assignment from literal nullptr
346 void operator=(std::nullptr_t) noexcept { reset(nullptr); }
347
348
349 // Housekeeping
350
351 //- Identical to good(), or bool operator
352 bool valid() const noexcept { return bool(ptr_); }
353
354 //- Identical to is_pointer(). Prefer is_pointer() or movable().
355 //
356 // \deprecated(2020-07) - prefer is_pointer() or movable()
357 bool isTmp() const noexcept { return is_pointer(); }
358
359 //- Deprecated(2020-07) True if a null managed pointer
360 //
361 // \deprecated(2020-07) - use bool operator
362 FOAM_DEPRECATED_FOR(2020-07, "bool operator")
363 bool empty() const noexcept { return !ptr_; }
364
365
366 // Expression templates
367
368 //- Wrap value as expression
369 auto expr() const
370 {
371 // Add 'using is_GeometricField = void' to GeometricField
372 // Add 'using is_List = void' to List
373
374 if constexpr
376 std::is_void_v<typename T::is_GeometricField>
377 )
378 {
381 else //if constexpr (std::is_void_v<T::is_List>)
382 {
384 }
385 //else
386 //{
387 // //static_assert("XXX",YYY);
388 // return;
389 //}
390 }
391};
392
393
394// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
395
396// Does not need std::swap or Foam::Swap() specialization
397// since tmp is MoveConstructible and MoveAssignable
398
399
400// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
401
402} // End namespace Foam
403
404// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405
406#include "tmpI.H"
407
408// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409
410#endif
411
412// ************************************************************************* //
Expression wrap of const tmp to GeometricField.
Expression wrap of const tmp to List.
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition GeoMesh.H:46
Generic GeometricField class.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
Reference counter for various OpenFOAM components.
Definition refCount.H:45
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for managing temporary objects.
Definition tmp.H:75
void operator=(std::nullptr_t) noexcept
Reset via assignment from literal nullptr.
Definition tmp.H:473
T & emplace(Args &&... args)
Reset with emplace construction. Return reference to the new content.
Definition tmpI.H:357
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
bool is_pointer() const noexcept
True if this is a managed pointer (not a reference).
Definition tmpI.H:198
static word typeName()
The type-name, constructed from type-name of T.
Definition tmpI.H:28
void protect(bool on) noexcept
Use specified protection against moving for the managed pointer. No-op for references.
Definition tmpI.H:307
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition tmpI.H:221
auto expr() const
Wrap value as expression.
Definition tmp.H:506
T & constCast() const
Return non-const reference to the object or to the contents of a (non-null) managed pointer,...
Definition tmp.H:306
bool empty() const noexcept
Deprecated(2020-07) True if a null managed pointer.
Definition tmp.H:498
Foam::refCount refCount
Reference counter class.
Definition tmp.H:127
T * get() noexcept
Return pointer without nullptr checking.
Definition tmp.H:277
bool good() const noexcept
True if pointer/reference is non-null.
Definition tmp.H:249
T * pointer
Pointer to type of object being managed or referenced.
Definition tmp.H:121
const T * get() const noexcept
Return const pointer without nullptr checking.
Definition tmp.H:282
void clear() const noexcept
If object pointer points to valid object: delete object and set pointer to nullptr.
Definition tmpI.H:289
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition tmpI.H:214
void operator=(const tmp< T > &other)
Transfer ownership of the managed pointer.
Definition tmpI.H:470
bool valid() const noexcept
Identical to good(), or bool operator.
Definition tmp.H:481
void reset(tmp< T > &&other) noexcept
Clear existing and transfer ownership.
Definition tmpI.H:338
void reset(const refPtr< T > &)=delete
Avoid inadvertent casting (to object).
bool isTmp() const noexcept
Identical to is_pointer(). Prefer is_pointer() or movable().
Definition tmp.H:489
bool is_const() const noexcept
If the stored/referenced content is const.
Definition tmpI.H:191
static tmp< T > New(Args &&... args)
Construct tmp with forwarding arguments.
Definition tmp.H:215
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
T element_type
Type of object being managed or referenced.
Definition tmp.H:116
static tmp< T > NewFrom(Args &&... args)
Construct tmp from derived type with forwarding arguments.
Definition tmp.H:230
T & ref() const
Return non-const reference to the contents of a non-null managed pointer.
Definition tmpI.H:235
constexpr tmp() noexcept
Construct with no managed pointer.
Definition tmpI.H:54
A class for handling words, derived from Foam::string.
Definition word.H:66
U
Definition pEqn.H:72
volScalarField & p
const volScalarField & T
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
A namespace for expression templates.
Namespace for OpenFOAM.
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43