I have got a very small program which starts a thread and dispatches events to the registered set of handlers. Handlers class look as follows.
class Handler
{
public:
/* OnEvent must return void. This is part of the user interface agreement */
virtual void OnEvent(struct Event*) = 0;
virtual ~Handler();
};
Handlers are kept inside the vector
std::vector<Handler*> handlers;
I can add a handler to the vector using register function and delete a handler from the vector using unregister function. Handlers can either be added or removed within the OnEvent function.
Prototypes are below
void registerHandler(Handler *h);
void unregisterHandler(Handler *h);
Thread function looks as follows. It will block on event and once event is received it will dispatch this event to every handler which is in the vector
void thread_function()
{
struct Event* e = getEvent();
/* Function will receive an event and will invoke handlers one by one */
for(auto s = 0; s < handlers.size(); ++s)
{
/* Register and unregister function can be called inside OnEvent. */
handlers[s]->OnEvent();
}
}
What would be the most optimal way to add and remove a handler from the vector inside OnEvent function?
My idea was. When register is called inside OnEvent I will push back a handler to the handlers list. But I cant think of any way how to remove handler from the vector. If I remove a handler from the vector the iterators will not be valid anymore. Will be very interesting to hear your thoughts, guys.
Thank you
Aucun commentaire:
Enregistrer un commentaire