tmpNrcI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include <typeinfo>
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class T>
35 template<class... Args>
37 {
38  return tmpNrc<T>(new T(std::forward<Args>(args)...));
39 }
40 
41 
42 template<class T>
43 template<class U, class... Args>
45 {
46  return tmpNrc<T>(new U(std::forward<Args>(args)...));
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
52 template<class T>
53 inline constexpr Foam::tmpNrc<T>::tmpNrc() noexcept
54 :
55  ptr_(nullptr),
56  type_(PTR)
57 {}
58 
59 
60 template<class T>
61 inline constexpr Foam::tmpNrc<T>::tmpNrc(std::nullptr_t) noexcept
62 :
63  ptr_(nullptr),
64  type_(PTR)
65 {}
66 
67 
68 template<class T>
69 inline Foam::tmpNrc<T>::tmpNrc(T* p) noexcept
70 :
71  ptr_(p),
72  type_(PTR)
73 {}
74 
75 
76 template<class T>
77 inline Foam::tmpNrc<T>::tmpNrc(const T& obj) noexcept
78 :
79  ptr_(const_cast<T*>(&obj)),
80  type_(CREF)
81 {}
82 
83 
84 template<class T>
85 inline Foam::tmpNrc<T>::tmpNrc(tmpNrc<T>&& t) noexcept
86 :
87  ptr_(t.ptr_),
88  type_(t.type_)
89 {
90  t.ptr_ = nullptr;
91  t.type_ = PTR;
92 }
93 
94 
95 template<class T>
97 :
98  ptr_(t.ptr_),
99  type_(t.type_)
100 {
101  if (isTmp())
102  {
103  if (ptr_)
104  {
105  t.type_ = CREF;
106  }
107  else
108  {
110  << "Attempted copy of a deallocated " << typeName()
111  << abort(FatalError);
112  }
113  }
114 }
115 
116 
117 template<class T>
118 inline Foam::tmpNrc<T>::tmpNrc(const tmpNrc<T>& t, bool reuse)
119 :
120  ptr_(t.ptr_),
121  type_(t.type_)
122 {
123  if (isTmp())
124  {
125  if (ptr_)
126  {
127  if (reuse)
128  {
129  t.ptr_ = nullptr; // t.type_ already set as PTR
130  }
131  else
132  {
133  t.type_ = CREF;
134  }
135  }
136  else
137  {
139  << "Attempted copy of a deallocated " << typeName()
140  << abort(FatalError);
141  }
142  }
143 }
144 
145 
146 template<class T>
148 {
149  clear();
150 }
151 
152 
153 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
154 
155 template<class T>
156 inline bool Foam::tmpNrc<T>::isTmp() const noexcept
157 {
158  return type_ == PTR;
159 }
160 
161 
162 template<class T>
163 inline bool Foam::tmpNrc<T>::empty() const noexcept
164 {
165  return (!ptr_ && isTmp());
166 }
167 
168 
169 template<class T>
170 inline bool Foam::tmpNrc<T>::valid() const noexcept
171 {
172  return (ptr_ || type_ == CREF);
173 }
174 
175 
176 template<class T>
177 inline bool Foam::tmpNrc<T>::movable() const noexcept
178 {
179  return (type_ == PTR && ptr_);
180 }
181 
182 
183 template<class T>
185 {
186  return "tmpNrc<" + word(typeid(T).name()) + '>';
187 }
188 
189 
190 template<class T>
191 inline T* Foam::tmpNrc<T>::get() noexcept
192 {
193  return ptr_; // non-const pointer
194 }
195 
196 
197 template<class T>
198 inline const T* Foam::tmpNrc<T>::get() const noexcept
199 {
200  return ptr_; // const pointer
201 }
202 
203 
204 template<class T>
205 inline const T& Foam::tmpNrc<T>::cref() const
206 {
207  if (isTmp())
208  {
209  if (!ptr_)
210  {
212  << typeName() << " deallocated"
213  << abort(FatalError);
214  }
215  }
216 
217  return *ptr_; // const reference
218 }
219 
220 
221 template<class T>
222 inline T& Foam::tmpNrc<T>::ref() const
223 {
224  if (isTmp())
225  {
226  if (!ptr_)
227  {
229  << typeName() << " deallocated"
230  << abort(FatalError);
231  }
232  }
233  else
234  {
236  << "Attempted non-const reference to const object from a "
237  << typeName()
238  << abort(FatalError);
239  }
240 
241  return *ptr_; // non-const reference
242 }
243 
244 
245 template<class T>
247 {
248  if (isTmp() && !ptr_)
249  {
251  << typeName() << " deallocated"
252  << abort(FatalError);
253  }
254 
255  return const_cast<T&>(*ptr_);
256 }
257 
258 
259 template<class T>
260 inline T* Foam::tmpNrc<T>::ptr() const
261 {
262  if (isTmp())
263  {
264  if (!ptr_)
265  {
267  << typeName() << " deallocated"
268  << abort(FatalError);
269  }
270 
271  T* ptr = ptr_;
272  ptr_ = nullptr;
273 
274  return ptr;
275  }
276 
277  return ptr_->clone().ptr();
278 }
279 
280 
281 template<class T>
282 inline void Foam::tmpNrc<T>::clear() const noexcept
283 {
284  if (isTmp() && ptr_)
285  {
286  delete ptr_;
287  ptr_ = nullptr;
288  }
289 }
290 
291 
292 template<class T>
293 inline void Foam::tmpNrc<T>::reset() noexcept
294 {
295  clear();
296  ptr_ = nullptr;
297  type_ = PTR;
298 }
299 
300 
301 template<class T>
302 inline void Foam::tmpNrc<T>::reset(T* p) noexcept
303 {
304  clear();
305  ptr_ = p;
306  type_ = PTR;
307 }
308 
309 
310 template<class T>
311 inline void Foam::tmpNrc<T>::reset(tmpNrc<T>&& other) noexcept
312 {
313  if (&other == this)
314  {
315  return; // Self-assignment is a no-op
316  }
317 
318  clear();
319  ptr_ = other.ptr_;
320  type_ = other.type_;
321 
322  other.ptr_ = nullptr;
323  other.type_ = PTR;
324 }
325 
326 
327 template<class T>
328 inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept
329 {
330  clear();
331  ptr_ = const_cast<T*>(&obj);
332  type_ = CREF;
333 }
334 
335 
336 template<class T>
337 inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
338 {
339  if (&other == this)
340  {
341  return; // Self-swap is a no-op
342  }
343 
344  // Copy/assign for pointer types
345  T* p = ptr_;
346  ptr_ = other.ptr_;
347  other.ptr_ = p;
348 
349  refType t = type_;
350  type_ = other.type_;
351  other.type_ = t;
352 }
353 
354 
355 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
356 
357 template<class T>
358 inline const T& Foam::tmpNrc<T>::operator()() const
359 {
360  return cref();
361 }
362 
363 
364 template<class T>
365 inline Foam::tmpNrc<T>::operator const T&() const
366 {
367  return cref();
368 }
369 
370 
371 template<class T>
372 inline const T* Foam::tmpNrc<T>::operator->() const
373 {
374  if (!ptr_ && isTmp())
375  {
377  << typeName() << " deallocated"
378  << abort(FatalError);
379  }
380 
381  return ptr_;
382 }
383 
384 
385 template<class T>
387 {
388  if (isTmp())
389  {
390  if (!ptr_)
391  {
393  << typeName() << " deallocated"
394  << abort(FatalError);
395  }
396  }
397  else
398  {
400  << "Attempt to cast const object to non-const for a " << typeName()
401  << abort(FatalError);
402  }
403 
404  return ptr_;
405 }
406 
407 
408 template<class T>
409 inline Foam::tmpNrc<T>::operator bool() const noexcept
410 {
411  return (ptr_ || type_ == CREF);
412 }
413 
414 
415 template<class T>
417 {
418  clear();
419 
420  if (!p)
421  {
423  << "Attempted copy of a deallocated " << typeName()
424  << abort(FatalError);
425  }
426 
427  ptr_ = p;
428  type_ = PTR;
429 }
430 
431 
432 template<class T>
434 {
435  if (&t == this)
436  {
437  return; // Self-assignment is a no-op
438  }
439 
440  clear();
441 
442  if (t.isTmp())
443  {
444  ptr_ = t.ptr_;
445  type_ = PTR;
446  t.ptr_ = nullptr;
447 
448  if (!ptr_)
449  {
451  << "Attempted assignment to a deallocated " << typeName()
452  << abort(FatalError);
453  }
454  }
455  else
456  {
458  << "Attempted assignment to a const reference to an object"
459  << " of type " << typeid(T).name()
460  << abort(FatalError);
461  }
462 }
463 
464 
465 template<class T>
466 inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept
467 {
468  if (&other == this)
469  {
470  return; // Self-assignment is a no-op
471  }
472 
473  clear();
474  ptr_ = other.ptr_;
475  type_ = other.type_;
476 
477  other.ptr_ = nullptr;
478  other.type_ = PTR;
479 }
480 
481 
482 template<class T>
484 {
485  if (isTmp())
486  {
487  return tmp<T>(ptr());
488  }
489  else
490  {
491  return tmp<T>(cref());
492  }
493 }
494 
495 
496 // ************************************************************************* //
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::tmpNrc::~tmpNrc
~tmpNrc()
Destructor: deletes managed pointer.
Definition: tmpNrcI.H:147
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmpNrc::typeName
word typeName() const
Return type-name of the tmp, constructed from type-name of T.
Definition: tmpNrcI.H:184
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::tmpNrc::cref
const T & cref() const
Definition: tmpNrcI.H:205
Foam::tmpNrc::swap
void swap(tmpNrc< T > &other) noexcept
Swaps the managed object with other.
Definition: tmpNrcI.H:337
error.H
Foam::tmpNrc
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:56
Foam::tmpNrc::ptr
T * ptr() const
Definition: tmpNrcI.H:260
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::tmpNrc::constCast
T & constCast() const
Non-const dereference, even if the object is const.
Definition: tmpNrcI.H:246
Foam::tmpNrc::operator=
void operator=(T *p)
Take ownership of the pointer.
Definition: tmpNrcI.H:416
Foam::tmpNrc::clear
void clear() const noexcept
Definition: tmpNrcI.H:282
Foam::FatalError
error FatalError
Foam::tmpNrc::reset
void reset() noexcept
Release ownership of managed temporary object.
Definition: tmpNrcI.H:293
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::tmpNrc::New
static tmpNrc< T > New(Args &&... args)
Construct tmpNrc of T with forwarding arguments.
U
U
Definition: pEqn.H:72
Foam::tmpNrc::NewFrom
static tmpNrc< T > NewFrom(Args &&... args)
Construct tmpNrc from derived type with forwarding arguments.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
clear
patchWriters clear()
Foam::tmpNrc::valid
bool valid() const noexcept
Definition: tmpNrcI.H:170
Foam::tmpNrc::movable
bool movable() const noexcept
True if this is a non-null managed pointer with a unique ref-count.
Definition: tmpNrcI.H:177
bool
bool
Definition: EEqn.H:20
Foam::tmpNrc::empty
bool empty() const noexcept
True if this is a non-null managed pointer.
Definition: tmpNrcI.H:163
Foam::tmpNrc::tmpNrc
constexpr tmpNrc() noexcept
Construct with no managed pointer.
Definition: tmpNrcI.H:53
args
Foam::argList args(argc, argv)
Foam::tmpNrc::operator->
const T * operator->() const
Dereferences (const) pointer to the managed object.
Definition: tmpNrcI.H:372
Foam::tmpNrc::isTmp
bool isTmp() const noexcept
True if this is a managed pointer (not a const reference)
Definition: tmpNrcI.H:156
Foam::tmpNrc::ref
T & ref() const
Definition: tmpNrcI.H:222
Foam::tmpNrc::get
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmpNrcI.H:191
Foam::tmpNrc::operator()
const T & operator()() const
Return const reference to the object.
Definition: tmpNrcI.H:358