baselistcounter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /****************************************************************************
  2. ;
  3. ; MODULE: BASELISTCOUNTER (.H)
  4. ;
  5. ; PURPOSE:
  6. ;
  7. ; HISTORY:
  8. ;
  9. ; NOTICE: Copyright (c) 1998, MONOLITH, Inc.
  10. ;
  11. ****************************************************************************/
  12. #ifndef BASELISTCOUNTER_H
  13. #define BASELISTCOUNTER_H
  14. #include "lithtypes.h"
  15. #include "baselist.h"
  16. #include "limits.h"
  17. // User must derive all list elements from this class.
  18. class CBaseListCounterItem : public CBaseListItem {
  19. public:
  20. CBaseListCounterItem* Next() { return (CBaseListCounterItem*)CBaseListItem::Next(); }; // Returns the next element in the list after this one
  21. CBaseListCounterItem* Prev() { return (CBaseListCounterItem*)CBaseListItem::Prev(); }; // Returns the previous element in the list before this one
  22. public:
  23. friend class CBaseListCounter;
  24. protected:
  25. };
  26. // User can derive new list classes from this class, or use it as is.
  27. class CBaseListCounter : public CBaseList {
  28. public:
  29. // Constructors and destructors
  30. CBaseListCounter() { m_nCount = 0; CBaseList::CBaseList(); };
  31. ~CBaseListCounter() { };
  32. // member insert and delete functions
  33. void Insert(CBaseListItem* pItem) { InsertFirst(pItem); }; // Same as InsertFirst
  34. void InsertFirst(CBaseListItem* pItem); // Inserts item at the start of the list
  35. void InsertLast(CBaseListItem* pItem); // Inserts item at the end of the list
  36. void InsertAfter(CBaseListItem* pBeforeItem, CBaseListItem* pNewItem); // Inserts item after pBeforeItem in the list (if pBeforeItem is NULL puts at start of list)
  37. void InsertBefore(CBaseListItem* pAfterItem, CBaseListItem* pNewItem); // Inserts item before pAfterItem in the list (if pAfterItem is NULL puts at end of list)
  38. void Delete(CBaseListItem* pItem); // Removes the given item from the list
  39. // member access functions
  40. CBaseListCounterItem* GetFirst() { return (CBaseListCounterItem*)CBaseList::GetFirst(); }; // Returns the first element in the list (NULL if list is empty)
  41. CBaseListCounterItem* GetLast() { return (CBaseListCounterItem*)CBaseList::GetLast(); }; // Returns the last element in the list (NULL if list is empty)
  42. // this function will reinitialize the list first and last pointer making the list seem empty without deleting the items
  43. // this does not clean up the item pointers so this should only be used when the items in the list will never be used again
  44. void FastDeleteAll() { m_nCount = 0; CBaseList::FastDeleteAll(); };
  45. // counter functions
  46. unsigned int GetNumItems() { return m_nCount; };
  47. BOOL IsEmpty() { return (m_nCount == 0); };
  48. protected:
  49. // internal member variables
  50. unsigned int m_nCount;
  51. };
  52. inline void CBaseListCounter::InsertFirst(CBaseListItem* pItem)
  53. {
  54. ASSERT(m_nCount < UINT_MAX);
  55. m_nCount++;
  56. CBaseList::InsertFirst(pItem);
  57. };
  58. inline void CBaseListCounter::InsertLast(CBaseListItem* pItem)
  59. {
  60. ASSERT(m_nCount < UINT_MAX);
  61. m_nCount++;
  62. CBaseList::InsertLast(pItem);
  63. };
  64. inline void CBaseListCounter::InsertAfter(CBaseListItem* pBeforeItem, CBaseListItem* pNewItem)
  65. {
  66. ASSERT(m_nCount < UINT_MAX);
  67. m_nCount++;
  68. CBaseList::InsertAfter(pBeforeItem, pNewItem);
  69. };
  70. inline void CBaseListCounter::InsertBefore(CBaseListItem* pAfterItem, CBaseListItem* pNewItem)
  71. {
  72. ASSERT(m_nCount < UINT_MAX);
  73. m_nCount++;
  74. CBaseList::InsertBefore(pAfterItem, pNewItem);
  75. };
  76. inline void CBaseListCounter::Delete(CBaseListItem* pItem)
  77. {
  78. ASSERT(m_nCount > 0);
  79. m_nCount--;
  80. CBaseList::Delete(pItem);
  81. };
  82. #endif