simEvents.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SIMEVENTS_H_
  23. #define _SIMEVENTS_H_
  24. #ifndef _SIM_H_
  25. #include "console/sim.h"
  26. #endif
  27. #ifndef _UTIL_DELEGATE_H_
  28. #include "core/util/delegate.h"
  29. #endif
  30. // Forward Refs
  31. class SimObject;
  32. class Semaphore;
  33. class ConsoleValue;
  34. /// Represents a queued event in the sim.
  35. ///
  36. /// Sim provides an event queue for your convenience, which
  37. /// can be used to schedule events. A few things which use
  38. /// this event queue:
  39. ///
  40. /// - The scene lighting system. In order to keep the game
  41. /// responsive while scene lighting occurs, the lighting
  42. /// process is divided into little chunks. In implementation
  43. /// terms, there is a subclass of SimEvent called
  44. /// SceneLightingProcessEvent. The process method of this
  45. /// subclass calls into the lighting code, telling it to
  46. /// perform the next chunk of lighting calculations.
  47. /// - The schedule() console function uses a subclass of
  48. /// SimEvent called SimConsoleEvent to keep track of
  49. /// scheduled events.
  50. class SimEvent
  51. {
  52. public:
  53. SimEvent *nextEvent; ///< Linked list details - pointer to next item in the list.
  54. SimTime startTime; ///< When the event was posted.
  55. SimTime time; ///< When the event is scheduled to occur.
  56. U32 sequenceCount; ///< Unique ID. These are assigned sequentially based on order
  57. /// of addition to the list.
  58. SimObject *destObject; ///< Object on which this event will be applied.
  59. SimEvent() { destObject = NULL; }
  60. virtual ~SimEvent() {} ///< Destructor
  61. ///
  62. /// A dummy virtual destructor is required
  63. /// so that subclasses can be deleted properly
  64. /// Function called when event occurs.
  65. ///
  66. /// This is where the meat of your event's implementation goes.
  67. ///
  68. /// See any of the subclasses for ideas of what goes in here.
  69. ///
  70. /// The event is deleted immediately after processing. If the
  71. /// object referenced in destObject is deleted, then the event
  72. /// is not called. The even will be executed unconditionally if
  73. /// the object referenced is NULL.
  74. ///
  75. /// @param object Object stored in destObject.
  76. virtual void process(SimObject *object)=0;
  77. };
  78. class ConsoleValueRef;
  79. /// Implementation of schedule() function.
  80. ///
  81. /// This allows you to set a console function to be
  82. /// called at some point in the future.
  83. class SimConsoleEvent : public SimEvent
  84. {
  85. protected:
  86. S32 mArgc;
  87. ConsoleValueRef *mArgv;
  88. bool mOnObject;
  89. public:
  90. /// Constructor
  91. ///
  92. /// Pass the arguments of a function call, optionally on an object.
  93. ///
  94. /// The object for the call to be executed on is specified by setting
  95. /// onObject and storing a reference to the object in destObject. If
  96. /// onObject is false, you don't need to store anything into destObject.
  97. ///
  98. /// The parameters here are passed unmodified to Con::execute() at the
  99. /// time of the event.
  100. ///
  101. /// @see Con::execute(S32 argc, const char *argv[])
  102. /// @see Con::execute(SimObject *object, S32 argc, const char *argv[])
  103. SimConsoleEvent(S32 argc, ConsoleValueRef *argv, bool onObject);
  104. ~SimConsoleEvent();
  105. virtual void process(SimObject *object);
  106. /// Creates a reference to our internal args list in argv
  107. void populateArgs(ConsoleValueRef *argv);
  108. };
  109. // NOTE: SimConsoleThreadExecCallback & SimConsoleThreadExecEvent moved to engineAPI.h
  110. /// Used by Con::threadSafeExecute()
  111. struct SimConsoleThreadExecCallback
  112. {
  113. Semaphore *sem;
  114. ConsoleValueRef retVal;
  115. SimConsoleThreadExecCallback();
  116. ~SimConsoleThreadExecCallback();
  117. void handleCallback(ConsoleValueRef ret);
  118. ConsoleValueRef waitForResult();
  119. };
  120. class SimConsoleThreadExecEvent : public SimConsoleEvent
  121. {
  122. SimConsoleThreadExecCallback *cb;
  123. public:
  124. SimConsoleThreadExecEvent(S32 argc, ConsoleValueRef *argv, bool onObject, SimConsoleThreadExecCallback *callback);
  125. SimConsoleThreadExecCallback& getCB() { return *cb; }
  126. virtual void process(SimObject *object);
  127. };
  128. /// General purpose SimEvent which calls a Delegate<void()> callback.
  129. class SimDelegateEvent : public SimEvent
  130. {
  131. public:
  132. U32 *mEventId;
  133. Delegate<void()> mCallback;
  134. void process( SimObject* )
  135. {
  136. // Clear the event id and call the delegate.
  137. *mEventId = InvalidEventId;
  138. mCallback();
  139. }
  140. };
  141. #endif // _SIMEVENTS_H_