From: Wolfgang Bangerth Date: Thu, 3 Sep 1998 12:30:16 +0000 (+0000) Subject: Change the internal way the subscriptors and SmartPointers/References work. X-Git-Tag: v8.0.0~22708 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc753ff6906273f07bc3ac0b6310e3b1319202ee;p=dealii.git Change the internal way the subscriptors and SmartPointers/References work. git-svn-id: https://svn.dealii.org/trunk@549 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/subscriptor.h b/deal.II/base/include/base/subscriptor.h index a9d1afb8fe..588bb2728b 100644 --- a/deal.II/base/include/base/subscriptor.h +++ b/deal.II/base/include/base/subscriptor.h @@ -4,6 +4,12 @@ #define __subscriptor_H /*---------------------------- subscriptor.h ---------------------------*/ + + +#include + + + /** * Handling of subscriptions. * @@ -14,136 +20,171 @@ */ class Subscriptor { - unsigned counter; -public: - /// Object may not be deleted, since it is used. - DeclException0(InUse);//, unsigned, "Object is used " << counter << " times"); - DeclException0(NotUsed); + /** + * Store the number of objects which + * subscribed to this object. Initialally, + * this number is zero, and upon + * destruction it shall be zero again + * (i.e. all objects which subscribed + * should have unsubscribed again). + */ + mutable unsigned int counter; + public: + /** + * Object may not be deleted, since + * it is used. + */ + DeclException0(InUse);//, unsigned, "Object is used " << counter << " times"); + DeclException0(NotUsed); - /// Constructor setting the counter to zero. - Subscriptor() - : counter(0) + /** + * Constructor setting the counter to zero. + */ + Subscriptor() + : counter(0) {} - /// Destructor, asserting that the counter is zero. - ~Subscriptor() + /** + * Destructor, asserting that the counter + * is zero. + */ + + ~Subscriptor() { Assert(counter==0, InUse()); } - /** - * Copy-constructor. - * - * The counter of the copy is zero, - * since references point to the - * original object. */ - - Subscriptor(const Subscriptor&) - : counter(0) + + /** + * Copy-constructor. + * + * The counter of the copy is zero, + * since references point to the + * original object. + */ + Subscriptor(const Subscriptor&) + : counter(0) {} - /** - * Assignment operator. - * - * This has to be handled with - * care, too, because the counter - * has to remain the same. */ - Subscriptor& operator=(const Subscriptor&) + /** + * Assignment operator. + * + * This has to be handled with + * care, too, because the counter + * has to remain the same. + */ + Subscriptor& operator = (const Subscriptor&) { return *this; } - - /// Subscribes a user of the object. - void subscribe() //mutable + + /** + * Subscribes a user of the object. + */ + void subscribe () const { ++counter; } - /// Unsubscribes a user from the object. - void unsubscribe() //mutable + /** + * Unsubscribes a user from the object. + */ + void unsubscribe () const { Assert(counter>0, NotUsed()); --counter; } }; + + + + /** * Smart references avoid destruction of a referenced object. */ template class SmartReference { - T& t; -public: - /// Constructor taking a normal reference. - SmartReference(const T& tt) - : t(/*const_cast*/ tt) + T& t; + + public: + /** + * Constructor taking a normal reference. + */ + SmartReference(const T& tt) + : t(tt) { t.subscribe(); } - /// Destructor, removing the subscription. - ~SmartReference() + + /** + * Destructor, removing the subscription. + */ + ~SmartReference() { t.unsubscribe(); } - /// Conversion to normal reference - operator T& () - { - return t; - } - /// Conversion to normal const reference. - operator const T& () const + /** + * Conversion to normal reference + */ + operator T& () { return t; } }; + + + /** * 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* t; + + public: + /** + * Constructor taking a normal pointer. + */ + SmartPointer(T* tt) + : t(tt) { t->subscribe(); } - /// Destructor, removing the subscription. - ~SmartPointer() + + /** + * Destructor, removing the subscription. + */ + ~SmartPointer() { t->unsubscribe(); } - - /// Conversion to normal pointer. - operator T* () - { - return t; - } - /// Conversion to normal const pointer. - operator const T* () const + + /** + * Conversion to normal pointer. + */ + operator T* () const { return t; } - T& operator* () - { - return *t; - } - const T& operator* () const + + /** + * Dereferencing operator. + */ + T& operator* () const { return *t; } - T* operator -> () - { - return t; - } - const T* operator -> () const + + /** + * Dereferencing operator. + */ + T* operator -> () const { return t; } - };