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   CS_BEGIN_TYPED_VECTOR (MACRO, NAME, TYPE)                             \
00186   private:                                                              \
00187     csNamedObjectVector ObjVec;                                         \
00188   public:                                                               \
00189     NAME (int ilimit = 16, int ithreshold = 16) :                       \
00190       NAME##_Helper (ilimit, ithreshold)                                \
00191       {ObjVec.SetVector (this);}                                        \
00192     virtual ~NAME ()                                                    \
00193       { DeleteAll (); }                                                 \
00194     inline csNamedObjectVector *GetObjectVector ()                      \
00195       { return &ObjVec; }                                               \
00196     inline const csNamedObjectVector *GetObjectVector () const          \
00197       { return &ObjVec; }                                               \
00198     int GetIndexByName (const char* iName) const                        \
00199       { return ObjVec.GetIndexByName (iName); }                         \
00200     TYPE *FindByName (const char* iName) const                          \
00201       { int n = GetIndexByName (iName);                                 \
00202         return (n==-1) ? 0 : Get(n); }                                  \
00203     iObject *FindObjectByName (const char* iName) const                 \
00204       { return ObjVec.FindByName (iName); }                             \
00205     virtual int Compare (csSome Item1, csSome Item2, int Mode) const    \
00206       { return csNamedObjectVector::Compare (Item1, Item2, Mode); }     \
00207     virtual int CompareKey (csSome Item, csConstSome Key, int Mode) const \
00208       { return csNamedObjectVector::CompareKey (Item, Key, Mode); }     \
00209   CS_FINISH_TYPED_VECTOR
00210 
00211 // implementation of inline functions follows
00212 inline csNamedObjectVector::csNamedObjectVector ()
00213   { Vector = 0; }
00214 inline void csNamedObjectVector::SetVector (void *v)
00215   { Vector = (csVector*)v; }
00216 inline void csNamedObjectVector::SetLength (int n)
00217   { Vector->SetLength(n); }
00218 inline int csNamedObjectVector::Length () const
00219   { return Vector->Length (); }
00220 inline int csNamedObjectVector::Limit () const
00221   { return Vector->Limit (); }
00222 inline void csNamedObjectVector::Exchange (int n1, int n2)
00223   { Vector->Exchange (n1, n2); }
00224 inline void csNamedObjectVector::QuickSort (int Left, int Right, int Mode)
00225   { Vector->QuickSort (Left, Right, Mode); }
00226 inline void csNamedObjectVector::QuickSort (int Mode)
00227   { Vector->QuickSort (Mode); }
00228 inline int csNamedObjectVector::FindKey (csConstSome Key, int Mode) const
00229   { return Vector->FindKey (Key, Mode); }
00230 inline int csNamedObjectVector::FindSortedKey (csConstSome Key, int Mode) const
00231   { return Vector->FindSortedKey (Key, Mode); }
00232 inline iObject *csNamedObjectVector::Pop ()
00233   {
00234     iBase *objbase = (iBase*)Vector->Pop ();
00235     if (!objbase) return 0;
00236     iObject *obj = SCF_QUERY_INTERFACE_FAST (objbase, iObject);
00237     CS_ASSERT (obj);
00238     obj->DecRef ();
00239     return obj;
00240   }
00241 inline iObject *csNamedObjectVector::Top () const
00242   {
00243     iBase *objbase = (iBase*)Vector->Top ();
00244     if (!objbase) return 0;
00245     iObject *obj = SCF_QUERY_INTERFACE_FAST (objbase, iObject);
00246     CS_ASSERT (obj);
00247     obj->DecRef ();
00248     return obj;
00249   }
00250 inline bool csNamedObjectVector::Delete (int n)
00251   { return Vector->Delete (n); }
00252 inline void csNamedObjectVector::DeleteAll ()
00253   { Vector->DeleteAll (); }
00254 inline iObject *csNamedObjectVector::Get (int n) const
00255   { 
00256     iBase *objbase = (iBase*)Vector->Get (n);
00257     iObject *o = objbase ? SCF_QUERY_INTERFACE_FAST (objbase, iObject) : 0;
00258     if (o) o->DecRef ();
00259     return o;
00260   }
00261 inline iObject *csNamedObjectVector::operator[] (int n) const
00262   { 
00263     iBase *objbase = (iBase*)Vector->Get (n);
00264     iObject *o = objbase ? SCF_QUERY_INTERFACE_FAST (objbase, iObject) : 0;
00265     if (o) o->DecRef ();
00266     return o;
00267   }
00268 
00269 #endif // __NOBJVEC_H__

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