]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Minor restructuring, mostly cleanups.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 15 Dec 1998 22:42:47 +0000 (22:42 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 15 Dec 1998 22:42:47 +0000 (22:42 +0000)
git-svn-id: https://svn.dealii.org/trunk@712 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/smartpointer.h
deal.II/base/include/base/smartreference.h
deal.II/base/include/base/subscriptor.h
deal.II/base/source/subscriptor.cc [new file with mode: 0644]

index eb246ff1a797a8b9c8985a3b3e4b33658a6dbe0e..4051d83c620101152024e85a8d8d2a1d8ca320a6 100644 (file)
  * }
  * delete p;
  * \end{verbatim}
+ *
+ * Note that a smart pointer can handle #const#ness of an object, i.e.
+ * a #SmartPointer<const ABC># really behaves as if it were a pointer to
+ * a constant object (disallowing write access when dereferenced), while
+ * #SmartPointer<ABC># is a mutable pointer.
  */
-template<class T>
+template<typename T>
 class SmartPointer
 {
-    T* t;
-
   public:
+                                    /**
+                                     * Standard constructor for null pointer.
+                                     */
+    SmartPointer();
+
                                     /**
                                      * Constructor taking a normal pointer.
+                                     * If possible, i.e. if the pointer
+                                     * is not a null pointer, the constructor
+                                     * subscribes to the given object to
+                                     * lock it, i.e. to prevent its
+                                     * destruction before the end of its use.
                                      */
-    SmartPointer(T* tt) :
-                   t(tt) 
-      {
-       if (t)
-         t->subscribe();
-      }
-
-                                  /**
-                                   * Standard constructor for null pointer.
-                                   */
-  SmartPointer() :
-                 t(0)
-      {}
+    SmartPointer (T *t);
+
+    
 
                                     /**
                                      * Destructor, removing the subscription.
                                      */
-    ~SmartPointer()
-      {
-       if (t)
-         t->unsubscribe();
-      }
-                                  /**
-                                   * Assignment operator. Change of
-                                   * subscription is necessary.
-                                   */
-  SmartPointer<T>& operator=(T* tt)
-      {
-       if (t)
-         t->unsubscribe();
-       t = tt;
-       if (tt)
-         tt->subscribe();
-       return *this;
-      }
+    ~SmartPointer();
+    
+                                    /**
+                                     * Assignment operator. Change of
+                                     * subscription is necessary.
+                                     */
+    SmartPointer<T> & operator= (T *tt);
   
 
                                     /**
                                      * Conversion to normal pointer.
                                      */
-    operator T* () const
-      {
-       return t;
-      }
-
+    operator T* () const;
+    
                                     /**
                                      * Dereferencing operator.
                                      */
-    T& operator* () const
-      {
-       return *t;
-      }
-
+    T& operator * () const;
+    
                                     /**
                                      * Dereferencing operator.
                                      */
-    T* operator -> () const
-      {
-       return t;
-      }
+    T * operator -> () const;
+    
+  private:
+                                    /**
+                                     * Pointer to the object we want
+                                     * to subscribt to.
+                                     */
+    const T* t;
+};
+
+
+
+
+
+/* --------------------------- Template functions ------------------------------*/
+
+
+template <typename T>
+SmartPointer<T>::SmartPointer () :
+               t (0)
+{};
+
+
+
+template <typename T>
+SmartPointer<T>::SmartPointer (const T *t) :
+               t (t)
+{
+  if (t)
+    t->subscribe();
+};
+
+
+
+template <typename T>
+SmartPointer<T>::~SmartPointer () {
+  if (t)
+    t->unsubscribe();
+};
+
+
+
+template <typename T>
+SmartPointer<T> & operator = (const T *tt) {
+  if (t)
+    t->unsubscribe();
+  t = tt;
+  if (tt)
+    tt->subscribe();
+  return *this;
 };
 
 
 
+template <typename T>
+inline
+SmartPointer<T>::operator T* () const {
+  return t;
+};
+
+
+
+template <typename T>
+inline
+T & SmartPointer<T>::operator * () const {
+  return *t;
+};
+
+
+
+template <typename T>
+inline
+T * SmartPointer<T>::operator -> () const {
+  return t;
+};
+
+
+
+
+
 /*----------------------------   smartpointer.h     ---------------------------*/
 /* end of #ifndef __smartpointer_H */
 #endif
index 0ffcc819b2c54dd05e5e62f8936c5cffc70f7db4..d143fa85a06bfe1d128f2e39dbfd5f9b86541e3d 100644 (file)
@@ -12,7 +12,7 @@
  * resolve the dot operator in a convenient manner. The use of
  * #SmartPointer# is recommended, instead.
  */
-template<class T>
+template<typename T>
 class SmartReference
 {
     T& t;
index 34f780ba240a15869baccb50179bd3fd9cf8f9b0..20aabf2be615389a6dc95e6d74275d2cfe824327 100644 (file)
  * This class, as a base class, allows to keep track of other objects
  * using a specific object. It is used, when an object, given to a
  * constructor by reference, is stored. Then, the original object may
- * not be deleted before the dependent object is deleted.
+ * not be deleted before the dependent object is deleted. You can assert
+ * this constraint by letting the object passed be derived from this class
+ * and let the user #subscribe# to this object. The destructor the used
+ * object inherits from the #Subscriptor# class then will lead to an error
+ * when destruction is attempted while there are still subscriptions.
  */
 class Subscriptor
 {
-                                    /**
-                                     * 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).
-                                     *
-                                     * The creator (and owner) of an object
-                                     * is not counted.
-                                     */
-    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)
-      {}
+    Subscriptor();
+
                                     /**
                                      * Destructor, asserting that the counter
                                      * is zero.
                                      */
     
-    ~Subscriptor()
-      {
-       Assert(counter==0, InUse());
-      }
-
+    ~Subscriptor();
+    
                                     /**
                                      * Copy-constructor.
                                      *
@@ -63,40 +44,65 @@ class Subscriptor
                                      * since references point to the
                                      * original object.
                                      */
-    Subscriptor(const Subscriptor&)
-                   : counter(0)
-      {}
-
+    Subscriptor(const Subscriptor&);
+    
                                     /**
                                      * Assignment operator.
                                      *
                                      * This has to be handled with
                                      * care, too, because the counter
-                                     * has to remain the same.
+                                     * has to remain the same. It therefore
+                                     * does nothing more than returning
+                                     * #*this#.
                                      */
-    Subscriptor& operator = (const Subscriptor&) 
-      {
-       return *this;
-      }
-    
+    Subscriptor& operator = (const Subscriptor&);
+        
                                     /**
                                      * Subscribes a user of the object.
                                      */
-    void subscribe () const
-      {
-       ++counter;
-      }
-  
+    void subscribe () const;
+      
                                     /**
                                      * Unsubscribes a user from the object.
                                      */
-    void unsubscribe () const
-      {
-       Assert(counter>0, NotUsed());
-       --counter;
-      }
+    void unsubscribe () const;
+    
+                                    /**
+                                     * Exception:
+                                     * Object may not be deleted, since
+                                     * it is used.
+                                     */
+    DeclException0(InUse);
+                                    /**
+                                     * Exception: object should be used
+                                     * when #unsubscribe# is called.
+                                     */
+    DeclException0(NotUsed);
+  
+
+  private:
+                                    /**
+                                     * 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).
+                                     *
+                                     * The creator (and owner) of an object
+                                     * is not counted.
+                                     *
+                                     * We use the #mutable# keyword in order
+                                     * to allow subscription to constant
+                                     * objects also.
+                                     */
+    mutable unsigned int counter;
 };
 
+
+
+
+
 /*----------------------------   subscriptor.h     ---------------------------*/
 /* end of #ifndef __subscriptor_H */
 #endif
diff --git a/deal.II/base/source/subscriptor.cc b/deal.II/base/source/subscriptor.cc
new file mode 100644 (file)
index 0000000..65c8c15
--- /dev/null
@@ -0,0 +1,40 @@
+/*      $Id$                 */
+
+#include <base/subscriptor.h>
+
+
+
+Subscriptor::Subscriptor () :
+               counter (0)
+{};
+
+
+
+Subscriptor::Subscriptor (const Subscriptor &) :
+               counter (0)
+{};
+
+
+
+Subscriptor::~Subscriptor () {
+  Assert (counter == 0, InUse());
+};
+
+
+
+Subscriptor & Subscriptor::operator = (const Subscriptor &) {
+  return *this;
+};
+
+
+
+void Subscriptor::subscribe () const {
+  ++counter;
+};
+
+
+
+void Subscriptor::unsubscribe () const {
+  Assert (counter>0, NotUsed());
+  --counter;
+};

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.