Loading...
Searching...
No Matches
autoPtr.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-2017 OpenFOAM Foundation
9 Copyright (C) 2016-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::autoPtr
29
30Description
31 Pointer management similar to std::unique_ptr, with some additional
32 methods and type checking.
33
34Note
35 Parts of the interface now mirror std::unique_ptr, but since it pre-dates
36 both C++11 and std::unique_ptr, it has some additional idiosyncrasies.
37 The const-reference constructors and assignment operators
38 actually use move semantics to allow their participation in
39 default constructible, default assignable classes.
40
41SourceFiles
42 autoPtrI.H
43
44See also
45 Foam::refPtr
46
47\*---------------------------------------------------------------------------*/
49#ifndef Foam_autoPtr_H
50#define Foam_autoPtr_H
51
52// Transitional features/misfeatures
53#define Foam_autoPtr_copyConstruct
54#define Foam_autoPtr_castOperator
55// #define Foam_autoPtr_nodeprecate_setMethod
56
57#include "stdFoam.H"
58
59// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60
61namespace Foam
62{
63
64/*---------------------------------------------------------------------------*\
65 Class autoPtr Declaration
66\*---------------------------------------------------------------------------*/
67
68template<class T>
69class autoPtr
70{
71 //- The managed pointer
72 T* ptr_;
73
74
75public:
76
77 // STL type definitions
78
79 //- Type of object being managed
80 typedef T element_type;
81
82 //- Pointer to type of object being managed
83 typedef T* pointer;
85
86 // Constructors
87
88 //- Construct with no managed pointer.
89 constexpr autoPtr() noexcept
90 :
91 ptr_(nullptr)
92 {}
93
94 //- Implicit construct from literal nullptr: no managed pointer
95 constexpr autoPtr(std::nullptr_t) noexcept
96 :
97 ptr_(nullptr)
98 {}
99
100 //- Construct, taking ownership of the pointer.
101 explicit autoPtr(T* p) noexcept
102 :
103 ptr_(p)
104 {}
105
106 //- Move construct, transferring ownership.
109 ptr_(rhs.release())
110 {}
111
112 //- Move construct, transferring ownership from derived type.
113 // U must be derivable from T
114 // \note Future check std::enable_if + std::is_convertible ?
115 template<class U>
117 :
118 ptr_(rhs.release())
119 {}
120
121 //- Move construct from unique_ptr, transferring ownership.
122 explicit autoPtr(std::unique_ptr<T>&& rhs) noexcept
123 :
124 ptr_(rhs.release())
125 {}
126
127 //- A move construct disguised as a copy construct (transfers ownership)
128 // \remark This should ideally be deleted - pending cleanup of code
129 // currently relying on this behaviour.
130 #ifdef Foam_autoPtr_copyConstruct
132 :
133 ptr_(const_cast<autoPtr<T>&>(rhs).release())
134 {}
135 #else
136 autoPtr(const autoPtr<T>&) = delete;
137 #endif
138
139
140 //- Destructor: deletes managed pointer
142 {
143 delete ptr_;
144 }
145
146
147 // Factory Methods
149 //- Construct autoPtr with forwarding arguments
150 // \param args list of arguments with which an instance of T
151 // will be constructed.
152 //
153 // \note Similar to std::make_unique, but the overload for
154 // array types is not disabled.
155 template<class... Args>
156 static autoPtr<T> New(Args&&... args)
157 {
158 return autoPtr<T>(new T(std::forward<Args>(args)...));
159 }
161 //- Construct autoPtr from derived type with forwarding arguments
162 // \param args list of arguments with which an instance of U
163 // will be constructed.
164 //
165 // \note Similar to New but for derived types.
166 // Future check std::enable_if + std::is_convertible ?
167 template<class U, class... Args>
168 static autoPtr<T> NewFrom(Args&&... args)
169 {
170 return autoPtr<T>(new U(std::forward<Args>(args)...));
171 }
172
173
174 // Member Functions
175
176 // Query
177
178 //- True if the managed pointer is non-null
179 bool good() const noexcept { return bool(ptr_); }
180
181
182 // Access
183
184 //- Return pointer to managed object without nullptr checking.
185 // Pointer remains under autoPtr management.
186 T* get() noexcept { return ptr_; }
187
188 //- Return const pointer to managed object without nullptr checking.
189 // Pointer remains under autoPtr management.
190 const T* get() const noexcept { return ptr_; }
191
192 //- Return reference to the managed object without nullptr checking.
193 // When get() == nullptr, additional guards may be required to avoid
194 // inadvertent access of a nullptr.
195 T& ref() { return *ptr_; }
196
197
198 // Edit
199
200 //- Release ownership and return the pointer.
201 // \remark Method naming consistent with std::unique_ptr
202 inline T* release() noexcept;
203
204 //- Same as \c release().
205 // \remark Method naming consistent with Foam::tmp
206 T* ptr() noexcept { return release(); }
207
208 //- Same as \c reset(nullptr)
209 // \remark Method naming consistent with Foam::tmp
210 void clear() noexcept { reset(nullptr); }
211
212 //- Delete managed object and set to new given pointer
213 inline void reset(T* p = nullptr) noexcept;
214
215 //- Delete managed object and set to new given pointer
216 // \remark Same as move assign, but better for code documentation
217 inline void reset(autoPtr<T>&& other) noexcept;
218
219 //- Reset with emplace construction.
220 //- Return reference to the new content.
221 template<class... Args>
222 inline T& emplace(Args&&... args);
224 //- Swaps the managed object with other autoPtr.
225 inline void swap(autoPtr<T>& other) noexcept;
226
227
228 // Other
229
230 //- Copy construct by invoking clone on underlying managed object
231 // A no-op if no pointer is managed
232 // \param args list of arguments for clone
233 template<class... Args>
234 inline autoPtr<T> clone(Args&&... args) const;
235
236
237 // Member Operators
238
239 //- Return reference to the managed object.
240 // FatalError if no pointer is managed
241 inline T& operator*();
242
243 //- Return const reference to the object.
244 // FatalError if no pointer is managed
245 inline const T& operator*() const;
246
247 //- Dereferences (non-const) pointer to the managed object
248 // FatalError if no pointer is managed
249 inline T* operator->();
250
251 //- Dereferences (const) pointer to the managed object
252 // FatalError if no pointer is managed
253 inline const T* operator->() const;
254
255 //- Return reference to the object data.
256 // FatalError if no pointer is managed
257 inline T& operator()();
258
259 //- Return const reference to the object data
260 // FatalError if no pointer is managed
261 inline const T& operator()() const;
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 //- Implicit cast to \em const pointer type.
270 // \note no cast to non-const pointer,
271 // since this would add uncertainty of ownership.
272 operator const T*() const noexcept { return ptr_; }
273
274 //- Deprecated(2019-01) Automatic cast conversion to underlying type
275 // FatalError if no pointer is managed
276 // \deprecated(2019-01) Can result in inadvertent conversions
277 // where the user should really know or check if the pointer
278 // is valid prior to using.
279 #ifdef Foam_autoPtr_castOperator
280 operator const T&() const { return operator*(); }
281 #else
282 operator const T&() const = delete;
283 #endif
284
285
286 // Assignment
287
288 //- No copy assignment from plain pointer (uncontrolled access)
289 void operator=(T* p) = delete;
290
291 //- No move assignment disguised as a copy assignment
292 // \deprecated(2018-02) can have unintended behaviour
293 void operator=(const autoPtr<T>&) = delete;
294
295 //- Transfer object ownership from parameter
296 void operator=(autoPtr<T>&& other) noexcept { reset(std::move(other)); }
297
298 //- Transfer object ownership from parameter
299 // \note Future check std::enable_if + std::is_convertible ?
300 template<class U>
301 void operator=(autoPtr<U>&& other) noexcept
302 {
303 reset(other.release());
304 }
305
306 //- Transfer ownership by move assignment from unique_ptr
307 void operator=(std::unique_ptr<T>&& other) { reset(other.release()); }
309 //- Reset via assignment from literal nullptr
310 void operator=(std::nullptr_t) noexcept { reset(nullptr); }
311
312
313 // Housekeeping
314
315 //- Identical to good(), or bool operator
316 bool valid() const noexcept { return bool(ptr_); }
317
318 //- Deprecated(2020-07) True if the managed pointer is null
319 //
320 // \deprecated(2020-07) - use bool operator
321 FOAM_DEPRECATED_FOR(2020-07, "bool operator")
322 bool empty() const noexcept { return !ptr_; }
323
324 //- Deprecated(2018-02) Identical to reset().
325 // \note Provided for backward compatibility - the older version
326 // enforced a run-time check (Fatal if pointer was already set)
327 // but this was rarely used.
328 // \deprecated(2018-02) Identical to reset().
329 #ifndef Foam_autoPtr_nodeprecate_setMethod
330 FOAM_DEPRECATED_FOR(2022-01, "reset() - same behaviour")
331 #endif
332 void set(T* p) noexcept { reset(p); }
333};
334
335
336// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
337
338// Does not need std::swap or Foam::Swap() specialization
339// since autoPtr is MoveConstructible and MoveAssignable
340
341
342// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
343
344} // End namespace Foam
345
346// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347
348#include "autoPtrI.H"
349
350// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
351
352#endif
353
354// ************************************************************************* //
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
void operator=(std::nullptr_t) noexcept
Reset via assignment from literal nullptr.
Definition autoPtr.H:407
void clear() noexcept
Same as reset(nullptr).
Definition autoPtr.H:255
Function1 & emplace(Args &&... args)
void operator=(std::unique_ptr< T > &&other)
Transfer ownership by move assignment from unique_ptr.
Definition autoPtr.H:402
void operator=(autoPtr< U > &&other) noexcept
Transfer object ownership from parameter.
Definition autoPtr.H:394
autoPtr(const autoPtr< T > &rhs) noexcept
A move construct disguised as a copy construct (transfers ownership).
Definition autoPtr.H:148
constexpr autoPtr(std::nullptr_t) noexcept
Implicit construct from literal nullptr: no managed pointer.
Definition autoPtr.H:100
T & ref()
Return reference to the managed object without nullptr checking.
Definition autoPtr.H:231
void swap(autoPtr< Function1 > &other) noexcept
T & operator*()
Return reference to the managed object.
Definition autoPtrI.H:94
bool empty() const noexcept
Definition autoPtr.H:424
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition autoPtr.H:216
bool good() const noexcept
True if the managed pointer is non-null.
Definition autoPtr.H:206
T * pointer
Pointer to type of object being managed.
Definition autoPtr.H:84
const T * get() const noexcept
Return const pointer to managed object without nullptr checking.
Definition autoPtr.H:223
Function1 * release() noexcept
void operator=(T *p)=delete
No copy assignment from plain pointer (uncontrolled access).
static autoPtr< T > NewFrom(Args &&... args)
Construct autoPtr from derived type with forwarding arguments.
Definition autoPtr.H:193
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition autoPtr.H:92
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition autoPtrI.H:37
bool valid() const noexcept
Identical to good(), or bool operator.
Definition autoPtr.H:415
autoPtr< Function1 > clone(Args &&... args) const
autoPtr(T *p) noexcept
Construct, taking ownership of the pointer.
Definition autoPtr.H:108
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
autoPtr(std::unique_ptr< T > &&rhs) noexcept
Move construct from unique_ptr, transferring ownership.
Definition autoPtr.H:136
Function1 * ptr() noexcept
Definition autoPtr.H:248
void operator=(const autoPtr< T > &)=delete
No move assignment disguised as a copy assignment.
void set(T *p) noexcept
Deprecated(2018-02) Identical to reset().
Definition autoPtr.H:437
T element_type
Type of object being managed.
Definition autoPtr.H:79
void operator=(autoPtr< T > &&other) noexcept
Transfer object ownership from parameter.
Definition autoPtr.H:386
~autoPtr() noexcept
Destructor: deletes managed pointer.
Definition autoPtr.H:160
autoPtr(autoPtr< U > &&rhs)
Move construct, transferring ownership from derived type.
Definition autoPtr.H:128
autoPtr(autoPtr< T > &&rhs) noexcept
Move construct, transferring ownership.
Definition autoPtr.H:116
U
Definition pEqn.H:72
volScalarField & p
const volScalarField & T
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)
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43