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-2023 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  t.type_ = CONST_REF;
99  }
100  }
101  else
102  {
104  << "Attempted copy of a deallocated " << typeName()
105  << abort(FatalError);
106  }
107  }
108 }
109 
110 
111 template<class T>
113 {
114  clear();
115 }
116 
117 
118 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
119 
120 template<class T>
121 inline bool Foam::tmpNrc<T>::isTmp() const
122 {
123  return type_ == TMP;
124 }
125 
126 
127 template<class T>
128 inline bool Foam::tmpNrc<T>::empty() const
129 {
130  return (isTmp() && !ptr_);
131 }
132 
133 
134 template<class T>
135 inline bool Foam::tmpNrc<T>::valid() const
136 {
137  return (!isTmp() || (isTmp() && ptr_));
138 }
139 
140 
141 template<class T>
143 {
144  return "tmpNrc<" + word(typeid(T).name()) + '>';
145 }
146 
147 
148 template<class T>
150 {
151  if (isTmp())
152  {
153  if (!ptr_)
154  {
156  << typeName() << " deallocated"
158  }
159  }
160  else
161  {
163  << "Attempt to acquire non-const reference to const object"
164  << " from a " << typeName()
165  << abort(FatalError);
166  }
167 
168  return *ptr_;
169 }
170 
171 
172 template<class T>
173 inline T* Foam::tmpNrc<T>::ptr() const
174 {
175  if (isTmp())
176  {
177  if (!ptr_)
178  {
180  << typeName() << " deallocated"
181  << abort(FatalError);
182  }
183 
184  T* ptr = ptr_;
185  ptr_ = 0;
186 
187  return ptr;
188  }
189  else
190  {
191  return ptr_->clone().ptr();
192  }
193 }
194 
195 
196 template<class T>
197 inline void Foam::tmpNrc<T>::clear() const
198 {
199  if (isTmp() && ptr_)
200  {
201  delete ptr_;
202  ptr_ = 0;
203  }
204 }
205 
206 
207 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
208 
209 template<class T>
210 inline const T& Foam::tmpNrc<T>::operator()() const
211 {
212  if (isTmp())
213  {
214  if (!ptr_)
215  {
217  << typeName() << " deallocated"
218  << abort(FatalError);
219  }
220  }
221 
222  // Return const reference
223  return *ptr_;
224 }
225 
226 
227 template<class T>
228 inline Foam::tmpNrc<T>::operator const T&() const
229 {
230  return operator()();
231 }
232 
233 
234 template<class T>
236 {
237  if (isTmp())
238  {
239  if (!ptr_)
240  {
242  << typeName() << " deallocated"
243  << abort(FatalError);
244  }
245  }
246  else
247  {
249  << "Attempt to cast const object to non-const for a " << typeName()
250  << abort(FatalError);
251  }
252 
253  return ptr_;
254 }
255 
256 
257 template<class T>
258 inline const T* Foam::tmpNrc<T>::operator->() const
259 {
260  if (isTmp() && !ptr_)
261  {
263  << typeName() << " deallocated"
264  << abort(FatalError);
265  }
266 
267  return ptr_;
268 }
269 
270 
271 template<class T>
272 inline void Foam::tmpNrc<T>::operator=(T* tPtr)
273 {
274  clear();
275 
276  if (!tPtr)
277  {
279  << "Attempted copy of a deallocated " << typeName()
280  << abort(FatalError);
281  }
282 
283  type_ = TMP;
284  ptr_ = tPtr;
285 }
286 
287 
288 template<class T>
290 {
291  clear();
292 
293  if (t.isTmp())
294  {
295  type_ = TMP;
296 
297  // if (!t.ptr_)
298  // {
299  // FatalErrorInFunction
300  // << "Attempted assignment to a deallocated " << typeName()
301  // << abort(FatalError);
302  // }
303 
304  ptr_ = t.ptr_;
305  t.ptr_ = 0;
306  }
307  else
308  {
310  << "Attempted assignment to a const reference to an object"
311  << " of type " << typeid(T).name()
312  << abort(FatalError);
313  }
314 }
315 
316 
317 template<class T>
318 inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>&& t)
319 {
320  clear();
321 
322  type_ = t.type_;
323  ptr_ = t.ptr_;
324 
325  if (isTmp())
326  {
327  t.ptr_ = 0;
328  }
329 }
330 
331 
332 // ************************************************************************* //
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:272
bool valid() const
Is this temporary object valid,.
Definition: tmpNrcI.H:135
T & ref()
Return non-const reference or generate a fatal error.
Definition: tmpNrcI.H:149
T * operator->()
Return object pointer.
Definition: tmpNrcI.H:235
word typeName() const
Return the type name of the tmpNrc.
Definition: tmpNrcI.H:142
~tmpNrc()
Destructor: deletes temporary object when the reference count is 0.
Definition: tmpNrcI.H:112
bool empty() const
Return true if this temporary object empty,.
Definition: tmpNrcI.H:128
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpNrcI.H:121
tmpNrc(T *=0)
Store object pointer.
Definition: tmpNrcI.H:32
T * ptr() const
Return tmpNrc pointer for reuse.
Definition: tmpNrcI.H:173
void clear() const
If object pointer points to valid object:
Definition: tmpNrcI.H:197
const T & operator()() const
Const dereference operator.
Definition: tmpNrcI.H:210
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()
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)