queue_vs.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN STANDARD TEMPLATE LIBRARY
  3. // (c) 2002 Activision
  4. //
  5. //
  6. // Queue Template
  7. // --------------
  8. // The queue is a circular buffer of objects which supports a push at the "front" and a
  9. // pop at the "back". Therefore it is:
  10. //
  11. // First In, First Out
  12. //
  13. // As the pointers to the push and pop locations are changed it wrapps around the end
  14. // of the array and back to the front. There are asserts to make sure it never goes
  15. // beyond the capacity of the object.
  16. //
  17. //
  18. //
  19. // NOTES:
  20. //
  21. //
  22. //
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24. #if !defined(RATL_QUEUE_VS_INC)
  25. #define RATL_QUEUE_VS_INC
  26. ////////////////////////////////////////////////////////////////////////////////////////
  27. // Includes
  28. ////////////////////////////////////////////////////////////////////////////////////////
  29. #if !defined(RATL_COMMON_INC)
  30. #include "ratl_common.h"
  31. #endif
  32. namespace ratl
  33. {
  34. ////////////////////////////////////////////////////////////////////////////////////////
  35. // The Queue Class
  36. ////////////////////////////////////////////////////////////////////////////////////////
  37. template <class T>
  38. class queue_base : public ratl_base
  39. {
  40. public:
  41. typedef typename T TStorageTraits;
  42. typedef typename T::TValue TTValue;
  43. ////////////////////////////////////////////////////////////////////////////////////
  44. // Capacity Enum
  45. ////////////////////////////////////////////////////////////////////////////////////
  46. enum
  47. {
  48. CAPACITY = T::CAPACITY
  49. };
  50. private:
  51. ////////////////////////////////////////////////////////////////////////////////////
  52. // Data
  53. ////////////////////////////////////////////////////////////////////////////////////
  54. array_base<TStorageTraits> mData; // The Memory
  55. int mPush; // Address Of Next Add Location
  56. int mPop; // Address Of Next Remove Location
  57. int mSize;
  58. int push_low()
  59. {
  60. assert(size()<CAPACITY);
  61. // Add It
  62. //--------
  63. mPush++;
  64. mSize++;
  65. // Update Push Location
  66. //----------------------
  67. if (mPush>=CAPACITY)
  68. {
  69. mPush=0;
  70. return CAPACITY-1;
  71. }
  72. return mPush-1;
  73. }
  74. public:
  75. typedef T TStorageTraits;
  76. ////////////////////////////////////////////////////////////////////////////////////
  77. // Constructor
  78. ////////////////////////////////////////////////////////////////////////////////////
  79. queue_base() : mPush(0), mPop(0), mSize(0)
  80. {
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////////
  83. // Get The Size (The Difference Between The Push And Pop "Pointers")
  84. ////////////////////////////////////////////////////////////////////////////////////
  85. int size() const
  86. {
  87. return mSize;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////
  90. // Check To See If The Size Is Zero
  91. ////////////////////////////////////////////////////////////////////////////////////
  92. bool empty() const
  93. {
  94. return !mSize;
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////////
  97. // Check To See If The Size Is Full
  98. ////////////////////////////////////////////////////////////////////////////////////
  99. bool full() const
  100. {
  101. return mSize>=CAPACITY;
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////////
  104. // Empty Out The Entire Queue
  105. ////////////////////////////////////////////////////////////////////////////////////
  106. void clear()
  107. {
  108. mPush = 0;
  109. mPop = 0;
  110. mSize = 0;
  111. mData.clear();
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////////
  114. // Add A Value, returns a reference to the value in place
  115. ////////////////////////////////////////////////////////////////////////////////////
  116. TTValue & push()
  117. {
  118. int idx=push_low();
  119. mData.construct(idx);
  120. return mData[idx];
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////////
  123. // Add A Value to the Queue
  124. ////////////////////////////////////////////////////////////////////////////////////
  125. void push(const TTValue& v)
  126. {
  127. mData.construct(push_low(),v);
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////////
  130. // Add A Value to the Queue, returning a void * to the memory
  131. ////////////////////////////////////////////////////////////////////////////////////
  132. TRatlNew *push_raw()
  133. {
  134. return mData.alloc_raw(push_low());
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////////
  137. // Remove A Value From The Queue
  138. ////////////////////////////////////////////////////////////////////////////////////
  139. void pop()
  140. {
  141. assert(size()>0);
  142. mData.destruct(mPop);
  143. // Update Pop Location
  144. //---------------------
  145. mPop++;
  146. if (mPop>=CAPACITY)
  147. {
  148. mPop=0;
  149. }
  150. mSize--;
  151. }
  152. TTValue & top()
  153. {
  154. assert(size()>0);
  155. return mData[mPop];
  156. }
  157. const TTValue & top() const
  158. {
  159. assert(size()>0);
  160. return mData[mPop];
  161. }
  162. template<class CAST_TO>
  163. CAST_TO *verify_alloc(CAST_TO *p) const
  164. {
  165. return mData.verify_alloc(p);
  166. }
  167. };
  168. template<class T, int ARG_CAPACITY>
  169. class queue_vs : public queue_base<storage::value_semantics<T,ARG_CAPACITY> >
  170. {
  171. public:
  172. typedef typename storage::value_semantics<T,ARG_CAPACITY> TStorageTraits;
  173. typedef typename TStorageTraits::TValue TTValue;
  174. enum
  175. {
  176. CAPACITY = ARG_CAPACITY
  177. };
  178. queue_vs() {}
  179. };
  180. template<class T, int ARG_CAPACITY>
  181. class queue_os : public queue_base<storage::object_semantics<T,ARG_CAPACITY> >
  182. {
  183. public:
  184. typedef typename storage::object_semantics<T,ARG_CAPACITY> TStorageTraits;
  185. typedef typename TStorageTraits::TValue TTValue;
  186. enum
  187. {
  188. CAPACITY = ARG_CAPACITY
  189. };
  190. queue_os() {}
  191. };
  192. template<class T, int ARG_CAPACITY, int ARG_MAX_CLASS_SIZE>
  193. class queue_is : public queue_base<storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> >
  194. {
  195. public:
  196. typedef typename storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> TStorageTraits;
  197. typedef typename TStorageTraits::TValue TTValue;
  198. enum
  199. {
  200. CAPACITY = ARG_CAPACITY,
  201. MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
  202. };
  203. queue_is() {}
  204. };
  205. }
  206. #endif