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-2018 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 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
133 
134 template<class T>
136 {
137  if (!ptr_)
138  {
140  << "object of type " << typeid(T).name()
141  << " is not allocated"
142  << abort(FatalError);
143  }
144 
145  return *ptr_;
146 }
147 
148 
149 template<class T>
150 inline const T& Foam::autoPtr<T>::operator()() const
151 {
152  if (!ptr_)
153  {
155  << "object of type " << typeid(T).name()
156  << " is not allocated"
157  << abort(FatalError);
158  }
159 
160  return *ptr_;
161 }
162 
163 
164 template<class T>
166 {
167  if (!ptr_)
168  {
170  << "object of type " << typeid(T).name()
171  << " is not allocated"
172  << abort(FatalError);
173  }
174 
175  return *ptr_;
176 }
177 
178 
179 template<class T>
180 inline const T& Foam::autoPtr<T>::operator*() const
181 {
182  if (!ptr_)
183  {
185  << "object of type " << typeid(T).name()
186  << " is not allocated"
187  << abort(FatalError);
188  }
189 
190  return *ptr_;
191 }
192 
193 
194 template<class T>
195 inline Foam::autoPtr<T>::operator const T&() const
196 {
197  return operator()();
198 }
199 
200 
201 template<class T>
203 {
204  if (!ptr_)
205  {
207  << "object of type " << typeid(T).name()
208  << " is not allocated"
209  << abort(FatalError);
210  }
211 
212  return ptr_;
213 }
214 
215 
216 template<class T>
217 inline const T* Foam::autoPtr<T>::operator->() const
218 {
219  return const_cast<autoPtr<T>&>(*this).operator->();
220 }
221 
222 
223 template<class T>
225 {
226  reset(p);
227 }
228 
229 
230 template<class T>
232 {
233  if (this != &ap)
234  {
235  reset(const_cast<autoPtr<T>&>(ap).ptr());
236  }
237 }
238 
239 
240 // ************************************************************************* //
tUEqn clear()
T & operator*()
Return reference to the object data.
Definition: autoPtrI.H:165
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set)
Definition: autoPtrI.H:76
void clear()
Delete object (if the pointer is valid) and set pointer to.
Definition: autoPtrI.H:126
autoPtr(T *=nullptr)
Store object pointer.
Definition: autoPtrI.H:32
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void operator=(T *)
Take over the object pointer from parameter.
Definition: autoPtrI.H:224
~autoPtr()
Destructor, delete object if pointer is not nullptr.
Definition: autoPtrI.H:67
const volScalarField & T
T * operator->()
Return object pointer.
Definition: autoPtrI.H:202
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
volScalarField & p
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:135