IdBank.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // IdBank.H
  19. //
  20. // History:
  21. // 01/29/97 JMI Started.
  22. //
  23. // 02/24/97 JMI Changed Add() to Insert() and created new Add() that
  24. // adds at the end.
  25. // Also, removed m_u16HeadUsedId. It wasn't useful.
  26. //
  27. //////////////////////////////////////////////////////////////////////////////
  28. //
  29. // This API dispenses unique IDs to all those who request such until all the
  30. // IDs are exhausted. IDs must be released when done.
  31. // Once an object gets an ID (which causes it to be registered in the ID
  32. // lookup table), it can be referenced by that ID.
  33. //
  34. //////////////////////////////////////////////////////////////////////////////
  35. #ifndef IDBANK_H
  36. #define IDBANK_H
  37. //////////////////////////////////////////////////////////////////////////////
  38. // C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
  39. //////////////////////////////////////////////////////////////////////////////
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // RSPiX Headers.
  42. // If PATHS_IN_INCLUDES macro is defined, we can utilize relative
  43. // paths to a header file. In this case we generally go off of our
  44. // RSPiX root directory. System.h MUST be included before this macro
  45. // is evaluated. System.h is the header that, based on the current
  46. // platform (or more so in this case on the compiler), defines
  47. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  48. // instead.
  49. ///////////////////////////////////////////////////////////////////////////////
  50. #include "System.h"
  51. #ifdef PATHS_IN_INCLUDES
  52. #else
  53. #endif
  54. //////////////////////////////////////////////////////////////////////////////
  55. // Postal Headers.
  56. //////////////////////////////////////////////////////////////////////////////
  57. #include "thing.h"
  58. //////////////////////////////////////////////////////////////////////////////
  59. // Macros.
  60. //////////////////////////////////////////////////////////////////////////////
  61. //////////////////////////////////////////////////////////////////////////////
  62. // Protos.
  63. //////////////////////////////////////////////////////////////////////////////
  64. //////////////////////////////////////////////////////////////////////////////
  65. // Typedefs.
  66. //////////////////////////////////////////////////////////////////////////////
  67. class CIdBank
  68. {
  69. //////////////////////////////////////////////////////////////////////////////
  70. public: // Macros.
  71. //////////////////////////////////////////////////////////////////////////////
  72. enum
  73. {
  74. NumIds = 10000,
  75. IdNil = 0xFFFF
  76. };
  77. //////////////////////////////////////////////////////////////////////////////
  78. public: // Typedefs.
  79. //////////////////////////////////////////////////////////////////////////////
  80. typedef struct
  81. {
  82. CThing* pthing; // CThing associate with this ID.
  83. U16 u16IdNext; // Next ID in used or free list.
  84. U16 u16IdPrev; // Prev ID in used or free list.
  85. } IdNode;
  86. //////////////////////////////////////////////////////////////////////////////
  87. public: // Con/Destruction.
  88. //////////////////////////////////////////////////////////////////////////////
  89. // Default constructor.
  90. CIdBank()
  91. {
  92. Reset();
  93. }
  94. // Destructor.
  95. ~CIdBank()
  96. {
  97. }
  98. //////////////////////////////////////////////////////////////////////////////
  99. public: // Public calls.
  100. //////////////////////////////////////////////////////////////////////////////
  101. // Resets all IDs to free. Called by constructor.
  102. void Reset(void);
  103. // Get a unique ID and associate it with a thing (CThing, that is).
  104. short Get( // Returns 0 on success.
  105. CThing* pthing, // In: Thing that wants to get an ID and be put in
  106. // the ID table.
  107. U16* pu16ID); // Out: ID for this particular CThing.
  108. // Take a unique ID and associate it with a thing (CThing).
  109. short Take( // Returns 0 on success.
  110. CThing* pthing, // In: Thing that wants to take an ID and be put in
  111. // the ID table.
  112. U16 u16ID); // In: ID for this particular CThing.
  113. // Release ID and unregister thing associated with it.
  114. void Release( // Returns nothing.
  115. U16 u16ID); // ID to release.
  116. // Get a CThing via its ID.
  117. short GetThingByID( // Returns 0 on success.
  118. CThing** ppthing, // Out: Ptr to CThing identified by u16ID.
  119. U16 u16ID); // In: ID of thing to get.
  120. //////////////////////////////////////////////////////////////////////////////
  121. protected: // Internal calls.
  122. //////////////////////////////////////////////////////////////////////////////
  123. // Helper to insert an ID into a particular list.
  124. void Insert( // Returns nothing.
  125. U16 u16Id, // ID to insert.
  126. U16* pu16IdHead); // Head of list to add to.
  127. // Helper to add an ID to a particular list.
  128. void Add( // Returns nothing.
  129. U16 u16Id, // ID to add.
  130. U16* pu16IdTail); // Tail of list to add to.
  131. // Helper to remove an ID from a particular list.
  132. void Remove( // Returns nothing.
  133. U16 u16Id, // ID to remove.
  134. U16* pu16IdHead, // Head of list to remove from.
  135. U16* pu16IdTail); // Tail of list to remove from.
  136. //////////////////////////////////////////////////////////////////////////////
  137. protected: // Internal variables.
  138. //////////////////////////////////////////////////////////////////////////////
  139. // Bank of ID nodes.
  140. IdNode m_aids[NumIds];
  141. // Head of Free IDs.
  142. U16 m_u16HeadFreeId;
  143. // Tail of Free IDs.
  144. U16 m_u16TailFreeId;
  145. };
  146. #endif // IDBANK_H
  147. //////////////////////////////////////////////////////////////////////////////
  148. // EOF
  149. //////////////////////////////////////////////////////////////////////////////