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 // Move support
84 
85 
86  intrusive_ptr(intrusive_ptr && rhs) noexcept : p_( rhs.p_ )
87  {
88  rhs.p_ = 0;
89  }
90 
91  intrusive_ptr & operator=(intrusive_ptr && rhs) noexcept
92  {
93  this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
94  return *this;
95  }
96 
97  template<class U> friend class intrusive_ptr;
98 
99  template<class U>
101  : p_( rhs.p_ )
102  {
103  rhs.p_ = 0;
104  }
105 
106  template<class U>
107  intrusive_ptr & operator=(intrusive_ptr<U> && rhs) noexcept
108  {
109  this_type( static_cast< intrusive_ptr<U> && >( rhs ) ).swap(*this);
110  return *this;
111  }
112 
113  intrusive_ptr & operator=(intrusive_ptr const & rhs)
114  {
115  this_type(rhs).swap(*this);
116  return *this;
117  }
118 
119  intrusive_ptr & operator=(T * rhs)
120  {
121  this_type(rhs).swap(*this);
122  return *this;
123  }
124 
125  T * get() const
126  {
127  return p_;
128  }
129 
130  T & operator*() const
131  {
132  return *p_;
133  }
134 
135  T * operator->() const
136  {
137  return p_;
138  }
139 
140 
141  typedef T * this_type::*unspecified_bool_type;
142 
143  operator unspecified_bool_type () const
144  {
145  return p_ == 0? 0: &this_type::p_;
146  }
147 
148  // operator! is a Borland-specific workaround
149  bool operator! () const
150  {
151  return p_ == 0;
152  }
153 
154  void swap(intrusive_ptr & rhs)
155  {
156  T * tmp = p_;
157  p_ = rhs.p_;
158  rhs.p_ = tmp;
159  }
160 
161 private:
162 
163  T * p_;
164 };
165 
166 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
167 {
168  return a.get() == b.get();
169 }
170 
171 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
172 {
173  return a.get() != b.get();
174 }
175 
176 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
177 {
178  return a.get() == b;
179 }
180 
181 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
182 {
183  return a.get() != b;
184 }
185 
186 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
187 {
188  return a == b.get();
189 }
190 
191 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
192 {
193  return a != b.get();
194 }
195 
196 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
197 {
198  return std::less<T *>()(a.get(), b.get());
199 }
200 
201 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
202 {
203  lhs.swap(rhs);
204 }
205 
206 // mem_fn support
207 
208 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
209 {
210  return p.get();
211 }
212 
213 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
214 {
215  return static_cast<T *>(p.get());
216 }
217 
218 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
219 {
220  return const_cast<T *>(p.get());
221 }
222 
223 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
224 {
225  return dynamic_cast<T *>(p.get());
226 }
227 
228 // operator<<
229 
230 
231 template<class E, class T, class Y>
232 std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
233 {
234  os << p.get();
235  return os;
236 }
237 
238 
239 } // namespace booster
240 
241 #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