vrshoot
diff libs/assimp/boost/shared_array.hpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/boost/shared_array.hpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,228 @@ 1.4 + 1.5 +#ifndef INCLUDED_AI_BOOST_SHARED_ARRAY 1.6 +#define INCLUDED_AI_BOOST_SHARED_ARRAY 1.7 + 1.8 +#ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED 1.9 + 1.10 +// ------------------------------ 1.11 +// Internal stub 1.12 +namespace boost { 1.13 + namespace array_detail { 1.14 + class controller { 1.15 + public: 1.16 + 1.17 + controller() 1.18 + : cnt(1) 1.19 + {} 1.20 + 1.21 + public: 1.22 + 1.23 + template <typename T> 1.24 + controller* decref(T* pt) { 1.25 + if (--cnt <= 0) { 1.26 + delete this; 1.27 + delete[] pt; 1.28 + } 1.29 + return NULL; 1.30 + } 1.31 + 1.32 + controller* incref() { 1.33 + ++cnt; 1.34 + return this; 1.35 + } 1.36 + 1.37 + long get() const { 1.38 + return cnt; 1.39 + } 1.40 + 1.41 + private: 1.42 + long cnt; 1.43 + }; 1.44 + 1.45 + struct empty {}; 1.46 + 1.47 + template <typename DEST, typename SRC> 1.48 + struct is_convertible_stub { 1.49 + 1.50 + struct yes {char s[1];}; 1.51 + struct no {char s[2];}; 1.52 + 1.53 + static yes foo(DEST*); 1.54 + static no foo(...); 1.55 + 1.56 + enum {result = (sizeof(foo((SRC*)0)) == sizeof(yes) ? 1 : 0)}; 1.57 + }; 1.58 + 1.59 + template <bool> struct enable_if {}; 1.60 + template <> struct enable_if<true> { 1.61 + typedef empty result; 1.62 + }; 1.63 + 1.64 + template <typename DEST, typename SRC> 1.65 + struct is_convertible : public enable_if<is_convertible_stub<DEST,SRC>::result > { 1.66 + }; 1.67 + } 1.68 + 1.69 +// ------------------------------ 1.70 +// Small replacement for boost::shared_array, not threadsafe because no 1.71 +// atomic reference counter is in use. 1.72 +// ------------------------------ 1.73 +template <class T> 1.74 +class shared_array 1.75 +{ 1.76 + template <typename TT> friend class shared_array; 1.77 + 1.78 + template<class TT> friend bool operator== (const shared_array<TT>& a, const shared_array<TT>& b); 1.79 + template<class TT> friend bool operator!= (const shared_array<TT>& a, const shared_array<TT>& b); 1.80 + template<class TT> friend bool operator< (const shared_array<TT>& a, const shared_array<TT>& b); 1.81 + 1.82 +public: 1.83 + 1.84 + typedef T element_type; 1.85 + 1.86 +public: 1.87 + 1.88 + // provide a default constructor 1.89 + shared_array() 1.90 + : ptr() 1.91 + , ctr(NULL) 1.92 + { 1.93 + } 1.94 + 1.95 + // construction from an existing object of type T 1.96 + explicit shared_array(T* ptr) 1.97 + : ptr(ptr) 1.98 + , ctr(ptr ? new array_detail::controller() : NULL) 1.99 + { 1.100 + } 1.101 + 1.102 + shared_array(const shared_array& r) 1.103 + : ptr(r.ptr) 1.104 + , ctr(r.ctr ? r.ctr->incref() : NULL) 1.105 + { 1.106 + } 1.107 + 1.108 + template <typename Y> 1.109 + shared_array(const shared_array<Y>& r,typename detail::is_convertible<T,Y>::result = detail::empty()) 1.110 + : ptr(r.ptr) 1.111 + , ctr(r.ctr ? r.ctr->incref() : NULL) 1.112 + { 1.113 + } 1.114 + 1.115 + // automatic destruction of the wrapped object when all 1.116 + // references are freed. 1.117 + ~shared_array() { 1.118 + if (ctr) { 1.119 + ctr = ctr->decref(ptr); 1.120 + } 1.121 + } 1.122 + 1.123 + shared_array& operator=(const shared_array& r) { 1.124 + if (this == &r) { 1.125 + return *this; 1.126 + } 1.127 + if (ctr) { 1.128 + ctr->decref(ptr); 1.129 + } 1.130 + ptr = r.ptr; 1.131 + ctr = ptr?r.ctr->incref():NULL; 1.132 + return *this; 1.133 + } 1.134 + 1.135 + template <typename Y> 1.136 + shared_array& operator=(const shared_array<Y>& r) { 1.137 + if (this == &r) { 1.138 + return *this; 1.139 + } 1.140 + if (ctr) { 1.141 + ctr->decref(ptr); 1.142 + } 1.143 + ptr = r.ptr; 1.144 + ctr = ptr?r.ctr->incref():NULL; 1.145 + return *this; 1.146 + } 1.147 + 1.148 + // pointer access 1.149 + inline operator T*() { 1.150 + return ptr; 1.151 + } 1.152 + 1.153 + inline T* operator-> () const { 1.154 + return ptr; 1.155 + } 1.156 + 1.157 + // standard semantics 1.158 + inline T* get() { 1.159 + return ptr; 1.160 + } 1.161 + 1.162 + T& operator[] (std::ptrdiff_t index) const { 1.163 + return ptr[index]; 1.164 + } 1.165 + 1.166 + inline const T* get() const { 1.167 + return ptr; 1.168 + } 1.169 + 1.170 + inline operator bool () const { 1.171 + return ptr != NULL; 1.172 + } 1.173 + 1.174 + inline bool unique() const { 1.175 + return use_count() == 1; 1.176 + } 1.177 + 1.178 + inline long use_count() const { 1.179 + return ctr->get(); 1.180 + } 1.181 + 1.182 + inline void reset (T* t = 0) { 1.183 + if (ctr) { 1.184 + ctr->decref(ptr); 1.185 + } 1.186 + ptr = t; 1.187 + ctr = ptr?new array_detail::controller():NULL; 1.188 + } 1.189 + 1.190 + void swap(shared_array & b) { 1.191 + std::swap(ptr, b.ptr); 1.192 + std::swap(ctr, b.ctr); 1.193 + } 1.194 + 1.195 + 1.196 +private: 1.197 + 1.198 + // encapsulated object pointer 1.199 + T* ptr; 1.200 + 1.201 + // control block 1.202 + array_detail::controller* ctr; 1.203 +}; 1.204 + 1.205 +template<class T> 1.206 +inline void swap(shared_array<T> & a, shared_array<T> & b) 1.207 +{ 1.208 + a.swap(b); 1.209 +} 1.210 + 1.211 +template<class T> 1.212 +bool operator== (const shared_array<T>& a, const shared_array<T>& b) { 1.213 + return a.ptr == b.ptr; 1.214 +} 1.215 +template<class T> 1.216 +bool operator!= (const shared_array<T>& a, const shared_array<T>& b) { 1.217 + return a.ptr != b.ptr; 1.218 +} 1.219 + 1.220 +template<class T> 1.221 +bool operator< (const shared_array<T>& a, const shared_array<T>& b) { 1.222 + return a.ptr < b.ptr; 1.223 +} 1.224 + 1.225 + 1.226 +} // end of namespace boost 1.227 + 1.228 +#else 1.229 +# error "shared_array.h was already included" 1.230 +#endif 1.231 +#endif // INCLUDED_AI_BOOST_SHARED_ARRAY