txList.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef TRANSFRMX_LIST_H
  6. #define TRANSFRMX_LIST_H
  7. #include "txCore.h"
  8. class txListIterator;
  9. /**
  10. * Represents an ordered list of Object pointers. Modeled after a Java 2 List.
  11. **/
  12. class txList : public txObject {
  13. friend class txListIterator;
  14. public:
  15. /**
  16. * Creates an empty txList
  17. **/
  18. txList();
  19. /**
  20. * txList destructor, object references will not be deleted.
  21. **/
  22. ~txList();
  23. /**
  24. * Returns the number of items in this txList
  25. **/
  26. int32_t getLength();
  27. /**
  28. * Returns true if there are no items in this txList
  29. */
  30. inline bool isEmpty()
  31. {
  32. return itemCount == 0;
  33. }
  34. /**
  35. * Adds the given Object to the list
  36. **/
  37. nsresult add(void* objPtr);
  38. /*
  39. * Removes all the objects from the list
  40. */
  41. void clear();
  42. protected:
  43. struct ListItem {
  44. ListItem* nextItem;
  45. ListItem* prevItem;
  46. void* objPtr;
  47. };
  48. /**
  49. * Removes the given ListItem pointer from the list
  50. **/
  51. ListItem* remove(ListItem* sItem);
  52. private:
  53. txList(const txList& aOther); // not implemented
  54. ListItem* firstItem;
  55. ListItem* lastItem;
  56. int32_t itemCount;
  57. nsresult insertAfter(void* objPtr, ListItem* sItem);
  58. nsresult insertBefore(void* objPtr, ListItem* sItem);
  59. };
  60. /**
  61. * An Iterator for the txList Class
  62. **/
  63. class txListIterator {
  64. public:
  65. /**
  66. * Creates a new txListIterator for the given txList
  67. * @param list, the txList to create an Iterator for
  68. **/
  69. explicit txListIterator(txList* list);
  70. /**
  71. * Adds the Object pointer to the txList pointed to by this txListIterator.
  72. * The Object pointer is inserted as the next item in the txList
  73. * based on the current position within the txList
  74. * @param objPtr the Object pointer to add to the list
  75. **/
  76. nsresult addAfter(void* objPtr);
  77. /**
  78. * Adds the Object pointer to the txList pointed to by this txListIterator.
  79. * The Object pointer is inserted as the previous item in the txList
  80. * based on the current position within the txList
  81. * @param objPtr the Object pointer to add to the list
  82. **/
  83. nsresult addBefore(void* objPtr);
  84. /**
  85. * Returns true if a successful call to the next() method can be made
  86. * @return true if a successful call to the next() method can be made,
  87. * otherwise false
  88. **/
  89. bool hasNext();
  90. /**
  91. * Returns the next Object pointer from the list
  92. **/
  93. void* next();
  94. /**
  95. * Returns the previous Object pointer from the list
  96. **/
  97. void* previous();
  98. /**
  99. * Returns the current Object
  100. **/
  101. void* current();
  102. /**
  103. * Removes the Object last returned by the next() or previous() methods;
  104. * @return the removed Object pointer
  105. **/
  106. void* remove();
  107. /**
  108. * Resets the current location within the txList to the beginning of the txList
  109. **/
  110. void reset();
  111. /**
  112. * Resets the current location within the txList to the end of the txList
  113. **/
  114. void resetToEnd();
  115. private:
  116. //-- points to the current list item
  117. txList::ListItem* currentItem;
  118. //-- points to the list to iterator over
  119. txList* list;
  120. //-- we've moved off the end of the list
  121. bool atEndOfList;
  122. };
  123. typedef txList List;
  124. #endif