Commit 51f93d3b authored by Henry Weller's avatar Henry Weller
Browse files

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.
parent 4a359439
......@@ -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
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment