From bf64a8e35208545cdd219f18bad3c5fe98ec302c Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 27 Oct 2000 13:17:28 +0000 Subject: [PATCH] Add `swap' functions to SmartPointers. git-svn-id: https://svn.dealii.org/trunk@3467 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/smartpointer.h | 120 ++++++++++++++++++++++- 1 file changed, 118 insertions(+), 2 deletions(-) diff --git a/deal.II/base/include/base/smartpointer.h b/deal.II/base/include/base/smartpointer.h index 59b1da370e..3205c707f5 100644 --- a/deal.II/base/include/base/smartpointer.h +++ b/deal.II/base/include/base/smartpointer.h @@ -47,6 +47,8 @@ * a @p{SmartPointer} really behaves as if it were a pointer to * a constant object (disallowing write access when dereferenced), while * @p{SmartPointer} is a mutable pointer. + * + * @author Guido Kanschat, Wolfgang Bangerth, 1998, 1999, 2000 */ template class SmartPointer @@ -100,7 +102,7 @@ class SmartPointer * automatically and unsubscribes * to an old one if it exists. */ - SmartPointer & operator= (const SmartPointer& tt); + SmartPointer & operator= (const SmartPointer &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 &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::SmartPointer () : {}; + template SmartPointer::SmartPointer (T *t) : t (t) @@ -144,6 +187,7 @@ SmartPointer::SmartPointer (T *t) : }; + template SmartPointer::SmartPointer (const SmartPointer &tt) : t (tt.t) @@ -153,6 +197,7 @@ SmartPointer::SmartPointer (const SmartPointer &tt) : }; + template SmartPointer::~SmartPointer () { @@ -161,6 +206,7 @@ SmartPointer::~SmartPointer () }; + template SmartPointer & SmartPointer::operator = (T *tt) { @@ -178,6 +224,7 @@ SmartPointer & SmartPointer::operator = (T *tt) }; + template SmartPointer & SmartPointer::operator = (const SmartPointer& tt) { @@ -196,6 +243,7 @@ SmartPointer & SmartPointer::operator = (const SmartPointer& tt) }; + template inline SmartPointer::operator T* () const @@ -204,6 +252,7 @@ SmartPointer::operator T* () const }; + template inline T & SmartPointer::operator * () const @@ -212,6 +261,7 @@ T & SmartPointer::operator * () const }; + template inline T * SmartPointer::operator -> () const @@ -220,4 +270,70 @@ T * SmartPointer::operator -> () const }; + +template +inline +void SmartPointer::swap (SmartPointer &tt) +{ + swap (t, tt.t); +}; + + + +template +inline +void SmartPointer::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 +inline +void swap (SmartPointer &t1, SmartPointer &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 +inline +void swap (SmartPointer &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 +inline +void swap (T *&t1, SmartPointer &t2) +{ + t2.swap (t1); +}; + + #endif -- 2.39.5