* 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
* 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.
* 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;
};
{};
+
template <typename T>
SmartPointer<T>::SmartPointer (T *t) :
t (t)
};
+
template <typename T>
SmartPointer<T>::SmartPointer (const SmartPointer<T> &tt) :
t (tt.t)
};
+
template <typename T>
SmartPointer<T>::~SmartPointer ()
{
};
+
template <typename T>
SmartPointer<T> & SmartPointer<T>::operator = (T *tt)
{
};
+
template <typename T>
SmartPointer<T> & SmartPointer<T>::operator = (const SmartPointer<T>& tt)
{
};
+
template <typename T>
inline
SmartPointer<T>::operator T* () const
};
+
template <typename T>
inline
T & SmartPointer<T>::operator * () const
};
+
template <typename T>
inline
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