autoPtrI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include <typeinfo>
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class T>
33 :
34  ptr_(p)
35 {}
36 
37 
38 template<class T>
40 :
41  ptr_(ap.ptr_)
42 {
43  ap.ptr_ = nullptr;
44 }
45 
46 
47 template<class T>
48 inline Foam::autoPtr<T>::autoPtr(const autoPtr<T>& ap, const bool reuse)
49 {
50  if (reuse)
51  {
52  ptr_ = ap.ptr_;
53  ap.ptr_ = nullptr;
54  }
55  else if (ap.valid())
56  {
57  ptr_ = ap().clone().ptr();
58  }
59  else
60  {
61  ptr_ = nullptr;
62  }
63 }
64 
65 
66 template<class T>
68 {
69  clear();
70 }
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
75 template<class T>
76 inline bool Foam::autoPtr<T>::empty() const
77 {
78  return !ptr_;
79 }
80 
81 
82 template<class T>
83 inline bool Foam::autoPtr<T>::valid() const
84 {
85  return ptr_;
86 }
87 
88 
89 template<class T>
91 {
92  T* ptr = ptr_;
93  ptr_ = nullptr;
94  return ptr;
95 }
96 
97 
98 template<class T>
99 inline void Foam::autoPtr<T>::set(T* p)
100 {
101  if (ptr_)
102  {
104  << "object of type " << typeid(T).name()
105  << " already allocated"
106  << abort(FatalError);
107  }
108 
109  ptr_ = p;
110 }
111 
112 
113 template<class T>
115 {
116  if (ptr_)
117  {
118  delete ptr_;
119  }
120 
121  ptr_ = p;
122 }
123 
124 
125 template<class T>
127 {
128  reset(nullptr);
129 }
130 
131 
132 template<class T>
134 {
135  if (ptr_)
136  {
137  delete ptr_;
138  ptr_ = nullptr;
139  return true;
140  }
141  else
142  {
143  return false;
144  }
145 }
146 
147 
148 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
149 
150 template<class T>
152 {
153  if (!ptr_)
154  {
156  << "object of type " << typeid(T).name()
157  << " is not allocated"
158  << abort(FatalError);
159  }
160 
161  return *ptr_;
162 }
163 
164 
165 template<class T>
166 inline const T& Foam::autoPtr<T>::operator()() const
167 {
168  if (!ptr_)
169  {
171  << "object of type " << typeid(T).name()
172  << " is not allocated"
173  << abort(FatalError);
174  }
175 
176  return *ptr_;
177 }
178 
179 
180 template<class T>
182 {
183  if (!ptr_)
184  {
186  << "object of type " << typeid(T).name()
187  << " is not allocated"
188  << abort(FatalError);
189  }
190 
191  return *ptr_;
192 }
193 
194 
195 template<class T>
196 inline const T& Foam::autoPtr<T>::operator*() const
197 {
198  if (!ptr_)
199  {
201  << "object of type " << typeid(T).name()
202  << " is not allocated"
203  << abort(FatalError);
204  }
205 
206  return *ptr_;
207 }
208 
209 
210 template<class T>
211 inline Foam::autoPtr<T>::operator const T&() const
212 {
213  return operator()();
214 }
215 
216 
217 template<class T>
219 {
220  if (!ptr_)
221  {
223  << "object of type " << typeid(T).name()
224  << " is not allocated"
225  << abort(FatalError);
226  }
227 
228  return ptr_;
229 }
230 
231 
232 template<class T>
233 inline const T* Foam::autoPtr<T>::operator->() const
234 {
235  return const_cast<autoPtr<T>&>(*this).operator->();
236 }
237 
238 
239 template<class T>
241 {
242  reset(p);
243 }
244 
245 
246 template<class T>
248 {
249  if (this != &ap)
250  {
251  reset(const_cast<autoPtr<T>&>(ap).ptr());
252  }
253 }
254 
255 
256 // ************************************************************************* //
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
bool cleared()
Delete object if the pointer is valid and return true.
Definition: autoPtrI.H:133
autoPtr(T *=nullptr)
Store object pointer.
Definition: autoPtrI.H:32
~autoPtr()
Destructor, delete object if pointer is not nullptr.
Definition: autoPtrI.H:67
void operator=(T *)
Take over the object pointer from parameter.
Definition: autoPtrI.H:240
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
T & operator*()
Return reference to the object data.
Definition: autoPtrI.H:181
T * operator->()
Return object pointer.
Definition: autoPtrI.H:218
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:151
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set)
Definition: autoPtrI.H:76
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
errorManip< error > abort(error &err)
Definition: errorManip.H:131
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
void T(GeometricField< Type, GeoMesh, PrimitiveField1 > &gf, const GeometricField< Type, GeoMesh, PrimitiveField2 > &gf1)
volScalarField & p