scheduler_vs.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN STANDARD TEMPLATE LIBRARY
  3. // (c) 2002 Activision
  4. //
  5. //
  6. // Scheduler
  7. // ---------
  8. // The scheduler is a common piece of game functionality. To use it, simply add events
  9. // at the given time, and call update() with the current time as frequently as you wish.
  10. //
  11. // Your event class MUST define a Fire() function which accepts a TCALLBACKPARAMS
  12. // parameter.
  13. //
  14. // NOTES:
  15. //
  16. //
  17. //
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #if !defined(RATL_SCHEDULER_VS_INC)
  20. #define RATL_SCHEDULER_VS_INC
  21. ////////////////////////////////////////////////////////////////////////////////////////
  22. // Includes
  23. ////////////////////////////////////////////////////////////////////////////////////////
  24. #if !defined(RATL_COMMON_INC)
  25. #include "ratl_common.h"
  26. #endif
  27. #if !defined(RATL_POOL_VS_INC)
  28. #include "pool_vs.h"
  29. #endif
  30. #if !defined(RATL_HEAP_VS_INC)
  31. #include "heap_vs.h"
  32. #endif
  33. namespace ratl
  34. {
  35. ////////////////////////////////////////////////////////////////////////////////////////
  36. // The Scheduler Class
  37. ////////////////////////////////////////////////////////////////////////////////////////
  38. template <class T>
  39. class scheduler_base : public ratl_base
  40. {
  41. public:
  42. typedef typename T TStorageTraits;
  43. typedef typename T::TValue TTValue;
  44. ////////////////////////////////////////////////////////////////////////////////////
  45. // Capacity Enum
  46. ////////////////////////////////////////////////////////////////////////////////////
  47. enum
  48. {
  49. CAPACITY = T::CAPACITY
  50. };
  51. private:
  52. ////////////////////////////////////////////////////////////////////////////////////
  53. // The Timed Event Class
  54. //
  55. // This class stores two numbers, a timer and an iterator to the events list. We
  56. // don't store the event directly in the heap to make the swap operation in the
  57. // heap faster. We define a less than operator so we can sort in the heap.
  58. //
  59. ////////////////////////////////////////////////////////////////////////////////////
  60. struct timed_event
  61. {
  62. float mTime;
  63. int mEvent;
  64. timed_event() {}
  65. timed_event(float time, int event) : mTime(time), mEvent(event) {}
  66. bool operator< (const timed_event& t) const
  67. {
  68. return (mTime > t.mTime);
  69. }
  70. };
  71. pool_base<TStorageTraits> mEvents;
  72. heap_vs<timed_event, CAPACITY> mHeap;
  73. public:
  74. ////////////////////////////////////////////////////////////////////////////////////
  75. // How Many Objects Are In This List
  76. ////////////////////////////////////////////////////////////////////////////////////
  77. int size() const
  78. {
  79. // warning during a fire call, there will be one extra event
  80. return mEvents.size();
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////////
  83. // Are There Any Objects In This List?
  84. ////////////////////////////////////////////////////////////////////////////////////
  85. bool empty() const
  86. {
  87. return !size();
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////
  90. // Is This List Filled?
  91. ////////////////////////////////////////////////////////////////////////////////////
  92. bool full() const
  93. {
  94. return mEvents.full();
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////////
  97. // Clear All Elements
  98. ////////////////////////////////////////////////////////////////////////////////////
  99. void clear()
  100. {
  101. mEvents.clear();
  102. mHeap.clear();
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////////
  105. // Add An Event
  106. ////////////////////////////////////////////////////////////////////////////////////
  107. void add(float time, const TTValue& e)
  108. {
  109. int nLoc = mEvents.alloc(e);
  110. mHeap.push(timed_event(time, nLoc));
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////////
  113. // Add An Event
  114. ////////////////////////////////////////////////////////////////////////////////////
  115. TTValue & add(float time)
  116. {
  117. int nLoc = mEvents.alloc();
  118. mHeap.push(timed_event(time, nLoc));
  119. return mEvents[nLoc];
  120. }
  121. ////////////////////////////////////////////////////////////////////////////////////
  122. // Add A Raw Event for placement new
  123. ////////////////////////////////////////////////////////////////////////////////////
  124. TRatlNew * add_raw(float time)
  125. {
  126. TRatlNew *ret = mEvents.alloc_raw();
  127. mHeap.push(timed_event(time, mEvents.pointer_to_index(ret)));
  128. return ret;
  129. }
  130. template<class TCALLBACKPARAMS>
  131. void update(float time, TCALLBACKPARAMS& Params)
  132. {
  133. while (!mHeap.empty())
  134. {
  135. timed_event Next = mHeap.top();
  136. if (Next.mTime>=time)
  137. {
  138. break;
  139. }
  140. mHeap.pop();
  141. mEvents[Next.mEvent].Fire(Params);
  142. mEvents.free(Next.mEvent);
  143. }
  144. }
  145. void update(float time)
  146. {
  147. while (!mHeap.empty())
  148. {
  149. timed_event Next = mHeap.top();
  150. if (Next.mTime>=time)
  151. {
  152. break;
  153. }
  154. mHeap.pop();
  155. mEvents[Next.mEvent].Fire();
  156. mEvents.free(Next.mEvent);
  157. }
  158. }
  159. };
  160. template<class T, int ARG_CAPACITY>
  161. class scheduler_vs : public scheduler_base<storage::value_semantics<T,ARG_CAPACITY> >
  162. {
  163. public:
  164. typedef typename storage::value_semantics<T,ARG_CAPACITY> TStorageTraits;
  165. typedef typename TStorageTraits::TValue TTValue;
  166. enum
  167. {
  168. CAPACITY = ARG_CAPACITY
  169. };
  170. scheduler_vs() {}
  171. };
  172. template<class T, int ARG_CAPACITY>
  173. class scheduler_os : public scheduler_base<storage::object_semantics<T,ARG_CAPACITY> >
  174. {
  175. public:
  176. typedef typename storage::object_semantics<T,ARG_CAPACITY> TStorageTraits;
  177. typedef typename TStorageTraits::TValue TTValue;
  178. enum
  179. {
  180. CAPACITY = ARG_CAPACITY
  181. };
  182. scheduler_os() {}
  183. };
  184. template<class T, int ARG_CAPACITY, int ARG_MAX_CLASS_SIZE>
  185. class scheduler_is : public scheduler_base<storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> >
  186. {
  187. public:
  188. typedef typename storage::virtual_semantics<T,ARG_CAPACITY,ARG_MAX_CLASS_SIZE> TStorageTraits;
  189. typedef typename TStorageTraits::TValue TTValue;
  190. enum
  191. {
  192. CAPACITY = ARG_CAPACITY,
  193. MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE
  194. };
  195. scheduler_is() {}
  196. };
  197. }
  198. #endif