Overview
yaze includes a message passing and notification system as part of its core library. Supports message filtering, dynamic method binding, swizzling, and reflection. This message system was inspired by Objective-C and Cocoa's message passing system. It aims to overcome some of the difficulties with handling events in ImGui.
This system is currently in development and most of the content here was generated using ChatGPT to help me organize my thoughts. I will be updating this document as I continue to develop the system.
Key Components
- Message: A basic unit of communication between components. It contains a type, sender, and an optional payload.
- IMessageListener: An interface for objects that want to receive messages. Implement this interface to handle incoming messages.
- MessageDispatcher: The central hub that registers listeners, dispatches messages, and manages protocols, filters, and dynamic handlers.
- AsyncMessageDispatcher: An extension of
MessageDispatcher
that supports asynchronous message dispatching via a queue.
- IMessageProtocol: An interface for defining custom protocols that can filter or handle messages based on specific criteria.
- MessageFilter: A class used to filter which messages a listener should receive.
- Swizzler: A utility that allows you to dynamically replace (swizzle) methods on objects at runtime, enabling dynamic behavior changes.
- Reflectable: An interface that allows for runtime inspection and manipulation of objects' properties.
- ObjectFactory: A factory for creating instances of
Reflectable
objects dynamically based on type names.
Getting Started
1. Setting Up the Message Dispatcher
To start using the message passing system, first create an instance of MessageDispatcher
or AsyncMessageDispatcher
depending on whether you need synchronous or asynchronous message handling.
async_dispatcher.
Start();
The EditorManager in the main yaze app will have a MessageDispatcher instance which can be injected into the various Editor components. This will allow the components to communicate with each other without needing to know about each other.
2. Registering Listeners
Components that need to listen for messages must implement the IMessageListener
interface and register themselves with the dispatcher.
public:
}
};
MyListener listener;
virtual absl::Status OnMessageReceived(const Message &message)=0
void RegisterListener(const std::string &message_type, IMessageListener *listener)
3. Sending Messages
To communicate between components, create a Message
and send it through the dispatcher.
void SendMessage(const Message &message)
4. Using Protocols and Filters
For more advanced message handling, you can define custom protocols and filters. Protocols can determine if they can handle a message, while filters can refine which messages are received by a listener.
public:
return message.
type ==
"MyMessageType";
}
};
public:
return true;
}
};
virtual bool CanHandleMessage(const Message &message) const =0
void RegisterProtocol(IMessageProtocol *protocol)
void RegisterFilteredListener(const std::string &message_type, IMessageListener *listener, std::unique_ptr< MessageFilter > filter)
virtual bool ShouldReceiveMessage(const Message &message) const =0
5. Dynamic Method Binding and Swizzling
To dynamically bind methods to message types or change the behavior of methods at runtime, use the Swizzler
and BindHandler
methods.
});
Swizzler swizzler;
swizzler.Swizzle(&some_object, &SomeClass::OriginalMethod, []() {
});
void BindHandler(const std::string &message_type, MessageHandler handler)
6. Reflection and Object Creation
For systems that require dynamic inspection and manipulation of objects, implement the Reflectable
interface and use the ObjectFactory
to create instances dynamically.
public:
std::string
GetTypeName()
const override {
return "MyObject"; }
std::vector<std::string>
GetPropertyNames()
const override {
return {
"property"}; }
if (property_name == "property") return property_;
return {};
}
void SetPropertyValue(
const std::string& property_name,
const std::any& value)
override {
if (property_name == "property") property_ = std::any_cast<int>(value);
}
private:
int property_;
};
std::unique_ptr< Reflectable > CreateObject(const std::string &type_name) const
void RegisterType(const std::string &type_name)
virtual void SetPropertyValue(const std::string &property_name, const std::any &value)=0
virtual std::vector< std::string > GetPropertyNames() const =0
virtual std::string GetTypeName() const =0
virtual std::any GetPropertyValue(const std::string &property_name) const =0