CrystalSpace

Public API Reference

csutil/weakreferenced.h

Go to the documentation of this file.
00001 /*
00002     Crystal Space Weak Reference Counting Implementation
00003     Copyright (C) 2006 by Jorrit Tyberghein
00004               (C) 2006 by Amir Taaki
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the Free
00018     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #ifndef __CS_CSUTIL_WEAKREFERENCED_H__
00022 #define __CS_CSUTIL_WEAKREFERENCED_H__
00023 
00028 #include "array.h"
00029 
00030 namespace CS
00031 {
00032 namespace Utility
00033 {
00034 
00042 class WeakReferenced
00043 {
00044 public:
00045   WeakReferenced () : weakref_owners (0) {}
00046   ~WeakReferenced ()
00047   {
00048     if (weakref_owners)
00049     {
00050       for (size_t i = 0; i < weakref_owners->GetSize(); i++)
00051       {
00052         *((*weakref_owners)[i]) = 0;
00053       }
00054       delete weakref_owners;
00055     }
00056   }
00057 
00058   void AddRefOwner (void** ref_owner)
00059   {
00060     if (!weakref_owners)
00061       weakref_owners = new WeakRefOwnerArray (0);
00062     weakref_owners->InsertSorted (ref_owner);
00063   }
00064   void RemoveRefOwner (void** ref_owner)
00065   {
00066     if (!weakref_owners)
00067       return;
00068 
00069     size_t index = weakref_owners->FindSortedKey (
00070       csArrayCmp<void**, void**>(ref_owner));
00071 
00072     if (index != csArrayItemNotFound)
00073       weakref_owners->DeleteIndex (index);
00074   }
00075 
00076 private:
00077   typedef csArray<void**,
00078     csArrayElementHandler<void**>,
00079     CS::Memory::AllocatorMalloc,
00080     csArrayCapacityLinear<csArrayThresholdFixed<4> > > WeakRefOwnerArray;
00081   WeakRefOwnerArray* weakref_owners;
00082 };
00083 
00084 } // namespace Utility
00085 } // namespace CS
00086 
00087 #endif // __CS_CSUTIL_WEAKREFERENCED_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3