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>&& other) noexcept
118 {
119  if (&other != this)
120  {
121  // Ignore self-assignment
122  reset(other.release());
123  }
124 }
125 
126 
127 template<class T>
128 inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
129 {
130  // Swap is just copy/assign for pointer and enum types
131  // Self-swap is effectively ignored
132  T* p = ptr_;
133  ptr_ = other.ptr_;
134  other.ptr_ = p;
135 }
136 
137 
138 template<class T>
139 template<class... Args>
141 {
142  if (ptr_)
143  {
144  return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
145  }
146 
147  return autoPtr<T>();
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
152 
153 template<class T>
155 {
156  if (!ptr_)
157  {
159  << "unallocated autoPtr of type " << typeid(T).name()
160  << abort(FatalError);
161  }
162  return *ptr_;
163 }
164 
165 
166 template<class T>
167 inline const T& Foam::autoPtr<T>::operator*() const
168 {
169  return const_cast<autoPtr<T>*>(this)->operator*();
170 }
171 
172 
173 template<class T>
175 {
176  if (!ptr_)
177  {
179  << "unallocated autoPtr of type " << typeid(T).name()
180  << abort(FatalError);
181  }
182  return ptr_;
183 }
184 
185 
186 template<class T>
187 inline const T* Foam::autoPtr<T>::operator->() const
188 {
189  return const_cast<autoPtr<T>*>(this)->operator->();
190 }
191 
192 
193 template<class T>
195 {
196  return operator*();
197 }
198 
199 
200 template<class T>
201 inline const T& Foam::autoPtr<T>::operator()() const
202 {
203  return operator*();
204 }
205 
206 
207 template<class T>
208 inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
209 {
210  if (this != &ap)
211  {
212  // Ignore self-assignment
213  reset(ap.release());
214  }
215 }
216 
217 
218 template<class T>
219 template<class U>
220 inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
221 {
222  if (this != &ap)
223  {
224  // Ignore self-assignment
225  reset(ap.release());
226  }
227 }
228 
229 
230 template<class T>
231 inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
232 {
233  reset(nullptr);
234 }
235 
236 
237 // ************************************************************************* //
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
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:128
Foam::autoPtr::operator->
T * operator->()
Dereferences (non-const) pointer to the managed object.
Definition: autoPtrI.H:174
Foam::autoPtr::operator()
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:194
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::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 pointer.
Definition: autoPtrI.H:91
Foam::autoPtr::release
T * release() noexcept
Release ownership and return the pointer.
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
reset
meshPtr reset(new Foam::fvMesh(Foam::IOobject(regionName, runTime.timeName(), runTime, Foam::IOobject::MUST_READ), false))
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
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::operator*
tmp< faMatrix< Type > > operator*(const areaScalarField &, const faMatrix< Type > &)
Foam::autoPtr::autoPtr
constexpr autoPtr() noexcept
Construct with no managed pointer.
Definition: autoPtrI.H:53
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
args
Foam::argList args(argc, argv)
Foam::autoPtr::clone
autoPtr< T > clone(Args &&... args) const
Copy construct by invoking clone on underlying managed object.