1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/intrusive for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef BOOST_INTRUSIVE_SMART_PTR_HPP
12 #define BOOST_INTRUSIVE_SMART_PTR_HPP
13 
14 #include <boost/intrusive/pointer_plus_bits.hpp>
15 #include <boost/intrusive/pointer_traits.hpp>
16 #include <boost/intrusive/detail/iterator.hpp>
17 #include <boost/move/adl_move_swap.hpp>
18 
19 #if (defined _MSC_VER)
20 #  pragma once
21 #endif
22 
23 namespace boost{
24 namespace intrusive{
25 
26 namespace detail {
27 
28 struct static_cast_tag {};
29 struct const_cast_tag {};
30 struct dynamic_cast_tag {};
31 struct reinterpret_cast_tag {};
32 
33 }  //namespace detail {
34 
35 //Empty class
36 struct empty_type{};
37 
38 template<class T>
39 struct random_it
40 : public iterator<std::random_access_iterator_tag, T, std::ptrdiff_t, T*, T&>
41 {
42    typedef const T*           const_pointer;
43    typedef const T&           const_reference;
44 };
45 
46 template<> struct random_it<void>
47 {
48    typedef const void *       const_pointer;
49    typedef empty_type&        reference;
50    typedef const empty_type&  const_reference;
51    typedef empty_type         difference_type;
52    typedef empty_type         iterator_category;
53 };
54 
55 template<> struct random_it<const void>
56 {
57    typedef const void *       const_pointer;
58    typedef const empty_type & reference;
59    typedef const empty_type & const_reference;
60    typedef empty_type         difference_type;
61    typedef empty_type         iterator_category;
62 };
63 
64 template<> struct random_it<volatile void>
65 {
66    typedef const volatile void * const_pointer;
67    typedef empty_type&           reference;
68    typedef const empty_type&     const_reference;
69    typedef empty_type            difference_type;
70    typedef empty_type            iterator_category;
71 };
72 
73 template<> struct random_it<const volatile void>
74 {
75    typedef const volatile void *    const_pointer;
76    typedef const empty_type &       reference;
77    typedef const empty_type &       const_reference;
78    typedef empty_type               difference_type;
79    typedef empty_type               iterator_category;
80 };
81 
82 }  //namespace intrusive {
83 }  //namespace boost {
84 
85 
86 namespace boost {
87 namespace intrusive {
88 
89 template <class PointedType>
90 class smart_ptr
91 {
92    typedef random_it<PointedType> random_it_t;
93    typedef smart_ptr<PointedType>                           self_t;
94    typedef typename random_it_t::const_pointer              const_pointer_t;
95    typedef typename random_it_t::const_reference            const_reference_t;
96 
unspecified_bool_type_func() const97    void unspecified_bool_type_func() const {}
98    typedef void (self_t::*unspecified_bool_type)() const;
99 
100    public:
101    typedef PointedType *                           pointer;
102    typedef typename random_it_t::reference         reference;
103    typedef PointedType                             value_type;
104    typedef typename random_it_t::difference_type   difference_type;
105    typedef typename random_it_t::iterator_category iterator_category;
106 
107    PointedType *m_ptr;
108 
109    public:   //Public Functions
110 
smart_ptr()111    smart_ptr()
112       :  m_ptr(0)
113    {}
114 
115    //!Constructor from other smart_ptr
smart_ptr(const smart_ptr & ptr)116    smart_ptr(const smart_ptr& ptr)
117       :  m_ptr(ptr.m_ptr)
118    {}
119 
pointer_to(reference r)120    static smart_ptr pointer_to(reference r)
121    {  smart_ptr p; p.m_ptr = &r; return p;  }
122 
123    //!Constructor from other smart_ptr. If pointers of pointee types are
124    //!convertible, offset_ptrs will be convertibles. Never throws.
125    template<class T2>
smart_ptr(const smart_ptr<T2> & ptr)126    smart_ptr(const smart_ptr<T2> &ptr)
127       :  m_ptr(ptr.m_ptr)
128    {}
129 
130    //!Pointer-like -> operator. It can return 0 pointer. Never throws.
operator ->() const131    pointer operator->() const
132    {  return m_ptr; }
133 
134    //!Dereferencing operator, if it is a null smart_ptr behavior
135    //!   is undefined. Never throws.
operator *() const136    reference operator* () const
137    {  return *m_ptr;   }
138 
139    //!Indexing operator. Never throws.
operator [](std::ptrdiff_t idx) const140    reference operator[](std::ptrdiff_t idx) const
141    {  return m_ptr[idx];  }
142 
143    //!Assignment from other smart_ptr. Never throws.
operator =(const smart_ptr & pt)144    smart_ptr& operator= (const smart_ptr & pt)
145    {  m_ptr = pt.m_ptr;  return *this;  }
146 
147    //!Assignment from related smart_ptr. If pointers of pointee types
148    //!   are assignable, offset_ptrs will be assignable. Never throws.
149    template <class T2>
operator =(const smart_ptr<T2> & pt)150    smart_ptr& operator= (const smart_ptr<T2> & pt)
151    {  m_ptr = pt.m_ptr;  return *this;  }
152 
153    //!smart_ptr + std::ptrdiff_t. Never throws.
operator +(std::ptrdiff_t offset) const154    smart_ptr operator+ (std::ptrdiff_t offset) const
155    {  smart_ptr s; s.m_ptr = m_ptr + offset; return s;   }
156 
157    //!smart_ptr - std::ptrdiff_t. Never throws.
operator -(std::ptrdiff_t offset) const158    smart_ptr operator- (std::ptrdiff_t offset) const
159    {  smart_ptr s; s.m_ptr = m_ptr - offset; return s;   }
160 
161    //!smart_ptr += std::ptrdiff_t. Never throws.
operator +=(std::ptrdiff_t offset)162    smart_ptr &operator+= (std::ptrdiff_t offset)
163    {  m_ptr += offset;   return *this;  }
164 
165    //!smart_ptr -= std::ptrdiff_t. Never throws.
operator -=(std::ptrdiff_t offset)166    smart_ptr &operator-= (std::ptrdiff_t offset)
167    {  m_ptr -= offset;  return *this;  }
168 
169    //!++smart_ptr. Never throws.
operator ++(void)170    smart_ptr& operator++ (void)
171    {  ++m_ptr;   return *this;  }
172 
173    //!smart_ptr++. Never throws.
operator ++(int)174    smart_ptr operator++ (int)
175    {  smart_ptr temp(*this); ++*this; return temp; }
176 
177    //!--smart_ptr. Never throws.
operator --(void)178    smart_ptr& operator-- (void)
179    {  --m_ptr;   return *this;  }
180 
181    //!smart_ptr--. Never throws.
operator --(int)182    smart_ptr operator-- (int)
183    {  smart_ptr temp(*this); --*this; return temp; }
184 
185    //!safe bool conversion operator. Never throws.
operator unspecified_bool_type() const186    operator unspecified_bool_type() const
187    {  return m_ptr? &self_t::unspecified_bool_type_func : 0;   }
188 
189    //!Not operator. Not needed in theory, but improves portability.
190    //!Never throws.
operator !() const191    bool operator! () const
192    {  return m_ptr == 0;   }
193 
194    //!swap
swap(smart_ptr & x,smart_ptr & y)195    friend void swap(smart_ptr& x, smart_ptr& y)
196    {  boost::adl_move_swap(x.m_ptr, y.m_ptr);   }
197 };
198 
199 //!smart_ptr<T1> == smart_ptr<T2>. Never throws.
200 template<class T1, class T2>
operator ==(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)201 inline bool operator== (const smart_ptr<T1> &pt1,
202                         const smart_ptr<T2> &pt2)
203 {  return pt1.operator->() == pt2.operator->();  }
204 
205 //!smart_ptr<T1> != smart_ptr<T2>. Never throws.
206 template<class T1, class T2>
operator !=(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)207 inline bool operator!= (const smart_ptr<T1> &pt1,
208                         const smart_ptr<T2> &pt2)
209 {  return pt1.operator->() != pt2.operator->();  }
210 
211 //!smart_ptr<T1> < smart_ptr<T2>. Never throws.
212 template<class T1, class T2>
operator <(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)213 inline bool operator< (const smart_ptr<T1> &pt1,
214                        const smart_ptr<T2> &pt2)
215 {  return pt1.operator->() < pt2.operator->();  }
216 
217 //!smart_ptr<T1> <= smart_ptr<T2>. Never throws.
218 template<class T1, class T2>
operator <=(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)219 inline bool operator<= (const smart_ptr<T1> &pt1,
220                         const smart_ptr<T2> &pt2)
221 {  return pt1.operator->() <= pt2.operator->();  }
222 
223 //!smart_ptr<T1> > smart_ptr<T2>. Never throws.
224 template<class T1, class T2>
operator >(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)225 inline bool operator> (const smart_ptr<T1> &pt1,
226                        const smart_ptr<T2> &pt2)
227 {  return pt1.operator->() > pt2.operator->();  }
228 
229 //!smart_ptr<T1> >= smart_ptr<T2>. Never throws.
230 template<class T1, class T2>
operator >=(const smart_ptr<T1> & pt1,const smart_ptr<T2> & pt2)231 inline bool operator>= (const smart_ptr<T1> &pt1,
232                         const smart_ptr<T2> &pt2)
233 {  return pt1.operator->() >= pt2.operator->();  }
234 
235 //!operator<<
236 template<class E, class T, class Y>
operator <<(std::basic_ostream<E,T> & os,smart_ptr<Y> const & p)237 inline std::basic_ostream<E, T> & operator<<
238    (std::basic_ostream<E, T> & os, smart_ptr<Y> const & p)
239 {  return os << p.operator->();   }
240 
241 //!operator>>
242 template<class E, class T, class Y>
operator >>(std::basic_istream<E,T> & os,smart_ptr<Y> & p)243 inline std::basic_istream<E, T> & operator>>
244    (std::basic_istream<E, T> & os, smart_ptr<Y> & p)
245 {  Y * tmp; return os >> tmp; p = tmp;   }
246 
247 //!std::ptrdiff_t + smart_ptr
248 template<class T>
operator +(std::ptrdiff_t diff,const smart_ptr<T> & right)249 inline smart_ptr<T> operator+(std::ptrdiff_t diff, const smart_ptr<T>& right)
250 {  return right + diff;  }
251 
252 //!smart_ptr - smart_ptr
253 template<class T, class T2>
operator -(const smart_ptr<T> & pt,const smart_ptr<T2> & pt2)254 inline std::ptrdiff_t operator- (const smart_ptr<T> &pt, const smart_ptr<T2> &pt2)
255 {  return pt.operator->()- pt2.operator->();   }
256 
257 }  //namespace intrusive {
258 }  //namespace boost {
259 
260 namespace boost{
261 
262 //This is to support embedding a bit in the pointer
263 //for intrusive containers, saving space
264 namespace intrusive {
265 
266 template<std::size_t Alignment>
267 struct max_pointer_plus_bits<smart_ptr<void>, Alignment>
268 {
269    static const std::size_t value = max_pointer_plus_bits<void*, Alignment>::value;
270 };
271 
272 template<class T, std::size_t NumBits>
273 struct pointer_plus_bits<smart_ptr<T>, NumBits>
274 {
275    typedef smart_ptr<T>         pointer;
276 
get_pointerboost::intrusive::pointer_plus_bits277    static pointer get_pointer(const pointer &n)
278    {
279       return  pointer_traits<pointer>::pointer_to
280          (*pointer_plus_bits<T*, NumBits>::get_pointer(n.operator->()));
281    }
282 
set_pointerboost::intrusive::pointer_plus_bits283    static void set_pointer(pointer &n, pointer p)
284    {
285       T *raw_n = n.operator->();
286       pointer_plus_bits<T*, NumBits>::set_pointer(raw_n, p.operator->());
287       n = pointer_traits<pointer>::pointer_to(*raw_n);
288    }
289 
get_bitsboost::intrusive::pointer_plus_bits290    static std::size_t get_bits(const pointer &n)
291    {  return pointer_plus_bits<T*, NumBits>::get_bits(n.operator->());  }
292 
set_bitsboost::intrusive::pointer_plus_bits293    static void set_bits(pointer &n, std::size_t c)
294    {
295       T *raw_n = n.operator->();
296       pointer_plus_bits<T*, NumBits>::set_bits(raw_n, c);
297       n = pointer_traits<pointer>::pointer_to(*raw_n);
298    }
299 };
300 
301 }  //namespace intrusive
302 }  //namespace boost{
303 
304 #endif //#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP
305