00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef SGE_SHARED_PTR_HPP_INCLUDED
00022 #define SGE_SHARED_PTR_HPP_INCLUDED
00023
00024 #include <boost/shared_ptr.hpp>
00025 #include "heap_deleter.hpp"
00026
00027 namespace sge
00028 {
00029
00030 using boost::weak_ptr;
00031
00032 template<typename T, template<typename> class Deleter = heap_deleter>
00033 class shared_ptr {
00034 public:
00035 typedef boost::shared_ptr<T> impl_type;
00036 typedef typename impl_type::element_type element_type;
00037 typedef typename impl_type::value_type value_type;
00038 typedef typename impl_type::pointer pointer;
00039 typedef typename impl_type::reference reference;
00040
00041 shared_ptr()
00042 {}
00043
00044
00045 template<typename U>
00046 explicit shared_ptr(const boost::shared_ptr<U>& p)
00047 : impl(p)
00048 {}
00049
00050 template<class Y>
00051 explicit shared_ptr( Y *const p )
00052 : impl(p, deleter())
00053 {}
00054
00055 template<class Y, class D, class A> shared_ptr( Y *const p, const A& a )
00056 : impl(p, deleter(), a)
00057 {}
00058
00059 template<class Y>
00060 explicit shared_ptr(weak_ptr<Y> const & r)
00061 : impl(r)
00062 {}
00063
00064 template<class Y>
00065 shared_ptr(shared_ptr<Y> const & r)
00066 : impl(r.get_boost_ptr())
00067 {}
00068
00069 template<class Y>
00070 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag)
00071 : impl(r.get_boost_ptr(), boost::detail::static_cast_tag())
00072 {}
00073
00074 template<class Y>
00075 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag)
00076 : impl(r.get_boost_ptr(), boost::detail::const_cast_tag())
00077 {}
00078
00079 template<class Y>
00080 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag)
00081 : impl(r.get_boost_ptr(), boost::detail::dynamic_cast_tag())
00082 {}
00083
00084 template<class Y>
00085 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag)
00086 : impl(r.get_boost_ptr(), boost::detail::polymorphic_cast_tag())
00087 {}
00088 #ifndef BOOST_NO_AUTO_PTR
00089
00090 template<class Y>
00091 explicit shared_ptr(std::auto_ptr<Y> & r)
00092 : impl(r)
00093 {}
00094
00095
00096
00097
00098
00099
00100
00101 #endif // BOOST_NO_AUTO_PTR
00102
00103 template<class Y>
00104 shared_ptr & operator=(shared_ptr<Y> const & r)
00105 {
00106 impl = r.impl;
00107 return *this;
00108 }
00109
00110 #ifndef BOOST_NO_AUTO_PTR
00111
00112 template<class Y>
00113 shared_ptr & operator=( std::auto_ptr<Y> & r )
00114 {
00115 impl = r;
00116 return *this;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 #endif // BOOST_NO_AUTO_PTR
00129
00130 void reset()
00131 {
00132 impl.reset();
00133 }
00134
00135 template<class Y> void reset(Y *const p)
00136 {
00137 impl.reset(p, deleter());
00138 }
00139
00140 template<class Y, class A> void reset( Y *const p, const A& a )
00141 {
00142 impl.reset(p, deleter(), a);
00143 }
00144
00145 reference operator* () const
00146 {
00147 return *impl;
00148 }
00149
00150 pointer operator-> () const
00151 {
00152 return impl.operator->();
00153 }
00154
00155 pointer get() const
00156 {
00157 return impl.get();
00158 }
00159
00160 typedef typename impl_type::unspecified_bool_type unspecified_bool_type;
00161
00162 operator unspecified_bool_type() const
00163 {
00164 return impl;
00165 }
00166
00167 bool operator! () const
00168 {
00169 return !impl;
00170 }
00171
00172 bool unique() const
00173 {
00174 return impl.unique();
00175 }
00176
00177 long use_count() const
00178 {
00179 return impl.use_count();
00180 }
00181
00182 void swap(shared_ptr<T> & other)
00183 {
00184 std::swap(impl, other.impl);
00185 }
00186
00187 const impl_type& get_boost_ptr() const
00188 {
00189 return impl;
00190 }
00191 private:
00192 impl_type impl;
00193
00194 static Deleter<T> deleter()
00195 {
00196 return Deleter<T>();
00197 }
00198
00199 template<typename Other, template<typename> class OtherDeleter> friend class shared_ptr;
00200 };
00201
00202 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
00203 {
00204 return a.get_boost_ptr() == b.get_boost_ptr();
00205 }
00206
00207 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
00208 {
00209 return !(a==b);
00210 }
00211
00212 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
00213 {
00214 return a.get_boost_ptr() < b.get_boost_ptr();
00215 }
00216
00217 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
00218 {
00219 a.swap(b);
00220 }
00221
00222 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
00223 {
00224 return shared_ptr<T>(r, boost::detail::static_cast_tag());
00225 }
00226
00227 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
00228 {
00229 return shared_ptr<T>(r, boost::detail::const_cast_tag());
00230 }
00231
00232 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
00233 {
00234 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
00235 }
00236
00237 template<typename T, typename U> shared_ptr<T> polymorphic_pointer_cast(const shared_ptr<U>& r)
00238 {
00239 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
00240 }
00241
00242 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
00243 {
00244 return p.get();
00245 }
00246
00247 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
00248 {
00249 os << p.get();
00250 return os;
00251 }
00252
00253 }
00254
00255 #endif