diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H index 67f8b4c9f..8eee3182f 100644 --- a/src/OpenFOAM/memory/refCount/refCount.H +++ b/src/OpenFOAM/memory/refCount/refCount.H @@ -27,6 +27,10 @@ Class Description Reference counter for various OpenFOAM components. +SeeAlso + Foam::tmp + Foam::token::compound + \*---------------------------------------------------------------------------*/ #ifndef refCount_H @@ -49,6 +53,7 @@ class refCount int count_; + // Private Member Functions //- Dissallow copy @@ -58,26 +63,27 @@ class refCount void operator=(const refCount&); -public: +protected: // Constructors - //- Construct null with zero count + //- Construct null initializing count to 0 refCount() : count_(0) {} +public: + // Member Functions - //- Return the reference count + //- Return the current reference count int count() const { return count_; } - //- Return true if the reference count is zero bool unique() const { @@ -85,13 +91,6 @@ public: } - //- Reset the reference count to zero - void resetRefCount() - { - count_ = 0; - } - - // Member Operators //- Increment the reference count diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index b431e73b8..4b320d8e0 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -30,13 +30,17 @@ Description SourceFiles tmpI.H +SeeAlso + Foam::refCount + Foam::autoPtr + \*---------------------------------------------------------------------------*/ #ifndef tmp_H #define tmp_H #include "refCount.H" -#include +#include "word.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -102,6 +106,11 @@ public: // ie, it is a reference or a temporary that has been allocated inline bool valid() const; + //- Return the type name of the tmp + // constructed from the type name of T + inline word typeName() const; + + // Edit //- Return non-const reference or generate a fatal error diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 4d0ad7561..a6a2824d7 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -33,7 +33,15 @@ inline Foam::tmp::tmp(T* tPtr) : type_(TMP), ptr_(tPtr) -{} +{ + if (tPtr && !tPtr->unique()) + { + FatalErrorInFunction + << "Attempted construction of a " << typeName() + << " from non-unique pointer" + << abort(FatalError); + } +} template @@ -59,8 +67,7 @@ inline Foam::tmp::tmp(const tmp& t) else { FatalErrorInFunction - << "Attempted copy of a deallocated temporary" - << " of type " << typeid(T).name() + << "Attempted copy of a deallocated " << typeName() << abort(FatalError); } } @@ -88,8 +95,7 @@ inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) else { FatalErrorInFunction - << "Attempted copy of a deallocated temporary" - << " of type " << typeid(T).name() + << "Attempted copy of a deallocated " << typeName() << abort(FatalError); } } @@ -100,18 +106,7 @@ inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) template inline Foam::tmp::~tmp() { - if (isTmp() && ptr_) - { - if (ptr_->unique()) - { - delete ptr_; - ptr_ = 0; - } - else - { - ptr_->operator--(); - } - } + clear(); } @@ -138,27 +133,34 @@ inline bool Foam::tmp::valid() const } +template +inline Foam::word Foam::tmp::typeName() const +{ + return "tmp<" + word(typeid(T).name()) + '>'; +} + + template inline T& Foam::tmp::ref() { - if (type_ == TMP) + if (isTmp()) { if (!ptr_) { FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" + << typeName() << " deallocated" << abort(FatalError); } - - return *ptr_; } else { FatalErrorInFunction << "Attempt to acquire non-const reference to const object" + << " from a " << typeName() << abort(FatalError); - return *ptr_; } + + return *ptr_; } @@ -170,7 +172,7 @@ inline T* Foam::tmp::ptr() const if (!ptr_) { FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" + << typeName() << " deallocated" << abort(FatalError); } @@ -178,7 +180,7 @@ inline T* Foam::tmp::ptr() const { FatalErrorInFunction << "Attempt to acquire pointer to object referred to" - " by multiple 'tmp's" + << " by multiple temporaries of type " << typeName() << abort(FatalError); } @@ -219,23 +221,19 @@ inline void Foam::tmp::clear() const template inline T& Foam::tmp::operator()() { - if (type_ == TMP) + if (isTmp()) { if (!ptr_) { FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" + << typeName() << " deallocated" << abort(FatalError); } + } - return *ptr_; - } - else - { - // Const-ness is automatically cast-away which is why this operator is - // deprecated. Use ref() where non-const access is required. - return *ptr_; - } + // Const-ness is automatically cast-away which is why this operator is + // deprecated. Use ref() where non-const access is required. + return *ptr_; } #endif @@ -243,22 +241,18 @@ inline T& Foam::tmp::operator()() template inline const T& Foam::tmp::operator()() const { - if (type_ == TMP) + if (isTmp()) { if (!ptr_) { FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" + << typeName() << " deallocated" << abort(FatalError); } + } - return *ptr_; - } - else - { - // Return const reference - return *ptr_; - } + // Return const reference + return *ptr_; } @@ -274,91 +268,67 @@ inline T* Foam::tmp::operator->() { if (isTmp()) { - if (!ptr_) - { - FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" - << abort(FatalError); - } - - return ptr_; + if (!ptr_) + { + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } } else { - FatalErrorInFunction << "Const object cast to non-const" + FatalErrorInFunction + << "Attempt to cast const object to non-const for a " << typeName() << abort(FatalError); - return ptr_; } + + return ptr_; } template inline const T* Foam::tmp::operator->() const { - if (isTmp()) + if (isTmp() && !ptr_) { - if (!ptr_) - { - FatalErrorInFunction - << "Temporary of type " << typeid(T).name() << " deallocated" - << abort(FatalError); - } + FatalErrorInFunction + << typeName() << " deallocated" + << abort(FatalError); + } - return ptr_; - } - else - { - return ptr_; - } + return ptr_; } template inline void Foam::tmp::operator=(T* tPtr) { - if (isTmp() && ptr_) - { - if (ptr_->unique()) - { - delete ptr_; - ptr_ = 0; - } - else - { - ptr_->operator--(); - } - } - - type_ = TMP; + clear(); if (!tPtr) { FatalErrorInFunction - << "Attempted copy of a deallocated temporary" - << " of type " << typeid(T).name() + << "Attempted copy of a deallocated " << typeName() << abort(FatalError); } + if (tPtr && !tPtr->unique()) + { + FatalErrorInFunction + << "Attempted assignment of a " << typeName() + << " to non-unique pointer" + << abort(FatalError); + } + + type_ = TMP; ptr_ = tPtr; - ptr_->resetRefCount(); } template inline void Foam::tmp::operator=(const tmp& t) { - if (isTmp() && ptr_) - { - if (ptr_->unique()) - { - delete ptr_; - ptr_ = 0; - } - else - { - ptr_->operator--(); - } - } + clear(); if (t.isTmp()) { @@ -367,8 +337,7 @@ inline void Foam::tmp::operator=(const tmp& t) if (!t.ptr_) { FatalErrorInFunction - << "Attempted assignment to a deallocated temporary" - << " of type " << typeid(T).name() + << "Attempted assignment to a deallocated " << typeName() << abort(FatalError); } @@ -379,7 +348,7 @@ inline void Foam::tmp::operator=(const tmp& t) else { FatalErrorInFunction - << "Attempted assignment to a const reference to constant object" + << "Attempted assignment to a const reference to an object" << " of type " << typeid(T).name() << abort(FatalError); }