00001 #ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
00002 #define BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
00003
00004
00005
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <sysc/packages/boost/detail/interlocked.hpp>
00028 #include <typeinfo>
00029
00030 namespace boost
00031 {
00032
00033 namespace detail
00034 {
00035
00036 class sp_counted_base
00037 {
00038 private:
00039
00040 sp_counted_base( sp_counted_base const & );
00041 sp_counted_base & operator= ( sp_counted_base const & );
00042
00043 long use_count_;
00044 long weak_count_;
00045
00046 public:
00047
00048 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
00049 {
00050 }
00051
00052 virtual ~sp_counted_base()
00053 {
00054 }
00055
00056
00057
00058
00059 virtual void dispose() = 0;
00060
00061
00062
00063 virtual void destroy()
00064 {
00065 delete this;
00066 }
00067
00068 virtual void * get_deleter( std::type_info const & ti ) = 0;
00069
00070 void add_ref_copy()
00071 {
00072 BOOST_INTERLOCKED_INCREMENT( &use_count_ );
00073 }
00074
00075 bool add_ref_lock()
00076 {
00077 for( ;; )
00078 {
00079 long tmp = static_cast< long const volatile& >( use_count_ );
00080 if( tmp == 0 ) return false;
00081 if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
00082 }
00083 }
00084
00085 void release()
00086 {
00087 if( BOOST_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
00088 {
00089 dispose();
00090 weak_release();
00091 }
00092 }
00093
00094 void weak_add_ref()
00095 {
00096 BOOST_INTERLOCKED_INCREMENT( &weak_count_ );
00097 }
00098
00099 void weak_release()
00100 {
00101 if( BOOST_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
00102 {
00103 destroy();
00104 }
00105 }
00106
00107 long use_count() const
00108 {
00109 return static_cast<long const volatile &>( use_count_ );
00110 }
00111 };
00112
00113 }
00114
00115 }
00116
00117 #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED