sp_counted_base_nt.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/sp_counted_base_nt.hpp
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  18. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/cstdint.hpp>
  21. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  22. #include <boost/config/pragma_message.hpp>
  23. BOOST_PRAGMA_MESSAGE("Using single-threaded, non-atomic sp_counted_base")
  24. #endif
  25. namespace boost
  26. {
  27. namespace detail
  28. {
  29. class BOOST_SYMBOL_VISIBLE sp_counted_base
  30. {
  31. private:
  32. sp_counted_base( sp_counted_base const & );
  33. sp_counted_base & operator= ( sp_counted_base const & );
  34. boost::int_least32_t use_count_; // #shared
  35. boost::int_least32_t weak_count_; // #weak + (#shared != 0)
  36. public:
  37. sp_counted_base() BOOST_SP_NOEXCEPT: use_count_( 1 ), weak_count_( 1 )
  38. {
  39. }
  40. virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
  41. {
  42. }
  43. // dispose() is called when use_count_ drops to zero, to release
  44. // the resources managed by *this.
  45. virtual void dispose() BOOST_SP_NOEXCEPT = 0; // nothrow
  46. // destroy() is called when weak_count_ drops to zero.
  47. virtual void destroy() BOOST_SP_NOEXCEPT // nothrow
  48. {
  49. delete this;
  50. }
  51. virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  52. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  53. virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
  54. void add_ref_copy() BOOST_SP_NOEXCEPT
  55. {
  56. ++use_count_;
  57. }
  58. bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
  59. {
  60. if( use_count_ == 0 ) return false;
  61. ++use_count_;
  62. return true;
  63. }
  64. void release() BOOST_SP_NOEXCEPT
  65. {
  66. if( --use_count_ == 0 )
  67. {
  68. dispose();
  69. weak_release();
  70. }
  71. }
  72. void weak_add_ref() BOOST_SP_NOEXCEPT
  73. {
  74. ++weak_count_;
  75. }
  76. void weak_release() BOOST_SP_NOEXCEPT
  77. {
  78. if( --weak_count_ == 0 )
  79. {
  80. destroy();
  81. }
  82. }
  83. long use_count() const BOOST_SP_NOEXCEPT
  84. {
  85. return use_count_;
  86. }
  87. };
  88. } // namespace detail
  89. } // namespace boost
  90. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED