From 51f93d3b437c8b4ee418600cab12bdd6231ae599 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Sat, 27 Feb 2016 18:11:09 +0000 Subject: [PATCH] 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. --- src/OpenFOAM/memory/tmp/tmp.H | 5 +++++ src/OpenFOAM/memory/tmp/tmpI.H | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index 9afb78bcf..72a49def8 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -70,6 +70,11 @@ class tmp mutable T* ptr_; + // Private member operators + + inline void operator++(); + + public: // Constructors diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 819c47142..a0dfdc6d2 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -26,6 +26,23 @@ License #include "error.H" #include +// * * * * * * * * * * * * * Private Member Operators * * * * * * * * * * * // + +template +inline void Foam::tmp::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 @@ -62,7 +79,7 @@ inline Foam::tmp::tmp(const tmp& t) { if (ptr_) { - ptr_->operator++(); + operator++(); } else { @@ -90,7 +107,7 @@ inline Foam::tmp::tmp(const tmp& t, bool allowTransfer) } else { - ptr_->operator++(); + operator++(); } } else @@ -342,8 +359,7 @@ inline void Foam::tmp::operator=(const tmp& t) } ptr_ = t.ptr_; - - ptr_->operator++(); + t.ptr_ = 0; } else {