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-2019 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_copyAssign
52 #define Foam_autoPtr_castOperator
53 
54 #include <utility>
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 /*---------------------------------------------------------------------------*\
62  Class autoPtr Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class T>
66 class autoPtr
67 {
68  //- Pointer to managed object
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 object.
107  inline constexpr autoPtr() noexcept;
108 
109  //- Construct with no managed object (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 object if such is present
138  inline ~autoPtr() noexcept;
139 
140 
141  // Member Functions
142 
143  // Check
144 
145  //- True if the managed pointer is null
146  inline bool empty() const noexcept;
147 
148  //- True if the managed pointer is non-null
149  inline bool valid() const noexcept;
150 
151 
152  // Access
153 
154  //- Return pointer to managed object without nullptr checking.
155  // Pointer remains under autoPtr management.
156  inline T* get() noexcept;
157 
158  //- Return const pointer to managed object without nullptr checking.
159  // Pointer remains under autoPtr management.
160  inline const T* get() const noexcept;
161 
162  //- Return reference to the managed object without nullptr checking.
163  // When get() == nullptr, additional guards may be required to avoid
164  // inadvertent access to a nullptr.
165  inline T& ref();
166 
167 
168  // Edit
169 
170  //- Return pointer to the managed object and release ownership.
171  inline T* release() noexcept;
172 
173  //- Return pointer to the managed object and release ownership.
174  //- Identical behaviour to release().
175  // \note Provided for method naming consistent with Foam::tmp
176  inline T* ptr() noexcept;
177 
178  //- Delete managed object and set pointer to nullptr
179  inline void clear() noexcept;
180 
181  //- Delete managed object and set to new given pointer
182  inline void reset(T* p = nullptr) noexcept;
183 
184  //- Delete managed object and set to new given pointer
185  // \remark Same as move assign, but better for code documentation
186  inline void reset(autoPtr<T>&& ap) noexcept;
187 
188  //- Swaps the managed object with other autoPtr.
189  inline void swap(autoPtr<T>& other) noexcept;
190 
191 
192  // Other
193 
194  //- Construct copy by invoking clone on underlying managed object
195  // A no-op if no pointer is managed
196  // \param args list of arguments for clone
197  template<class... Args>
198  inline autoPtr<T> clone(Args&&... args) const;
199 
200 
201  // Member Operators
202 
203  //- Return reference to the managed object.
204  // FatalError if no pointer is managed
205  inline T& operator*();
206 
207  //- Return const reference to the object.
208  // FatalError if no pointer is managed
209  inline const T& operator*() const;
210 
211  //- Dereferences (non-const) pointer to the managed object
212  // FatalError if no pointer is managed
213  inline T* operator->();
214 
215  //- Dereferences (const) pointer to the managed object
216  // FatalError if no pointer is managed
217  inline const T* operator->() const;
218 
219  //- Return reference to the object data.
220  // FatalError if no pointer is managed
221  inline T& operator()();
222 
223  //- Return const reference to the object data
224  // FatalError if no pointer is managed
225  inline const T& operator()() const;
226 
227  //- Deprecated(2019-01) Automatic cast conversion to underlying type
228  // FatalError if no pointer is managed
229  // \deprecated(2019-01) Can result in inadvertent conversions
230  // where the user should really know or check if the pointer
231  // is valid prior to using.
232  #ifdef Foam_autoPtr_castOperator
233  operator const T&() const { return operator*(); }
234  #else
235  operator const T&() const = delete;
236  #endif
237 
238  //- Cast to pointer type
239  operator const T*() const noexcept { return get(); }
240 
241  //- Cast to pointer type
242  operator T*() noexcept { return get(); }
243 
244  //- True if the managed pointer is non-null - same as valid()
245  explicit inline operator bool() const noexcept;
246 
247  //- Transfer object ownership from parameter
248  inline void operator=(autoPtr<T>&& ap) noexcept;
249 
250  //- Transfer object ownership from parameter
251  template<class U>
252  inline void operator=(autoPtr<U>&& ap) noexcept;
253 
254  #ifdef Foam_autoPtr_copyAssign
255  //- A move assignment disguised as a copy assignment
256  // \remark Non-standard definition - should just be movable
257  void operator=(const autoPtr<T>& ap) noexcept
258  {
259  operator=(std::move(const_cast<autoPtr<T>&>(ap)));
260  }
261  #else
262  void operator=(const autoPtr<T>& ap) = delete;
263  #endif
264 
265  //- Clear via assignment from literal nullptr
266  inline void operator=(std::nullptr_t) noexcept;
267 
268 
269  // Housekeeping
270 
271  //- Deprecated(2018-02) Identical to reset().
272  // \note Provided for backward compatibility - the older version
273  // enforced a run-time check (Fatal if pointer was already set)
274  // but this was rarely used.
275  // \deprecated(2018-02) Identical to reset().
276  void set(T* p) noexcept { reset(p); }
277 
278  //- Deprecated(2018-02) No copy assignment from plain pointer
279  // \deprecated(2018-02) Convenient, but uncontrolled access
280  void operator=(T* p) = delete;
281 };
282 
283 
284 // Global Functions
285 
286 //- Specializes the Swap algorithm for autoPtr.
287 // Swaps the pointers of lhs and rhs. Calls \c lhs.swap(rhs)
288 template<class T>
289 void Swap(autoPtr<T>& lhs, autoPtr<T>& rhs)
290 {
291  lhs.swap(rhs);
292 }
293 
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 } // End namespace Foam
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 
301 #include "autoPtrI.H"
302 
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 
305 #endif
306 
307 // ************************************************************************* //
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:158
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::autoPtr::swap
void swap(autoPtr< T > &other) noexcept
Swaps the managed object with other autoPtr.
Definition: autoPtrI.H:173
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
True if the managed pointer is non-null.
Definition: autoPtrI.H:107
Foam::Swap
void Swap(DynamicList< T, SizeMin1 > &a, DynamicList< T, SizeMin2 > &b)
Definition: DynamicListI.H:909
Foam::autoPtr::operator*
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:198
Foam::autoPtr::empty
bool empty() const noexcept
True if the managed pointer is null.
Definition: autoPtrI.H:100
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:275
Foam::autoPtr::~autoPtr
~autoPtr() noexcept
Deletes the managed object if such is present.
Definition: autoPtrI.H:91
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
Definition: autoPtrI.H:135
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::autoPtr::ptr
T * ptr() noexcept
Definition: autoPtrI.H:144
Foam::autoPtr::get
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtrI.H:114
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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::operator=
void operator=(autoPtr< T > &&ap) noexcept
Transfer object ownership from parameter.
Definition: autoPtrI.H:259
bool
bool
Definition: EEqn.H:20
Foam::autoPtr::autoPtr
constexpr autoPtr() noexcept
Construct with no managed object.
Definition: autoPtrI.H:53
Foam::autoPtr::clear
void clear() noexcept
Delete managed object and set pointer to nullptr.
Definition: autoPtrI.H:151
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
Construct copy by invoking clone on underlying managed object.
Foam::autoPtr::ref
T & ref()
Return reference to the managed object without nullptr checking.
Definition: autoPtrI.H:128