ScriptNode.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. *
  22. * DESCRIPTION
  23. *
  24. * PROGRAMMER
  25. * Denzil E. Long, Jr.
  26. *
  27. * VERSION INFO
  28. * $Author: Denzil_l $
  29. * $Revision: 2 $
  30. * $Modtime: 3/29/00 2:20p $
  31. * $Archive: /Commando/Code/Scripts/ScriptNode.h $
  32. *
  33. ******************************************************************************/
  34. #ifndef _SCRIPTNODE_H_
  35. #define _SCRIPTNODE_H_
  36. #include "dprint.h"
  37. class ScriptNode
  38. {
  39. public:
  40. virtual ~ScriptNode() {}
  41. protected:
  42. friend class ScriptList;
  43. friend class ScriptListIter;
  44. // Retrieve next node
  45. inline ScriptNode* GetNext(void) const
  46. {
  47. return mNext;
  48. }
  49. // Set next node
  50. inline void SetNext(ScriptNode* link)
  51. {
  52. if (mNext != NULL) {
  53. assert(link != NULL);
  54. link->SetNext(mNext);
  55. }
  56. mNext = link;
  57. }
  58. // Can only be derived
  59. ScriptNode() : mNext(NULL) {}
  60. private:
  61. ScriptNode* mNext;
  62. };
  63. class ScriptList
  64. {
  65. public:
  66. ScriptList() : mHead(NULL) {DebugPrint("ScriptList\n");}
  67. virtual ~ScriptList() {}
  68. // Add a node to this list
  69. void AddNode(ScriptNode* node)
  70. {
  71. if ((node != NULL) && !HasNode(node)) {
  72. node->SetNext(mHead);
  73. mHead = node;
  74. }
  75. }
  76. // Remove a node from the list
  77. bool RemoveNode(ScriptNode* node)
  78. {
  79. ScriptNode* previous = NULL;
  80. ScriptNode* current = mHead;
  81. while (current != NULL) {
  82. ScriptNode* next = current->GetNext();
  83. if (current == node) {
  84. // Handle head of list condition
  85. if (previous == NULL) {
  86. mHead = next;
  87. } else {
  88. previous->SetNext(next);
  89. }
  90. return true;
  91. }
  92. // Advance to next node
  93. previous = current;
  94. current = next;
  95. }
  96. return false;
  97. }
  98. // Count the number of node in the list.
  99. int CountNodes(void) const
  100. {
  101. ScriptNode* node = mHead;
  102. int count = 0;
  103. while (node != NULL) {
  104. count++;
  105. node = node->GetNext();
  106. }
  107. return count;
  108. }
  109. // Check if a node exists in the list.
  110. bool HasNode(ScriptNode* node) const
  111. {
  112. ScriptNode* current = mHead;
  113. while (current != NULL) {
  114. if (current == node) {
  115. return true;
  116. }
  117. current = current->GetNext();
  118. }
  119. return false;
  120. }
  121. // Clear the list
  122. void Clear(void)
  123. {
  124. mHead = NULL;
  125. }
  126. private:
  127. friend class ScriptListIter;
  128. ScriptNode* mHead;
  129. };
  130. // Script list iterator: This class is used to traverse or iterate a list.
  131. class ScriptListIter
  132. {
  133. public:
  134. ScriptListIter(const ScriptList& list)
  135. : mList(&list), mCurrent(list.mHead)
  136. {}
  137. // Set iterator to first node
  138. inline ScriptNode* First(void)
  139. {
  140. mCurrent = mList->mHead;
  141. return mCurrent;
  142. }
  143. // Retrieve current node
  144. inline ScriptNode* Current(void)
  145. {
  146. return mCurrent;
  147. }
  148. // Advance to next node
  149. inline void Next(void)
  150. {
  151. mCurrent = mCurrent->GetNext();
  152. }
  153. // Test if at end of list
  154. inline bool AtEnd(void)
  155. {
  156. return (mCurrent->GetNext() == NULL);
  157. }
  158. private:
  159. const ScriptList* mList;
  160. ScriptNode* mCurrent;
  161. };
  162. #endif // _SCRIPTNODE_H_