L
Luc Tremblay
Given the typical following code:
void Listener::HandleEvent(const Event& event)
{
// handling code
}
In a "clean" fashion, how is it possible to add custom data (to be
subsequently accessed) to the Event instance? By custom data i mean
practically anything, from a class to a single int. Particularly to my case,
it would be assumed that such data would be appropriately destroyed as the
Event is consumed/destroyed.
I have considered several options but still can't find one that satisfies
the "clean" requisite. One implies keeping a void* "userData" in the Event
class, and using setData<T>/getData<T> template methods to access and
automagically cast the data to the proper type, with type safety checks in
debug. Problems arise if I want the Event class to handle the destruction of
the custom data because internally to Event, the custom data's type is
unknown. Again particularly to my case, I could use a memory pool for the
custom data, write the custom data to it using placement new and then clean
the pool after each frame as all events are considered "consumed" between
event iterations. Potential problems arise if classes are used as custom
data, since cleaning the memory pool wouldn't call the destructors of the
custom data.
I would guess Boost::any would also be a solution, but we are trying to
avoid using it in our project.
Subclassing the Event class and adding the custom data as members, and then
downcasting to the proper type, is probably also a solution. I am thinking
it is possibly the only interesting solution (as it is very common), but I
hope to think someone thought of an elegant alternative. I think it is
likely that there is a templated solution, yet i can't figure it out.
It has to be kept in mind that the Event class, in my case, would
potentially be part of an external library, so using a union member of all
possible custom data types isn't a viable solution (not to say it is quite
ugly...)
Regards,
Luc Tremblay
void Listener::HandleEvent(const Event& event)
{
// handling code
}
In a "clean" fashion, how is it possible to add custom data (to be
subsequently accessed) to the Event instance? By custom data i mean
practically anything, from a class to a single int. Particularly to my case,
it would be assumed that such data would be appropriately destroyed as the
Event is consumed/destroyed.
I have considered several options but still can't find one that satisfies
the "clean" requisite. One implies keeping a void* "userData" in the Event
class, and using setData<T>/getData<T> template methods to access and
automagically cast the data to the proper type, with type safety checks in
debug. Problems arise if I want the Event class to handle the destruction of
the custom data because internally to Event, the custom data's type is
unknown. Again particularly to my case, I could use a memory pool for the
custom data, write the custom data to it using placement new and then clean
the pool after each frame as all events are considered "consumed" between
event iterations. Potential problems arise if classes are used as custom
data, since cleaning the memory pool wouldn't call the destructors of the
custom data.
I would guess Boost::any would also be a solution, but we are trying to
avoid using it in our project.
Subclassing the Event class and adding the custom data as members, and then
downcasting to the proper type, is probably also a solution. I am thinking
it is possibly the only interesting solution (as it is very common), but I
hope to think someone thought of an elegant alternative. I think it is
likely that there is a templated solution, yet i can't figure it out.
It has to be kept in mind that the Event class, in my case, would
potentially be part of an external library, so using a union member of all
possible custom data types isn't a viable solution (not to say it is quite
ugly...)
Regards,
Luc Tremblay