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  // Self-swap is effectively ignored
127  T* p = ptr_;
128  ptr_ = other.ptr_;
129  other.ptr_ = p;
130 }
131 
132 
133 template<class T>
134 template<class... Args>
136 {
137  if (ptr_)
138  {
139  return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
140  }
141 
142  return autoPtr<T>();
143 }
144 
145 
146 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
147 
148 template<class T>
150 {
151  if (!ptr_)
152  {
154  << "unallocated autoPtr of type " << typeid(T).name()
155  << abort(FatalError);
156  }
157  return *ptr_;
158 }
159 
160 
161 template<class T>
162 inline const T& Foam::autoPtr<T>::operator*() const
163 {
164  return const_cast<autoPtr<T>*>(this)->operator*();
165 }
166 
167 
168 template<class T>
170 {
171  if (!ptr_)
172  {
174  << "unallocated autoPtr of type " << typeid(T).name()
175  << abort(FatalError);
176  }
177  return ptr_;
178 }
179 
180 
181 template<class T>
182 inline const T* Foam::autoPtr<T>::operator->() const
183 {
184  return const_cast<autoPtr<T>*>(this)->operator->();
185 }
186 
187 
188 template<class T>
190 {
191  return operator*();
192 }
193 
194 
195 template<class T>
196 inline const T& Foam::autoPtr<T>::operator()() const
197 {
198  return operator*();
199 }
200 
201 
202 template<class T>
203 inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
204 {
205  if (this != &ap)
206  {
207  // Ignore self-assignment
208  reset(ap.release());
209  }
210 }
211 
212 
213 template<class T>
214 template<class U>
215 inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
216 {
217  if (this != &ap)
218  {
219  // Ignore self-assignment
220  reset(ap.release());
221  }
222 }
223 
224 
225 template<class T>
226 inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
227 {
228  reset(nullptr);
229 }
230 
231 
232 // ************************************************************************* //
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:169
Foam::autoPtr::operator()
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:189
Foam::autoPtr::operator*
T & operator*()
Return reference to the managed object.
Definition: autoPtrI.H:149
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: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:372
Foam::autoPtr::operator=
void operator=(autoPtr< T > &&ap) noexcept
Transfer object ownership from parameter.
Definition: autoPtrI.H:203
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.