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-2020 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 T* Foam::autoPtr<T>::release() noexcept
101 {
102  T* p = ptr_;
103  ptr_ = nullptr;
104  return p;
105 }
106 
107 
108 template<class T>
109 inline void Foam::autoPtr<T>::reset(T* p) noexcept
110 {
111  if (ptr_) delete ptr_;
112  ptr_ = p;
113 }
114 
115 
116 template<class T>
117 inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept
118 {
119  reset(ap.release());
120 }
121 
122 
123 template<class T>
124 inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
125 {
126  // Swap is just copy/assign for pointer and enum types
127  // Self-swap is effectively ignored
128  T* p = ptr_;
129  ptr_ = other.ptr_;
130  other.ptr_ = p;
131 }
132 
133 
134 template<class T>
135 template<class... Args>
137 {
138  if (ptr_)
139  {
140  return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
141  }
142 
143  return autoPtr<T>();
144 }
145 
146 
147 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
148 
149 template<class T>
151 {
152  if (!ptr_)
153  {
155  << "unallocated autoPtr of type " << typeid(T).name()
156  << abort(FatalError);
157  }
158  return *ptr_;
159 }
160 
161 
162 template<class T>
163 inline const T& Foam::autoPtr<T>::operator*() const
164 {
165  return const_cast<autoPtr<T>*>(this)->operator*();
166 }
167 
168 
169 template<class T>
171 {
172  if (!ptr_)
173  {
175  << "unallocated autoPtr of type " << typeid(T).name()
176  << abort(FatalError);
177  }
178  return ptr_;
179 }
180 
181 
182 template<class T>
183 inline const T* Foam::autoPtr<T>::operator->() const
184 {
185  return const_cast<autoPtr<T>*>(this)->operator->();
186 }
187 
188 
189 template<class T>
191 {
192  return operator*();
193 }
194 
195 
196 template<class T>
197 inline const T& Foam::autoPtr<T>::operator()() const
198 {
199  return operator*();
200 }
201 
202 
203 template<class T>
204 inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
205 {
206  if (this != &ap)
207  {
208  // Ignore self-assignment
209  reset(ap.release());
210  }
211 }
212 
213 
214 template<class T>
215 template<class U>
216 inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
217 {
218  if (this != &ap)
219  {
220  // Ignore self-assignment
221  reset(ap.release());
222  }
223 }
224 
225 
226 template<class T>
227 inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
228 {
229  reset(nullptr);
230 }
231 
232 
233 // ************************************************************************* //
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:109
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:124
Foam::autoPtr::operator->
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition: autoPtrI.H:170
Foam::autoPtr::operator()
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:190
Foam::autoPtr::operator*
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:150
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:100
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
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:381
Foam::autoPtr::operator=
void operator=(autoPtr< T > &&ap) noexcept
Transfer object ownership from parameter.
Definition: autoPtrI.H:204
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::autoPtr::autoPtr
constexpr autoPtr() noexcept
Construct with no managed object.
Definition: autoPtrI.H:53
args
Foam::argList args(argc, argv)
Foam::autoPtr::clone
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.