ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

212 lines
4.1 KiB

  1. #ifndef BOOSTER_INTRUSIVE_PTR_H_INCLUDED
  2. #define BOOSTER_INTRUSIVE_PTR_H_INCLUDED
  3. //
  4. // intrusive_ptr.hpp
  5. //
  6. // Copyright (c) 2001, 2002 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
  13. //
  14. #include <functional> // for std::less
  15. #include <iosfwd> // for std::basic_ostream
  16. namespace booster
  17. {
  18. ///
  19. /// \brief intrusive_ptr is the class taken as-is from boost.
  20. ///
  21. ///
  22. /// A smart pointer that uses intrusive reference counting.
  23. ///
  24. /// Relies on unqualified calls to
  25. ///
  26. /// void intrusive_ptr_add_ref(T * p);
  27. /// void intrusive_ptr_release(T * p);
  28. ///
  29. /// (p != 0)
  30. ///
  31. /// The object is responsible for destroying itself.
  32. ///
  33. /// See: http://www.boost.org/doc/libs/release/libs/smart_ptr
  34. ///
  35. template<class T> class intrusive_ptr
  36. {
  37. private:
  38. typedef intrusive_ptr this_type;
  39. public:
  40. typedef T element_type;
  41. intrusive_ptr(): p_(0)
  42. {
  43. }
  44. intrusive_ptr(T * p, bool add_ref = true): p_(p)
  45. {
  46. if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
  47. }
  48. T *release()
  49. {
  50. T *r = p_;
  51. p_ = 0;
  52. return r;
  53. }
  54. intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
  55. {
  56. if(p_ != 0) intrusive_ptr_add_ref(p_);
  57. }
  58. ~intrusive_ptr()
  59. {
  60. if(p_ != 0) intrusive_ptr_release(p_);
  61. }
  62. template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
  63. {
  64. if(p_ != 0) intrusive_ptr_add_ref(p_);
  65. }
  66. intrusive_ptr & operator=(intrusive_ptr const & rhs)
  67. {
  68. this_type(rhs).swap(*this);
  69. return *this;
  70. }
  71. intrusive_ptr & operator=(T * rhs)
  72. {
  73. this_type(rhs).swap(*this);
  74. return *this;
  75. }
  76. T * get() const
  77. {
  78. return p_;
  79. }
  80. T & operator*() const
  81. {
  82. return *p_;
  83. }
  84. T * operator->() const
  85. {
  86. return p_;
  87. }
  88. typedef T * this_type::*unspecified_bool_type;
  89. operator unspecified_bool_type () const
  90. {
  91. return p_ == 0? 0: &this_type::p_;
  92. }
  93. // operator! is a Borland-specific workaround
  94. bool operator! () const
  95. {
  96. return p_ == 0;
  97. }
  98. void swap(intrusive_ptr & rhs)
  99. {
  100. T * tmp = p_;
  101. p_ = rhs.p_;
  102. rhs.p_ = tmp;
  103. }
  104. private:
  105. T * p_;
  106. };
  107. template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
  108. {
  109. return a.get() == b.get();
  110. }
  111. template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
  112. {
  113. return a.get() != b.get();
  114. }
  115. template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
  116. {
  117. return a.get() == b;
  118. }
  119. template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
  120. {
  121. return a.get() != b;
  122. }
  123. template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
  124. {
  125. return a == b.get();
  126. }
  127. template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
  128. {
  129. return a != b.get();
  130. }
  131. template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
  132. {
  133. return std::less<T *>()(a.get(), b.get());
  134. }
  135. template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
  136. {
  137. lhs.swap(rhs);
  138. }
  139. // mem_fn support
  140. template<class T> T * get_pointer(intrusive_ptr<T> const & p)
  141. {
  142. return p.get();
  143. }
  144. template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
  145. {
  146. return static_cast<T *>(p.get());
  147. }
  148. template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
  149. {
  150. return const_cast<T *>(p.get());
  151. }
  152. template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
  153. {
  154. return dynamic_cast<T *>(p.get());
  155. }
  156. // operator<<
  157. template<class E, class T, class Y>
  158. std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
  159. {
  160. os << p.get();
  161. return os;
  162. }
  163. } // namespace booster
  164. #endif // #ifndef BOOSTER_INTRUSIVE_PTR_H_INCLUDED