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.
 
 
 
 
 
 

164 lines
3.7 KiB

  1. #ifndef BOOSTER_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  2. #define BOOSTER_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  3. //
  4. // weak_ptr.hpp
  5. //
  6. // Copyright (c) 2001, 2002, 2003 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/weak_ptr.htm for documentation.
  13. //
  14. #include <booster/auto_ptr_inc.h> // boost.TR1 include order fix
  15. #include <booster/smart_ptr/shared_count.h>
  16. #include <booster/shared_ptr.h>
  17. #ifdef BOOSTER_MSVC // moved here to work around VC++ compiler crash
  18. # pragma warning(push)
  19. # pragma warning(disable:4284) // odd return type for operator->
  20. #endif
  21. namespace booster
  22. {
  23. template<class T> class weak_ptr
  24. {
  25. private:
  26. // Borland 5.5.1 specific workarounds
  27. typedef weak_ptr<T> this_type;
  28. public:
  29. typedef T element_type;
  30. weak_ptr(): px(0), pn() // never throws in 1.30+
  31. {
  32. }
  33. // generated copy constructor, assignment, destructor are fine
  34. //
  35. // The "obvious" converting constructor implementation:
  36. //
  37. // template<class Y>
  38. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
  39. // {
  40. // }
  41. //
  42. // has a serious problem.
  43. //
  44. // r.px may already have been invalidated. The px(r.px)
  45. // conversion may require access to *r.px (virtual inheritance).
  46. //
  47. // It is not possible to avoid spurious access violations since
  48. // in multithreaded programs r.px may be invalidated at any point.
  49. //
  50. template<class Y>
  51. weak_ptr( weak_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
  52. : px(r.lock().get()), pn(r.pn) // never throws
  53. {
  54. }
  55. template<class Y>
  56. weak_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
  57. : px( r.px ), pn( r.pn ) // never throws
  58. {
  59. }
  60. template<class Y>
  61. weak_ptr & operator=(weak_ptr<Y> const & r) // never throws
  62. {
  63. px = r.lock().get();
  64. pn = r.pn;
  65. return *this;
  66. }
  67. template<class Y>
  68. weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
  69. {
  70. px = r.px;
  71. pn = r.pn;
  72. return *this;
  73. }
  74. shared_ptr<T> lock() const // never throws
  75. {
  76. return shared_ptr<element_type>( *this, booster::detail::sp_nothrow_tag() );
  77. }
  78. long use_count() const // never throws
  79. {
  80. return pn.use_count();
  81. }
  82. bool expired() const // never throws
  83. {
  84. return pn.use_count() == 0;
  85. }
  86. bool _empty() const // extension, not in std::weak_ptr
  87. {
  88. return pn.empty();
  89. }
  90. void reset() // never throws in 1.30+
  91. {
  92. this_type().swap(*this);
  93. }
  94. void swap(this_type & other) // never throws
  95. {
  96. std::swap(px, other.px);
  97. pn.swap(other.pn);
  98. }
  99. void _internal_assign(T * px2, booster::detail::shared_count const & pn2)
  100. {
  101. px = px2;
  102. pn = pn2;
  103. }
  104. template<class Y> bool _internal_less(weak_ptr<Y> const & rhs) const
  105. {
  106. return pn < rhs.pn;
  107. }
  108. // Tasteless as this may seem, making all members public allows member templates
  109. // to work in the absence of member template friends. (Matthew Langston)
  110. private:
  111. template<class Y> friend class weak_ptr;
  112. template<class Y> friend class shared_ptr;
  113. T * px; // contained pointer
  114. booster::detail::weak_count pn; // reference counter
  115. }; // weak_ptr
  116. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b)
  117. {
  118. return a._internal_less(b);
  119. }
  120. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b)
  121. {
  122. a.swap(b);
  123. }
  124. } // namespace boost
  125. #ifdef BOOSTER_MSVC
  126. # pragma warning(pop)
  127. #endif
  128. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED