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:
parent
4a35943909
commit
51f93d3b43
2 changed files with 25 additions and 4 deletions
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue