simEvents.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// Represents a queued event in the sim.
  34. ///
  35. /// Sim provides an event queue for your convenience, which
  36. /// can be used to schedule events. A few things which use
  37. /// this event queue:
  38. ///
  39. /// - The scene lighting system. In order to keep the game
  40. /// responsive while scene lighting occurs, the lighting
  41. /// process is divided into little chunks. In implementation
  42. /// terms, there is a subclass of SimEvent called
  43. /// SceneLightingProcessEvent. The process method of this
  44. /// subclass calls into the lighting code, telling it to
  45. /// perform the next chunk of lighting calculations.
  46. /// - The schedule() console function uses a subclass of
  47. /// SimEvent called SimConsoleEvent to keep track of
  48. /// scheduled events.
  49. class SimEvent
  50. {
  51. public:
  52. SimEvent *nextEvent; ///< Linked list details - pointer to next item in the list.
  53. SimTime startTime; ///< When the event was posted.
  54. SimTime time; ///< When the event is scheduled to occur.
  55. U32 sequenceCount; ///< Unique ID. These are assigned sequentially based on order
  56. /// of addition to the list.
  57. SimObject *destObject; ///< Object on which this event will be applied.
  58. SimEvent() { destObject = NULL; }
  59. virtual ~SimEvent() {} ///< Destructor
  60. ///
  61. /// A dummy virtual destructor is required
  62. /// so that subclasses can be deleted properly
  63. /// Function called when event occurs.
  64. ///
  65. /// This is where the meat of your event's implementation goes.
  66. ///
  67. /// See any of the subclasses for ideas of what goes in here.
  68. ///
  69. /// The event is deleted immediately after processing. If the
  70. /// object referenced in destObject is deleted, then the event
  71. /// is not called. The even will be executed unconditionally if
  72. /// the object referenced is NULL.
  73. ///
  74. /// @param object Object stored in destObject.
  75. virtual void process(SimObject *object)=0;
  76. };
  77. /// Implementation of schedule() function.
  78. ///
  79. /// This allows you to set a console function to be
  80. /// called at some point in the future.
  81. class SimConsoleEvent : public SimEvent
  82. {
  83. protected:
  84. S32 mArgc;
  85. char **mArgv;
  86. bool mOnObject;
  87. public:
  88. /// Constructor
  89. ///
  90. /// Pass the arguments of a function call, optionally on an object.
  91. ///
  92. /// The object for the call to be executed on is specified by setting
  93. /// onObject and storing a reference to the object in destObject. If
  94. /// onObject is false, you don't need to store anything into destObject.
  95. ///
  96. /// The parameters here are passed unmodified to Con::execute() at the
  97. /// time of the event.
  98. ///
  99. /// @see Con::execute(S32 argc, const char *argv[])
  100. /// @see Con::execute(SimObject *object, S32 argc, const char *argv[])
  101. SimConsoleEvent(S32 argc, const char **argv, bool onObject);
  102. ~SimConsoleEvent();
  103. virtual void process(SimObject *object);
  104. };
  105. /// Used by Con::threadSafeExecute()
  106. struct SimConsoleThreadExecCallback
  107. {
  108. Semaphore *sem;
  109. const char *retVal;
  110. SimConsoleThreadExecCallback();
  111. ~SimConsoleThreadExecCallback();
  112. void handleCallback(const char *ret);
  113. const char *waitForResult();
  114. };
  115. class SimConsoleThreadExecEvent : public SimConsoleEvent
  116. {
  117. SimConsoleThreadExecCallback *cb;
  118. public:
  119. SimConsoleThreadExecEvent(S32 argc, const char **argv, bool onObject, SimConsoleThreadExecCallback *callback);
  120. virtual void process(SimObject *object);
  121. };
  122. /// General purpose SimEvent which calls a Delegate<void()> callback.
  123. class SimDelegateEvent : public SimEvent
  124. {
  125. public:
  126. U32 *mEventId;
  127. Delegate<void()> mCallback;
  128. void process( SimObject* )
  129. {
  130. // Clear the event id and call the delegate.
  131. *mEventId = InvalidEventId;
  132. mCallback();
  133. }
  134. };
  135. #endif // _SIMEVENTS_H_