Lunchbox  1.4.0
referenced.h
00001 
00002 /* Copyright (c) 2005-2012, 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 LUNCHBOX_REFERENCED_H
00019 #define LUNCHBOX_REFERENCED_H
00020 
00021 #include <lunchbox/api.h>      // for LUNCHBOX_API
00022 #include <lunchbox/atomic.h>   // member
00023 #include <lunchbox/debug.h>    // for LBERROR
00024 
00025 //#define LUNCHBOX_REFERENCED_DEBUG
00026 #ifdef LUNCHBOX_REFERENCED_DEBUG
00027 #  include <lunchbox/hash.h>
00028 #  include <lunchbox/lock.h>
00029 #  include <lunchbox/lockable.h>
00030 #  include <lunchbox/scopedMutex.h>
00031 #endif
00032 
00033 namespace lunchbox
00034 {
00044     class Referenced 
00045     {
00046     public:
00048         void ref( const void* holder = 0 ) const
00049         {
00050 #ifndef NDEBUG
00051             LBASSERT( !_hasBeenDeleted );
00052 #endif
00053             ++_refCount;
00054 
00055 #ifdef LUNCHBOX_REFERENCED_DEBUG
00056             if( holder )
00057             {
00058                 std::stringstream cs;
00059                 cs << backtrace;
00060                 ScopedMutex<> referencedMutex( _holders );
00061                 HolderHash::iterator i = _holders->find( holder );
00062                 LBASSERTINFO( i == _holders->end(), i->second );
00063                 _holders.data[ holder ] = cs.str();
00064             }
00065 #endif
00066         }
00067 
00074         void unref( const void* holder = 0 ) const
00075             { 
00076 #ifndef NDEBUG
00077                 LBASSERT( !_hasBeenDeleted );
00078 #endif
00079                 LBASSERT( _refCount > 0 ); 
00080                 const bool deleteMe = (--_refCount==0);
00081                 if( deleteMe )
00082                     deleteReferenced( this );
00083 #ifdef LUNCHBOX_REFERENCED_DEBUG
00084                 else if( holder )
00085                 {
00086                     ScopedMutex<> referencedMutex( _holders );
00087                     HolderHash::iterator i = _holders->find( holder );
00088                     LBASSERT( i != _holders->end( ));
00089                     _holders->erase( i );
00090                 }
00091 #endif
00092             }
00093 
00095         int  getRefCount() const { return _refCount; }
00096 
00098         void printHolders( std::ostream& os ) const
00099             {
00100 #ifdef LUNCHBOX_REFERENCED_DEBUG
00101                 os << disableFlush << disableHeader << std::endl;
00102                 ScopedMutex<> referencedMutex( _holders );
00103                 for( HolderHash::const_iterator i = _holders->begin();
00104                      i != _holders->end(); ++i )
00105                 {
00106                     os << "Holder " << i->first << ": " << i->second 
00107                        << std::endl;
00108                 }
00109                 os << enableHeader << enableFlush;
00110 #endif
00111             }
00112 
00113     protected:
00115         Referenced()
00116                 : _refCount( 0 )
00117                 , _hasBeenDeleted( false )
00118             {}
00119 
00121         Referenced( const Referenced& ) 
00122                 : _refCount( 0 )
00123                 , _hasBeenDeleted( false )
00124             {}
00125 
00127         virtual ~Referenced() 
00128             {
00129 #ifndef NDEBUG
00130                 LBASSERT( !_hasBeenDeleted );
00131                 _hasBeenDeleted = true;
00132 #endif
00133                 LBASSERTINFO( _refCount == 0,
00134                               "Deleting object with ref count " << _refCount );
00135             }
00136 
00138         Referenced& operator = ( const Referenced& rhs ) { return *this; }
00139 
00140         LUNCHBOX_API void deleteReferenced( const Referenced* object ) const;
00141 
00142     private:
00143         mutable a_int32_t _refCount;
00144         bool _hasBeenDeleted;
00145 
00146 #ifdef LUNCHBOX_REFERENCED_DEBUG
00147         typedef PtrHash< const void*, std::string > HolderHash;
00148         mutable Lockable< HolderHash, Lock > _holders;
00149 #endif
00150     };
00151 }
00152 
00153 namespace boost
00154 {
00156     inline void intrusive_ptr_add_ref( lunchbox::Referenced* referenced )
00157     {
00158         referenced->ref();
00159     }
00160 
00162     inline void intrusive_ptr_release( lunchbox::Referenced* referenced )
00163     {
00164         referenced->unref();
00165     }
00166 }
00167 
00168 #endif //LUNCHBOX_REFERENCED_H