X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fstlplus%2Fcontainers%2Fsmart_ptr.hpp;fp=src%2Fmoof%2Fstlplus%2Fsmart_ptr.hpp;h=2d37e2e4314f5f7fc9a3539f2a23f89e75ac7a9e;hp=d1395f5bc1c4d0c4c233ebef024a76170da07064;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hpb=85783316365181491a3e3c0c63659972477cebba diff --git a/src/moof/stlplus/smart_ptr.hpp b/src/stlplus/containers/smart_ptr.hpp old mode 100755 new mode 100644 similarity index 83% rename from src/moof/stlplus/smart_ptr.hpp rename to src/stlplus/containers/smart_ptr.hpp index d1395f5..2d37e2e --- a/src/moof/stlplus/smart_ptr.hpp +++ b/src/stlplus/containers/smart_ptr.hpp @@ -8,7 +8,7 @@ // License: BSD License, see ../docs/license.html // A smart pointer is a memory-managing pointer to an object. If you like, it -// is a zero-dimensional container. +// is a zero-dimensional container. // Assignment of smart pointers result in multiple aliases of the same object. // The term alias is used to differentiate from conventional pointers because @@ -27,6 +27,7 @@ //////////////////////////////////////////////////////////////////////////////// #include "containers_fixes.hpp" #include "exceptions.hpp" +#include "copy_functors.hpp" #include #include @@ -61,7 +62,7 @@ namespace stlplus smart_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 + // 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 smart_ptr_base(const T& data) throw(illegal_copy); @@ -71,7 +72,12 @@ namespace stlplus explicit smart_ptr_base(T* data); // copy constructor implements aliasing so no copy is made - explicit smart_ptr_base(const smart_ptr_base& r); + // note that the copy constructor should NOT be explicit, as this breaks + // the returning of pointer objects from functions (at least within GCC 4.4) + smart_ptr_base(const smart_ptr_base& r); + + // assignment operator - required, else the output of GCC suffers segmentation faults + smart_ptr_base& operator=(const smart_ptr_base& r); // destructor decrements the reference count and delete only when the last reference is destroyed ~smart_ptr_base(void); @@ -159,48 +165,8 @@ namespace stlplus public: // internal use only - had to make them public because they need to be // accessed by routines that could not be made friends - void* handle(void) const; - void make_alias(void* handle); - }; - - //////////////////////////////////////////////////////////////////////////////// - // copy functors implementing the three possible copy semantics - - // constructor_copy uses the copy constructor of the object - used for simple types - - template - class constructor_copy - { - public: - T* operator() (const T& from) throw() - { - return new T(from); - } - }; - - // clone_copy uses the clone method of the object - used for polymorphic types - - template - class clone_copy - { - public: - T* operator() (const T& from) throw() - { - return from.clone(); - } - }; - - // no_copy throws an exception - used for types that cannot be copied - - template - class no_copy - { - public: - T* operator() (const T& from) throw(illegal_copy) - { - throw illegal_copy("no_copy functor called"); - return 0; - } + smart_ptr_holder* _handle(void) const; + void _make_alias(smart_ptr_holder* handle); }; //////////////////////////////////////////////////////////////////////////////// @@ -214,7 +180,7 @@ namespace stlplus explicit smart_ptr(const T& data) : smart_ptr_base >(data) {} explicit smart_ptr(T* data) : smart_ptr_base >(data) {} smart_ptr& operator=(const T& data) {set_value(data); return *this;} - smart_ptr& operator=(const smart_ptr& r) {alias(r); return *this;} + smart_ptr& operator=(T* data) {set(data); return *this;} ~smart_ptr(void) {} }; @@ -229,7 +195,7 @@ namespace stlplus explicit smart_ptr_clone(const T& data) : smart_ptr_base >(data) {} explicit smart_ptr_clone(T* data) : smart_ptr_base >(data) {} smart_ptr_clone& operator=(const T& data) {set_value(data); return *this;} - smart_ptr_clone& operator=(const smart_ptr_clone& r) {alias(r); return *this;} + smart_ptr_clone& operator=(T* data) {set(data); return *this;} ~smart_ptr_clone(void) {} }; @@ -244,7 +210,7 @@ namespace stlplus explicit smart_ptr_nocopy(const T& data) : smart_ptr_base >(data) {} explicit smart_ptr_nocopy(T* data) : smart_ptr_base >(data) {} smart_ptr_nocopy& operator=(const T& data) {set_value(data); return *this;} - smart_ptr_nocopy& operator=(const smart_ptr_nocopy& r) {alias(r); return *this;} + smart_ptr_nocopy& operator=(T* data) {set(data); return *this;} ~smart_ptr_nocopy(void) {} };