sp_counted_base_nt.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. namespace boost
  22. {
  23. namespace detail
  24. {
  25. class BOOST_SYMBOL_VISIBLE sp_counted_base
  26. {
  27. private:
  28. sp_counted_base( sp_counted_base const & );
  29. sp_counted_base & operator= ( sp_counted_base const & );
  30. boost::int_least32_t use_count_; // #shared
  31. boost::int_least32_t weak_count_; // #weak + (#shared != 0)
  32. public:
  33. sp_counted_base() BOOST_SP_NOEXCEPT: use_count_( 1 ), weak_count_( 1 )
  34. {
  35. }
  36. virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
  37. {
  38. }
  39. // dispose() is called when use_count_ drops to zero, to release
  40. // the resources managed by *this.
  41. virtual void dispose() BOOST_SP_NOEXCEPT = 0; // nothrow
  42. // destroy() is called when weak_count_ drops to zero.
  43. virtual void destroy() BOOST_SP_NOEXCEPT // nothrow
  44. {
  45. delete this;
  46. }
  47. virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  48. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  49. virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
  50. void add_ref_copy() BOOST_SP_NOEXCEPT
  51. {
  52. ++use_count_;
  53. }
  54. bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
  55. {
  56. if( use_count_ == 0 ) return false;
  57. ++use_count_;
  58. return true;
  59. }
  60. void release() BOOST_SP_NOEXCEPT
  61. {
  62. if( --use_count_ == 0 )
  63. {
  64. dispose();
  65. weak_release();
  66. }
  67. }
  68. void weak_add_ref() BOOST_SP_NOEXCEPT
  69. {
  70. ++weak_count_;
  71. }
  72. void weak_release() BOOST_SP_NOEXCEPT
  73. {
  74. if( --weak_count_ == 0 )
  75. {
  76. destroy();
  77. }
  78. }
  79. long use_count() const BOOST_SP_NOEXCEPT
  80. {
  81. return use_count_;
  82. }
  83. };
  84. } // namespace detail
  85. } // namespace boost
  86. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_NT_HPP_INCLUDED