csutil/refarr.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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,
    00092           class Allocator = CS::Container::ArrayAllocDefault,
    00093           class CapacityHandler = CS::Container::ArrayCapacityDefault>
    00094 class csRefArray :
    00095   public csArray<T*, csRefArrayElementHandler<T*>, Allocator, CapacityHandler>
    00096 {
    00097 public:
    00102   csRefArray (int ilimit = 0,
    00103     const CapacityHandler& ch = CapacityHandler())
    00104     : csArray<T*, csRefArrayElementHandler<T*>, Allocator, CapacityHandler> (
    00105         ilimit, ch)
    00106   {
    00107   }
    00108
    00110   csRef<T> Pop ()
    00111   {
    00112     CS_ASSERT (this->GetSize () > 0);
    00113     csRef<T> ret = this->Get (this->GetSize () - 1); // see *1*
    00114     SetSize (this->GetSize () - 1);
    00115     return ret;
    00116   }
    00117 };
    00118
    00119 #undef CSREFARR_TRACK_INCREF
    00120 #undef CSREFARR_TRACK_DECREF
    00121
    00122 #endif // __CS_REFARR_H__