Irrlicht 3D Engine
IReferenceCounted.h
Go to the documentation of this file.
00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __I_IREFERENCE_COUNTED_H_INCLUDED__
00006 #define __I_IREFERENCE_COUNTED_H_INCLUDED__
00007 
00008 #include "irrTypes.h"
00009 
00010 namespace irr
00011 {
00012 
00014 
00041     class IReferenceCounted
00042     {
00043     public:
00044 
00046         IReferenceCounted()
00047             : DebugName(0), ReferenceCounter(1)
00048         {
00049         }
00050 
00052         virtual ~IReferenceCounted()
00053         {
00054         }
00055 
00057 
00086         void grab() const { ++ReferenceCounter; }
00087 
00089 
00116         bool drop() const
00117         {
00118             // someone is doing bad reference counting.
00119             _IRR_DEBUG_BREAK_IF(ReferenceCounter <= 0)
00120 
00121             --ReferenceCounter;
00122             if (!ReferenceCounter)
00123             {
00124                 delete this;
00125                 return true;
00126             }
00127 
00128             return false;
00129         }
00130 
00132 
00133         s32 getReferenceCount() const
00134         {
00135             return ReferenceCounter;
00136         }
00137 
00139 
00142         const c8* getDebugName() const
00143         {
00144             return DebugName;
00145         }
00146 
00147     protected:
00148 
00150 
00153         void setDebugName(const c8* newName)
00154         {
00155             DebugName = newName;
00156         }
00157 
00158     private:
00159 
00161         const c8* DebugName;
00162 
00164         mutable s32 ReferenceCounter;
00165     };
00166 
00167 } // end namespace irr
00168 
00169 #endif
00170