diff --git a/src/foam/memory/tmp/tmp.H b/src/foam/memory/tmp/tmp.H index 6b5734a9b..c6570cc7a 100644 --- a/src/foam/memory/tmp/tmp.H +++ b/src/foam/memory/tmp/tmp.H @@ -62,7 +62,7 @@ class tmp //- Pointer to temporary object mutable T* ptr_; - // Const reference to constant object + //- Const reference to constant object const T& ref_; @@ -79,11 +79,12 @@ public: //- Construct copy and increment reference count inline tmp(const tmp&); + //- Construct copy transferring content of temporary if required + inline tmp(const tmp&, bool allowTransfer); - // Destructor - //- Delete object when reference count == 0 - inline ~tmp(); + //- Destructor, delete object when reference count == 0 + inline ~tmp(); // Member Functions diff --git a/src/foam/memory/tmp/tmpI.H b/src/foam/memory/tmp/tmpI.H index 540db73bc..26add1c1f 100644 --- a/src/foam/memory/tmp/tmpI.H +++ b/src/foam/memory/tmp/tmpI.H @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "error.H" +#include // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -60,14 +61,48 @@ inline Foam::tmp::tmp(const tmp& t) } else { - FatalErrorIn("tmp::tmp(const tmp&)") + FatalErrorIn("Foam::tmp::tmp(const tmp&)") << "attempted copy of a deallocated temporary" + << " of type " << typeid(T).name() << abort(FatalError); } } } +template +inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) +: + isTmp_(t.isTmp_), + ptr_(t.ptr_), + ref_(t.ref_) +{ + if (isTmp_) + { + if (allowTransfer) + { + const_cast&>(t).ptr_ = 0; + } + else + { + if (ptr_) + { + ptr_->operator++(); + } + else + { + FatalErrorIn + ( + "Foam::tmp::tmp(const tmp&, bool allowTransfer)" + ) << "attempted copy of a deallocated temporary" + << " of type " << typeid(T).name() + << abort(FatalError); + } + } + } +} + + template inline Foam::tmp::~tmp() { @@ -116,8 +151,8 @@ inline T* Foam::tmp::ptr() const { if (!ptr_) { - FatalErrorIn("tmp::ptr() const") - << "temporary deallocated" + FatalErrorIn("Foam::tmp::ptr() const") + << "temporary of type " << typeid(T).name() << " deallocated" << abort(FatalError); } @@ -155,8 +190,8 @@ inline T& Foam::tmp::operator()() { if (!ptr_) { - FatalErrorIn("T& tmp::operator()()") - << "temporary deallocated" + FatalErrorIn("T& Foam::tmp::operator()()") + << "temporary of type " << typeid(T).name() << " deallocated" << abort(FatalError); } @@ -184,8 +219,8 @@ inline const T& Foam::tmp::operator()() const { if (!ptr_) { - FatalErrorIn("const T& tmp::operator()() const") - << "temporary deallocated" + FatalErrorIn("const T& Foam::tmp::operator()() const") + << "temporary of type " << typeid(T).name() << " deallocated" << abort(FatalError); } @@ -212,8 +247,8 @@ inline T* Foam::tmp::operator->() { if (!ptr_) { - FatalErrorIn("tmp::operator->()") - << "temporary deallocated" + FatalErrorIn("Foam::tmp::operator->()") + << "temporary of type " << typeid(T).name() << " deallocated" << abort(FatalError); } @@ -260,15 +295,17 @@ inline void Foam::tmp::operator=(const tmp& t) } else { - FatalErrorIn("tmp::operator=(const tmp& t)") + FatalErrorIn("Foam::tmp::operator=(const tmp&)") << "attempted copy of a deallocated temporary" + << " of type " << typeid(T).name() << abort(FatalError); } } else { - FatalErrorIn("tmp::operator=(const tmp& t)") + FatalErrorIn("Foam::tmp::operator=(const tmp&)") << "attempted to assign to a const reference to constant object" + << " of type " << typeid(T).name() << abort(FatalError); } }