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-2021 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 \*---------------------------------------------------------------------------*/
45 
46 #ifndef autoPtr_H
47 #define autoPtr_H
48 
49 // Transitional features/misfeatures
50 #define Foam_autoPtr_copyConstruct
51 #define Foam_autoPtr_castOperator
52 // #define Foam_autoPtr_deprecate_setMethod
53 
54 #include "stdFoam.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 /*---------------------------------------------------------------------------*\
62  Class autoPtr Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class T>
66 class autoPtr
67 {
68  //- The managed pointer
69  T* ptr_;
70 
71 
72 public:
73 
74  // STL type definitions
75 
76  //- Type of object being managed
77  typedef T element_type;
78 
79  //- Pointer to type of object being managed
80  typedef T* pointer;
81 
82 
83  // Factory Methods
84 
85  //- Construct autoPtr of T with forwarding arguments
86  // \param args list of arguments with which an instance of T
87  // will be constructed.
88  //
89  // \note Similar to std::make_unique, but the overload for
90  // array types is not disabled.
91  template<class... Args>
92  inline static autoPtr<T> New(Args&&... args);
93 
94  //- Construct autoPtr from derived type with forwarding arguments
95  // \param args list of arguments with which an instance of U
96  // will be constructed.
97  //
98  // \note Similar to New but for derived types.
99  // Future check for std::is_base_of ?
100  template<class U, class... Args>
101  inline static autoPtr<T> NewFrom(Args&&... args);
102 
103 
104  // Constructors
105 
106  //- Construct with no managed pointer.
107  inline constexpr autoPtr() noexcept;
108 
109  //- Construct with no managed pointer (literal nullptr).
110  inline constexpr autoPtr(std::nullptr_t) noexcept;
111 
112  //- Construct, taking ownership of the pointer.
113  inline explicit autoPtr(T* p) noexcept;
114 
115  //- Move construct, transferring ownership.
116  inline autoPtr(autoPtr<T>&& ap) noexcept;
117 
118  //- Move construct, transferring ownership from derived type.
119  // U must be derivable from T
120  // \note Future check for std::is_base_of ?
121  template<class U>
122  inline explicit autoPtr(autoPtr<U>&& ap);
123 
124  //- A move construct disguised as a copy construct (transfers ownership)
125  // \remark This should ideally be deleted - pending cleanup of code
126  // currently relying on this behaviour.
127  #ifdef Foam_autoPtr_copyConstruct
128  autoPtr(const autoPtr<T>& ap) noexcept
129  :
130  ptr_(const_cast<autoPtr<T>&>(ap).release())
131  {}
132  #else
133  autoPtr(const autoPtr<T>& ap) = delete;
134  #endif
135 
136 
137  //- Deletes the managed pointer
138  inline ~autoPtr() noexcept;
139 
140 
141  // Member Functions
142 
143  // Query
144 
145  //- True if the managed pointer is non-null
146  bool good() const noexcept { return bool(ptr_); }
147 
148 
149  // Access
150 
151  //- Return pointer to managed object without nullptr checking.
152  // Pointer remains under autoPtr management.
153  T* get() noexcept { return ptr_; }
154 
155  //- Return const pointer to managed object without nullptr checking.
156  // Pointer remains under autoPtr management.
157  const T* get() const noexcept { return ptr_; }
158 
159  //- Return reference to the managed object without nullptr checking.
160  // When get() == nullptr, additional guards may be required to avoid
161  // inadvertent access of a nullptr.
162  T& ref() { return *ptr_; }
163 
164 
165  // Edit
166 
167  //- Release ownership and return the pointer.
168  // \remark Method naming consistent with std::unique_ptr
169  inline T* release() noexcept;
170 
171  //- Same as \c release().
172  // \remark Method naming consistent with Foam::tmp
173  T* ptr() noexcept { return release(); }
174 
175  //- Same as \c reset(nullptr)
176  // \remark Method naming consistent with Foam::tmp
177  void clear() noexcept { reset(nullptr); }
178 
179 
180  //- Delete managed object and set to new given pointer
181  // \remark Same as move assign, but better for code documentation
182  inline void reset(autoPtr<T>&& other) noexcept;
183 
184  //- Delete managed object and set to new given pointer
185  inline void reset(T* p = nullptr) noexcept;
186 
187  //- Swaps the managed object with other autoPtr.
188  inline void swap(autoPtr<T>& other) noexcept;
189 
190 
191  // Other
192 
193  //- Copy construct by invoking clone on underlying managed object
194  // A no-op if no pointer is managed
195  // \param args list of arguments for clone
196  template<class... Args>
197  inline autoPtr<T> clone(Args&&... args) const;
198 
199 
200  // Member Operators
201 
202  //- Return reference to the managed object.
203  // FatalError if no pointer is managed
204  inline T& operator*();
205 
206  //- Return const reference to the object.
207  // FatalError if no pointer is managed
208  inline const T& operator*() const;
209 
210  //- Dereferences (non-const) pointer to the managed object
211  // FatalError if no pointer is managed
212  inline T* operator->();
213 
214  //- Dereferences (const) pointer to the managed object
215  // FatalError if no pointer is managed
216  inline const T* operator->() const;
217 
218  //- Return reference to the object data.
219  // FatalError if no pointer is managed
220  inline T& operator()();
221 
222  //- Return const reference to the object data
223  // FatalError if no pointer is managed
224  inline const T& operator()() const;
225 
226 
227  // Casting
228 
229  //- True if pointer/reference is non-null. Same as good()
230  explicit operator bool() const noexcept { return bool(ptr_); }
231 
232  //- Cast to pointer type
233  operator const T*() const noexcept { return ptr_; }
234 
235  //- Cast to pointer type
236  operator T*() noexcept { return ptr_; }
237 
238  //- Deprecated(2019-01) Automatic cast conversion to underlying type
239  // FatalError if no pointer is managed
240  // \deprecated(2019-01) Can result in inadvertent conversions
241  // where the user should really know or check if the pointer
242  // is valid prior to using.
243  #ifdef Foam_autoPtr_castOperator
244  operator const T&() const { return operator*(); }
245  #else
246  operator const T&() const = delete;
247  #endif
248 
249 
250  // Assignment
251 
252  //- No copy assignment from plain pointer (uncontrolled access)
253  void operator=(T* p) = delete;
254 
255  //- Transfer object ownership from parameter
256  inline void operator=(autoPtr<T>&& ap) noexcept;
257 
258  //- Transfer object ownership from parameter
259  template<class U>
260  inline void operator=(autoPtr<U>&& ap) noexcept;
261 
262  //- No move assignment disguised as a copy assignment
263  // \deprecated(2018-02) can have unintended behaviour
264  void operator=(const autoPtr<T>& ap) = delete;
265 
266  //- Reset via assignment from literal nullptr
267  inline void operator=(std::nullptr_t) noexcept;
268 
269 
270  // Housekeeping
271 
272  //- Identical to good(), or bool operator
273  bool valid() const noexcept { return bool(ptr_); }
274 
275  //- Deprecated(2020-07) True if the managed pointer is null
276  //
277  // \deprecated(2020-07) - use bool operator
278  FOAM_DEPRECATED_FOR(2020-07, "bool operator")
279  bool empty() const noexcept { return !ptr_; }
280 
281  //- Deprecated(2018-02) Identical to reset().
282  // \note Provided for backward compatibility - the older version
283  // enforced a run-time check (Fatal if pointer was already set)
284  // but this was rarely used.
285  // \deprecated(2018-02) Identical to reset().
286  #ifdef Foam_autoPtr_deprecate_setMethod
287  FOAM_DEPRECATED_FOR(2018-02, "reset() - same behaviour")
288  #endif
289  void set(T* p) noexcept { reset(p); }
290 };
291 
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 // Global Functions
296 
297 //- Specializes the Swap algorithm for autoPtr.
298 // Swaps the pointers of lhs and rhs. Calls \c lhs.swap(rhs)
299 template<class T>
300 void Swap(autoPtr<T>& lhs, autoPtr<T>& rhs)
301 {
302  lhs.swap(rhs);
303 }
304 
305 
306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
307 
308 } // End namespace Foam
309 
310 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311 
312 #include "autoPtrI.H"
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 
316 #endif
317 
318 // ************************************************************************* //
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Swap
void Swap(DynamicList< T, SizeMinA > &a, DynamicList< T, SizeMinB > &b)
Definition: DynamicList.H:429
Foam::autoPtr::swap
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition: autoPtrI.H:128
Foam::autoPtr::element_type
T element_type
Type of object being managed.
Definition: autoPtr.H:76
autoPtrI.H
Foam::autoPtr::pointer
T * pointer
Pointer to type of object being managed.
Definition: autoPtr.H:79
Foam::autoPtr::valid
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: autoPtr.H:272
Foam::autoPtr::operator*
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:154
Foam::autoPtr::operator=
void operator=(T *p)=delete
No copy assignment from plain pointer (uncontrolled access)
Foam::autoPtr::empty
bool empty() const noexcept
Deprecated(2020-07) True if the managed pointer is null.
Definition: autoPtr.H:278
Foam::autoPtr::NewFrom
static autoPtr< T > NewFrom(Args &&... args)
Construct autoPtr from derived type with forwarding arguments.
Foam::autoPtr::set
void set(T *p) noexcept
Deprecated(2018-02) Identical to reset().
Definition: autoPtr.H:288
Foam::autoPtr::~autoPtr
~autoPtr() noexcept
Deletes the managed pointer.
Definition: autoPtrI.H:91
Foam::autoPtr::release
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
Foam::autoPtr::get
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtr.H:152
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::autoPtr::good
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
Foam::autoPtr::ptr
T * ptr() noexcept
Same as release().
Definition: autoPtr.H:172
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
U
U
Definition: pEqn.H:72
Foam_autoPtr_castOperator
#define Foam_autoPtr_castOperator
Definition: autoPtr.H:50
Foam::autoPtr::ref
T & ref()
Return reference to the managed object without nullptr checking.
Definition: autoPtr.H:161
stdFoam.H
bool
bool
Definition: EEqn.H:20
Foam::autoPtr::autoPtr
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition: autoPtrI.H:53
Foam::FOAM_DEPRECATED_FOR
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:69
Foam::autoPtr::clear
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:176
Foam::autoPtr::autoPtr
autoPtr(const autoPtr< T > &ap) noexcept
A move construct disguised as a copy construct (transfers ownership)
Definition: autoPtr.H:127
args
Foam::argList args(argc, argv)
Foam::autoPtr::clone
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.