Equalizer 1.0

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>   // EQ_REFERENCED_ARGS
00025 
00026 #ifdef EQ_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( EQ_REFERENCED_ARGS ) const
00053         {
00054 #ifndef NDEBUG
00055             EQASSERTINFO( !_hasBeenDeleted, className( this ));
00056 #endif
00057             ++_refCount;
00058 
00059 #ifdef EQ_REFERENCED_DEBUG
00060             std::stringstream cs;
00061             cs << backtrace;
00062             ScopedMutex<> referencedMutex( _holders );
00063             HolderHash::iterator i = _holders->find( holder );
00064             EQASSERTINFO( i == _holders->end(), i->second );
00065             _holders.data[ holder ] = cs.str();
00066 #endif
00067         }
00068 
00075         void unref( EQ_REFERENCED_ARGS ) const
00076             { 
00077 #ifndef NDEBUG
00078                 EQASSERT( !_hasBeenDeleted );
00079 #endif
00080                 EQASSERT( _refCount > 0 ); 
00081                 const bool deleteMe = (--_refCount==0);
00082                 if( deleteMe )
00083                     deleteReferenced( this );
00084 #ifdef EQ_REFERENCED_DEBUG
00085                 else
00086                 {
00087                     ScopedMutex<> referencedMutex( _holders );
00088                     HolderHash::iterator i = _holders->find( holder );
00089                     EQASSERT( i != _holders->end( ));
00090                     _holders->erase( i );
00091                 }
00092 #endif
00093             }
00094 
00096         int  getRefCount() const { return _refCount; }
00097 
00099         void printHolders( std::ostream& os ) const
00100             {
00101 #ifdef EQ_REFERENCED_DEBUG
00102                 os << disableFlush << disableHeader;
00103                 ScopedMutex<> referencedMutex( _holders );
00104                 for( HolderHash::const_iterator i = _holders->begin();
00105                      i != _holders->end(); ++i )
00106                 {
00107                     os << "Holder " << i->first << ": " << i->second 
00108                        << std::endl;
00109                 }
00110                 os << enableHeader << enableFlush;
00111 #endif
00112             }
00113 
00114     protected:
00116         Referenced()
00117                 : _refCount( 0 )
00118                 , _hasBeenDeleted( false )
00119             {}
00120 
00122         Referenced( const Referenced& ) 
00123                 : _refCount( 0 )
00124                 , _hasBeenDeleted( false )
00125             {}
00126 
00128         virtual ~Referenced() 
00129             {
00130 #ifndef NDEBUG
00131                 EQASSERT( !_hasBeenDeleted );
00132                 _hasBeenDeleted = true;
00133 #endif
00134                 EQASSERTINFO( _refCount == 0,
00135                               "Deleting object with ref count " << _refCount );
00136             }
00137 
00138         COBASE_API void deleteReferenced( const Referenced* object ) const;
00139 
00140     private:
00141         mutable a_int32_t _refCount;
00142         bool _hasBeenDeleted;
00143 
00144 #ifdef EQ_REFERENCED_DEBUG
00145         typedef PtrHash< const void*, std::string > HolderHash;
00146         mutable Lockable< HolderHash, Lock > _holders;
00147 #endif
00148     };
00149 }
00150 
00151 }
00152 #endif //COBASE_REFERENCED_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3