From 4bb7cbb422d2d4142c4fdd9cd7b5953893a9828b Mon Sep 17 00:00:00 2001 From: aramis_acg Date: Thu, 1 Apr 2010 20:25:07 +0000 Subject: [PATCH] Add boost.shared_ptr workaround. git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@631 67173fc5-114c-0410-ac8e-9d2fd5bffc1f --- code/BoostWorkaround/boost/shared_ptr.hpp | 160 ++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 code/BoostWorkaround/boost/shared_ptr.hpp diff --git a/code/BoostWorkaround/boost/shared_ptr.hpp b/code/BoostWorkaround/boost/shared_ptr.hpp new file mode 100644 index 000000000..081980739 --- /dev/null +++ b/code/BoostWorkaround/boost/shared_ptr.hpp @@ -0,0 +1,160 @@ + +#ifndef INCLUDED_AI_BOOST_SHARED_PTR +#define INCLUDED_AI_BOOST_SHARED_PTR + +#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED + +#include + +// ------------------------------ +// Internal stub +namespace boost { + namespace detail { + class controller { + + public: + + controller() + : cnt() + {} + + template + controller(T* ptr) + : cnt(1) + {} + + public: + + template + controller* decref(T* pt) { + if (--cnt <= 0) { + delete this; + delete pt; + } + return NULL; + } + + controller* incref() { + ++cnt; + return this; + } + + private: + long cnt; + }; + + struct empty {}; + + template + struct is_convertible_stub { + + struct yes {char s[1];}; + struct no {char s[2];}; + + static yes foo(DEST*); + static no foo(...); + + enum {result = sizeof foo((SRC*)0) == sizeof yes}; + }; + + template struct enable_if {}; + template <> struct enable_if { + typedef empty result; + }; + + template + struct is_convertible : public enable_if::result > { + }; + } + +// ------------------------------ +// Small replacement for boost::shared_ptr, not threadsafe because no +// atomic reference counter is in use. +// ------------------------------ +template +class shared_ptr +{ + friend class shared_ptr; +public: + + // provide a default construtctor + shared_ptr() + : ptr() + , ctr(new detail::controller()) + { + } + + // construction from an existing object of type T + shared_ptr(T* _ptr) + : ptr(_ptr) + , ctr(new detail::controller(ptr)) + { + } + + shared_ptr(const shared_ptr& o) + : ptr(o.ptr) + , ctr(o.ctr->incref()) + { + } + + template + shared_ptr(const shared_ptr& o,typename detail::is_convertible::result = detail::empty()) + : ptr(o.ptr) + , ctr(o.ctr->incref()) + { + } + + // automatic destruction of the wrapped object when all + // references are freed. + ~shared_ptr() { + ctr = ctr->decref(ptr); + } + + // pointer access + inline operator T*() { + return ptr; + } + + inline T* operator-> () { + return ptr; + } + + // standard semantics + inline T* get() { + return ptr; + } + + inline void reset (T* t = 0) { + ctr = ctr->decref(ptr); + ptr = t; + if(ptr) { + ctr = new detail::controller(ptr); + } + } + + void swap(shared_ptr & b) { + std::swap(ptr, b.ptr); + std::swap(ctr, b.ctr); + } + +private: + + // encapsulated object pointer + T* ptr; + + // control block + detail::controller* ctr; +}; + +template +inline void swap(shared_ptr & a, shared_ptr & b) +{ + a.swap(b); +} + +} // end of namespace boost + +#else +# error "shared_ptr.h was already included" +#endif +#endif // INCLUDED_AI_BOOST_SCOPED_PTR