diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 7ca3a13ca27752ba62954bae8586194e224adac0..783d06feb863a3bb9aac126af7e9b283c410133e 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -125,7 +125,10 @@ public: //- Return const object pointer inline const T* operator->() const; - //- Assignment operator + //- Assignment to pointer changing this tmp to a temporary T + inline void operator=(T*); + + //- Assignment transfering the temporary T to this tmp inline void operator=(const tmp<T>&); }; diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index e289a06ca026d8a00aeb79848aadab044df09737..4f3ae9c662f7ed9da0145ed9b2b68294921d4a2a 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -267,7 +267,7 @@ inline const T* Foam::tmp<T>::operator->() const template<class T> -inline void Foam::tmp<T>::operator=(const tmp<T>& t) +inline void Foam::tmp<T>::operator=(T* tPtr) { if (isTmp_ && ptr_) { @@ -282,27 +282,57 @@ inline void Foam::tmp<T>::operator=(const tmp<T>& t) } } - if (t.isTmp_) + isTmp_ = true; + + if (!tPtr) { - isTmp_ = true; - ptr_ = t.ptr_; + FatalErrorInFunction + << "attempted copy of a deallocated temporary" + << " of type " << typeid(T).name() + << abort(FatalError); + } - if (ptr_) + ptr_ = tPtr; + ptr_->resetRefCount(); +} + + +template<class T> +inline void Foam::tmp<T>::operator=(const tmp<T>& t) +{ + if (isTmp_ && ptr_) + { + if (ptr_->okToDelete()) { - ptr_->operator++(); + delete ptr_; + ptr_ = 0; } else + { + ptr_->operator--(); + } + } + + if (t.isTmp_) + { + isTmp_ = true; + + if (!t.ptr_) { FatalErrorInFunction - << "attempted copy of a deallocated temporary" + << "attempted assignment to a deallocated temporary" << " of type " << typeid(T).name() << abort(FatalError); } + + ptr_ = t.ptr_; + t.ptr_ = 0; + ptr_->resetRefCount(); } else { FatalErrorInFunction - << "attempted to assign to a const reference to constant object" + << "attempted assignment to a const reference to constant object" << " of type " << typeid(T).name() << abort(FatalError); }