tmpNrcI.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) 2016-2025 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  type_(TMP),
35  ptr_(tPtr)
36 {}
37 
38 
39 template<class T>
40 inline Foam::tmpNrc<T>::tmpNrc(const T& tRef)
41 :
42  type_(CONST_REF),
43  ptr_(const_cast<T*>(&tRef))
44 {}
45 
46 
47 template<class T>
49 :
50  type_(t.type_),
51  ptr_(t.ptr_)
52 {
53  if (isTmp())
54  {
55  if (ptr_)
56  {
57  t.type_ = CONST_REF;
58  }
59  else
60  {
62  << "Attempted copy of a deallocated " << typeName()
63  << abort(FatalError);
64  }
65  }
66 }
67 
68 
69 template<class T>
71 :
72  type_(t.type_),
73  ptr_(t.ptr_)
74 {
75  if (isTmp())
76  {
77  t.ptr_ = 0;
78  }
79 }
80 
81 
82 template<class T>
83 inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t, bool allowTransfer)
84 :
85  type_(t.type_),
86  ptr_(t.ptr_)
87 {
88  if (isTmp())
89  {
90  if (ptr_)
91  {
92  if (allowTransfer)
93  {
94  t.ptr_ = 0;
95  }
96  else
97  {
98  type_ = CONST_REF;
99  }
100  }
101  }
102 }
103 
104 
105 template<class T>
107 {
108  clear();
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
114 template<class T>
115 inline bool Foam::tmpNrc<T>::isTmp() const
116 {
117  return type_ == TMP;
118 }
119 
120 
121 template<class T>
122 inline bool Foam::tmpNrc<T>::empty() const
123 {
124  return (isTmp() && !ptr_);
125 }
126 
127 
128 template<class T>
129 inline bool Foam::tmpNrc<T>::valid() const
130 {
131  return (!isTmp() || (isTmp() && ptr_));
132 }
133 
134 
135 template<class T>
137 {
138  return "tmpNrc<" + word(typeid(T).name()) + '>';
139 }
140 
141 
142 template<class T>
144 {
145  if (isTmp())
146  {
147  if (!ptr_)
148  {
150  << typeName() << " deallocated"
152  }
153  }
154  else
155  {
157  << "Attempt to acquire non-const reference to const object"
158  << " from a " << typeName()
159  << abort(FatalError);
160  }
161 
162  return *ptr_;
163 }
164 
165 
166 template<class T>
167 inline T* Foam::tmpNrc<T>::ptr() const
168 {
169  if (isTmp())
170  {
171  if (!ptr_)
172  {
174  << typeName() << " deallocated"
175  << abort(FatalError);
176  }
177 
178  T* ptr = ptr_;
179  ptr_ = 0;
180 
181  return ptr;
182  }
183  else
184  {
185  return ptr_->clone().ptr();
186  }
187 }
188 
189 
190 template<class T>
191 inline void Foam::tmpNrc<T>::clear() const
192 {
193  if (isTmp() && ptr_)
194  {
195  delete ptr_;
196  ptr_ = 0;
197  }
198 }
199 
200 
201 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
202 
203 template<class T>
204 inline const T& Foam::tmpNrc<T>::operator()() const
205 {
206  if (isTmp())
207  {
208  if (!ptr_)
209  {
211  << typeName() << " deallocated"
212  << abort(FatalError);
213  }
214  }
215 
216  // Return const reference
217  return *ptr_;
218 }
219 
220 
221 template<class T>
222 inline Foam::tmpNrc<T>::operator const T&() const
223 {
224  return operator()();
225 }
226 
227 
228 template<class T>
230 {
231  if (isTmp())
232  {
233  if (!ptr_)
234  {
236  << typeName() << " deallocated"
237  << abort(FatalError);
238  }
239  }
240  else
241  {
243  << "Attempt to cast const object to non-const for a " << typeName()
244  << abort(FatalError);
245  }
246 
247  return ptr_;
248 }
249 
250 
251 template<class T>
252 inline const T* Foam::tmpNrc<T>::operator->() const
253 {
254  if (isTmp() && !ptr_)
255  {
257  << typeName() << " deallocated"
258  << abort(FatalError);
259  }
260 
261  return ptr_;
262 }
263 
264 
265 template<class T>
266 inline void Foam::tmpNrc<T>::operator=(T* tPtr)
267 {
268  clear();
269 
270  if (!tPtr)
271  {
273  << "Attempted copy of a deallocated " << typeName()
274  << abort(FatalError);
275  }
276 
277  type_ = TMP;
278  ptr_ = tPtr;
279 }
280 
281 
282 template<class T>
284 {
285  clear();
286 
287  if (t.isTmp())
288  {
289  type_ = TMP;
290 
291  if (!t.ptr_)
292  {
294  << "Attempted assignment to a deallocated " << typeName()
295  << abort(FatalError);
296  }
297 
298  ptr_ = t.ptr_;
299  t.ptr_ = 0;
300  }
301  else
302  {
304  << "Attempted assignment to a const reference to an object"
305  << " of type " << typeid(T).name()
306  << abort(FatalError);
307  }
308 }
309 
310 
311 template<class T>
313 {
314  clear();
315 
316  type_ = t.type_;
317  ptr_ = t.ptr_;
318 
319  if (isTmp())
320  {
321  t.ptr_ = 0;
322  }
323 }
324 
325 
326 // ************************************************************************* //
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:53
void operator=(T *)
Assignment to pointer changing this tmpNrc to a temporary T.
Definition: tmpNrcI.H:266
bool valid() const
Is this temporary object valid,.
Definition: tmpNrcI.H:129
T & ref()
Return non-const reference or generate a fatal error.
Definition: tmpNrcI.H:143
T * operator->()
Return object pointer.
Definition: tmpNrcI.H:229
word typeName() const
Return the type name of the tmpNrc.
Definition: tmpNrcI.H:136
~tmpNrc()
Destructor: deletes object if it is temporary.
Definition: tmpNrcI.H:106
bool empty() const
Return true if this temporary object empty,.
Definition: tmpNrcI.H:122
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpNrcI.H:115
tmpNrc(T *=0)
Store object pointer.
Definition: tmpNrcI.H:32
T * ptr() const
Return tmpNrc pointer for reuse.
Definition: tmpNrcI.H:167
void clear() const
If object pointer points to valid object:
Definition: tmpNrcI.H:191
const T & operator()() const
Const dereference operator.
Definition: tmpNrcI.H:204
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError