referenced.h

00001 
00002 /* Copyright (c) 2005-2009, 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 EQBASE_REFERENCED_H
00019 #define EQBASE_REFERENCED_H
00020 
00021 #include <eq/base/base.h>     // for EQ_EXPORT
00022 #include <eq/base/debug.h>    // for EQERROR
00023 #include <eq/base/atomic.h>   // member
00024 #include <typeinfo>
00025 
00026 namespace eq
00027 {
00028 namespace base
00029 {
00039     class Referenced 
00040     {
00041     public:
00043         void ref()   
00044         {
00045 #ifndef NDEBUG
00046             EQASSERTINFO( !_hasBeenDeleted, typeid( *this ).name( ));
00047 #endif
00048             ++_refCount;
00049         }
00050 
00055         void unref() 
00056             { 
00057 #ifndef NDEBUG
00058                 EQASSERT( !_hasBeenDeleted );
00059 #endif
00060                 EQASSERT( _refCount > 0 ); 
00061                 const bool deleteMe = (--_refCount==0);
00062                 if( deleteMe )
00063                     deleteReferenced( this );
00064             }
00065 
00067         int  getRefCount() const { return _refCount; }
00068 
00069     protected:
00071         Referenced()
00072             : _refCount(0)
00073 #ifndef NDEBUG
00074             , _hasBeenDeleted( false )
00075 #endif
00076             {}
00077 
00079         Referenced( const Referenced& ) 
00080             : _refCount(0)
00081 #ifndef NDEBUG
00082             , _hasBeenDeleted( false )
00083 #endif
00084             {}
00085 
00087         virtual ~Referenced() 
00088             {
00089 #ifndef NDEBUG
00090                 EQASSERT( !_hasBeenDeleted );
00091                 _hasBeenDeleted = true;
00092 #endif
00093                 EQASSERTINFO( _refCount == 0,
00094                               "Deleting object with ref count " << _refCount );
00095             }
00096 
00097     protected:
00098         EQ_EXPORT void deleteReferenced( Referenced* object );
00099 
00100     private:
00101         mtLong _refCount;
00102         bool _hasBeenDeleted;
00103     };
00104 }
00105 
00106 }
00107 #endif //EQBASE_REFERENCED_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8