From 630005619b977bb700b0577070e51a97fd822af8 Mon Sep 17 00:00:00 2001 From: aramis_acg Date: Sun, 11 Apr 2010 02:46:52 +0000 Subject: [PATCH] Add missing boost workaround files to CMakeLists.txt. Boost workaround's shared_xxx stuff now matches boosts documented API. Add shared_array.hpp to boost workaround (forgot to commit it when I added shared_ptr.hpp). git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@668 67173fc5-114c-0410-ac8e-9d2fd5bffc1f --- code/BoostWorkaround/boost/scoped_array.hpp | 2 - code/BoostWorkaround/boost/scoped_ptr.hpp | 2 - code/BoostWorkaround/boost/shared_array.hpp | 204 ++++++++++++++++++++ code/BoostWorkaround/boost/shared_ptr.hpp | 55 +++++- code/CMakeLists.txt | 4 + 5 files changed, 254 insertions(+), 13 deletions(-) create mode 100644 code/BoostWorkaround/boost/shared_array.hpp diff --git a/code/BoostWorkaround/boost/scoped_array.hpp b/code/BoostWorkaround/boost/scoped_array.hpp index 0d83f4581..7b1d8cc42 100644 --- a/code/BoostWorkaround/boost/scoped_array.hpp +++ b/code/BoostWorkaround/boost/scoped_array.hpp @@ -4,8 +4,6 @@ #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED -#include - namespace boost { // small replacement for boost::scoped_array diff --git a/code/BoostWorkaround/boost/scoped_ptr.hpp b/code/BoostWorkaround/boost/scoped_ptr.hpp index 8b1604bbb..8f4bde3fc 100644 --- a/code/BoostWorkaround/boost/scoped_ptr.hpp +++ b/code/BoostWorkaround/boost/scoped_ptr.hpp @@ -4,8 +4,6 @@ #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED -#include - namespace boost { // small replacement for boost::scoped_ptr diff --git a/code/BoostWorkaround/boost/shared_array.hpp b/code/BoostWorkaround/boost/shared_array.hpp new file mode 100644 index 000000000..98f62edee --- /dev/null +++ b/code/BoostWorkaround/boost/shared_array.hpp @@ -0,0 +1,204 @@ + +#ifndef INCLUDED_AI_BOOST_SHARED_ARRAY +#define INCLUDED_AI_BOOST_SHARED_ARRAY + +#ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED + +// ------------------------------ +// Internal stub +namespace boost { + namespace detail { + class controller { + + public: + + controller() + : cnt(0) + {} + + template + controller(T* ptr) + : cnt(ptr?1:0) + {} + + public: + + template + controller* decref(T* pt) { + if (!pt) { + return NULL; + } + if (--cnt <= 0) { + delete this; + delete[] pt; + } + return NULL; + } + + controller* incref() { + ++cnt; + return this; + } + + long get() const { + return cnt; + } + + 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) ? 1 : 0)}; + }; + + template struct enable_if {}; + template <> struct enable_if { + typedef empty result; + }; + + template + struct is_convertible : public enable_if::result > { + }; + } + +// ------------------------------ +// Small replacement for boost::shared_array, not threadsafe because no +// atomic reference counter is in use. +// ------------------------------ +template +class shared_array +{ + template friend class shared_array; + + template friend bool operator== (const shared_array& a, const shared_array& b); + template friend bool operator!= (const shared_array& a, const shared_array& b); + template friend bool operator< (const shared_array& a, const shared_array& b); + +public: + + typedef T element_type; + +public: + + // provide a default constructor + shared_array() + : ptr() + , ctr(new detail::controller()) + { + } + + // construction from an existing object of type T + explicit shared_array(T* _ptr) + : ptr(_ptr) + , ctr(new detail::controller(ptr)) + { + } + + template + shared_array(const shared_array& o,typename detail::is_convertible::result = detail::empty()) + : ptr(o.ptr) + , ctr(o.ctr->incref()) + { + } + + shared_array& operator= (const shared_array& r) { + if(r == *this) { + return; + } + ctr->decref(ptr); + ctr = r.ctr->incref(); + ptr = r.ptr; + } + + // automatic destruction of the wrapped object when all + // references are freed. + ~shared_array() { + ctr = ctr->decref(ptr); + } + + // pointer access + inline operator T*() { + return ptr; + } + + inline T* operator-> () { + return ptr; + } + + // standard semantics + inline T* get() { + return ptr; + } + + inline operator bool () const { + return ptr != NULL; + } + + inline bool unique() const { + return use_count() == 1; + } + + inline long use_count() const { + return ctr->get(); + } + + inline void reset (T* t = 0) { + ctr = ctr->decref(ptr); + ptr = t; + if(ptr) { + ctr = new detail::controller(ptr); + } + } + + void swap(shared_array & 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_array & a, shared_array & b) +{ + a.swap(b); +} + +template +bool operator== (const shared_array& a, const shared_array& b) { + return a.ptr == b.ptr; +} +template +bool operator!= (const shared_array& a, const shared_array& b) { + return a.ptr != b.ptr; +} + +template +bool operator< (const shared_array& a, const shared_array& b) { + return a.ptr < b.ptr; +} + + +} // end of namespace boost + +#else +# error "shared_array.h was already included" +#endif +#endif // INCLUDED_AI_BOOST_SHARED_ARRAY diff --git a/code/BoostWorkaround/boost/shared_ptr.hpp b/code/BoostWorkaround/boost/shared_ptr.hpp index 986c88404..7876948d6 100644 --- a/code/BoostWorkaround/boost/shared_ptr.hpp +++ b/code/BoostWorkaround/boost/shared_ptr.hpp @@ -4,8 +4,6 @@ #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED -#include - // ------------------------------ // Internal stub namespace boost { @@ -42,6 +40,10 @@ namespace boost { return this; } + long get() const { + return cnt; + } + private: long cnt; }; @@ -78,9 +80,18 @@ template class shared_ptr { template friend class shared_ptr; + + template friend bool operator== (const shared_ptr& a, const shared_ptr& b); + template friend bool operator!= (const shared_ptr& a, const shared_ptr& b); + template friend bool operator< (const shared_ptr& a, const shared_ptr& b); + public: - // provide a default construtctor + typedef T element_type; + +public: + + // provide a default constructor shared_ptr() : ptr() , ctr(new detail::controller()) @@ -94,12 +105,6 @@ public: { } - 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) @@ -107,6 +112,15 @@ public: { } + shared_ptr& operator= (const shared_ptr& r) { + if(r == *this) { + return; + } + ctr->decref(ptr); + ctr = r.ctr->incref(); + ptr = r.ptr; + } + // automatic destruction of the wrapped object when all // references are freed. ~shared_ptr() { @@ -131,6 +145,14 @@ public: return ptr != NULL; } + inline bool unique() const { + return use_count() == 1; + } + + inline long use_count() const { + return ctr->get(); + } + inline void reset (T* t = 0) { ctr = ctr->decref(ptr); ptr = t; @@ -159,6 +181,21 @@ inline void swap(shared_ptr & a, shared_ptr & b) a.swap(b); } +template +bool operator== (const shared_ptr& a, const shared_ptr& b) { + return a.ptr == b.ptr; +} +template +bool operator!= (const shared_ptr& a, const shared_ptr& b) { + return a.ptr != b.ptr; +} + +template +bool operator< (const shared_ptr& a, const shared_ptr& b) { + return a.ptr < b.ptr; +} + + } // end of namespace boost #else diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 71306cadb..365dca7ad 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -78,6 +78,8 @@ SOURCE_GROUP( Boost FILES BoostWorkaround/boost/format.hpp BoostWorkaround/boost/scoped_array.hpp BoostWorkaround/boost/scoped_ptr.hpp + BoostWorkaround/boost/shared_array.hpp + BoostWorkaround/boost/shared_ptr.hpp BoostWorkaround/boost/static_assert.hpp BoostWorkaround/boost/tuple/tuple.hpp ) @@ -681,6 +683,8 @@ ADD_LIBRARY( assimp SHARED BoostWorkaround/boost/format.hpp BoostWorkaround/boost/scoped_array.hpp BoostWorkaround/boost/scoped_ptr.hpp + BoostWorkaround/boost/shared_array.hpp + BoostWorkaround/boost/shared_ptr.hpp BoostWorkaround/boost/static_assert.hpp BoostWorkaround/boost/tuple/tuple.hpp ${PUBLIC_HEADERS}