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-2022 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::autoPtr
29 
30 Description
31  Pointer management similar to std::unique_ptr, with some additional
32  methods and type checking.
33 
34 Note
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 
41 SourceFiles
42  autoPtrI.H
43 
44 See also
45  Foam::refPtr
46 
47 \*---------------------------------------------------------------------------*/
48 
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 
61 namespace Foam
62 {
63 
64 /*---------------------------------------------------------------------------*\
65  Class autoPtr Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 template<class T>
69 class autoPtr
70 {
71  //- The managed pointer
72  T* ptr_;
73 
74 
75 public:
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;
84 
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.
108  :
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>
116  explicit autoPtr(autoPtr<U>&& rhs)
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
131  autoPtr(const autoPtr<T>& rhs) noexcept
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  //- Swaps the managed object with other autoPtr.
220  inline void swap(autoPtr<T>& other) noexcept;
221 
222 
223  // Other
224 
225  //- Copy construct by invoking clone on underlying managed object
226  // A no-op if no pointer is managed
227  // \param args list of arguments for clone
228  template<class... Args>
229  inline autoPtr<T> clone(Args&&... args) const;
230 
232  // Member Operators
233 
234  //- Return reference to the managed object.
235  // FatalError if no pointer is managed
236  inline T& operator*();
237 
238  //- Return const reference to the object.
239  // FatalError if no pointer is managed
240  inline const T& operator*() const;
241 
242  //- Dereferences (non-const) pointer to the managed object
243  // FatalError if no pointer is managed
244  inline T* operator->();
245 
246  //- Dereferences (const) pointer to the managed object
247  // FatalError if no pointer is managed
248  inline const T* operator->() const;
249 
250  //- Return reference to the object data.
251  // FatalError if no pointer is managed
252  inline T& operator()();
253 
254  //- Return const reference to the object data
255  // FatalError if no pointer is managed
256  inline const T& operator()() const;
257 
258 
259  // Casting
260 
261  //- True if pointer/reference is non-null. Same as good()
262  explicit operator bool() const noexcept { return bool(ptr_); }
263 
264  //- Cast to pointer type
265  operator const T*() const noexcept { return ptr_; }
266 
267  //- Cast to pointer type
268  operator T*() noexcept { return ptr_; }
269 
270  //- Deprecated(2019-01) Automatic cast conversion to underlying type
271  // FatalError if no pointer is managed
272  // \deprecated(2019-01) Can result in inadvertent conversions
273  // where the user should really know or check if the pointer
274  // is valid prior to using.
275  #ifdef Foam_autoPtr_castOperator
276  operator const T&() const { return operator*(); }
277  #else
278  operator const T&() const = delete;
279  #endif
280 
281 
282  // Assignment
283 
284  //- No copy assignment from plain pointer (uncontrolled access)
285  void operator=(T* p) = delete;
286 
287  //- No move assignment disguised as a copy assignment
288  // \deprecated(2018-02) can have unintended behaviour
289  void operator=(const autoPtr<T>&) = delete;
290 
291  //- Transfer object ownership from parameter
292  void operator=(autoPtr<T>&& other) noexcept { reset(std::move(other)); }
293 
294  //- Transfer object ownership from parameter
295  // \note Future check std::enable_if + std::is_convertible ?
296  template<class U>
297  void operator=(autoPtr<U>&& other) noexcept
298  {
299  reset(other.release());
300  }
301 
302  //- Transfer ownership by move assignment from unique_ptr
303  void operator=(std::unique_ptr<T>&& other) { reset(other.release()); }
304 
305  //- Reset via assignment from literal nullptr
306  void operator=(std::nullptr_t) noexcept { reset(nullptr); }
307 
308 
309  // Housekeeping
310 
311  //- Identical to good(), or bool operator
312  bool valid() const noexcept { return bool(ptr_); }
313 
314  //- Deprecated(2020-07) True if the managed pointer is null
315  //
316  // \deprecated(2020-07) - use bool operator
317  FOAM_DEPRECATED_FOR(2020-07, "bool operator")
318  bool empty() const noexcept { return !ptr_; }
319 
320  //- Deprecated(2018-02) Identical to reset().
321  // \note Provided for backward compatibility - the older version
322  // enforced a run-time check (Fatal if pointer was already set)
323  // but this was rarely used.
324  // \deprecated(2018-02) Identical to reset().
325  #ifndef Foam_autoPtr_nodeprecate_setMethod
326  FOAM_DEPRECATED_FOR(2022-01, "reset() - same behaviour")
327  #endif
328  void set(T* p) noexcept { reset(p); }
329 };
330 
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 // Global Functions
335 
336 //- Specializes the Swap algorithm for autoPtr (swaps pointers).
337 template<class T>
338 void Swap(autoPtr<T>& lhs, autoPtr<T>& rhs)
339 {
340  lhs.swap(rhs);
341 }
343 
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
345 
346 } // End namespace Foam
348 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
349 
350 #include "autoPtrI.H"
351 
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
353 
354 #endif
355 
356 // ************************************************************************* //
T element_type
Type of object being managed.
Definition: autoPtr.H:79
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition: autoPtrI.H:58
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:84
bool empty() const noexcept
Deprecated(2020-07) True if the managed pointer is null.
Definition: autoPtr.H:419
static autoPtr< T > NewFrom(Args &&... args)
Construct autoPtr from derived type with forwarding arguments.
Definition: autoPtr.H:193
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:255
autoPtr(std::unique_ptr< T > &&rhs) noexcept
Move construct from unique_ptr, transferring ownership.
Definition: autoPtr.H:136
T * pointer
Pointer to type of object being managed.
Definition: autoPtr.H:84
~autoPtr() noexcept
Destructor: deletes managed pointer.
Definition: autoPtr.H:160
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:37
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition: autoPtr.H:92
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:206
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:28
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: autoPtr.H:410
const direction noexcept
Definition: Scalar.H:258
autoPtr(autoPtr< T > &&rhs) noexcept
Move construct, transferring ownership.
Definition: autoPtr.H:116
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
T & ref()
Return reference to the managed object without nullptr checking.
Definition: autoPtr.H:231
T * ptr() noexcept
Same as release().
Definition: autoPtr.H:248
U
Definition: pEqn.H:72
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:647
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
void operator=(T *p)=delete
No copy assignment from plain pointer (uncontrolled access)
volScalarField & p
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
Foam::argList args(argc, argv)
Namespace for OpenFOAM.