foreign_ptr.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // helper code for dealing with tracking non-boost shared_ptr/weak_ptr
  2. // Copyright Frank Mori Hess 2009.
  3. // Distributed under the Boost Software License, Version
  4. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/signals2 for library home page.
  7. #ifndef BOOST_SIGNALS2_FOREIGN_PTR_HPP
  8. #define BOOST_SIGNALS2_FOREIGN_PTR_HPP
  9. #include <algorithm>
  10. #include <boost/config.hpp>
  11. #include <boost/assert.hpp>
  12. #include <boost/scoped_ptr.hpp>
  13. #include <boost/smart_ptr/bad_weak_ptr.hpp>
  14. #include <boost/utility/swap.hpp>
  15. #ifndef BOOST_NO_CXX11_SMART_PTR
  16. #include <memory>
  17. #endif
  18. namespace boost
  19. {
  20. template<typename T> class shared_ptr;
  21. template<typename T> class weak_ptr;
  22. namespace signals2
  23. {
  24. template<typename WeakPtr> struct weak_ptr_traits
  25. {};
  26. template<typename T> struct weak_ptr_traits<boost::weak_ptr<T> >
  27. {
  28. typedef boost::shared_ptr<T> shared_type;
  29. };
  30. #ifndef BOOST_NO_CXX11_SMART_PTR
  31. template<typename T> struct weak_ptr_traits<std::weak_ptr<T> >
  32. {
  33. typedef std::shared_ptr<T> shared_type;
  34. };
  35. #endif
  36. template<typename SharedPtr> struct shared_ptr_traits
  37. {};
  38. template<typename T> struct shared_ptr_traits<boost::shared_ptr<T> >
  39. {
  40. typedef boost::weak_ptr<T> weak_type;
  41. };
  42. #ifndef BOOST_NO_CXX11_SMART_PTR
  43. template<typename T> struct shared_ptr_traits<std::shared_ptr<T> >
  44. {
  45. typedef std::weak_ptr<T> weak_type;
  46. };
  47. #endif
  48. namespace detail
  49. {
  50. struct foreign_shared_ptr_impl_base
  51. {
  52. virtual ~foreign_shared_ptr_impl_base() {}
  53. virtual foreign_shared_ptr_impl_base * clone() const = 0;
  54. };
  55. template<typename FSP>
  56. class foreign_shared_ptr_impl: public foreign_shared_ptr_impl_base
  57. {
  58. public:
  59. foreign_shared_ptr_impl(const FSP &p): _p(p)
  60. {}
  61. virtual foreign_shared_ptr_impl * clone() const
  62. {
  63. return new foreign_shared_ptr_impl(*this);
  64. }
  65. private:
  66. FSP _p;
  67. };
  68. class foreign_void_shared_ptr
  69. {
  70. public:
  71. foreign_void_shared_ptr():
  72. _p(0)
  73. {}
  74. foreign_void_shared_ptr(const foreign_void_shared_ptr &other):
  75. _p(other._p->clone())
  76. {}
  77. template<typename FSP>
  78. explicit foreign_void_shared_ptr(const FSP &fsp):
  79. _p(new foreign_shared_ptr_impl<FSP>(fsp))
  80. {}
  81. ~foreign_void_shared_ptr()
  82. {
  83. delete _p;
  84. }
  85. foreign_void_shared_ptr & operator=(const foreign_void_shared_ptr &other)
  86. {
  87. if(&other == this) return *this;
  88. foreign_void_shared_ptr(other).swap(*this);
  89. return *this;
  90. }
  91. void swap(foreign_void_shared_ptr &other)
  92. {
  93. boost::swap(_p, other._p);
  94. }
  95. private:
  96. foreign_shared_ptr_impl_base *_p;
  97. };
  98. struct foreign_weak_ptr_impl_base
  99. {
  100. virtual ~foreign_weak_ptr_impl_base() {}
  101. virtual foreign_void_shared_ptr lock() const = 0;
  102. virtual bool expired() const = 0;
  103. virtual foreign_weak_ptr_impl_base * clone() const = 0;
  104. };
  105. template<typename FWP>
  106. class foreign_weak_ptr_impl: public foreign_weak_ptr_impl_base
  107. {
  108. public:
  109. foreign_weak_ptr_impl(const FWP &p): _p(p)
  110. {}
  111. virtual foreign_void_shared_ptr lock() const
  112. {
  113. return foreign_void_shared_ptr(_p.lock());
  114. }
  115. virtual bool expired() const
  116. {
  117. return _p.expired();
  118. }
  119. virtual foreign_weak_ptr_impl * clone() const
  120. {
  121. return new foreign_weak_ptr_impl(*this);
  122. }
  123. private:
  124. FWP _p;
  125. };
  126. class foreign_void_weak_ptr
  127. {
  128. public:
  129. foreign_void_weak_ptr()
  130. {}
  131. foreign_void_weak_ptr(const foreign_void_weak_ptr &other):
  132. _p(other._p->clone())
  133. {}
  134. template<typename FWP>
  135. explicit foreign_void_weak_ptr(const FWP &fwp):
  136. _p(new foreign_weak_ptr_impl<FWP>(fwp))
  137. {}
  138. foreign_void_weak_ptr & operator=(const foreign_void_weak_ptr &other)
  139. {
  140. if(&other == this) return *this;
  141. foreign_void_weak_ptr(other).swap(*this);
  142. return *this;
  143. }
  144. void swap(foreign_void_weak_ptr &other)
  145. {
  146. boost::swap(_p, other._p);
  147. }
  148. foreign_void_shared_ptr lock() const
  149. {
  150. return _p->lock();
  151. }
  152. bool expired() const
  153. {
  154. return _p->expired();
  155. }
  156. private:
  157. boost::scoped_ptr<foreign_weak_ptr_impl_base> _p;
  158. };
  159. } // namespace detail
  160. } // namespace signals2
  161. } // namespace boost
  162. #endif // BOOST_SIGNALS2_FOREIGN_PTR_HPP