X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fstlplus%2Fcontainers%2Fsimple_ptr.hpp;h=ffe2c8afeee60a56feeab042205db5a54f5b013f;hp=08fff036eb91612865a8207b777e57deaa91e303;hb=4f6e4488a55f7e3ba3f7485d78177f793c0eab9a;hpb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c diff --git a/src/stlplus/containers/simple_ptr.hpp b/src/stlplus/containers/simple_ptr.hpp index 08fff03..ffe2c8a 100644 --- a/src/stlplus/containers/simple_ptr.hpp +++ b/src/stlplus/containers/simple_ptr.hpp @@ -2,9 +2,8 @@ #define STLPLUS_SIMPLE_PTR //////////////////////////////////////////////////////////////////////////////// -// Author: Daniel Milton, Andy Rushton -// Copyright: (c) Southampton University 1999-2004 -// (c) Daniel Milton, Andy Rushton 2004-2009 +// Author: Daniel Milton +// Copyright: (c) Daniel Milton 2002 onwards // License: BSD License, see ../docs/license.html // A smart pointer is a memory-managing pointer to an object. If you like, it @@ -76,11 +75,6 @@ namespace stlplus // create a null pointer simple_ptr_base(void); - // create a pointer containing a *copy* of the object using the template parameter C - // this copy is taken because the pointer class maintains a dynamically allocated object - // and the T& may not be (usually is not) dynamically allocated - explicit simple_ptr_base(const T& data) throw(illegal_copy); - // create a pointer containing a dynamically created object // Note: the object must be allocated *by the user* with new // constructor form - must be called in the form smart_ptr_base x(new type(args)) @@ -122,8 +116,6 @@ namespace stlplus ////////////////////////////////////////////////////////////////////////////// // explicit function forms of the above assignment and dereference operators - // set the value - note that this does a copy using the C template parameter - void set_value(const T& data) throw(illegal_copy); // get the value T& value(void) throw(null_dereference); const T& value(void) const throw(null_dereference); @@ -167,6 +159,14 @@ namespace stlplus // to copy the object with the right copy semantics. If the copy functor // is no_copy, an exception will be thrown. + // create a pointer containing a *copy* of the object using the template parameter C + // this copy is taken because the pointer class maintains a dynamically allocated object + // and the T& may not be (usually is not) dynamically allocated + explicit simple_ptr_base(const T& data) throw(illegal_copy); + + // set the value - note that this does a copy using the C template parameter + void set_value(const T& data) throw(illegal_copy); + // make this pointer unique with respect to any other references to the same object // if this pointer is already unique, it does nothing - otherwise it copies the object void make_unique(void) throw(illegal_copy); @@ -176,22 +176,6 @@ namespace stlplus void copy(const simple_ptr_base&) throw(illegal_copy); ////////////////////////////////////////////////////////////////////////////// - // functions that involve casting - -#ifdef STLPLUS_MEMBER_TEMPLATES - - // dynamic cast of underlying pointer to a derived/parent - template simple_ptr_base dyn_cast(void) const; - - // static cast of underlying pointer to a derived/parent - template simple_ptr_base stat_cast(void) const; - - // cast of underlying pointer to a base - while keeping the same ref-counted object - template simple_ptr_base cast(void) const; - -#endif - - ////////////////////////////////////////////////////////////////////////////// protected: T* m_pointer; @@ -224,10 +208,30 @@ namespace stlplus simple_ptr& operator=(const T& data) {set_value(data); return *this;} simple_ptr& operator=(T* data) {set(data); return *this;} ~simple_ptr(void) {} + +#ifdef STLPLUS_MEMBER_TEMPLATES + // functions that involve casting + // moved from base class for two main reasons, though the second is a feature of the first: + + // 1. GCC cannot cast the previous base result of simple_ptr_base > + // as a simple_ptr even though it used to look like a duck and quack like a duck. + // I think it was really complaining that the copy class was not guaranteed to be the same. + + // 2. Within the cast routines, one pointer type tried accessing private data of the other + // pointer type and even though they are really the same type, was not allowed. Because + // of this, the "private" function _make_alias is utilised to get the same result. + + // By having the cast functions in each derived class, you are guaranteed to use the same + // copy class - no question. GCC is ok with this. + + template simple_ptr dyn_cast(void) const; + template simple_ptr stat_cast(void) const; + template simple_ptr cast(void) const; +#endif }; //////////////////////////////////////////////////////////////////////////////// - // smart_ptr_clone for polymorphic class hierarchies which have a clone method + // simple_ptr_clone for polymorphic class hierarchies which have a clone method template class simple_ptr_clone : public simple_ptr_base > @@ -239,21 +243,35 @@ namespace stlplus simple_ptr_clone& operator=(const T& data) {set_value(data); return *this;} simple_ptr_clone& operator=(T* data) {set(data); return *this;} ~simple_ptr_clone(void) {} - }; + +#ifdef STLPLUS_MEMBER_TEMPLATES + // functions that involve casting + // moved from base class - see simple_ptr above + template simple_ptr_clone dyn_cast(void) const; + template simple_ptr_clone stat_cast(void) const; + template simple_ptr_clone cast(void) const; +#endif +}; //////////////////////////////////////////////////////////////////////////////// - // smart_ptr_nocopy for any class that cannot or should not be copied + // simple_ptr_nocopy for any class that cannot or should not be copied template class simple_ptr_nocopy : public simple_ptr_base > { public: simple_ptr_nocopy(void) {} - explicit simple_ptr_nocopy(const T& data) : simple_ptr_base >(data) {} explicit simple_ptr_nocopy(T* data) : simple_ptr_base >(data) {} - simple_ptr_nocopy& operator=(const T& data) {set_value(data); return *this;} simple_ptr_nocopy& operator=(T* data) {set(data); return *this;} ~simple_ptr_nocopy(void) {} + +#ifdef STLPLUS_MEMBER_TEMPLATES + // functions that involve casting + // moved from base class - see simple_ptr above + template simple_ptr_nocopy dyn_cast(void) const; + template simple_ptr_nocopy stat_cast(void) const; + template simple_ptr_nocopy cast(void) const; +#endif }; ////////////////////////////////////////////////////////////////////////////////