simEvents.h

Engine/source/console/simEvents.h

More...

Classes:

class

Implementation of schedule() function.

class

Used by Con::threadSafeExecute()

class

General purpose SimEvent which calls a Delegate callback.

class

Represents a queued event in the sim.

Detailed Description

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