From 6fde55cbd65db9d51d38ac52a40db1dfade78b0a Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Fri, 12 Feb 2016 14:08:38 +0000 Subject: [PATCH] tmp: Added assignment to pointer operator to initialize null-tmp to an allocated object This is a convenient method to set a null-constructed tmp in a conditional statement. --- src/OpenFOAM/memory/tmp/tmp.H | 7 +++-- src/OpenFOAM/memory/tmp/tmpI.H | 48 +++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 7ca3a13ca2..783d06feb8 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&); }; diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index e289a06ca0..4f3ae9c662 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 @@ -266,6 +266,37 @@ inline const T* Foam::tmp::operator->() const } +template +inline void Foam::tmp::operator=(T* tPtr) +{ + if (isTmp_ && ptr_) + { + if (ptr_->okToDelete()) + { + delete ptr_; + ptr_ = 0; + } + else + { + ptr_->operator--(); + } + } + + isTmp_ = true; + + if (!tPtr) + { + FatalErrorInFunction + << "attempted copy of a deallocated temporary" + << " of type " << typeid(T).name() + << abort(FatalError); + } + + ptr_ = tPtr; + ptr_->resetRefCount(); +} + + template inline void Foam::tmp::operator=(const tmp& t) { @@ -285,24 +316,23 @@ inline void Foam::tmp::operator=(const tmp& t) if (t.isTmp_) { isTmp_ = true; - ptr_ = t.ptr_; - if (ptr_) - { - ptr_->operator++(); - } - else + 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); }