Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

nobjvec.h

00001 /*
00002     Crystal Space: Named Object Vector class
00003     Copyright (C) 1998,1999 by Andrew Zabolotny <bit@eltech.ru>
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 __NOBJVEC_H__
00021 #define __NOBJVEC_H__
00022 
00023 #include "csutil/csobjvec.h"
00024 #include "csutil/typedvec.h"
00025 #include "iutil/object.h"
00026 
00027 class csObject;
00028 struct iObject;
00029 
00030 SCF_DECLARE_FAST_INTERFACE (iObject);
00031 
00038 class csNamedObjVector : public csObjVector
00039 {
00040 public:
00042   csNamedObjVector (int ilimit = 8, int ithreshold = 16) :
00043     csObjVector (ilimit, ithreshold) {}
00044 
00046   csObject *FindByName (const char* iName) const;
00047 
00049   virtual int Compare (csSome Item1, csSome Item2, int Mode) const;
00050 
00052   virtual int CompareKey (csSome Item, csConstSome Key, int Mode) const;
00053 
00055   csObject *Get (int idx) const
00056   { return (csObject *)csVector::Get (idx); }
00057 };
00058 
00089 class csNamedObjectVector
00090 {
00091 private:
00092   // the wrapped vector
00093   csVector *Vector;
00094 
00095 public:
00097   inline csNamedObjectVector ();
00098 
00100   int GetIndexByName (const char *name) const;
00102   iObject *FindByName (const char *name) const;
00103 
00105   inline void SetLength (int n);
00107   inline int Length () const;
00109   inline int Limit () const;
00111   inline void Exchange (int n1, int n2);
00113   inline void QuickSort (int Left, int Right, int Mode = 0);
00115   inline void QuickSort (int Mode = 0);
00117   int Find (iObject *obj) const;
00119   inline int FindKey (csConstSome Key, int Mode = 0) const;
00121   inline int FindSortedKey (csConstSome Key, int Mode = 0) const;
00123   inline iObject *Pop ();
00125   inline iObject *Top () const;
00127   inline bool Delete (int n);
00129   bool Delete (iObject *obj);
00131   inline void DeleteAll ();
00133   inline iObject *Get (int n) const;
00135   inline iObject *operator[] (int n) const;
00136 
00137   // private method: Compare two objects by their names
00138   static int Compare (csSome Item1, csSome Item2, int Mode);
00139   // private method: Compare object's name with a string
00140   static int CompareKey (csSome Item, csConstSome Key, int Mode);
00141   // private method: set the wrapped vector
00142   inline void SetVector (void *vec);
00143 };
00144 
00149 #define CS_DECLARE_OBJECT_VECTOR_NOREF(NAME,TYPE)                       \
00150   CS_PRIVATE_DECLARE_OBJECT_VECTOR_NOREF (NAME, TYPE)
00151 
00157 #define CS_DECLARE_RESTRICTED_ACCESS_OBJECT_VECTOR(NAME,TYPE)           \
00158   CS_PRIVATE_DECLARE_RESTRICTED_ACCESS_OBJECT_VECTOR (NAME, TYPE)
00159 
00165 #define CS_DECLARE_OBJECT_VECTOR(NAME,TYPE)                             \
00166   CS_PRIVATE_DECLARE_OBJECT_VECTOR (NAME, TYPE)
00167 
00168 //----------------------------------------------------------------------------
00169 //--- implementation of the above macros follows -----------------------------
00170 //----------------------------------------------------------------------------
00171 
00172 #define CS_PRIVATE_DECLARE_OBJECT_VECTOR(NAME,TYPE)                     \
00173   CS_PRIVATE_DECLARE_OBJECT_VECTOR_COMMON (                             \
00174     CS_DECLARE_TYPED_IBASE_VECTOR, NAME, TYPE)
00175 
00176 #define CS_PRIVATE_DECLARE_OBJECT_VECTOR_NOREF(NAME,TYPE)               \
00177   CS_PRIVATE_DECLARE_OBJECT_VECTOR_COMMON (                             \
00178     CS_DECLARE_TYPED_VECTOR_NODELETE, NAME, TYPE)
00179 
00180 #define CS_PRIVATE_DECLARE_RESTRICTED_ACCESS_OBJECT_VECTOR(NAME,TYPE)   \
00181   CS_PRIVATE_DECLARE_OBJECT_VECTOR_COMMON (                             \
00182     CS_DECLARE_TYPED_RESTRICTED_ACCESS_VECTOR, NAME, TYPE)
00183 
00184 #define CS_PRIVATE_DECLARE_OBJECT_VECTOR_COMMON(MACRO,NAME,TYPE)        \
00185   MACRO (NAME##_Helper, TYPE);                                          \
00186   class NAME : public NAME##_Helper {                                   \
00187   private:                                                              \
00188     csNamedObjectVector ObjVec;                                         \
00189   public:                                                               \
00190     NAME (int ilimit = 16, int ithreshold = 16) :                       \
00191       NAME##_Helper (ilimit, ithreshold)                                \
00192       {ObjVec.SetVector (this);}                                        \
00193     virtual ~NAME ()                                                    \
00194       { DeleteAll (); }                                                 \
00195     inline csNamedObjectVector *GetObjectVector ()                      \
00196       { return &ObjVec; }                                               \
00197     inline const csNamedObjectVector *GetObjectVector () const          \
00198       { return &ObjVec; }                                               \
00199     int GetIndexByName (const char* iName) const                        \
00200       { return ObjVec.GetIndexByName (iName); }                         \
00201     TYPE *FindByName (const char* iName) const                          \
00202       { int n = GetIndexByName (iName);                                 \
00203         return (n==-1) ? 0 : Get(n); }                                  \
00204     iObject *FindObjectByName (const char* iName) const                 \
00205       { return ObjVec.FindByName (iName); }                             \
00206     virtual int Compare (csSome Item1, csSome Item2, int Mode) const    \
00207       { return csNamedObjectVector::Compare (Item1, Item2, Mode); }     \
00208     virtual int CompareKey (csSome Item, csConstSome Key, int Mode) const \
00209       { return csNamedObjectVector::CompareKey (Item, Key, Mode); }     \
00210   }
00211 
00212 // implementation of inline functions follows
00213 inline csNamedObjectVector::csNamedObjectVector ()
00214   { Vector = 0; }
00215 inline void csNamedObjectVector::SetVector (void *v)
00216   { Vector = (csVector*)v; }
00217 inline void csNamedObjectVector::SetLength (int n)
00218   { Vector->SetLength(n); }
00219 inline int csNamedObjectVector::Length () const
00220   { return Vector->Length (); }
00221 inline int csNamedObjectVector::Limit () const
00222   { return Vector->Limit (); }
00223 inline void csNamedObjectVector::Exchange (int n1, int n2)
00224   { Vector->Exchange (n1, n2); }
00225 inline void csNamedObjectVector::QuickSort (int Left, int Right, int Mode)
00226   { Vector->QuickSort (Left, Right, Mode); }
00227 inline void csNamedObjectVector::QuickSort (int Mode)
00228   { Vector->QuickSort (Mode); }
00229 inline int csNamedObjectVector::FindKey (csConstSome Key, int Mode) const
00230   { return Vector->FindKey (Key, Mode); }
00231 inline int csNamedObjectVector::FindSortedKey (csConstSome Key, int Mode) const
00232   { return Vector->FindSortedKey (Key, Mode); }
00233 inline iObject *csNamedObjectVector::Pop ()
00234   {
00235     iBase *objbase = (iBase*)Vector->Pop ();
00236     if (!objbase) return 0;
00237     iObject *obj = SCF_QUERY_INTERFACE_FAST (objbase, iObject);
00238     CS_ASSERT (obj);
00239     obj->DecRef ();
00240     return obj;
00241   }
00242 inline iObject *csNamedObjectVector::Top () const
00243   {
00244     iBase *objbase = (iBase*)Vector->Top ();
00245     if (!objbase) return 0;
00246     iObject *obj = SCF_QUERY_INTERFACE_FAST (objbase, iObject);
00247     CS_ASSERT (obj);
00248     obj->DecRef ();
00249     return obj;
00250   }
00251 inline bool csNamedObjectVector::Delete (int n)
00252   { return Vector->Delete (n); }
00253 inline void csNamedObjectVector::DeleteAll ()
00254   { Vector->DeleteAll (); }
00255 inline iObject *csNamedObjectVector::Get (int n) const
00256   {
00257     iBase *objbase = (iBase*)Vector->Get (n);
00258     iObject *o = objbase ? SCF_QUERY_INTERFACE_FAST (objbase, iObject) : 0;
00259     if (o) o->DecRef ();
00260     return o;
00261   }
00262 inline iObject *csNamedObjectVector::operator[] (int n) const
00263   {
00264     iBase *objbase = (iBase*)Vector->Get (n);
00265     iObject *o = objbase ? SCF_QUERY_INTERFACE_FAST (objbase, iObject) : 0;
00266     if (o) o->DecRef ();
00267     return o;
00268   }
00269 
00270 #endif // __NOBJVEC_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000