00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __TYPEDVEC_H__
00021 #define __TYPEDVEC_H__
00022
00023 #include "csutil/scf.h"
00024 #include "csutil/csvector.h"
00025
00064 #define CS_DECLARE_TYPED_VECTOR(NAME,TYPE) \
00065 CS_PRIVATE_DECLARE_TYPED_VECTOR (NAME, TYPE)
00066
00071 #define CS_DECLARE_TYPED_VECTOR_NODELETE(NAME,TYPE) \
00072 CS_PRIVATE_DECLARE_TYPED_VECTOR_NODELETE (NAME, TYPE)
00073
00081 #define CS_DECLARE_TYPED_VECTOR_USERDELETE(NAME,TYPE) \
00082 CS_PRIVATE_DECLARE_TYPED_VECTOR_USERDELETE (NAME, TYPE)
00083
00088 #define CS_DECLARE_TYPED_VECTOR_DECREF(NAME, TYPE) \
00089 CS_PRIVATE_DECLARE_TYPED_VECTOR_DECREF(NAME, TYPE)
00090
00097 #define CS_IMPLEMENT_TYPED_VECTOR_DELETE(NAME,TYPE) \
00098 CS_PRIVATE_IMPLEMENT_TYPED_VECTOR_DELETE(NAME,TYPE)
00099
00109 #define CS_DECLARE_TYPED_IBASE_VECTOR(NAME,TYPE) \
00110 CS_PRIVATE_DECLARE_TYPED_IBASE_VECTOR (NAME, TYPE)
00111
00118 #define CS_DECLARE_TYPED_RESTRICTED_ACCESS_VECTOR(NAME,TYPE) \
00119 CS_PRIVATE_DECLARE_TYPED_RESTRICTED_ACCESS_VECTOR (NAME, TYPE)
00120
00143 #define CS_BEGIN_TYPED_VECTOR(MACRO,NAME,TYPE) \
00144 CS_PRIVATE_BEGIN_USER_VECTOR(MACRO,NAME,TYPE)
00145
00149 #define CS_FINISH_TYPED_VECTOR \
00150 CS_PRIVATE_FINISH_USER_VECTOR
00151
00156 #define CS_TYPED_VECTOR_CONSTRUCTOR(NAME) \
00157 CS_PRIVATE_TYPED_VECTOR_CONSTRUCTOR (NAME)
00158
00159
00160
00161
00162
00163
00172 class csRestrictedAccessVector : public csVector
00173 {
00174 public:
00175 virtual bool PrepareItem (csSome )
00176 { return true; }
00177 virtual bool FreeItem (csSome )
00178 { return true; }
00179
00180 inline csRestrictedAccessVector (int lim, int thr) : csVector (lim, thr) {}
00181 inline bool Delete (int n)
00182 {
00183 return csVector::Delete (n, true);
00184 }
00185 inline bool Delete (csSome what)
00186 {
00187 return csVector::Delete (what, true);
00188 }
00189 inline void DeleteAll ()
00190 {
00191 csVector::DeleteAll (true);
00192 }
00193 inline int Push (csSome what)
00194 {
00195 if (!PrepareItem (what)) return -1;
00196 return csVector::Push(what);
00197 }
00198 inline int PushSmart (csSome what)
00199 {
00200 int n = Find (what);
00201 if (n != -1) return n;
00202
00203 if (!PrepareItem (what)) return -1;
00204 return csVector::Push(what);
00205 }
00206 inline bool Insert (int n, csSome Item)
00207 {
00208 if (!PrepareItem (Item)) return false;
00209 return csVector::Insert (n, Item);
00210 }
00211 inline int InsertSorted (csSome Item, int *oEqual = NULL, int Mode = 0)
00212 {
00213 if (!PrepareItem (Item)) return -1;
00214 return csVector::InsertSorted (Item, oEqual, Mode);
00215 }
00216 inline bool Replace (int n, csSome what)
00217 {
00218 if (!PrepareItem (what)) return false;
00219 return csVector::Replace (n, what, true);
00220 }
00221 inline csSome Pop ()
00222 {
00223 if (FreeItem (Top ()))
00224 return csVector::Pop ();
00225 else
00226 return NULL;
00227 }
00228 };
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 class csIBaseVector : public csRestrictedAccessVector
00248 {
00249 public:
00250 inline csIBaseVector (int lim, int thr) : csRestrictedAccessVector (lim, thr) {}
00251
00252 virtual bool PrepareItem (csSome Item)
00253 {
00254 ((iBase*)Item)->IncRef ();
00255 return true;
00256 }
00257 virtual bool FreeItem (csSome Item)
00258 {
00259 ((iBase*)Item)->DecRef ();
00260 return true;
00261 }
00262 inline csSome Pop ()
00263 {
00264
00265
00266 csSome item = Top ();
00267 ((iBase*)item)->IncRef ();
00268
00269 if (FreeItem (item)) {
00270
00271 return csVector::Pop ();
00272 } else {
00273
00274 ((iBase*)item)->DecRef ();
00275 return NULL;
00276 }
00277 }
00278 };
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 #define CS_PRIVATE_DECLARE_TYPED_VECTOR_HELPER(NAME,TYPE) \
00290 inline NAME (int ilimit = 16, int ithreshold = 16) : \
00291 superclass (ilimit, ithreshold) {} \
00292 virtual ~NAME () \
00293 { DeleteAll (); } \
00294 inline void SetLength (int n) \
00295 { superclass::SetLength(n); } \
00296 inline int Length () const \
00297 { return count; } \
00298 inline int Limit () const \
00299 { return limit; } \
00300 inline void Exchange (int n1, int n2) \
00301 { superclass::Exchange (n1, n2); } \
00302 inline void QuickSort (int Left, int Right, int Mode = 0) \
00303 { superclass::QuickSort (Left, Right, Mode); } \
00304 inline void QuickSort (int Mode = 0) \
00305 { superclass::QuickSort (Mode); } \
00306 inline int Find (TYPE *which) const \
00307 { return superclass::Find ((csSome)which); } \
00308 inline int FindKey (csConstSome Key, int Mode = 0) const \
00309 { return superclass::FindKey (Key, Mode); } \
00310 inline int FindSortedKey (csConstSome Key, int Mode = 0) const \
00311 { return superclass::FindSortedKey (Key, Mode); } \
00312 inline int Push (TYPE *obj) \
00313 { return superclass::Push ((csSome)obj); } \
00314 inline int PushSmart (TYPE *obj) \
00315 { return superclass::PushSmart ((csSome)obj); } \
00316 inline TYPE *Pop () \
00317 { return (TYPE *)superclass::Pop(); } \
00318 inline TYPE *Top () const \
00319 { return (TYPE *)superclass::Top(); } \
00320 inline bool Insert (int n, TYPE *Item) \
00321 { return superclass::Insert (n, (csSome)Item); } \
00322 inline int InsertSorted (TYPE *Item, int *oEqual = NULL, int Mode = 0) \
00323 { return superclass::InsertSorted ((csSome)Item, oEqual, Mode); }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 #define CS_PRIVATE_BEGIN_TYPED_VECTOR(NAME,TYPE) \
00357 class NAME : private csVector \
00358 { \
00359 typedef csVector superclass; \
00360 public: \
00361 inline bool Delete (int n, bool FreeIt = true) \
00362 { return superclass::Delete (n, FreeIt); } \
00363 inline bool Delete (TYPE *Item, bool FreeIt = true) \
00364 { return superclass::Delete ((csSome)Item, FreeIt); } \
00365 inline void DeleteAll (bool FreeThem = true) \
00366 { superclass::DeleteAll (FreeThem); } \
00367 CS_PRIVATE_DECLARE_TYPED_VECTOR_HELPER (NAME, TYPE) \
00368 inline TYPE *& operator [] (int n) \
00369 { return (TYPE *&)superclass::operator [] (n); } \
00370 inline TYPE *& operator [] (int n) const \
00371 { return (TYPE *&)superclass::operator [] (n); } \
00372 inline TYPE *& Get (int n) const \
00373 { return (TYPE *&)superclass::Get(n); } \
00374 inline TYPE **GetArray () \
00375 { return (TYPE**)root; } \
00376 inline bool Replace (int n, TYPE *what, bool FreePrevious = true) \
00377 { return superclass::Replace(n, (csSome)what, FreePrevious); }
00378
00379
00380 #define CS_PRIVATE_FINISH_TYPED_VECTOR(TYPE) \
00381 virtual bool FreeItem (csSome Item) \
00382 { return FreeTypedItem ((TYPE *)Item); } \
00383 }
00384
00385
00386
00387
00388
00389
00390 #define CS_PRIVATE_DECLARE_TYPED_VECTOR(NAME,TYPE) \
00391 CS_PRIVATE_BEGIN_TYPED_VECTOR (NAME,TYPE) \
00392 inline bool FreeTypedItem (TYPE* obj) \
00393 { delete obj; return true; } \
00394 CS_PRIVATE_FINISH_TYPED_VECTOR (TYPE)
00395
00400 #define CS_PRIVATE_DECLARE_TYPED_VECTOR_NODELETE(NAME,TYPE) \
00401 CS_PRIVATE_BEGIN_TYPED_VECTOR (NAME,TYPE) \
00402 inline bool FreeTypedItem (TYPE*) \
00403 { return true; } \
00404 CS_PRIVATE_FINISH_TYPED_VECTOR (TYPE)
00405
00410 #define CS_PRIVATE_DECLARE_TYPED_VECTOR_USERDELETE(NAME,TYPE) \
00411 CS_PRIVATE_BEGIN_TYPED_VECTOR (NAME,TYPE) \
00412 bool FreeTypedItem (TYPE*); \
00413 CS_PRIVATE_FINISH_TYPED_VECTOR (TYPE)
00414
00415 #define CS_PRIVATE_DECLARE_TYPED_VECTOR_DECREF(NAME,TYPE) \
00416 CS_PRIVATE_BEGIN_TYPED_VECTOR (NAME,TYPE) \
00417 inline bool FreeTypedItem (TYPE *Item) \
00418 { Item->DecRef(); Item = NULL; return true; } \
00419 CS_PRIVATE_FINISH_TYPED_VECTOR (TYPE)
00420
00426 #define CS_PRIVATE_DECLARE_TYPED_RESTR_ACC_VECTOR(NAME,TYPE,sclass) \
00427 class NAME : private sclass \
00428 { \
00429 protected: \
00430 typedef sclass superclass; \
00431 virtual bool PrepareItem (csSome item) \
00432 { return superclass::PrepareItem (item); } \
00433 virtual bool FreeItem (csSome item) \
00434 { return superclass::FreeItem (item); } \
00435 public: \
00436 inline bool Delete (int n) \
00437 { return superclass::Delete (n); } \
00438 inline bool Delete (TYPE *Item) \
00439 { return superclass::Delete ((csSome)Item); } \
00440 inline void DeleteAll () \
00441 { superclass::DeleteAll (); } \
00442 CS_PRIVATE_DECLARE_TYPED_VECTOR_HELPER (NAME, TYPE) \
00443 inline TYPE *operator [] (int n) const \
00444 { return (TYPE *)superclass::operator [] (n); } \
00445 inline TYPE *Get (int n) const \
00446 { return (TYPE *)superclass::Get(n); } \
00447 inline bool Replace (int n, TYPE *what) \
00448 { return superclass::Replace(n, (csSome)what); } \
00449 }
00450
00451 #define CS_PRIVATE_DECLARE_TYPED_RESTRICTED_ACCESS_VECTOR(NAME,TYPE) \
00452 CS_PRIVATE_DECLARE_TYPED_RESTR_ACC_VECTOR (NAME, TYPE, csRestrictedAccessVector)
00453
00454 #define CS_PRIVATE_DECLARE_TYPED_IBASE_VECTOR(NAME,TYPE) \
00455 CS_PRIVATE_DECLARE_TYPED_RESTR_ACC_VECTOR (NAME, TYPE, csIBaseVector)
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 #define CS_PRIVATE_IMPLEMENT_TYPED_VECTOR_DELETE(NAME,TYPE) \
00477 bool NAME::FreeTypedItem (TYPE *Item) \
00478 { delete Item; return true; }
00479
00480 #define CS_PRIVATE_BEGIN_USER_VECTOR(MACRO,NAME,TYPE) \
00481 MACRO (NAME##_Helper, TYPE); \
00482 class NAME : public NAME##_Helper \
00483 { \
00484 public:
00485
00486 #define CS_PRIVATE_FINISH_USER_VECTOR \
00487 }
00488
00489 #define CS_PRIVATE_TYPED_VECTOR_CONSTRUCTOR(NAME) \
00490 NAME (int ilimit = 8, int ithreshold = 16) : \
00491 NAME##_Helper (ilimit, ithreshold) {}
00492
00493 #endif // __TYPEDVEC_H__