stack_vs.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN STANDARD TEMPLATE LIBRARY
  3. // (c) 2002 Activision
  4. //
  5. //
  6. // Stack
  7. // -----
  8. // This is a very simple wrapper around a stack object.
  9. //
  10. // First In, Last Out
  11. //
  12. //
  13. //
  14. // NOTES:
  15. //
  16. //
  17. //
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #if !defined(RATL_STACK_VS_INC)
  20. #define RATL_STACK_VS_INC
  21. ////////////////////////////////////////////////////////////////////////////////////////
  22. // Includes
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24. #if !defined(RATL_COMMON_INC)
  25. #include "ratl_common.h"
  26. #endif
  27. namespace ratl
  28. {
  29. ////////////////////////////////////////////////////////////////////////////////////////
  30. // The stack Class
  31. ////////////////////////////////////////////////////////////////////////////////////////
  32. template <class T>
  33. class stack_base : public ratl_base
  34. {
  35. public:
  36. typedef typename T TStorageTraits;
  37. typedef typename T::TValue TTValue;
  38. ////////////////////////////////////////////////////////////////////////////////////
  39. // Capacity Enum
  40. ////////////////////////////////////////////////////////////////////////////////////
  41. enum
  42. {
  43. CAPACITY = T::CAPACITY
  44. };
  45. private:
  46. ////////////////////////////////////////////////////////////////////////////////////
  47. // Data
  48. ////////////////////////////////////////////////////////////////////////////////////
  49. array_base<TStorageTraits> mData; // The Memory
  50. int mSize;
  51. public:
  52. ////////////////////////////////////////////////////////////////////////////////////
  53. // Constructor
  54. ////////////////////////////////////////////////////////////////////////////////////
  55. stack_base() : mSize(0)
  56. {
  57. }
  58. ////////////////////////////////////////////////////////////////////////////////////
  59. // Get The Size (The Difference Between The Push And Pop "Pointers")
  60. ////////////////////////////////////////////////////////////////////////////////////
  61. int size() const
  62. {
  63. return mSize;
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////////
  66. // Check To See If The Size Is Zero
  67. ////////////////////////////////////////////////////////////////////////////////////
  68. bool empty() const
  69. {
  70. return !mSize;
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////
  73. // Check To See If The Size Is Full
  74. ////////////////////////////////////////////////////////////////////////////////////
  75. bool full() const
  76. {
  77. return mSize>=CAPACITY;
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////
  80. // Empty Out The Entire stack
  81. ////////////////////////////////////////////////////////////////////////////////////
  82. void clear()
  83. {
  84. mSize = 0;
  85. mData.clear();
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////////
  88. // Add A Value, returns a reference to the value in place
  89. ////////////////////////////////////////////////////////////////////////////////////
  90. TTValue & push()
  91. {
  92. assert(!full());
  93. mData.construct(mSize);
  94. mSize++;
  95. return mData[mSize-1];
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////////
  98. // Add A Value to the stack
  99. ////////////////////////////////////////////////////////////////////////////////////
  100. void push(const TTValue& v)
  101. {
  102. assert(!full());
  103. mData.construct(mSize,v);
  104. mSize++;
  105. }
  106. ////////////////////////////////////////////////////////////////////////////////////
  107. // Add A Value to the stack, returning a void * to the memory
  108. ////////////////////////////////////////////////////////////////////////////////////
  109. TRatlNew *push_raw()
  110. {
  111. assert(!full());
  112. mSize++;
  113. return mData.alloc_raw(mSize-1);
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////////
  116. // Remove A Value From The stack
  117. ////////////////////////////////////////////////////////////////////////////////////
  118. void pop()
  119. {
  120. assert(!empty());
  121. mSize--;
  122. mData.destruct(mSize);
  123. }
  124. TTValue & top()
  125. {
  126. assert(!empty());
  127. return mData[mSize-1];
  128. }
  129. const TTValue & top() const
  130. {
  131. assert(!empty());
  132. return mData[mSize-1];
  133. }
  134. template<class CAST_TO>
  135. CAST_TO *verify_alloc(CAST_TO *p) const
  136. {
  137. return mData.verify_alloc(p);
  138. }
  139. };
  140. template<class T, int ARG_CAPACITY>
  141. class stack_vs : public stack_base<storage::value_semantics<T,ARG_CAPACITY> >
  142. {
  143. public:
  144. typedef typename storage::value_semantics<T,ARG_CAPACITY> TStorageTraits;
  145. typedef typename TStorageTraits::TValue TTValue;
  146. enum
  147. {
  148. CAPACITY = ARG_CAPACITY
  149. };
  150. stack_vs() {}
  151. };
  152. template<class T, int ARG_CAPACITY>
  153. class stack_os : public stack_base<storage::object_semantics<T,ARG_CAPACITY> >
  154. {
  155. public:
  156. typedef typename storage::object_semantics<T,ARG_CAPACITY> TStorageTraits;
  157. typedef typename TStorageTraits::TValue TTValue;
  158. enum
  159. {
  160. CAPACITY = ARG_CAPACITY
  161. };
  162. stack_os() {}
  163. };
  164. template<class T, int ARG_CAPACITY, int ARG_MAX_CLASS_SIZE>
  165. class stack_is : public stack_base<storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> >
  166. {
  167. public:
  168. typedef typename storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> TStorageTraits;
  169. typedef typename TStorageTraits::TValue TTValue;
  170. enum
  171. {
  172. CAPACITY = ARG_CAPACITY,
  173. MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
  174. };
  175. stack_is() {}
  176. };
  177. }
  178. #endif