sp_counted_base_pt.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  detail/sp_counted_base_pt.hpp
00012 //
00013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
00014 //  Copyright 2004-2005 Peter Dimov
00015 //
00016 // Distributed under the Boost Software License, Version 1.0. (See
00017 // accompanying file LICENSE_1_0.txt or copy at
00018 // http://www.boost.org/LICENSE_1_0.txt)
00019 //
00020 
00021 #include <boost/detail/sp_typeinfo.hpp>
00022 #include <pthread.h>
00023 
00024 namespace boost
00025 {
00026 
00027 namespace detail
00028 {
00029 
00030 class sp_counted_base
00031 {
00032 private:
00033 
00034     sp_counted_base( sp_counted_base const & );
00035     sp_counted_base & operator= ( sp_counted_base const & );
00036 
00037     long use_count_;        // #shared
00038     long weak_count_;       // #weak + (#shared != 0)
00039 
00040     mutable pthread_mutex_t m_;
00041 
00042 public:
00043 
00044     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
00045     {
00046 // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
00047 
00048 #if defined(__hpux) && defined(_DECTHREADS_)
00049         pthread_mutex_init( &m_, pthread_mutexattr_default );
00050 #else
00051         pthread_mutex_init( &m_, 0 );
00052 #endif
00053     }
00054 
00055     virtual ~sp_counted_base() // nothrow
00056     {
00057         pthread_mutex_destroy( &m_ );
00058     }
00059 
00060     // dispose() is called when use_count_ drops to zero, to release
00061     // the resources managed by *this.
00062 
00063     virtual void dispose() = 0; // nothrow
00064 
00065     // destroy() is called when weak_count_ drops to zero.
00066 
00067     virtual void destroy() // nothrow
00068     {
00069         delete this;
00070     }
00071 
00072     virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
00073 
00074     void add_ref_copy()
00075     {
00076         pthread_mutex_lock( &m_ );
00077         ++use_count_;
00078         pthread_mutex_unlock( &m_ );
00079     }
00080 
00081     bool add_ref_lock() // true on success
00082     {
00083         pthread_mutex_lock( &m_ );
00084         bool r = use_count_ == 0? false: ( ++use_count_, true );
00085         pthread_mutex_unlock( &m_ );
00086         return r;
00087     }
00088 
00089     void release() // nothrow
00090     {
00091         pthread_mutex_lock( &m_ );
00092         long new_use_count = --use_count_;
00093         pthread_mutex_unlock( &m_ );
00094 
00095         if( new_use_count == 0 )
00096         {
00097             dispose();
00098             weak_release();
00099         }
00100     }
00101 
00102     void weak_add_ref() // nothrow
00103     {
00104         pthread_mutex_lock( &m_ );
00105         ++weak_count_;
00106         pthread_mutex_unlock( &m_ );
00107     }
00108 
00109     void weak_release() // nothrow
00110     {
00111         pthread_mutex_lock( &m_ );
00112         long new_weak_count = --weak_count_;
00113         pthread_mutex_unlock( &m_ );
00114 
00115         if( new_weak_count == 0 )
00116         {
00117             destroy();
00118         }
00119     }
00120 
00121     long use_count() const // nothrow
00122     {
00123         pthread_mutex_lock( &m_ );
00124         long r = use_count_;
00125         pthread_mutex_unlock( &m_ );
00126 
00127         return r;
00128     }
00129 };
00130 
00131 } // namespace detail
00132 
00133 } // namespace boost
00134 
00135 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29