vrshoot
diff libs/assimp/boost/shared_ptr.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_ptr.hpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,257 @@ 1.4 + 1.5 +#ifndef INCLUDED_AI_BOOST_SHARED_PTR 1.6 +#define INCLUDED_AI_BOOST_SHARED_PTR 1.7 + 1.8 +#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED 1.9 + 1.10 +// ------------------------------ 1.11 +// Internal stub 1.12 +namespace boost { 1.13 + namespace 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_ptr, not threadsafe because no 1.71 +// atomic reference counter is in use. 1.72 +// ------------------------------ 1.73 +template <class T> 1.74 +class shared_ptr 1.75 +{ 1.76 + template <typename TT> friend class shared_ptr; 1.77 + 1.78 + template<class TT, class U> friend shared_ptr<TT> static_pointer_cast (shared_ptr<U> ptr); 1.79 + template<class TT, class U> friend shared_ptr<TT> dynamic_pointer_cast (shared_ptr<U> ptr); 1.80 + template<class TT, class U> friend shared_ptr<TT> const_pointer_cast (shared_ptr<U> ptr); 1.81 + 1.82 + template<class TT> friend bool operator== (const shared_ptr<TT>& a, const shared_ptr<TT>& b); 1.83 + template<class TT> friend bool operator!= (const shared_ptr<TT>& a, const shared_ptr<TT>& b); 1.84 + template<class TT> friend bool operator< (const shared_ptr<TT>& a, const shared_ptr<TT>& b); 1.85 + 1.86 +public: 1.87 + 1.88 + typedef T element_type; 1.89 + 1.90 +public: 1.91 + 1.92 + // provide a default constructor 1.93 + shared_ptr() 1.94 + : ptr() 1.95 + , ctr(NULL) 1.96 + { 1.97 + } 1.98 + 1.99 + // construction from an existing object of type T 1.100 + explicit shared_ptr(T* ptr) 1.101 + : ptr(ptr) 1.102 + , ctr(ptr ? new detail::controller() : NULL) 1.103 + { 1.104 + } 1.105 + 1.106 + shared_ptr(const shared_ptr& r) 1.107 + : ptr(r.ptr) 1.108 + , ctr(r.ctr ? r.ctr->incref() : NULL) 1.109 + { 1.110 + } 1.111 + 1.112 + template <typename Y> 1.113 + shared_ptr(const shared_ptr<Y>& r,typename detail::is_convertible<T,Y>::result = detail::empty()) 1.114 + : ptr(r.ptr) 1.115 + , ctr(r.ctr ? r.ctr->incref() : NULL) 1.116 + { 1.117 + } 1.118 + 1.119 + // automatic destruction of the wrapped object when all 1.120 + // references are freed. 1.121 + ~shared_ptr() { 1.122 + if (ctr) { 1.123 + ctr = ctr->decref(ptr); 1.124 + } 1.125 + } 1.126 + 1.127 + shared_ptr& operator=(const shared_ptr& r) { 1.128 + if (this == &r) { 1.129 + return *this; 1.130 + } 1.131 + if (ctr) { 1.132 + ctr->decref(ptr); 1.133 + } 1.134 + ptr = r.ptr; 1.135 + ctr = ptr?r.ctr->incref():NULL; 1.136 + return *this; 1.137 + } 1.138 + 1.139 + template <typename Y> 1.140 + shared_ptr& operator=(const shared_ptr<Y>& r) { 1.141 + if (this == &r) { 1.142 + return *this; 1.143 + } 1.144 + if (ctr) { 1.145 + ctr->decref(ptr); 1.146 + } 1.147 + ptr = r.ptr; 1.148 + ctr = ptr?r.ctr->incref():NULL; 1.149 + return *this; 1.150 + } 1.151 + 1.152 + // pointer access 1.153 + inline operator T*() const { 1.154 + return ptr; 1.155 + } 1.156 + 1.157 + inline T* operator-> () const { 1.158 + return ptr; 1.159 + } 1.160 + 1.161 + // standard semantics 1.162 + inline T* get() { 1.163 + return ptr; 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 detail::controller():NULL; 1.188 + } 1.189 + 1.190 + void swap(shared_ptr & b) { 1.191 + std::swap(ptr, b.ptr); 1.192 + std::swap(ctr, b.ctr); 1.193 + } 1.194 + 1.195 +private: 1.196 + 1.197 + 1.198 + // for use by the various xxx_pointer_cast helper templates 1.199 + explicit shared_ptr(T* ptr, detail::controller* ctr) 1.200 + : ptr(ptr) 1.201 + , ctr(ctr->incref()) 1.202 + { 1.203 + } 1.204 + 1.205 +private: 1.206 + 1.207 + // encapsulated object pointer 1.208 + T* ptr; 1.209 + 1.210 + // control block 1.211 + detail::controller* ctr; 1.212 +}; 1.213 + 1.214 +template<class T> 1.215 +inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) 1.216 +{ 1.217 + a.swap(b); 1.218 +} 1.219 + 1.220 +template<class T> 1.221 +bool operator== (const shared_ptr<T>& a, const shared_ptr<T>& b) { 1.222 + return a.ptr == b.ptr; 1.223 +} 1.224 +template<class T> 1.225 +bool operator!= (const shared_ptr<T>& a, const shared_ptr<T>& b) { 1.226 + return a.ptr != b.ptr; 1.227 +} 1.228 + 1.229 +template<class T> 1.230 +bool operator< (const shared_ptr<T>& a, const shared_ptr<T>& b) { 1.231 + return a.ptr < b.ptr; 1.232 +} 1.233 + 1.234 + 1.235 +template<class T, class U> 1.236 +inline shared_ptr<T> static_pointer_cast( shared_ptr<U> ptr) 1.237 +{ 1.238 + return shared_ptr<T>(static_cast<T*>(ptr.ptr),ptr.ctr); 1.239 +} 1.240 + 1.241 +template<class T, class U> 1.242 +inline shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> ptr) 1.243 +{ 1.244 + return shared_ptr<T>(dynamic_cast<T*>(ptr.ptr),ptr.ctr); 1.245 +} 1.246 + 1.247 +template<class T, class U> 1.248 +inline shared_ptr<T> const_pointer_cast( shared_ptr<U> ptr) 1.249 +{ 1.250 + return shared_ptr<T>(const_cast<T*>(ptr.ptr),ptr.ctr); 1.251 +} 1.252 + 1.253 + 1.254 + 1.255 +} // end of namespace boost 1.256 + 1.257 +#else 1.258 +# error "shared_ptr.h was already included" 1.259 +#endif 1.260 +#endif // INCLUDED_AI_BOOST_SCOPED_PTR