Hi everybody,

I'm considering implementing a general purpose event-based system in C++. Events will be asynchronous. My question regarding design is: should event handlers have a return value, or should it send back the "return value" in the form of an event, itself?
What is the most common way? What are the advantages and disadvantages of both?

I'll show some C++-pseudo code to describe what I mean. The exact implementation details will differ, but it's just to give you a general idea of what it would be used like.

The return value method would look similar to this:
Code:
SomeEventInterface iface;
[...]

AsyncEvent<ReturnType> ret1 = iface.send(E1);
AsyncEvent<ReturnType2> ret2 = iface.send(E2);

if(ret1.hasResult()) {
   [...]
}

waitResultAny(ret1, ret2);
Whereas the event return method would look similar to this:
Code:
ReplyInterface myIface; // <-- Maybe shared_ptr.
SomeEventInterface iface;

iface.send(myIface, E);
Where "ReplyInterface" itself is an event handler that handles the events that "SomeEventInterface" can send.

While the former is easier to work with, the former can be implemented as basis of the second. Ie the "AsyncEvent" class in the former example can be implemented to be the ReplyInterface itself. This is definitely true if there can only be one type of event sent as a reply (which is equal to the return type method which can have ony one type), but it does get a bit less clear if there can be more than one type of event (such as SuccessEvent and FailureEvent).

What is the "proper" way of doing this? And why?


Thanks in advance!