File indexing completed on 2025-01-18 09:51:43
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 defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
0094
0095
0096
0097 long tmp2 = tmp + 1;
0098 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
0099
0100 #else
0101
0102 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
0103
0104 #endif
0105 }
0106 }
0107
0108 void release()
0109 {
0110 if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
0111 {
0112 dispose();
0113 weak_release();
0114 }
0115 }
0116
0117 void weak_add_ref()
0118 {
0119 BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
0120 }
0121
0122 void weak_release()
0123 {
0124 if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
0125 {
0126 destroy();
0127 }
0128 }
0129
0130 long use_count() const
0131 {
0132 return static_cast<long const volatile &>( use_count_ );
0133 }
0134 };
0135
0136 }
0137
0138 }
0139
0140 #endif