00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __TOOLBOX_REFERENCE_H
00010 #define __TOOLBOX_REFERENCE_H
00011
00012
00013 namespace toolbox
00014 {
00015 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00016 class AutoDeleting : public CObject
00017 #else
00018 class AutoDeleting
00019 #endif
00020 {
00021 friend Reference;
00022
00023 private:
00024
00025 ArrayList<Reference> References;
00026
00027 void AddReference(class Reference * Reference);
00028
00029 void RemoveReference(class Reference * Reference);
00030
00031 public:
00032
00033 virtual ~AutoDeleting();
00034
00035 inline const ArrayList<Reference> & GetReferences() const;
00036 };
00037
00038 #if defined(_DEBUG) && (defined(_AFX) || defined(_AFXDLL))
00039 class Reference : public CObject
00040 #else
00041 class Reference
00042 #endif
00043 {
00044 friend AutoDeleting;
00045
00046 private:
00047
00048 void * Referrer;
00049
00050 protected:
00051
00052 Reference(void * referrer);
00053
00054 virtual void AddElement(AutoDeleting * Element) = NULL;
00055
00056 virtual ArrayList<AutoDeleting> * GetElements() const = NULL;
00057
00058 virtual void RemoveElement(AutoDeleting * Element) = NULL;
00059
00060 public:
00061
00062 virtual ~Reference();
00063
00064 inline void * GetReferrer() const;
00065 };
00066
00067 template <class ELEMENTTYPE> class SingleReference : public Reference
00068 {
00069 private:
00070
00071 class ELEMENTTYPE * Element;
00072
00073 static const AutoDeleting * ParameterConstraint;
00074
00075 protected:
00076
00077 virtual void AddElement(AutoDeleting * Element);
00078
00079 virtual ArrayList<AutoDeleting> * GetElements() const;
00080
00081 virtual void RemoveElement(AutoDeleting * Element);
00082
00083 public:
00084
00085 SingleReference(void * Referrer);
00086
00087 virtual ~SingleReference();
00088
00089 inline ELEMENTTYPE * GetElement() const;
00090
00091 void SetElement(ELEMENTTYPE * Element);
00092
00093 inline void UnsetElement();
00094 };
00095
00096 template <class ELEMENTTYPE> class ReferenceList : public Reference
00097 {
00098 private:
00099
00100 ArrayList<ELEMENTTYPE> * Elements;
00101
00102 static const AutoDeleting * ParameterConstraint;
00103
00104 protected:
00105
00106 virtual void AddElement(AutoDeleting * Element);
00107
00108 virtual ArrayList<AutoDeleting> * GetElements() const;
00109
00110 virtual void RemoveElement(AutoDeleting * Element);
00111
00112 public:
00113
00117 ReferenceList();
00118
00123 ReferenceList(long initSize);
00124
00131 ReferenceList(long initSize, int minimumFillRatio, int desiredFillRatio);
00132
00137 ~ReferenceList();
00138
00143 void Append(ELEMENTTYPE * Element);
00144
00151 void AppendAll(const ArrayList<ELEMENTTYPE> * Elements);
00152
00160 bool Contains(const ELEMENTTYPE * Element) const;
00161
00172 bool Contains(const ELEMENTTYPE * Element,
00173 int (* compare)(const ELEMENTTYPE * Element1, const ELEMENTTYPE * Element2)) const;
00174
00183 int Find(const ELEMENTTYPE * Element) const;
00184
00196 int Find(const ELEMENTTYPE * Element,
00197 int (* compare)(const ELEMENTTYPE * Element1, const ELEMENTTYPE * Element2)) const;
00198
00204 ELEMENTTYPE * Get(long nr) const;
00205
00211 inline long GetCount() const;
00212
00218 inline int GetDesiredFillRatio() const;
00219
00224 inline int GetFillRatio() const;
00225
00231 ELEMENTTYPE * GetFirst() const;
00232
00238 ELEMENTTYPE * GetLast() const;
00239
00245 inline int GetMinimumFillRatio() const;
00246
00253 void Insert(const ELEMENTTYPE * Element, long nr);
00254
00263 void InsertAll(const ArrayList<ELEMENTTYPE> * Elements, long nr);
00264
00268 inline bool IsEmpty() const;
00269
00274 inline void Prepend(const ELEMENTTYPE * Element);
00275
00282 inline void PrependAll(const ArrayList<ELEMENTTYPE> * Elements);
00283
00292 ELEMENTTYPE * Replace(ELEMENTTYPE * NewElement, long nr);
00293
00300 bool Remove(long nr);
00301
00311 bool Remove(long from, long count);
00312
00321 bool Remove(ELEMENTTYPE * Element);
00322
00327 void RemoveAll();
00328
00335 inline bool RemoveFirst();
00336
00343 bool RemoveLast();
00344
00345 #ifdef _TOOLBOX_TEST
00346
00352 static void RunTestSuite(int * performedTests, int * failedTests);
00353 #endif
00354
00358 void Shuffle();
00359
00366 inline void Sort();
00367
00378 void Sort(int (* compare)(const ELEMENTTYPE * Element1, const ELEMENTTYPE * Element2));
00379 };
00380
00381 template <class VALUETYPE> class ReferenceValueMap : Reference
00382 {
00383 protected:
00384
00385 virtual void AddElement(VALUETYPE * Element);
00386
00387 virtual void RemoveElement(VALUETYPE * Element);
00388
00389 public:
00390
00391 virtual ArrayList<AutoDeleting> * GetElements() const;
00392 };
00393
00394 template <class VALUETYPE> class IntKeyReferenceValueMap : Reference
00395 {
00396 protected:
00397
00398 virtual void AddElement(VALUETYPE * Element);
00399
00400 virtual void RemoveElement(VALUETYPE * Element);
00401
00402 public:
00403
00404 virtual ArrayList<AutoDeleting> * GetElements() const;
00405 };
00406
00407 template <class VALUETYPE> class StringKeyReferenceValueMap : Reference
00408 {
00409 protected:
00410
00411 virtual void AddElement(VALUETYPE * Element);
00412
00413 virtual void RemoveElement(VALUETYPE * Element);
00414
00415 public:
00416
00417 virtual ArrayList<AutoDeleting> * GetElements() const;
00418 };
00419
00420 template <class VALUETYPE> class ICStringKeyReferenceValueMap : Reference
00421 {
00422 protected:
00423
00424 virtual void AddElement(VALUETYPE * Element);
00425
00426 virtual void RemoveElement(VALUETYPE * Element);
00427
00428 public:
00429
00430 virtual ArrayList<AutoDeleting> * GetElements() const;
00431 };
00432
00433 template <class KEYTYPE, class VALUETYPE> class ReferenceKeyMap : Reference
00434 {
00435 protected:
00436
00437 virtual void AddElement(KEYTYPE * Element);
00438
00439 virtual void RemoveElement(KEYTYPE * Element);
00440
00441 public:
00442
00443 virtual ArrayList<AutoDeleting> * GetElements() const;
00444 };
00445 }
00446
00447
00448 #endif