autoPtrI.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 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include <typeinfo>
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class T>
35 template<class... Args>
37 {
38  return autoPtr<T>(new T(std::forward<Args>(args)...));
39 }
40 
41 
42 template<class T>
43 template<class U, class... Args>
45 {
46  return autoPtr<T>(new U(std::forward<Args>(args)...));
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
52 template<class T>
53 inline constexpr Foam::autoPtr<T>::autoPtr() noexcept
54 :
55  ptr_(nullptr)
56 {}
57 
58 
59 template<class T>
60 inline constexpr Foam::autoPtr<T>::autoPtr(std::nullptr_t) noexcept
61 :
62  ptr_(nullptr)
63 {}
64 
65 
66 template<class T>
67 inline Foam::autoPtr<T>::autoPtr(T* p) noexcept
68 :
69  ptr_(p)
70 {}
71 
72 
73 template<class T>
75 :
76  ptr_(ap.release())
77 {}
78 
79 
80 template<class T>
81 template<class U>
83 :
84  ptr_(ap.release())
85 {}
86 
87 
88 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
89 
90 template<class T>
91 inline Foam::autoPtr<T>::~autoPtr() noexcept
92 {
93  reset(nullptr);
94 }
95 
96 
97 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
98 
99 template<class T>
100 inline bool Foam::autoPtr<T>::empty() const noexcept
101 {
102  return !ptr_;
103 }
104 
105 
106 template<class T>
107 inline bool Foam::autoPtr<T>::valid() const noexcept
108 {
109  return ptr_;
110 }
111 
112 
113 template<class T>
114 inline T* Foam::autoPtr<T>::get() noexcept
115 {
116  return ptr_;
117 }
118 
119 
120 template<class T>
121 inline const T* Foam::autoPtr<T>::get() const noexcept
122 {
123  return ptr_;
124 }
125 
126 
127 template<class T>
129 {
130  return *ptr_;
131 }
132 
133 
134 template<class T>
135 inline T* Foam::autoPtr<T>::release() noexcept
136 {
137  T* p = ptr_;
138  ptr_ = nullptr;
139  return p;
140 }
141 
142 
143 template<class T>
144 inline T* Foam::autoPtr<T>::ptr() noexcept
145 {
146  return release();
147 }
148 
149 
150 template<class T>
151 inline void Foam::autoPtr<T>::clear() noexcept
152 {
153  reset(nullptr);
154 }
155 
156 
157 template<class T>
158 inline void Foam::autoPtr<T>::reset(T* p) noexcept
159 {
160  if (ptr_) delete ptr_;
161  ptr_ = p;
162 }
163 
164 
165 template<class T>
166 inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept
167 {
168  reset(ap.release());
169 }
170 
171 
172 template<class T>
173 inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
174 {
175  // Self-swap is effectively ignored
176  T* p = ptr_;
177  ptr_ = other.ptr_;
178  other.ptr_ = p;
179 }
180 
181 
182 template<class T>
183 template<class... Args>
185 {
186  if (ptr_)
187  {
188  return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
189  }
190 
191  return autoPtr<T>();
192 }
193 
194 
195 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
196 
197 template<class T>
199 {
200  if (!ptr_)
201  {
203  << "object of type " << typeid(T).name() << " is unallocated"
204  << abort(FatalError);
205  }
206  return *ptr_;
207 }
208 
209 
210 template<class T>
211 inline const T& Foam::autoPtr<T>::operator*() const
212 {
213  return const_cast<autoPtr<T>*>(this)->operator*();
214 }
215 
216 
217 template<class T>
219 {
220  if (!ptr_)
221  {
223  << "object of type " << typeid(T).name() << " is unallocated"
224  << abort(FatalError);
225  }
226  return ptr_;
227 }
228 
229 
230 template<class T>
231 inline const T* Foam::autoPtr<T>::operator->() const
232 {
233  return const_cast<autoPtr<T>*>(this)->operator->();
234 }
235 
236 
237 template<class T>
239 {
240  return operator*();
241 }
242 
243 
244 template<class T>
245 inline const T& Foam::autoPtr<T>::operator()() const
246 {
247  return operator*();
248 }
249 
250 
251 template<class T>
252 inline Foam::autoPtr<T>::operator bool() const noexcept
253 {
254  return ptr_;
255 }
256 
257 
258 template<class T>
259 inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
260 {
261  if (this != &ap)
262  {
263  // Ignore self-assignment
264  reset(ap.release());
265  }
266 }
267 
268 
269 template<class T>
270 template<class U>
271 inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
272 {
273  if (this != &ap)
274  {
275  // Ignore self-assignment
276  reset(ap.release());
277  }
278 }
279 
280 
281 template<class T>
282 inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
283 {
284  reset();
285 }
286 
287 
288 // ************************************************************************* //
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::operator->
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition: autoPtrI.H:218
Foam::autoPtr::valid
bool valid() const noexcept
True if the managed pointer is non-null.
Definition: autoPtrI.H:107
Foam::autoPtr::operator()
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:238
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.
error.H
Foam::autoPtr::~autoPtr
~autoPtr() noexcept
Deletes the managed object if such is present.
Definition: autoPtrI.H:91
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
Definition: autoPtrI.H:135
Foam::autoPtr::ptr
T * ptr() noexcept
Definition: autoPtrI.H:144
Foam::FatalError
error FatalError
Foam::autoPtr::get
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtrI.H:114
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
T
const volScalarField & T
Definition: createFieldRefs.H:2
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::autoPtr::operator=
void operator=(autoPtr< T > &&ap) noexcept
Transfer object ownership from parameter.
Definition: autoPtrI.H:259
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
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
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