// file : Utility/ReferenceCounting/DefaultImpl.ipp // author : Boris Kolpackov // copyright : Copyright (c) 2002-2003 Boris Kolpackov // license : http://kolpackov.net/license.html namespace Utility { namespace ReferenceCounting { // c-tor & d-tor template DefaultImpl:: DefaultImpl () throw (Interface::SystemException) : ref_count_ (1), lock_ () { } template DefaultImpl:: ~DefaultImpl () throw () { } // add_ref, remove_ref and refcount_value member functions template void DefaultImpl:: add_ref () const throw (Exception, SystemException) { WriteGuard_ guard (lock_); add_ref_i (); } template void DefaultImpl:: remove_ref () const throw () { bool destroy (false); try { WriteGuard_ guard (lock_); destroy = remove_ref_i (); } catch (...) { // there is nothing we can do } if (destroy) delete this; } template Interface::count_t DefaultImpl:: refcount_value () const throw (Exception, SystemException) { ReadGuard_ guard (lock_); return refcount_value_i (); } // add_ref_i, remove_ref_i and refcount_value_i member functions template void DefaultImpl:: add_ref_i () const throw (Exception, SystemException) { ref_count_++; } template bool DefaultImpl:: remove_ref_i () const throw (Exception, SystemException) { bool destroy (false); if (ref_count_ > 0) { if (--ref_count_ == 0) destroy = true; } else { throw InconsistentState ( "Utility::ReferenceCounting::DefaultImpl::_remove_ref_i: " "reference counter is zero."); } return destroy; } template Interface::count_t DefaultImpl:: refcount_value_i () const throw (Exception, SystemException) { return ref_count_; } template typename SynchPolicy::Mutex& DefaultImpl:: lock_i() const throw () { return lock_; } } } //$Id$