CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

csutil/refarr.h

00001 /*
00002   Crystal Space Smart Pointers
00003   Copyright (C) 2002 by Jorrit Tyberghein and Matthias Braun
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Library General Public License for more details.
00014 
00015   You should have received a copy of the GNU Library General Public
00016   License along with this library; if not, write to the Free
00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_REFARR_H__
00021 #define __CS_REFARR_H__
00022 
00023 //-----------------------------------------------------------------------------
00024 // Note *1*: The explicit "this->" is needed by modern compilers (such as gcc
00025 // 3.4.x) which distinguish between dependent and non-dependent names in
00026 // templates.  See: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
00027 //-----------------------------------------------------------------------------
00028 
00029 #include "ref.h"
00030 #include "array.h"
00031 
00032 #ifdef CS_REF_TRACKER
00033  #include <typeinfo>
00034  #include "csutil/reftrackeraccess.h"
00035 
00036  #define CSREFARR_TRACK(x, cmd, refCount, obj, tag) \
00037   {                                                 \
00038     const int rc = obj ? refCount : -1;             \
00039     if (obj) cmd;                                   \
00040     if (obj)                                        \
00041     {                                               \
00042       csRefTrackerAccess::SetDescription (obj,      \
00043         typeid(T).name());                          \
00044       csRefTrackerAccess::Match ## x (obj, rc, tag);\
00045     }                                               \
00046   }
00047  #define CSREFARR_TRACK_INCREF(obj,tag) \
00048   CSREFARR_TRACK(IncRef, obj->IncRef(), obj->GetRefCount(), obj, tag);
00049  #define CSREFARR_TRACK_DECREF(obj,tag) \
00050   CSREFARR_TRACK(DecRef, obj->DecRef(), obj->GetRefCount(), obj, tag);
00051 #else
00052  #define CSREFARR_TRACK_INCREF(obj,tag) \
00053   if (obj) obj->IncRef();
00054  #define CSREFARR_TRACK_DECREF(obj,tag) \
00055   if (obj) obj->DecRef();
00056 #endif
00057 
00058 template <class T>
00059 class csRefArrayElementHandler
00060 {
00061 public:
00062   static void Construct (T* address, T const& src)
00063   {
00064     *address = src;
00065     CSREFARR_TRACK_INCREF (src, address);
00066   }
00067 
00068   static void Destroy (T* address)
00069   {
00070     CSREFARR_TRACK_DECREF ((*address), address);
00071   }
00072 
00073   static void InitRegion (T* address, int count)
00074   {
00075     memset (address, 0, count*sizeof (T));
00076   }
00077 };
00078 
00082 template <class T>
00083 class csRefArray : public csArray<T*, csRefArrayElementHandler<T*> >
00084 {
00085 public:
00090   csRefArray (int ilimit = 0, int ithreshold = 0)
00091         : csArray<T*, csRefArrayElementHandler<T*> > (ilimit, ithreshold)
00092   {
00093   }
00094 
00096   csPtr<T> Pop ()
00097   {
00098     CS_ASSERT (this->Length () > 0);
00099     csRef<T> ret = this->Get (this->Length () - 1); // see *1*
00100     SetLength (this->Length () - 1);
00101     return csPtr<T> (ret);
00102   }
00103 
00104 };
00105 
00106 #undef CSREFARR_TRACK_INCREF
00107 #undef CSREFARR_TRACK_DECREF
00108 
00109 #endif // __CS_REFARR_H__

Generated for Crystal Space by doxygen 1.2.18