sp_counted_base_nt.hpp 2.0 KB

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