From e2b8716e99de504567f1649f5bb19c8eaa521e9a Mon Sep 17 00:00:00 2001 From: Guido Kanschat Date: Wed, 2 Sep 1998 22:10:13 +0000 Subject: [PATCH] SmartPointer git-svn-id: https://svn.dealii.org/trunk@547 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/subscriptor.h | 63 ++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/deal.II/base/include/base/subscriptor.h b/deal.II/base/include/base/subscriptor.h index c4d353bec1..a9d1afb8fe 100644 --- a/deal.II/base/include/base/subscriptor.h +++ b/deal.II/base/include/base/subscriptor.h @@ -17,8 +17,8 @@ class Subscriptor unsigned counter; public: /// Object may not be deleted, since it is used. - DeclException1(InUse, unsigned, "Object is used " << counter << " times"); - DeclException0(NotUsed, "Object cannot be unsubscribed, since it is not used"); + DeclException0(InUse);//, unsigned, "Object is used " << counter << " times"); + DeclException0(NotUsed); /// Constructor setting the counter to zero. Subscriptor() @@ -27,7 +27,7 @@ public: /// Destructor, asserting that the counter is zero. ~Subscriptor() { - Assert(counter==0, InUse(counter)); + Assert(counter==0, InUse()); } /** * Copy-constructor. @@ -52,13 +52,13 @@ public: } /// Subscribes a user of the object. - void subscribe() mutable + void subscribe() //mutable { ++counter; } /// Unsubscribes a user from the object. - void unsubscribe() mutable + void unsubscribe() //mutable { Assert(counter>0, NotUsed()); --counter; @@ -74,8 +74,8 @@ class SmartReference T& t; public: /// Constructor taking a normal reference. - SmartReference(T& t) - : t(t) + SmartReference(const T& tt) + : t(/*const_cast*/ tt) { t.subscribe(); } @@ -97,6 +97,55 @@ public: } }; +/** + * Smart pointers avoid destruction of an object in use. + */ +template +class SmartPointer +{ + T* t; +public: + /// Constructor taking a normal pointer. + SmartPointer(const T* tt) + : t(/*const_cast*/ tt) + { + t->subscribe(); + } + /// Destructor, removing the subscription. + ~SmartPointer() + { + t->unsubscribe(); + } + + /// Conversion to normal pointer. + operator T* () + { + return t; + } + /// Conversion to normal const pointer. + operator const T* () const + { + return t; + } + T& operator* () + { + return *t; + } + const T& operator* () const + { + return *t; + } + T* operator -> () + { + return t; + } + const T* operator -> () const + { + return t; + } + +}; + -- 2.39.5