simEvents.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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() { nextEvent = NULL; startTime = 0; time = 0; sequenceCount = 0; 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. /// Implementation of schedule() function.
  79. ///
  80. /// This allows you to set a console function to be
  81. /// called at some point in the future.
  82. class SimConsoleEvent : public SimEvent
  83. {
  84. protected:
  85. S32 mArgc;
  86. ConsoleValue *mArgv;
  87. bool mOnObject;
  88. public:
  89. /// Constructor
  90. ///
  91. /// Pass the arguments of a function call, optionally on an object.
  92. ///
  93. /// The object for the call to be executed on is specified by setting
  94. /// onObject and storing a reference to the object in destObject. If
  95. /// onObject is false, you don't need to store anything into destObject.
  96. ///
  97. /// The parameters here are passed unmodified to Con::execute() at the
  98. /// time of the event.
  99. ///
  100. /// @see Con::execute(S32 argc, const char *argv[])
  101. /// @see Con::execute(SimObject *object, S32 argc, const char *argv[])
  102. SimConsoleEvent(S32 argc, ConsoleValue *argv, bool onObject);
  103. ~SimConsoleEvent();
  104. virtual void process(SimObject *object);
  105. /// Creates a reference to our internal args list in argv
  106. void populateArgs(ConsoleValue *argv);
  107. };
  108. // NOTE: SimConsoleThreadExecCallback & SimConsoleThreadExecEvent moved to engineAPI.h
  109. /// Used by Con::threadSafeExecute()
  110. struct SimConsoleThreadExecCallback
  111. {
  112. Semaphore *sem;
  113. ConsoleValue retVal;
  114. SimConsoleThreadExecCallback();
  115. ~SimConsoleThreadExecCallback();
  116. void handleCallback(ConsoleValue ret);
  117. ConsoleValue waitForResult();
  118. };
  119. class SimConsoleThreadExecEvent : public SimConsoleEvent
  120. {
  121. SimConsoleThreadExecCallback *cb;
  122. public:
  123. SimConsoleThreadExecEvent(S32 argc, ConsoleValue *argv, bool onObject, SimConsoleThreadExecCallback *callback);
  124. SimConsoleThreadExecCallback& getCB() { return *cb; }
  125. virtual void process(SimObject *object);
  126. };
  127. /// General purpose SimEvent which calls a Delegate<void()> callback.
  128. class SimDelegateEvent : public SimEvent
  129. {
  130. public:
  131. U32 *mEventId;
  132. Delegate<void()> mCallback;
  133. void process( SimObject* )
  134. {
  135. // Clear the event id and call the delegate.
  136. *mEventId = InvalidEventId;
  137. mCallback();
  138. }
  139. };
  140. #endif // _SIMEVENTS_H_