kig
boost_intrusive_pointer.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
00025 #define MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <functional>
00041 #include <iosfwd>
00042
00043
00044 namespace myboost
00045 {
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 template<class T> class intrusive_ptr
00063 {
00064 private:
00065
00066 typedef intrusive_ptr this_type;
00067
00068 public:
00069
00070 typedef T element_type;
00071
00072 intrusive_ptr(): p_(0)
00073 {
00074 }
00075
00076 intrusive_ptr(T * p, bool add_ref = true): p_(p)
00077 {
00078 if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
00079 }
00080
00081 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00082
00083 template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
00084 {
00085 if(p_ != 0) intrusive_ptr_add_ref(p_);
00086 }
00087
00088 #endif
00089
00090 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
00091 {
00092 if(p_ != 0) intrusive_ptr_add_ref(p_);
00093 }
00094
00095 ~intrusive_ptr()
00096 {
00097 if(p_ != 0) intrusive_ptr_release(p_);
00098 }
00099
00100 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00101
00102 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
00103 {
00104 this_type(rhs).swap(*this);
00105 return *this;
00106 }
00107
00108 #endif
00109
00110 intrusive_ptr & operator=(intrusive_ptr const & rhs)
00111 {
00112 this_type(rhs).swap(*this);
00113 return *this;
00114 }
00115
00116 intrusive_ptr & operator=(T * rhs)
00117 {
00118 this_type(rhs).swap(*this);
00119 return *this;
00120 }
00121
00122 T * get() const
00123 {
00124 return p_;
00125 }
00126
00127 T & operator*() const
00128 {
00129 return *p_;
00130 }
00131
00132 T * operator->() const
00133 {
00134 return p_;
00135 }
00136
00137 typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
00138
00139 operator unspecified_bool_type () const
00140 {
00141 return p_ == 0? 0: &intrusive_ptr::get;
00142 }
00143
00144
00145 bool operator! () const
00146 {
00147 return p_ == 0;
00148 }
00149
00150 void swap(intrusive_ptr & rhs)
00151 {
00152 T * tmp = p_;
00153 p_ = rhs.p_;
00154 rhs.p_ = tmp;
00155 }
00156
00157 private:
00158
00159 T * p_;
00160 };
00161
00162 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
00163 {
00164 return a.get() == b.get();
00165 }
00166
00167 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
00168 {
00169 return a.get() != b.get();
00170 }
00171
00172 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
00173 {
00174 return a.get() == b;
00175 }
00176
00177 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
00178 {
00179 return a.get() != b;
00180 }
00181
00182 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
00183 {
00184 return a == b.get();
00185 }
00186
00187 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
00188 {
00189 return a != b.get();
00190 }
00191
00192 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00193
00194
00195
00196 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
00197 {
00198 return a.get() != b.get();
00199 }
00200
00201 #endif
00202
00203 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
00204 {
00205 return std::less<T *>()(a.get(), b.get());
00206 }
00207
00208 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
00209 {
00210 lhs.swap(rhs);
00211 }
00212
00213
00214
00215 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
00216 {
00217 return p.get();
00218 }
00219
00220 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
00221 {
00222 return static_cast<T *>(p.get());
00223 }
00224
00225 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
00226 {
00227 return dynamic_cast<T *>(p.get());
00228 }
00229
00230
00231
00232 #if defined(__GNUC__) && (__GNUC__ < 3)
00233
00234 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
00235 {
00236 os << p.get();
00237 return os;
00238 }
00239
00240 #else
00241
00242 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
00243 {
00244 os << p.get();
00245 return os;
00246 }
00247
00248 #endif
00249
00250 }
00251
00252 #ifdef BOOST_MSVC
00253 # pragma warning(pop)
00254 #endif
00255
00256 #endif // #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED