]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/stlplus/smart_ptr.hpp
initial working frustum culling implementation
[chaz/yoink] / src / Moof / stlplus / smart_ptr.hpp
diff --git a/src/Moof/stlplus/smart_ptr.hpp b/src/Moof/stlplus/smart_ptr.hpp
new file mode 100755 (executable)
index 0000000..d1395f5
--- /dev/null
@@ -0,0 +1,256 @@
+#ifndef STLPLUS_SMART_PTR\r
+#define STLPLUS_SMART_PTR\r
+////////////////////////////////////////////////////////////////////////////////\r
+\r
+//   Author:    Andy Rushton\r
+//   Copyright: (c) Southampton University 1999-2004\r
+//              (c) Andy Rushton           2004-2009\r
+//   License:   BSD License, see ../docs/license.html\r
+\r
+//   A smart pointer is a memory-managing pointer to an object. If you like, it\r
+//   is a zero-dimensional container. \r
+\r
+//   Assignment of smart pointers result in multiple aliases of the same object.\r
+//   The term alias is used to differentiate from conventional pointers because\r
+//   the semantics are different.\r
+\r
+//   Aliases can be turned into copies if the pointed-to class supports copying.\r
+\r
+//   The base class is smart_ptr_base which defines the common interface. Then\r
+//   there are three subclasses which have the same interface but different copy\r
+//   semantics:\r
+\r
+//   - smart_ptr        for simple types and classes which have copy constructors\r
+//   - smart_ptr_clone  for polymorphic class hierarchies which are copied using a clone method\r
+//   - smart_ptr_nocopy for any class that cannot or should not be copied\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "containers_fixes.hpp"\r
+#include "exceptions.hpp"\r
+#include <map>\r
+#include <string>\r
+\r
+namespace stlplus\r
+{\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // internals\r
+\r
+  template<typename T> class smart_ptr_holder;\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // Base class\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  template<typename T, typename C>\r
+  class smart_ptr_base\r
+  {\r
+  public:\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // member type definitions\r
+\r
+    typedef T value_type;\r
+    typedef T& reference;\r
+    typedef const T& const_reference;\r
+    typedef C value_copy;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // constructors and destructors\r
+\r
+    // create a null pointer\r
+    smart_ptr_base(void);\r
+\r
+    // create a pointer containing a *copy* of the object using the template parameter C\r
+    // this copy is taken because the pointer class maintains a dynamically allocated object \r
+    // and the T& may not be (usually is not) dynamically allocated\r
+    explicit smart_ptr_base(const T& data) throw(illegal_copy);\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 smart_ptr_base<type> x(new type(args))\r
+    explicit smart_ptr_base(T* data);\r
+\r
+    // copy constructor implements aliasing so no copy is made\r
+    explicit smart_ptr_base(const smart_ptr_base<T,C>& r);\r
+\r
+    // destructor decrements the reference count and delete only when the last reference is destroyed\r
+    ~smart_ptr_base(void);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // logical tests to see if there is anything contained in the pointer since it can be null\r
+\r
+    // there are two forms:explicit and implicit\r
+    // implicit: if(!r) or if(r)\r
+    // explicit: if(r.null()) or if(r.present())\r
+    operator bool(void) const;\r
+    bool operator!(void) const;\r
+    bool present(void) const;\r
+    bool null(void) const;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // dereference operators and functions\r
+\r
+    // dereference the smart pointer to get the object - use in the form *p1\r
+    T& operator*(void) throw(null_dereference);\r
+    const T& operator*(void) const throw(null_dereference);\r
+\r
+    // used as a prefix to a member access to the contained object e.g. p1->print() calls T::print()\r
+    T* operator->(void) throw(null_dereference);\r
+    const T* operator->(void) const throw(null_dereference);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // explicit function forms of the above assignment and dereference operators\r
+\r
+    // set the value - note that this does a copy using the C template parameter\r
+    void set_value(const T& data) throw(illegal_copy);\r
+    // get the value\r
+    T& value(void) throw(null_dereference);\r
+    const T& value(void) const throw(null_dereference);\r
+\r
+    // set the pointer\r
+    // deletes the previous pointer and adopts the passed pointer instead\r
+    // Note: the object must be allocated *by the user* with new\r
+    // Warning: it is very easy to break the memory management with this operation\r
+    void set(T* data = 0);\r
+    // get the pointer\r
+    T* pointer(void);\r
+    const T* pointer(void) const;\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // functions to manage aliases\r
+\r
+    // make this an alias of the passed object\r
+    void alias(const smart_ptr_base<T,C>&);\r
+\r
+    // test whether two pointers point to the same object(known as aliasing the object)\r
+    // used in the form if(a.aliases(b))\r
+    bool aliases(const smart_ptr_base<T,C>&) const;\r
+\r
+    // find the number of aliases - used when you need to know whether an\r
+    // object is still referred to from elsewhere (rare!)\r
+    unsigned alias_count(void) const;\r
+\r
+    // delete the object and make the pointer null - does not make it unique\r
+    // first, so all other pointers to this will be null too\r
+    void clear(void);\r
+\r
+    // make the pointer unique and null in one step - does not affect other\r
+    // pointers that were pointing to the same object\r
+    void clear_unique(void);\r
+\r
+    //////////////////////////////////////////////////////////////////////////////\r
+    // functions that involve copying\r
+\r
+    // these functions use the copy functor passed as the template parameter C\r
+    // to copy the object with the right copy semantics. If the copy functor\r
+    // is no_copy, an exception will be thrown.\r
+\r
+    // make this pointer unique with respect to any other references to the same object\r
+    // if this pointer is already unique, it does nothing - otherwise it copies the object\r
+    void make_unique(void) throw(illegal_copy);\r
+\r
+    // make this pointer a unique copy of the parameter\r
+    // useful for expressions like p1.copy(p2) which makes p1 a pointer to a unique copy of the contents of p2\r
+    void copy(const smart_ptr_base<T,C>&) throw(illegal_copy);\r
+\r
+  protected:\r
+    smart_ptr_holder<T>* m_holder;\r
+\r
+  public:\r
+    // internal use only - had to make them public because they need to be\r
+    // accessed by routines that could not be made friends\r
+    void* handle(void) const;\r
+    void make_alias(void* handle);\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // copy functors implementing the three possible copy semantics\r
+\r
+  // constructor_copy uses the copy constructor of the object - used for simple types\r
+\r
+  template <typename T>\r
+  class constructor_copy\r
+  {\r
+  public:\r
+    T* operator() (const T& from) throw()\r
+      {\r
+        return new T(from);\r
+      }\r
+  };\r
+\r
+  // clone_copy uses the clone method of the object - used for polymorphic types\r
+\r
+  template <typename T>\r
+  class clone_copy\r
+  {\r
+  public:\r
+    T* operator() (const T& from) throw()\r
+      {\r
+        return from.clone();\r
+      }\r
+  };\r
+\r
+  // no_copy throws an exception - used for types that cannot be copied\r
+\r
+  template <typename T>\r
+  class no_copy\r
+  {\r
+  public:\r
+    T* operator() (const T& from) throw(illegal_copy)\r
+      {\r
+        throw illegal_copy("no_copy functor called");\r
+        return 0;\r
+      }\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // smart_ptr        for simple types and classes which have copy constructors\r
+\r
+  template <typename T>\r
+  class smart_ptr : public smart_ptr_base<T, constructor_copy<T> >\r
+  {\r
+  public:\r
+    smart_ptr(void) {}\r
+    explicit smart_ptr(const T& data) : smart_ptr_base<T, constructor_copy<T> >(data) {}\r
+    explicit smart_ptr(T* data) : smart_ptr_base<T, constructor_copy<T> >(data) {}\r
+    smart_ptr<T>& operator=(const T& data) {set_value(data); return *this;}\r
+    smart_ptr<T>& operator=(const smart_ptr<T>& r) {alias(r); return *this;}\r
+    ~smart_ptr(void) {}\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // smart_ptr_clone  for polymorphic class hierarchies which have a clone method\r
+\r
+  template <typename T>\r
+  class smart_ptr_clone : public smart_ptr_base<T, clone_copy<T> >\r
+  {\r
+  public:\r
+    smart_ptr_clone(void) {}\r
+    explicit smart_ptr_clone(const T& data) : smart_ptr_base<T, clone_copy<T> >(data) {}\r
+    explicit smart_ptr_clone(T* data) : smart_ptr_base<T, clone_copy<T> >(data) {}\r
+    smart_ptr_clone<T>& operator=(const T& data) {set_value(data); return *this;}\r
+    smart_ptr_clone<T>& operator=(const smart_ptr_clone<T>& r) {alias(r); return *this;}\r
+    ~smart_ptr_clone(void) {}\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // smart_ptr_nocopy for any class that cannot or should not be copied\r
+\r
+  template <typename T>\r
+  class smart_ptr_nocopy : public smart_ptr_base<T, no_copy<T> >\r
+  {\r
+  public:\r
+    smart_ptr_nocopy(void) {}\r
+    explicit smart_ptr_nocopy(const T& data) : smart_ptr_base<T, no_copy<T> >(data) {}\r
+    explicit smart_ptr_nocopy(T* data) : smart_ptr_base<T, no_copy<T> >(data) {}\r
+    smart_ptr_nocopy<T>& operator=(const T& data) {set_value(data); return *this;}\r
+    smart_ptr_nocopy<T>& operator=(const smart_ptr_nocopy<T>& r) {alias(r); return *this;}\r
+    ~smart_ptr_nocopy(void) {}\r
+  };\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
+\r
+#include "smart_ptr.tpp"\r
+#endif\r
This page took 0.026189 seconds and 4 git commands to generate.