Equalizer  1.2.1
referenced.h
00001 
00002 /* Copyright (c) 2005-2011, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef COBASE_REFERENCED_H
00019 #define COBASE_REFERENCED_H
00020 
00021 #include <co/base/api.h>      // for COBASE_API
00022 #include <co/base/atomic.h>   // member
00023 #include <co/base/debug.h>    // for EQERROR
00024 #include <co/base/refPtr.h>   // CO_REFERENCED_ARGS
00025 
00026 #ifdef CO_REFERENCED_DEBUG
00027 #  include <co/base/hash.h>
00028 #  include <co/base/lock.h>
00029 #  include <co/base/lockable.h>
00030 #  include <co/base/scopedMutex.h>
00031 #endif
00032 
00033 #include <typeinfo>
00034 
00035 namespace co
00036 {
00037 namespace base
00038 {
00048     class Referenced 
00049     {
00050     public:
00052         void ref( CO_REFERENCED_ARGS ) const
00053         {
00054 #ifndef NDEBUG
00055             EQASSERT( !_hasBeenDeleted );
00056 #endif
00057             ++_refCount;
00058 
00059 #ifdef CO_REFERENCED_DEBUG
00060             if( holder )
00061             {
00062                 std::stringstream cs;
00063                 cs << backtrace;
00064                 ScopedMutex<> referencedMutex( _holders );
00065                 HolderHash::iterator i = _holders->find( holder );
00066                 EQASSERTINFO( i == _holders->end(), i->second );
00067                 _holders.data[ holder ] = cs.str();
00068             }
00069 #endif
00070         }
00071 
00078         void unref( CO_REFERENCED_ARGS ) const
00079             { 
00080 #ifndef NDEBUG
00081                 EQASSERT( !_hasBeenDeleted );
00082 #endif
00083                 EQASSERT( _refCount > 0 ); 
00084                 const bool deleteMe = (--_refCount==0);
00085                 if( deleteMe )
00086                     deleteReferenced( this );
00087 #ifdef CO_REFERENCED_DEBUG
00088                 else if( holder )
00089                 {
00090                     ScopedMutex<> referencedMutex( _holders );
00091                     HolderHash::iterator i = _holders->find( holder );
00092                     EQASSERT( i != _holders->end( ));
00093                     _holders->erase( i );
00094                 }
00095 #endif
00096             }
00097 
00099         int  getRefCount() const { return _refCount; }
00100 
00102         void printHolders( std::ostream& os ) const
00103             {
00104 #ifdef CO_REFERENCED_DEBUG
00105                 os << disableFlush << disableHeader;
00106                 ScopedMutex<> referencedMutex( _holders );
00107                 for( HolderHash::const_iterator i = _holders->begin();
00108                      i != _holders->end(); ++i )
00109                 {
00110                     os << "Holder " << i->first << ": " << i->second 
00111                        << std::endl;
00112                 }
00113                 os << enableHeader << enableFlush;
00114 #endif
00115             }
00116 
00117     protected:
00119         Referenced()
00120                 : _refCount( 0 )
00121                 , _hasBeenDeleted( false )
00122             {}
00123 
00125         Referenced( const Referenced& ) 
00126                 : _refCount( 0 )
00127                 , _hasBeenDeleted( false )
00128             {}
00129 
00131         virtual ~Referenced() 
00132             {
00133 #ifndef NDEBUG
00134                 EQASSERT( !_hasBeenDeleted );
00135                 _hasBeenDeleted = true;
00136 #endif
00137                 EQASSERTINFO( _refCount == 0,
00138                               "Deleting object with ref count " << _refCount );
00139             }
00140 
00142         Referenced& operator = ( const Referenced& rhs ) { return *this; }
00143 
00144         COBASE_API void deleteReferenced( const Referenced* object ) const;
00145 
00146     private:
00147         mutable a_int32_t _refCount;
00148         bool _hasBeenDeleted;
00149 
00150 #ifdef CO_REFERENCED_DEBUG
00151         typedef PtrHash< const void*, std::string > HolderHash;
00152         mutable Lockable< HolderHash, Lock > _holders;
00153 #endif
00154     };
00155 }
00156 }
00157 
00158 namespace boost
00159 {
00160 #ifdef CO_REFERENCED_DEBUG
00161 #  define CO_BOOSTREF_ARGS 0
00162 #  define CO_BOOSTREF_PARAM 0
00163 #else
00164 #  define CO_BOOSTREF_ARGS
00165 #  define CO_BOOSTREF_PARAM
00166 #endif
00167 
00169     inline void intrusive_ptr_add_ref( co::base::Referenced* referenced )
00170     {
00171         referenced->ref( CO_BOOSTREF_PARAM );
00172     }
00173 
00175     inline void intrusive_ptr_release( co::base::Referenced* referenced )
00176     {
00177         referenced->unref( CO_BOOSTREF_PARAM );
00178     }
00179 }
00180 
00181 #endif //COBASE_REFERENCED_H
Generated on Fri Jun 8 2012 15:44:32 for Equalizer 1.2.1 by  doxygen 1.8.0