00001
00002
00003
00004
00005
00006 #ifndef __WXLIST__
00007 #define __WXLIST__
00008
00009
00010 #ifndef __AFX_H__
00011 struct __POSITION { int unused; };
00012 typedef __POSITION* POSITION;
00013 #endif
00014
00015 const int DEFAULTCACHE = 10;
00016
00017 class CBaseList
00018 #ifdef DEBUG
00019 : public CBaseObject
00020 #endif
00021 {
00022
00023
00024 public:
00025
00026 #ifdef DEBUG
00027 class CNode : public CBaseObject {
00028 #else
00029 class CNode {
00030 #endif
00031
00032 CNode *m_pPrev;
00033 CNode *m_pNext;
00034 void *m_pObject;
00035
00036 public:
00037
00038
00039 CNode()
00040 #ifdef DEBUG
00041 : CBaseObject(NAME("List node"))
00042 #endif
00043 {
00044 };
00045
00046
00047 CNode *Prev() const { return m_pPrev; };
00048
00049
00050 CNode *Next() const { return m_pNext; };
00051
00052
00053 void SetPrev(CNode *p) { m_pPrev = p; };
00054
00055
00056 void SetNext(CNode *p) { m_pNext = p; };
00057
00058
00059 void *GetData() const { return m_pObject; };
00060
00061
00062 void SetData(void *p) { m_pObject = p; };
00063 };
00064
00065 class CNodeCache
00066 {
00067 public:
00068 CNodeCache(INT iCacheSize) : m_iCacheSize(iCacheSize),
00069 m_pHead(NULL),
00070 m_iUsed(0)
00071 {};
00072 ~CNodeCache() {
00073 CNode *pNode = m_pHead;
00074 while (pNode) {
00075 CNode *pCurrent = pNode;
00076 pNode = pNode->Next();
00077 delete pCurrent;
00078 }
00079 };
00080 void AddToCache(CNode *pNode)
00081 {
00082 if (m_iUsed < m_iCacheSize) {
00083 pNode->SetNext(m_pHead);
00084 m_pHead = pNode;
00085 m_iUsed++;
00086 } else {
00087 delete pNode;
00088 }
00089 };
00090 CNode *RemoveFromCache()
00091 {
00092 CNode *pNode = m_pHead;
00093 if (pNode != NULL) {
00094 m_pHead = pNode->Next();
00095 m_iUsed--;
00096 ASSERT(m_iUsed >= 0);
00097 } else {
00098 ASSERT(m_iUsed == 0);
00099 }
00100 return pNode;
00101 };
00102 private:
00103 INT m_iCacheSize;
00104 INT m_iUsed;
00105 CNode *m_pHead;
00106 };
00107
00108 protected:
00109
00110 CNode* m_pFirst;
00111 CNode* m_pLast;
00112 LONG m_Count;
00113
00114 private:
00115
00116 CNodeCache m_Cache;
00117
00118 private:
00119
00120
00121 CBaseList(const CBaseList &refList);
00122 CBaseList &operator=(const CBaseList &refList);
00123
00124 public:
00125
00126 CBaseList(TCHAR *pName,
00127 INT iItems);
00128
00129 CBaseList(TCHAR *pName);
00130 #ifdef UNICODE
00131 CBaseList(CHAR *pName,
00132 INT iItems);
00133
00134 CBaseList(CHAR *pName);
00135 #endif
00136 ~CBaseList();
00137
00138
00139 void RemoveAll();
00140
00141
00142 POSITION GetHeadPositionI() const;
00143
00144
00145 POSITION GetTailPositionI() const;
00146
00147
00148 int GetCountI() const;
00149
00150 protected:
00151
00152 void *GetNextI(POSITION& rp) const;
00153
00154
00155 void *GetI(POSITION p) const;
00156
00157 public:
00158
00159 POSITION Next(POSITION pos) const
00160 {
00161 if (pos == NULL) {
00162 return (POSITION) m_pFirst;
00163 }
00164 CNode *pn = (CNode *) pos;
00165 return (POSITION) pn->Next();
00166 }
00167
00168
00169 POSITION Prev(POSITION pos) const
00170 {
00171 if (pos == NULL) {
00172 return (POSITION) m_pLast;
00173 }
00174 CNode *pn = (CNode *) pos;
00175 return (POSITION) pn->Prev();
00176 }
00177
00178
00179 protected:
00180 POSITION FindI( void * pObj) const;
00181
00182
00183
00184
00185
00186
00187
00188 void *RemoveHeadI();
00189
00190
00191 void *RemoveTailI();
00192
00193
00194 void *RemoveI(POSITION p);
00195
00196
00197 POSITION AddTailI(void * pObj);
00198 public:
00199
00200
00201 BOOL AddTail(CBaseList *pList);
00202
00203
00204
00205
00206 protected:
00207 POSITION AddHeadI(void * pObj);
00208 public:
00209
00210
00211 BOOL AddHead(CBaseList *pList);
00212
00213
00214 protected:
00215 POSITION AddAfterI(POSITION p, void * pObj);
00216 public:
00217
00218
00219 BOOL AddAfter(POSITION p, CBaseList *pList);
00220
00221
00222 protected:
00223 POSITION AddBeforeI(POSITION p, void * pObj);
00224 public:
00225
00226
00227 BOOL AddBefore(POSITION p, CBaseList *pList);
00228
00229
00230
00231
00232
00233
00234 BOOL MoveToTail(POSITION pos, CBaseList *pList);
00235
00236
00237 BOOL MoveToHead(POSITION pos, CBaseList *pList);
00238
00239
00240 void Reverse();
00241
00242
00243 #define TRAVERSELIST(list, cursor) \
00244 for ( cursor = (list).GetHeadPosition() \
00245 ; cursor!=NULL \
00246 ; cursor = (list).Next(cursor) \
00247 )
00248
00249
00250 #define REVERSETRAVERSELIST(list, cursor) \
00251 for ( cursor = (list).GetTailPosition() \
00252 ; cursor!=NULL \
00253 ; cursor = (list).Prev(cursor) \
00254 )
00255
00256 };
00257
00258 template<class OBJECT> class CGenericList : public CBaseList
00259 {
00260 public:
00261 CGenericList(TCHAR *pName,
00262 INT iItems,
00263 BOOL bLock = TRUE,
00264 BOOL bAlert = FALSE) :
00265 CBaseList(pName, iItems) {
00266 UNREFERENCED_PARAMETER(bAlert);
00267 UNREFERENCED_PARAMETER(bLock);
00268 };
00269 CGenericList(TCHAR *pName) :
00270 CBaseList(pName) {
00271 };
00272
00273 POSITION GetHeadPosition() const { return (POSITION)m_pFirst; }
00274 POSITION GetTailPosition() const { return (POSITION)m_pLast; }
00275 int GetCount() const { return m_Count; }
00276
00277 OBJECT *GetNext(POSITION& rp) const { return (OBJECT *) GetNextI(rp); }
00278
00279 OBJECT *Get(POSITION p) const { return (OBJECT *) GetI(p); }
00280 OBJECT *GetHead() const { return Get(GetHeadPosition()); }
00281
00282 OBJECT *RemoveHead() { return (OBJECT *) RemoveHeadI(); }
00283
00284 OBJECT *RemoveTail() { return (OBJECT *) RemoveTailI(); }
00285
00286 OBJECT *Remove(POSITION p) { return (OBJECT *) RemoveI(p); }
00287 POSITION AddBefore(POSITION p, OBJECT * pObj) { return AddBeforeI(p, pObj); }
00288 POSITION AddAfter(POSITION p, OBJECT * pObj) { return AddAfterI(p, pObj); }
00289 POSITION AddHead(OBJECT * pObj) { return AddHeadI(pObj); }
00290 POSITION AddTail(OBJECT * pObj) { return AddTailI(pObj); }
00291 BOOL AddTail(CGenericList<OBJECT> *pList)
00292 { return CBaseList::AddTail((CBaseList *) pList); }
00293 BOOL AddHead(CGenericList<OBJECT> *pList)
00294 { return CBaseList::AddHead((CBaseList *) pList); }
00295 BOOL AddAfter(POSITION p, CGenericList<OBJECT> *pList)
00296 { return CBaseList::AddAfter(p, (CBaseList *) pList); };
00297 BOOL AddBefore(POSITION p, CGenericList<OBJECT> *pList)
00298 { return CBaseList::AddBefore(p, (CBaseList *) pList); };
00299 POSITION Find( OBJECT * pObj) const { return FindI(pObj); }
00300 };
00301
00302 typedef CGenericList<CBaseObject> CBaseObjectList;
00303 typedef CGenericList<IUnknown> CBaseInterfaceList;
00304
00305 #endif
00306