////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/intrusive for documentation. // ////////////////////////////////////////////////////////////////////////////// #ifndef BOOST_INTRUSIVE_SMART_PTR_HPP #define BOOST_INTRUSIVE_SMART_PTR_HPP #include #include #include #if (defined _MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif namespace boost{ namespace intrusive{ namespace detail { struct static_cast_tag {}; struct const_cast_tag {}; struct dynamic_cast_tag {}; struct reinterpret_cast_tag {}; } //namespace detail { //Empty class struct empty_type{}; template struct random_it : public boost::iterator { typedef const T* const_pointer; typedef const T& const_reference; }; template<> struct random_it { typedef const void * const_pointer; typedef empty_type& reference; typedef const empty_type& const_reference; typedef empty_type difference_type; typedef empty_type iterator_category; }; template<> struct random_it { typedef const void * const_pointer; typedef const empty_type & reference; typedef const empty_type & const_reference; typedef empty_type difference_type; typedef empty_type iterator_category; }; template<> struct random_it { typedef const volatile void * const_pointer; typedef empty_type& reference; typedef const empty_type& const_reference; typedef empty_type difference_type; typedef empty_type iterator_category; }; template<> struct random_it { typedef const volatile void * const_pointer; typedef const empty_type & reference; typedef const empty_type & const_reference; typedef empty_type difference_type; typedef empty_type iterator_category; }; } //namespace intrusive { } //namespace boost { namespace boost { namespace intrusive { template class smart_ptr { typedef random_it random_it_t; typedef smart_ptr self_t; typedef typename random_it_t::const_pointer const_pointer_t; typedef typename random_it_t::const_reference const_reference_t; void unspecified_bool_type_func() const {} typedef void (self_t::*unspecified_bool_type)() const; public: typedef PointedType * pointer; typedef typename random_it_t::reference reference; typedef PointedType value_type; typedef typename random_it_t::difference_type difference_type; typedef typename random_it_t::iterator_category iterator_category; PointedType *m_ptr; public: //Public Functions //!Constructor from raw pointer (allows "0" pointer conversion). Never throws. smart_ptr(pointer ptr = 0) : m_ptr(ptr) {} //!Constructor from other pointer. Never throws. template smart_ptr(T *ptr) : m_ptr(ptr) {} //!Constructor from other smart_ptr smart_ptr(const smart_ptr& ptr) : m_ptr(ptr.m_ptr) {} //!Constructor from other smart_ptr. If pointers of pointee types are //!convertible, offset_ptrs will be convertibles. Never throws. template smart_ptr(const smart_ptr &ptr) : m_ptr(ptr.m_ptr) {} //!Emulates static_cast operator. Never throws. template smart_ptr(const smart_ptr & r, detail::static_cast_tag) : m_ptr(static_cast(r.get())) {} //!Emulates const_cast operator. Never throws. template smart_ptr(const smart_ptr & r, detail::const_cast_tag) : m_ptr(const_cast(r.get())) {} //!Emulates dynamic_cast operator. Never throws. template smart_ptr(const smart_ptr & r, detail::dynamic_cast_tag) : m_ptr(dynamic_cast(r.get())) {} //!Emulates reinterpret_cast operator. Never throws. template smart_ptr(const smart_ptr & r, detail::reinterpret_cast_tag) : m_ptr(reinterpret_cast(r.get())) {} //!Obtains raw pointer from offset. Never throws. pointer get() const { return m_ptr; } //!Pointer-like -> operator. It can return 0 pointer. Never throws. pointer operator->() const { return this->get(); } //!Dereferencing operator, if it is a null smart_ptr behavior //! is undefined. Never throws. reference operator* () const { return *(this->get()); } //!Indexing operator. Never throws. reference operator[](std::ptrdiff_t idx) const { return this->get()[idx]; } //!Assignment from pointer (saves extra conversion). Never throws. smart_ptr& operator= (pointer from) { m_ptr = from; return *this; } //!Assignment from other smart_ptr. Never throws. smart_ptr& operator= (const smart_ptr & pt) { m_ptr = pt.m_ptr; return *this; } //!Assignment from related smart_ptr. If pointers of pointee types //! are assignable, offset_ptrs will be assignable. Never throws. template smart_ptr& operator= (const smart_ptr & pt) { m_ptr = pt.m_ptr; return *this; } //!smart_ptr + std::ptrdiff_t. Never throws. smart_ptr operator+ (std::ptrdiff_t offset) const { return smart_ptr(this->get()+offset); } //!smart_ptr - std::ptrdiff_t. Never throws. smart_ptr operator- (std::ptrdiff_t offset) const { return smart_ptr(this->get()-offset); } //!smart_ptr += std::ptrdiff_t. Never throws. smart_ptr &operator+= (std::ptrdiff_t offset) { m_ptr += offset; return *this; } //!smart_ptr -= std::ptrdiff_t. Never throws. smart_ptr &operator-= (std::ptrdiff_t offset) { m_ptr -= offset; return *this; } //!++smart_ptr. Never throws. smart_ptr& operator++ (void) { ++m_ptr; return *this; } //!smart_ptr++. Never throws. smart_ptr operator++ (int) { smart_ptr temp(*this); ++*this; return temp; } //!--smart_ptr. Never throws. smart_ptr& operator-- (void) { --m_ptr; return *this; } //!smart_ptr--. Never throws. smart_ptr operator-- (int) { smart_ptr temp(*this); --*this; return temp; } //!safe bool conversion operator. Never throws. operator unspecified_bool_type() const { return this->get()? &self_t::unspecified_bool_type_func : 0; } //!Not operator. Not needed in theory, but improves portability. //!Never throws. bool operator! () const { return this->get() == 0; } /* friend void swap (smart_ptr &pt, smart_ptr &pt2) { value_type *ptr = pt.get(); pt = pt2; pt2 = ptr; } */ }; //!smart_ptr == smart_ptr. Never throws. template inline bool operator== (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() == pt2.get(); } //!smart_ptr != smart_ptr. Never throws. template inline bool operator!= (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() != pt2.get(); } //!smart_ptr < smart_ptr. Never throws. template inline bool operator< (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() < pt2.get(); } //!smart_ptr <= smart_ptr. Never throws. template inline bool operator<= (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() <= pt2.get(); } //!smart_ptr > smart_ptr. Never throws. template inline bool operator> (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() > pt2.get(); } //!smart_ptr >= smart_ptr. Never throws. template inline bool operator>= (const smart_ptr &pt1, const smart_ptr &pt2) { return pt1.get() >= pt2.get(); } //!operator<< template inline std::basic_ostream & operator<< (std::basic_ostream & os, smart_ptr const & p) { return os << p.get(); } //!operator>> template inline std::basic_istream & operator>> (std::basic_istream & os, smart_ptr & p) { Y * tmp; return os >> tmp; p = tmp; } //!std::ptrdiff_t + smart_ptr template inline smart_ptr operator+(std::ptrdiff_t diff, const smart_ptr& right) { return right + diff; } //!smart_ptr - smart_ptr template inline std::ptrdiff_t operator- (const smart_ptr &pt, const smart_ptr &pt2) { return pt.get()- pt2.get(); } //!swap specialization template inline void swap (smart_ptr &pt, smart_ptr &pt2) { typename smart_ptr::value_type *ptr = pt.get(); pt = pt2; pt2 = ptr; } //!detail::get_pointer() enables boost::mem_fn to recognize smart_ptr. //!Never throws. template inline T* get_pointer(const smart_ptr & p) { return p.get(); } //!Simulation of static_cast between pointers. Never throws. template inline smart_ptr static_pointer_cast(smart_ptr const & r) { return smart_ptr(r, detail::static_cast_tag()); } //!Simulation of const_cast between pointers. Never throws. template inline smart_ptrconst_pointer_cast(smart_ptr const & r) { return smart_ptr(r, detail::const_cast_tag()); } //!Simulation of dynamic_cast between pointers. Never throws. template inline smart_ptr dynamic_pointer_cast(smart_ptr const & r) { return smart_ptr (r, detail::dynamic_cast_tag()); } //!Simulation of reinterpret_cast between pointers. Never throws. template inline smart_ptr reinterpret_pointer_cast(smart_ptr const & r) { return smart_ptr(r, detail::reinterpret_cast_tag()); } } //namespace intrusive { } //namespace boost { namespace boost{ //This is to support embedding a bit in the pointer //for intrusive containers, saving space namespace intrusive { template struct has_pointer_plus_bit, N> { static const bool value = has_pointer_plus_bit::value; }; //Specialization template struct pointer_plus_bit > { typedef smart_ptr pointer; static pointer get_pointer(const pointer &n) { return pointer_plus_bit::get_pointer(n.get()); } static void set_pointer(pointer &n, pointer p) { T *raw_n = n.get(); pointer_plus_bit::set_pointer(raw_n, p.get()); n = raw_n; } static bool get_bit(const pointer &n) { return pointer_plus_bit::get_bit(n.get()); } static void set_bit(pointer &n, bool c) { T *raw_n = n.get(); pointer_plus_bit::set_bit(raw_n, c); n = raw_n; } }; template struct has_pointer_plus_2_bits, N> { static const bool value = has_pointer_plus_2_bits::value; }; template struct pointer_plus_2_bits > { typedef smart_ptr pointer; static pointer get_pointer(const pointer &n) { return pointer_plus_2_bits::get_pointer(n.get()); } static void set_pointer(pointer &n, pointer p) { T *raw_n = n.get(); pointer_plus_2_bits::set_pointer(raw_n, p.get()); n = raw_n; } static std::size_t get_bits(const pointer &n) { return pointer_plus_2_bits::get_bits(n.get()); } static void set_bits(pointer &n, std::size_t c) { T *raw_n = n.get(); pointer_plus_2_bits::set_bits(raw_n, c); n = raw_n; } }; } //namespace intrusive } //namespace boost{ #endif //#ifndef BOOST_INTRUSIVE_SMART_PTR_HPP