CrystalSpace

Public API Reference

csutil/refarr.h

Go to the documentation of this file.
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 
00027 //-----------------------------------------------------------------------------
00028 // Note *1*: The explicit "this->" is needed by modern compilers (such as gcc
00029 // 3.4.x) which distinguish between dependent and non-dependent names in
00030 // templates.  See: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
00031 //-----------------------------------------------------------------------------
00032 
00033 #include "csutil/array.h"
00034 #include "csutil/ref.h"
00035 
00036 #ifdef CS_REF_TRACKER
00037  #include <typeinfo>
00038  #include "csutil/reftrackeraccess.h"
00039 
00040  #define CSREFARR_TRACK(x, cmd, refCount, obj, tag) \
00041   {                                                 \
00042     const int rc = obj ? refCount : -1;             \
00043     if (obj) cmd;                                   \
00044     if (obj)                                        \
00045     {                                               \
00046       csRefTrackerAccess::SetDescription (obj,      \
00047         typeid(T).name());                          \
00048       csRefTrackerAccess::Match ## x (obj, rc, tag);\
00049     }                                               \
00050   }
00051  #define CSREFARR_TRACK_INCREF(obj,tag) \
00052   CSREFARR_TRACK(IncRef, obj->IncRef(), obj->GetRefCount(), obj, tag);
00053  #define CSREFARR_TRACK_DECREF(obj,tag) \
00054   CSREFARR_TRACK(DecRef, obj->DecRef(), obj->GetRefCount(), obj, tag);
00055 #else
00056  #define CSREFARR_TRACK_INCREF(obj,tag) \
00057   if (obj) obj->IncRef();
00058  #define CSREFARR_TRACK_DECREF(obj,tag) \
00059   if (obj) obj->DecRef();
00060 #endif
00061 
00062 template <class T>
00063 class csRefArrayElementHandler : public csArrayElementHandler<T>
00064 {
00065 public:
00066   static void Construct (T* address, T const& src)
00067   {
00068     *address = src;
00069     CSREFARR_TRACK_INCREF (src, address);
00070   }
00071 
00072   static void Destroy (T* address)
00073   {
00074     CSREFARR_TRACK_DECREF ((*address), address);
00075   }
00076 
00077   static void InitRegion (T* address, size_t count)
00078   {
00079     memset (address, 0, count*sizeof (T));
00080   }
00081 };
00082 
00091 template <class T, class Allocator = CS::Memory::AllocatorMalloc>
00092 class csRefArray : public csArray<T*, csRefArrayElementHandler<T*>, Allocator>
00093 {
00094 public:
00099   csRefArray (int ilimit = 0, int ithreshold = 0)
00100     : csArray<T*, csRefArrayElementHandler<T*>, Allocator> (ilimit, ithreshold)
00101   {
00102   }
00103 
00105   csPtr<T> Pop ()
00106   {
00107     CS_ASSERT (this->Length () > 0);
00108     csRef<T> ret = this->Get (this->Length () - 1); // see *1*
00109     SetLength (this->Length () - 1);
00110     return csPtr<T> (ret);
00111   }
00112 };
00113 
00114 #undef CSREFARR_TRACK_INCREF
00115 #undef CSREFARR_TRACK_DECREF
00116 
00117 #endif // __CS_REFARR_H__

Generated for Crystal Space by doxygen 1.4.7