mirror of
https://github.com/OpenFOAM/OpenFOAM-6.git
synced 2025-12-08 06:57:46 +00:00
tmp: Limit the number of references to a temporary object to 2
which reduces the number of potential problems with the reuse of temporary objects. In order to avoid unnecessary creation of tmp's referring to temporary objects the assignment operator now transfers ownership of the object and resets the argument.
This commit is contained in:
@ -70,6 +70,11 @@ class tmp
|
||||
mutable T* ptr_;
|
||||
|
||||
|
||||
// Private member operators
|
||||
|
||||
inline void operator++();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
|
||||
@ -26,6 +26,23 @@ License
|
||||
#include "error.H"
|
||||
#include <typeinfo>
|
||||
|
||||
// * * * * * * * * * * * * * Private Member Operators * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
inline void Foam::tmp<T>::operator++()
|
||||
{
|
||||
ptr_->operator++();
|
||||
|
||||
if (ptr_->count() > 1)
|
||||
{
|
||||
FatalErrorInFunction
|
||||
<< "Attempt to create more than 2 tmp's referring to"
|
||||
" the same object of type " << typeName()
|
||||
<< abort(FatalError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template<class T>
|
||||
@ -62,7 +79,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t)
|
||||
{
|
||||
if (ptr_)
|
||||
{
|
||||
ptr_->operator++();
|
||||
operator++();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -90,7 +107,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t, bool allowTransfer)
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr_->operator++();
|
||||
operator++();
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -342,8 +359,7 @@ inline void Foam::tmp<T>::operator=(const tmp<T>& t)
|
||||
}
|
||||
|
||||
ptr_ = t.ptr_;
|
||||
|
||||
ptr_->operator++();
|
||||
t.ptr_ = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user