File indexing completed on 2025-09-17 08:50:36
0001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
0003
0004
0005
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #include <boost/smart_ptr/detail/sp_interlocked.hpp>
0028 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
0029 #include <boost/config/workaround.hpp>
0030 #include <boost/config.hpp>
0031
0032 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0033
0034 #include <boost/config/pragma_message.hpp>
0035 BOOST_PRAGMA_MESSAGE("Using Win32 sp_counted_base")
0036
0037 #endif
0038
0039 namespace boost
0040 {
0041
0042 namespace detail
0043 {
0044
0045 class BOOST_SYMBOL_VISIBLE sp_counted_base
0046 {
0047 private:
0048
0049 sp_counted_base( sp_counted_base const & );
0050 sp_counted_base & operator= ( sp_counted_base const & );
0051
0052 long use_count_;
0053 long weak_count_;
0054
0055 public:
0056
0057 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0058 {
0059 }
0060
0061 virtual ~sp_counted_base()
0062 {
0063 }
0064
0065
0066
0067
0068 virtual void dispose() = 0;
0069
0070
0071
0072 virtual void destroy()
0073 {
0074 delete this;
0075 }
0076
0077 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
0078 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
0079 virtual void * get_untyped_deleter() = 0;
0080
0081 void add_ref_copy()
0082 {
0083 BOOST_SP_INTERLOCKED_INCREMENT( &use_count_ );
0084 }
0085
0086 bool add_ref_lock()
0087 {
0088 for( ;; )
0089 {
0090 long tmp = static_cast< long const volatile& >( use_count_ );
0091 if( tmp == 0 ) return false;
0092
0093 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
0094 }
0095 }
0096
0097 void release()
0098 {
0099 if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
0100 {
0101 dispose();
0102 weak_release();
0103 }
0104 }
0105
0106 void weak_add_ref()
0107 {
0108 BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
0109 }
0110
0111 void weak_release()
0112 {
0113 if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
0114 {
0115 destroy();
0116 }
0117 }
0118
0119 long use_count() const
0120 {
0121 return static_cast<long const volatile &>( use_count_ );
0122 }
0123 };
0124
0125 }
0126
0127 }
0128
0129 #endif