Gaffer Docs

EventMgr

Now that we've discussed Events, and registering events in the EventFactory, we can discuss EventMgr, which is responsible for handling events at runtime and passing them to all registered listeners.

// Anyone wanting to get callbacks for events must pass in a
// compatible function object.
typedef boost::function EventHandler_t;

class EventMgr
{
public:
    EventMgr();
    ~EventMgr();

    void serializeEvent(const Event * event, std::ostream &os);
    Event * deserializeEvent(std::istream &is);

    // When registering, EventMgr now takes ownership of handler lifetime
    void addHandler(EventType_t type, void * owner,
                    EventHandler_t handler);

    void removeHandler(EventType_t type, void * owner);

    // Add handlers to every event type
    void addGlobalHandler(void * owner, EventHandler_t handler,
                          EventFlags_t inclusionMask = EVENT_FLAG_ALL,
                          EventFlags_t exclusionMask = EVENT_FLAG_NONE);

    // Remove handlers from every event type
    void removeGlobalHandler(void * owner);

    // Queue event for processing on all listeners.
    // EventMgr takes ownership of event pointer.  Client shouldn't delete it.
    // Client shouldn't count on it existing after this call.
    void queueEvent(Event * event);

    // Immediately call FireEvent on all listeners.
    // EventMgr takes ownership of event pointer.  Client shouldn't delete it.
    // Client shouldn't count on it existing after this call.
    void fireEvent(Event * event);

    // Process any queued events
    void fireQueuedEvents();
    
private:
    // We store handlers associated with their owners.
    // The void * is typically the address of the owning object.
    // Along with the handler, we also store the exclusionMask flags
    // for events to be skipped.
    struct HandlerInfo
    {
        EventHandler_t handler;
        EventFlags_t inclFlags;
        EventFlags_t exclFlags;

        HandlerInfo(EventHandler_t handler,
                    EventFlags_t inclFlags,
                    EventFlags_t exclFlags)
          : handler(handler),
            inclFlags(inclFlags),
            exclFlags(exclFlags) {}
    };

    typedef std::vector HandlersTableRedirect_t;
    typedef std::map HandlerMap_t;
    typedef std::vector HandlersTable_t;
    typedef std::vector HandlersTableVector_t;


    typedef std::list EventQueue_t;
    
    // Call all handler functions in the list for this event
    bool fireAllHandlers(Event * event, HandlerMap_t & handlerMap);

    HandlerMap_t & findHandlerMap(EventType_t type);

    // Our event queue
    EventQueue_t queue_;

    // For each client id, refers to a handlers table for that client
    HandlersTableVector_t handlersTableVector_;

    HandlerMap_t globalHandlerMap_;
};