]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add `swap' functions to SmartPointers.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Fri, 27 Oct 2000 13:17:28 +0000 (13:17 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Fri, 27 Oct 2000 13:17:28 +0000 (13:17 +0000)
git-svn-id: https://svn.dealii.org/trunk@3467 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/smartpointer.h

index 59b1da370e0d433050903d6109ef00021979b459..3205c707f515b4a9f46058b433c5e6027e3f8b6d 100644 (file)
@@ -47,6 +47,8 @@
  * a @p{SmartPointer<const ABC>} really behaves as if it were a pointer to
  * a constant object (disallowing write access when dereferenced), while
  * @p{SmartPointer<ABC>} is a mutable pointer.
+ *
+ * @author Guido Kanschat, Wolfgang Bangerth, 1998, 1999, 2000
  */
 template<typename T>
 class SmartPointer
@@ -100,7 +102,7 @@ class SmartPointer
                                      * automatically and unsubscribes
                                      * to an old one if it exists.
                                      */
-    SmartPointer<T> & operator= (const SmartPointer<T>tt);
+    SmartPointer<T> & operator= (const SmartPointer<T> &tt);
 
                                     /**
                                      * Conversion to normal pointer.
@@ -116,11 +118,51 @@ class SmartPointer
                                      * Dereferencing operator.
                                      */
     T * operator -> () const;
+
+                                    /**
+                                     * Exchange the pointers of this
+                                     * object and the argument. Since
+                                     * both the objects to which is
+                                     * pointed are subscribed to
+                                     * before and after, we do not
+                                     * have to change their
+                                     * subscription counters.
+                                     *
+                                     * Note that this function (with
+                                     * two arguments) and the
+                                     * respective functions where one
+                                     * of the arguments is a pointer
+                                     * and the other one is a C-style
+                                     * pointer are implemented in
+                                     * global namespace.
+                                     */
+    void swap (SmartPointer<T> &tt);
+
+                                    /**
+                                     * Swap pointers between this
+                                     * object and the pointer
+                                     * given. As this releases the
+                                     * object pointed to presently,
+                                     * we reduce its subscription
+                                     * count by one, and increase it
+                                     * at the object which we will
+                                     * point to in the future.
+                                     *
+                                     * Note that we indeed need a
+                                     * reference of a pointer, as we
+                                     * want to change the pointer
+                                     * variable which we are given.
+                                     */
+    void swap (T *&tt);
     
   private:
                                     /**
                                      * Pointer to the object we want
-                                     * to subscribt to.
+                                     * to subscribt to. Since it is
+                                     * often necessary to follow this
+                                     * pointer when debugging, we
+                                     * have deliberately chosen a
+                                     * short name.
                                      */
   T * t;
 };
@@ -135,6 +177,7 @@ SmartPointer<T>::SmartPointer () :
 {};
 
 
+
 template <typename T>
 SmartPointer<T>::SmartPointer (T *t) :
                t (t)
@@ -144,6 +187,7 @@ SmartPointer<T>::SmartPointer (T *t) :
 };
 
 
+
 template <typename T>
 SmartPointer<T>::SmartPointer (const SmartPointer<T> &tt) :
                t (tt.t)
@@ -153,6 +197,7 @@ SmartPointer<T>::SmartPointer (const SmartPointer<T> &tt) :
 };
 
 
+
 template <typename T>
 SmartPointer<T>::~SmartPointer ()
 {
@@ -161,6 +206,7 @@ SmartPointer<T>::~SmartPointer ()
 };
 
 
+
 template <typename T>
 SmartPointer<T> & SmartPointer<T>::operator = (T *tt)
 {
@@ -178,6 +224,7 @@ SmartPointer<T> & SmartPointer<T>::operator = (T *tt)
 };
 
 
+
 template <typename T>
 SmartPointer<T> & SmartPointer<T>::operator = (const SmartPointer<T>& tt)
 {
@@ -196,6 +243,7 @@ SmartPointer<T> & SmartPointer<T>::operator = (const SmartPointer<T>& tt)
 };
 
 
+
 template <typename T>
 inline
 SmartPointer<T>::operator T* () const
@@ -204,6 +252,7 @@ SmartPointer<T>::operator T* () const
 };
 
 
+
 template <typename T>
 inline
 T & SmartPointer<T>::operator * () const
@@ -212,6 +261,7 @@ T & SmartPointer<T>::operator * () const
 };
 
 
+
 template <typename T>
 inline
 T * SmartPointer<T>::operator -> () const
@@ -220,4 +270,70 @@ T * SmartPointer<T>::operator -> () const
 };
 
 
+
+template <typename T>
+inline
+void SmartPointer<T>::swap (SmartPointer<T> &tt)
+{
+  swap (t, tt.t);
+};
+
+
+
+template <typename T>
+inline
+void SmartPointer<T>::swap (T *&tt)
+{
+  t->unsubscribe ();
+  std::swap (t, tt);
+  t->subscribe ();
+};
+
+
+
+/**
+ * Global function to swap the contents of two smart pointers. As both
+ * objects to which the pointers point retain to be subscribed to, we
+ * do not have to change their subscription count.
+ */
+template <typename T>
+inline
+void swap (SmartPointer<T> &t1, SmartPointer<T> &t2)
+{
+  t1.swap (t2);
+};
+
+
+
+/**
+ * Global function to swap the contents of a smart pointer and a
+ * C-style pointer.
+ *
+ * Note that we indeed need a reference of a pointer, as we want to
+ * change the pointer variable which we are given.
+ */
+template <typename T>
+inline
+void swap (SmartPointer<T> &t1, T *&t2)
+{
+  t1.swap (t2);
+};
+
+
+
+/**
+ * Global function to swap the contents of a C-style pointer and a
+ * smart pointer.
+ *
+ * Note that we indeed need a reference of a pointer, as we want to
+ * change the pointer variable which we are given.
+ */
+template <typename T>
+inline
+void swap (T *&t1, SmartPointer<T> &t2)
+{
+  t2.swap (t1);
+};
+
+
 #endif

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.