shared_ptr_nmt_132.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
  2. #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
  3. //
  4. // detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates
  5. //
  6. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  7. // Copyright (c) 2001, 2002 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
  14. //
  15. #include <boost/assert.hpp>
  16. #include <boost/checked_delete.hpp>
  17. #include <boost/serialization/throw_exception.hpp>
  18. #include <boost/detail/atomic_count.hpp>
  19. #ifndef BOOST_NO_AUTO_PTR
  20. # include <memory> // for std::auto_ptr
  21. #endif
  22. #include <algorithm> // for std::swap
  23. #include <functional> // for std::less
  24. #include <new> // for std::bad_alloc
  25. namespace boost
  26. {
  27. template<class T> class shared_ptr
  28. {
  29. private:
  30. typedef detail::atomic_count count_type;
  31. public:
  32. typedef T element_type;
  33. typedef T value_type;
  34. explicit shared_ptr(T * p = 0): px(p)
  35. {
  36. #ifndef BOOST_NO_EXCEPTIONS
  37. try // prevent leak if new throws
  38. {
  39. pn = new count_type(1);
  40. }
  41. catch(...)
  42. {
  43. boost::checked_delete(p);
  44. throw;
  45. }
  46. #else
  47. pn = new count_type(1);
  48. if(pn == 0)
  49. {
  50. boost::checked_delete(p);
  51. boost::serialization::throw_exception(std::bad_alloc());
  52. }
  53. #endif
  54. }
  55. ~shared_ptr()
  56. {
  57. if(--*pn == 0)
  58. {
  59. boost::checked_delete(px);
  60. delete pn;
  61. }
  62. }
  63. shared_ptr(shared_ptr const & r): px(r.px) // never throws
  64. {
  65. pn = r.pn;
  66. ++*pn;
  67. }
  68. shared_ptr & operator=(shared_ptr const & r)
  69. {
  70. shared_ptr(r).swap(*this);
  71. return *this;
  72. }
  73. #ifndef BOOST_NO_AUTO_PTR
  74. explicit shared_ptr(std::auto_ptr< T > & r)
  75. {
  76. pn = new count_type(1); // may throw
  77. px = r.release(); // fix: moved here to stop leak if new throws
  78. }
  79. shared_ptr & operator=(std::auto_ptr< T > & r)
  80. {
  81. shared_ptr(r).swap(*this);
  82. return *this;
  83. }
  84. #endif
  85. void reset(T * p = 0)
  86. {
  87. BOOST_ASSERT(p == 0 || p != px);
  88. shared_ptr(p).swap(*this);
  89. }
  90. T & operator*() const // never throws
  91. {
  92. BOOST_ASSERT(px != 0);
  93. return *px;
  94. }
  95. T * operator->() const // never throws
  96. {
  97. BOOST_ASSERT(px != 0);
  98. return px;
  99. }
  100. T * get() const // never throws
  101. {
  102. return px;
  103. }
  104. long use_count() const // never throws
  105. {
  106. return *pn;
  107. }
  108. bool unique() const // never throws
  109. {
  110. return *pn == 1;
  111. }
  112. void swap(shared_ptr< T > & other) // never throws
  113. {
  114. std::swap(px, other.px);
  115. std::swap(pn, other.pn);
  116. }
  117. private:
  118. T * px; // contained pointer
  119. count_type * pn; // ptr to reference counter
  120. };
  121. template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
  122. {
  123. return a.get() == b.get();
  124. }
  125. template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
  126. {
  127. return a.get() != b.get();
  128. }
  129. template<class T> inline bool operator<(shared_ptr< T > const & a, shared_ptr< T > const & b)
  130. {
  131. return std::less<T*>()(a.get(), b.get());
  132. }
  133. template<class T> void swap(shared_ptr< T > & a, shared_ptr< T > & b)
  134. {
  135. a.swap(b);
  136. }
  137. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  138. template<class T> inline T * get_pointer(shared_ptr< T > const & p)
  139. {
  140. return p.get();
  141. }
  142. } // namespace boost
  143. #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED