CppCMS
intrusive_ptr.h
1 #ifndef BOOSTER_INTRUSIVE_PTR_H_INCLUDED
2 #define BOOSTER_INTRUSIVE_PTR_H_INCLUDED
3 
4 //
5 // intrusive_ptr.hpp
6 //
7 // Copyright (c) 2001, 2002 Peter Dimov
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
14 //
15 
16 
17 #include <functional> // for std::less
18 #include <iosfwd> // for std::basic_ostream
19 
20 
21 namespace booster
22 {
23 
41 
42 template<class T> class intrusive_ptr
43 {
44 private:
45 
46  typedef intrusive_ptr this_type;
47 
48 public:
49 
50  typedef T element_type;
51 
52  intrusive_ptr(): p_(0)
53  {
54  }
55 
56  intrusive_ptr(T * p, bool add_ref = true): p_(p)
57  {
58  if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
59  }
60 
61  T *release()
62  {
63  T *r = p_;
64  p_ = 0;
65  return r;
66  }
67 
68  intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
69  {
70  if(p_ != 0) intrusive_ptr_add_ref(p_);
71  }
72 
73  ~intrusive_ptr()
74  {
75  if(p_ != 0) intrusive_ptr_release(p_);
76  }
77 
78  template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
79  {
80  if(p_ != 0) intrusive_ptr_add_ref(p_);
81  }
82 
83  intrusive_ptr & operator=(intrusive_ptr const & rhs)
84  {
85  this_type(rhs).swap(*this);
86  return *this;
87  }
88 
89  intrusive_ptr & operator=(T * rhs)
90  {
91  this_type(rhs).swap(*this);
92  return *this;
93  }
94 
95  T * get() const
96  {
97  return p_;
98  }
99 
100  T & operator*() const
101  {
102  return *p_;
103  }
104 
105  T * operator->() const
106  {
107  return p_;
108  }
109 
110 
111  typedef T * this_type::*unspecified_bool_type;
112 
113  operator unspecified_bool_type () const
114  {
115  return p_ == 0? 0: &this_type::p_;
116  }
117 
118  // operator! is a Borland-specific workaround
119  bool operator! () const
120  {
121  return p_ == 0;
122  }
123 
124  void swap(intrusive_ptr & rhs)
125  {
126  T * tmp = p_;
127  p_ = rhs.p_;
128  rhs.p_ = tmp;
129  }
130 
131 private:
132 
133  T * p_;
134 };
135 
136 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
137 {
138  return a.get() == b.get();
139 }
140 
141 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
142 {
143  return a.get() != b.get();
144 }
145 
146 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
147 {
148  return a.get() == b;
149 }
150 
151 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
152 {
153  return a.get() != b;
154 }
155 
156 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
157 {
158  return a == b.get();
159 }
160 
161 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
162 {
163  return a != b.get();
164 }
165 
166 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
167 {
168  return std::less<T *>()(a.get(), b.get());
169 }
170 
171 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
172 {
173  lhs.swap(rhs);
174 }
175 
176 // mem_fn support
177 
178 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
179 {
180  return p.get();
181 }
182 
183 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
184 {
185  return static_cast<T *>(p.get());
186 }
187 
188 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
189 {
190  return const_cast<T *>(p.get());
191 }
192 
193 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
194 {
195  return dynamic_cast<T *>(p.get());
196 }
197 
198 // operator<<
199 
200 
201 template<class E, class T, class Y>
202 std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
203 {
204  os << p.get();
205  return os;
206 }
207 
208 
209 } // namespace booster
210 
211 #endif // #ifndef BOOSTER_INTRUSIVE_PTR_H_INCLUDED
intrusive_ptr is the class taken as-is from boost.
Definition: intrusive_ptr.h:42
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23