I've finally implemented an extendable message class and messageServer class. The message class dynamically assigns a unique static ID to each derived message type (ex. ObjectCreatedMessage might be assigned id = 1). The actual message data is a member of the derived class.
Messages can be pushed to MessageServer class by upcasting to MessageBase*.
Systems (or anything) can subscribe to Message Server by passing a std::function to:
typedef std::function<void(MessageBase*)> Callback; void registerCallback(int messageType, Callback callback);
When MessageServer is processing messages, it pops a message off the queue, checks the message type, then sends the message to each callback function registered to that type.
My Questions are:
- Is this efficient?
- How could I implement an "unregister" function? std::functions can't be compared by equality.
- What is a smart way to handle the callback failing? (perhaps the registered object went out of scope so the function pointer is undefined)