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  intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
62  {
63  if(p_ != 0) intrusive_ptr_add_ref(p_);
64  }
65 
66  ~intrusive_ptr()
67  {
68  if(p_ != 0) intrusive_ptr_release(p_);
69  }
70 
71  template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
72  {
73  if(p_ != 0) intrusive_ptr_add_ref(p_);
74  }
75 
76  intrusive_ptr & operator=(intrusive_ptr const & rhs)
77  {
78  this_type(rhs).swap(*this);
79  return *this;
80  }
81 
82  intrusive_ptr & operator=(T * rhs)
83  {
84  this_type(rhs).swap(*this);
85  return *this;
86  }
87 
88  T * get() const
89  {
90  return p_;
91  }
92 
93  T & operator*() const
94  {
95  return *p_;
96  }
97 
98  T * operator->() const
99  {
100  return p_;
101  }
102 
103 
104  typedef T * this_type::*unspecified_bool_type;
105 
106  operator unspecified_bool_type () const
107  {
108  return p_ == 0? 0: &this_type::p_;
109  }
110 
111  // operator! is a Borland-specific workaround
112  bool operator! () const
113  {
114  return p_ == 0;
115  }
116 
117  void swap(intrusive_ptr & rhs)
118  {
119  T * tmp = p_;
120  p_ = rhs.p_;
121  rhs.p_ = tmp;
122  }
123 
124 private:
125 
126  T * p_;
127 };
128 
129 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
130 {
131  return a.get() == b.get();
132 }
133 
134 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
135 {
136  return a.get() != b.get();
137 }
138 
139 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
140 {
141  return a.get() == b;
142 }
143 
144 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
145 {
146  return a.get() != b;
147 }
148 
149 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
150 {
151  return a == b.get();
152 }
153 
154 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
155 {
156  return a != b.get();
157 }
158 
159 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
160 {
161  return std::less<T *>()(a.get(), b.get());
162 }
163 
164 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
165 {
166  lhs.swap(rhs);
167 }
168 
169 // mem_fn support
170 
171 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
172 {
173  return p.get();
174 }
175 
176 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
177 {
178  return static_cast<T *>(p.get());
179 }
180 
181 template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
182 {
183  return const_cast<T *>(p.get());
184 }
185 
186 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
187 {
188  return dynamic_cast<T *>(p.get());
189 }
190 
191 // operator<<
192 
193 
194 template<class E, class T, class Y>
195 std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
196 {
197  os << p.get();
198  return os;
199 }
200 
201 
202 } // namespace booster
203 
204 #endif // #ifndef BOOSTER_INTRUSIVE_PTR_H_INCLUDED
intrusive_ptr is the class taken as-is from boost.
Definition: intrusive_ptr.h:42