+////////////////////////////////////////////////////////////////////////////////\r
+\r
+// Author: Daniel Milton\r
+// Copyright: (c) Daniel Milton 2002-2009\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+namespace stlplus\r
+{\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // simple_ptr_base class\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // constructors, assignments and destructors\r
+\r
+ // create a null pointer\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::simple_ptr_base(void) :\r
+ m_pointer(0),\r
+ m_count(new unsigned(1))\r
+ {\r
+ }\r
+\r
+ // create a pointer containing a *copy* of the object pointer\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::simple_ptr_base(const T& data) throw(illegal_copy) :\r
+ m_pointer(C()(data)),\r
+ m_count(new unsigned(1))\r
+ {\r
+ }\r
+\r
+ // create a pointer containing a dynamically created object\r
+ // Note: the object must be allocated *by the user* with new\r
+ // constructor form - must be called in the form simple_ptr<type> x(new type(args))\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::simple_ptr_base(T* data) :\r
+ m_pointer(data),\r
+ m_count(new unsigned(1))\r
+ {\r
+ }\r
+\r
+ // copy constructor implements counted referencing - no copy is made\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::simple_ptr_base(const simple_ptr_base<T,C>& r) :\r
+ m_pointer(r.m_pointer),\r
+ m_count(r.m_count)\r
+ {\r
+ increment();\r
+ }\r
+\r
+ // assignment operator - required, else the output of GCC suffers segmentation faults\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>& simple_ptr_base<T,C>::operator=(const simple_ptr_base<T,C>& r)\r
+ {\r
+ alias(r);\r
+ return *this;\r
+ }\r
+\r
+ // destructor decrements the reference count and delete only when the last reference is destroyed\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::~simple_ptr_base(void)\r
+ {\r
+ if(decrement()) \r
+ {\r
+ delete m_pointer;\r
+ delete m_count;\r
+ }\r
+ }\r
+\r
+ //////////////////////////////////////////////////////////////////////////////\r
+ // logical tests to see if there is anything contained in the pointer since it can be null\r
+\r
+ template <typename T, typename C>\r
+ bool simple_ptr_base<T,C>::null(void) const\r
+ {\r
+ return m_pointer==0;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ bool simple_ptr_base<T,C>::present(void) const\r
+ {\r
+ return m_pointer!=0;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ bool simple_ptr_base<T,C>::operator!(void) const\r
+ {\r
+ return m_pointer==0;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ simple_ptr_base<T,C>::operator bool(void) const\r
+ {\r
+ return m_pointer!=0;\r
+ }\r
+\r
+ //////////////////////////////////////////////////////////////////////////////\r
+ // dereference operators and functions\r
+\r
+ template <typename T, typename C>\r
+ T& simple_ptr_base<T,C>::operator*(void) throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::operator*");\r
+ return *m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ const T& simple_ptr_base<T,C>::operator*(void) const throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::operator*");\r
+ return *m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ T* simple_ptr_base<T,C>::operator->(void) throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::operator->");\r
+ return m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ const T* simple_ptr_base<T,C>::operator->(void) const throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::operator->");\r
+ return m_pointer;\r
+ }\r
+\r
+ //////////////////////////////////////////////////////////////////////////////\r
+ // explicit function forms of the above assignment dereference operators\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::set_value(const T& data) throw(illegal_copy)\r
+ {\r
+ set(C()(data));\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ T& simple_ptr_base<T,C>::value(void) throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::value");\r
+ return *m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ const T& simple_ptr_base<T,C>::value(void) const throw(null_dereference)\r
+ {\r
+ if (!m_pointer) throw null_dereference("null pointer dereferenced in simple_ptr::value");\r
+ return *m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::set(T* data)\r
+ {\r
+ unsigned& count = *m_count;\r
+ if (count<=1)\r
+ delete m_pointer;\r
+ else\r
+ {\r
+ --count;\r
+ m_count = new unsigned(1);\r
+ }\r
+ m_pointer = data;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ T* simple_ptr_base<T,C>::pointer(void)\r
+ {\r
+ return m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ const T* simple_ptr_base<T,C>::pointer(void) const\r
+ {\r
+ return m_pointer;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ // functions to manage counted referencing\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::increment(void)\r
+ {\r
+ ++(*m_count);\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ bool simple_ptr_base<T,C>::decrement(void)\r
+ {\r
+ unsigned& count = *m_count;\r
+ --count;\r
+ return count == 0;\r
+ }\r
+\r
+ // make this an alias of the passed object\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::alias(const simple_ptr_base<T,C>& r)\r
+ {\r
+ // make it alias-copy safe - this means that I don't try to do the\r
+ // assignment if r is either the same object or an alias of it\r
+ if (m_pointer==r.m_pointer) return;\r
+ if(decrement()) {\r
+ delete m_pointer;\r
+ delete m_count;\r
+ }\r
+ m_pointer = r.m_pointer;\r
+ m_count = r.m_count;\r
+ increment();\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ bool simple_ptr_base<T,C>::aliases(const simple_ptr_base<T,C>& r) const\r
+ {\r
+ return m_count == r.m_count;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ unsigned simple_ptr_base<T,C>::alias_count(void) const\r
+ {\r
+ return *m_count;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::clear(void)\r
+ {\r
+ set(0);\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::clear_unique(void)\r
+ {\r
+ set(0); // no difference between clear and clear_unique with the simple_ptr\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::make_unique(void) throw(illegal_copy)\r
+ {\r
+ unsigned& count = *m_count;\r
+ if (count <= 1) return;\r
+ --count;\r
+ if (m_pointer) m_pointer = C()(*m_pointer);\r
+ m_count = new unsigned(1);\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::copy(const simple_ptr_base<T,C>& data) throw(illegal_copy)\r
+ {\r
+ alias(data);\r
+ make_unique();\r
+ }\r
+\r
+#ifdef STLPLUS_MEMBER_TEMPLATES\r
+\r
+ // dynamic cast of underlying pointer to a derived/parent\r
+ template <typename T, typename C>\r
+ template <typename T2>\r
+ simple_ptr_base<T2,C> simple_ptr_base<T,C>::dyn_cast(void) const\r
+ {\r
+ simple_ptr_base<T2,C> rtn;\r
+ rtn.m_pointer = dynamic_cast<T2*>(m_pointer);\r
+ if (rtn.m_pointer) {\r
+ delete rtn.m_count;\r
+ rtn.m_count = m_count;\r
+ rtn.increment();\r
+ }\r
+ return rtn;\r
+ }\r
+\r
+ // static cast of underlying pointer to a derived/parent\r
+ template <typename T, typename C>\r
+ template <typename T2>\r
+ simple_ptr_base<T2,C> simple_ptr_base<T,C>::stat_cast(void) const\r
+ {\r
+ simple_ptr_base<T2,C> rtn;\r
+ rtn.m_pointer = static_cast<T2*>(m_pointer);\r
+ if (rtn.m_pointer) {\r
+ delete rtn.m_count;\r
+ rtn.m_count = m_count;\r
+ rtn.increment();\r
+ }\r
+ return rtn;\r
+ }\r
+\r
+ // cast of underlying pointer to a base - while keeping the same ref-counted object\r
+ template <typename T, typename C>\r
+ template <typename T2>\r
+ simple_ptr_base<T2,C> simple_ptr_base<T,C>::cast(void) const\r
+ {\r
+ simple_ptr_base<T2,C> rtn;\r
+ rtn.m_pointer = (T2*)m_pointer;\r
+ if (rtn.m_pointer) {\r
+ delete rtn.m_count;\r
+ rtn.m_count = m_count;\r
+ rtn.increment();\r
+ }\r
+ return rtn;\r
+ }\r
+\r
+#endif\r
+\r
+ // internal function for distinguishing unique simple_ptr objects\r
+ // used for example in persistence routines\r
+\r
+ template <typename T, typename C>\r
+ unsigned* simple_ptr_base<T,C>::_count(void) const\r
+ {\r
+ return m_count;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ T* simple_ptr_base<T,C>::_pointer(void) const\r
+ {\r
+ return m_pointer;\r
+ }\r
+\r
+ template <typename T, typename C>\r
+ void simple_ptr_base<T,C>::_make_alias(T* pointer, unsigned* count)\r
+ {\r
+ // make it alias-copy safe - this means that I don't try to do the\r
+ // assignment if r is either the same object or an alias of it\r
+ if (m_count != count)\r
+ {\r
+ if(decrement())\r
+ {\r
+ delete m_pointer;\r
+ delete m_count;\r
+ }\r
+ m_pointer = pointer;\r
+ m_count = count;\r
+ increment();\r
+ }\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
+\r