shared_array.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
  3. //
  4. // shared_array.hpp
  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_array.htm for documentation.
  14. //
  15. #include <boost/config.hpp> // for broken compiler workarounds
  16. #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  17. #include <boost/smart_ptr/detail/shared_array_nmt.hpp>
  18. #else
  19. #include <memory> // TR1 cyclic inclusion fix
  20. #include <boost/assert.hpp>
  21. #include <boost/checked_delete.hpp>
  22. #include <boost/smart_ptr/detail/shared_count.hpp>
  23. #include <boost/detail/workaround.hpp>
  24. #include <cstddef> // for std::ptrdiff_t
  25. #include <algorithm> // for std::swap
  26. #include <functional> // for std::less
  27. namespace boost
  28. {
  29. //
  30. // shared_array
  31. //
  32. // shared_array extends shared_ptr to arrays.
  33. // The array pointed to is deleted when the last shared_array pointing to it
  34. // is destroyed or reset.
  35. //
  36. template<class T> class shared_array
  37. {
  38. private:
  39. // Borland 5.5.1 specific workarounds
  40. typedef checked_array_deleter<T> deleter;
  41. typedef shared_array<T> this_type;
  42. public:
  43. typedef T element_type;
  44. explicit shared_array(T * p = 0): px(p), pn(p, deleter())
  45. {
  46. }
  47. //
  48. // Requirements: D's copy constructor must not throw
  49. //
  50. // shared_array will release p by calling d(p)
  51. //
  52. template<class D> shared_array(T * p, D d): px(p), pn(p, d)
  53. {
  54. }
  55. // generated copy constructor, assignment, destructor are fine
  56. void reset(T * p = 0)
  57. {
  58. BOOST_ASSERT(p == 0 || p != px);
  59. this_type(p).swap(*this);
  60. }
  61. template <class D> void reset(T * p, D d)
  62. {
  63. this_type(p, d).swap(*this);
  64. }
  65. T & operator[] (std::ptrdiff_t i) const // never throws
  66. {
  67. BOOST_ASSERT(px != 0);
  68. BOOST_ASSERT(i >= 0);
  69. return px[i];
  70. }
  71. T * get() const // never throws
  72. {
  73. return px;
  74. }
  75. // implicit conversion to "bool"
  76. #include <boost/smart_ptr/detail/operator_bool.hpp>
  77. bool unique() const // never throws
  78. {
  79. return pn.unique();
  80. }
  81. long use_count() const // never throws
  82. {
  83. return pn.use_count();
  84. }
  85. void swap(shared_array<T> & other) // never throws
  86. {
  87. std::swap(px, other.px);
  88. pn.swap(other.pn);
  89. }
  90. private:
  91. T * px; // contained pointer
  92. detail::shared_count pn; // reference counter
  93. }; // shared_array
  94. template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
  95. {
  96. return a.get() == b.get();
  97. }
  98. template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
  99. {
  100. return a.get() != b.get();
  101. }
  102. template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
  103. {
  104. return std::less<T*>()(a.get(), b.get());
  105. }
  106. template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
  107. {
  108. a.swap(b);
  109. }
  110. } // namespace boost
  111. #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  112. #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED