All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-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  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>
70 inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t, bool allowTransfer)
71 :
72  type_(t.type_),
73  ptr_(t.ptr_)
74 {
75  if (isTmp())
76  {
77  if (ptr_)
78  {
79  if (allowTransfer)
80  {
81  t.ptr_ = 0;
82  }
83  else
84  {
85  t.type_ = CONST_REF;
86  }
87  }
88  else
89  {
91  << "Attempted copy of a deallocated " << typeName()
92  << abort(FatalError);
93  }
94  }
95 }
96 
97 
98 template<class T>
100 {
101  clear();
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
107 template<class T>
108 inline bool Foam::tmpNrc<T>::isTmp() const
109 {
110  return type_ == TMP;
111 }
112 
113 
114 template<class T>
115 inline bool Foam::tmpNrc<T>::empty() const
116 {
117  return (isTmp() && !ptr_);
118 }
119 
120 
121 template<class T>
122 inline bool Foam::tmpNrc<T>::valid() const
123 {
124  return (!isTmp() || (isTmp() && ptr_));
125 }
126 
127 
128 template<class T>
130 {
131  return "tmpNrc<" + word(typeid(T).name()) + '>';
132 }
133 
134 
135 template<class T>
137 {
138  if (isTmp())
139  {
140  if (!ptr_)
141  {
143  << typeName() << " deallocated"
144  << abort(FatalError);
145  }
146  }
147  else
148  {
150  << "Attempt to acquire non-const reference to const object"
151  << " from a " << typeName()
152  << abort(FatalError);
153  }
154 
155  return *ptr_;
156 }
157 
158 
159 template<class T>
160 inline T* Foam::tmpNrc<T>::ptr() const
161 {
162  if (isTmp())
163  {
164  if (!ptr_)
165  {
167  << typeName() << " deallocated"
168  << abort(FatalError);
169  }
170 
171  T* ptr = ptr_;
172  ptr_ = 0;
173 
174  return ptr;
175  }
176  else
177  {
178  return ptr_->clone().ptr();
179  }
180 }
181 
182 
183 template<class T>
184 inline void Foam::tmpNrc<T>::clear() const
185 {
186  if (isTmp() && ptr_)
187  {
188  delete ptr_;
189  ptr_ = 0;
190  }
191 }
192 
193 
194 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
195 
196 template<class T>
197 inline const T& Foam::tmpNrc<T>::operator()() const
198 {
199  if (isTmp())
200  {
201  if (!ptr_)
202  {
204  << typeName() << " deallocated"
205  << abort(FatalError);
206  }
207  }
208 
209  // Return const reference
210  return *ptr_;
211 }
212 
213 
214 template<class T>
215 inline Foam::tmpNrc<T>::operator const T&() const
216 {
217  return operator()();
218 }
219 
220 
221 template<class T>
223 {
224  if (isTmp())
225  {
226  if (!ptr_)
227  {
229  << typeName() << " deallocated"
230  << abort(FatalError);
231  }
232  }
233  else
234  {
236  << "Attempt to cast const object to non-const for a " << typeName()
237  << abort(FatalError);
238  }
239 
240  return ptr_;
241 }
242 
243 
244 template<class T>
245 inline const T* Foam::tmpNrc<T>::operator->() const
246 {
247  if (isTmp() && !ptr_)
248  {
250  << typeName() << " deallocated"
251  << abort(FatalError);
252  }
253 
254  return ptr_;
255 }
256 
257 
258 template<class T>
259 inline void Foam::tmpNrc<T>::operator=(T* tPtr)
260 {
261  clear();
262 
263  if (!tPtr)
264  {
266  << "Attempted copy of a deallocated " << typeName()
267  << abort(FatalError);
268  }
269 
270  type_ = TMP;
271  ptr_ = tPtr;
272 }
273 
274 
275 template<class T>
277 {
278  clear();
279 
280  if (t.isTmp())
281  {
282  type_ = TMP;
283 
284  if (!t.ptr_)
285  {
287  << "Attempted assignment to a deallocated " << typeName()
288  << abort(FatalError);
289  }
290 
291  ptr_ = t.ptr_;
292  t.ptr_ = 0;
293  }
294  else
295  {
297  << "Attempted assignment to a const reference to an object"
298  << " of type " << typeid(T).name()
299  << abort(FatalError);
300  }
301 }
302 
303 
304 // ************************************************************************* //
T * ptr() const
Return tmpNrc pointer for reuse.
Definition: tmpNrcI.H:160
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
~tmpNrc()
Destructor: deletes temporary object when the reference count is 0.
Definition: tmpNrcI.H:99
T & ref()
Return non-const reference or generate a fatal error.
Definition: tmpNrcI.H:136
bool valid() const
Is this temporary object valid,.
Definition: tmpNrcI.H:122
void clear() const
If object pointer points to valid object:
Definition: tmpNrcI.H:184
bool empty() const
Return true if this temporary object empty,.
Definition: tmpNrcI.H:115
A class for handling words, derived from string.
Definition: word.H:59
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpNrcI.H:108
errorManip< error > abort(error &err)
Definition: errorManip.H:131
word typeName() const
Return the type name of the tmpNrc.
Definition: tmpNrcI.H:129
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:52
T * operator->()
Return object pointer.
Definition: tmpNrcI.H:222
void operator=(T *)
Assignment to pointer changing this tmpNrc to a temporary T.
Definition: tmpNrcI.H:259
const T & operator()() const
Const dereference operator.
Definition: tmpNrcI.H:197
tmpNrc(T *=0)
Store object pointer.
Definition: tmpNrcI.H:32