C++ Event Subscriptions API
Note
This is correct for only versions of sc-machine that >= 0.10.0.
The C++ Event Subscriptions API focuses on managing subscriptions to various events within the system. This documentation explains how agents can subscribe to specific events and receive notifications when these events occur. It is essential for building systems that require real-time updates and interactions based on event occurrences.
Note
To include this API provide #include <sc-memory/sc_event_subscription.hpp> in your hpp source.
ScEventSubscription
ScElementaryEventSubscription is base class for all sc-event subscription, it can be used to catch all sc-events for specified sc-element.
Each sc-event subscription constructor, besides ScElementaryEventSubscription constructor, takes 3 parameters:
contextis an object ofScMemoryContextthat will be used to work with sc-event;subscriptionElementAddris an object ofScAddrof sc-element that need to be listened for a specified sc-event;delegateFuncis a delegate to a callback function, that will be called on each event emit (void delegateFunc(TScEvent const &)), whereTScEventis corresponding sc-event class.
Note
Callback function will be called in another thread!
Warning
Subscription sc-element must be valid sc-element.
Constructor of ScElementaryEventSubscription class takes 4 parameters:
contextis an object ofScMemoryContextthat will be used to work with sc-event;eventClassAddris an object ofScAddrof sc-event class;subscriptionElementAddris an object ofScAddrof sc-element that need to be listened for a specified sc-event;delegateFuncis a delegate to a callback function, that will be called on each event emit (void delegateFunc(ScElementaryEvent const &)).
All these constructors are private, you can't call these. We provide more safe API to generate subscription. Use C++ Agent Context API to generate sc-event subscriptions.
All sc-event classes are in core keynodes:
ScKeynodes::sc_event_after_generate_connector;ScKeynodes::sc_event_after_generate_outgoing_arc;ScKeynodes::sc_event_after_generate_incoming_arc;ScKeynodes::sc_event_after_generate_edge;ScKeynodes::sc_event_before_erase_connector;ScKeynodes::sc_event_before_erase_outgoing_arc;ScKeynodes::sc_event_before_erase_incoming_arc;ScKeynodes::sc_event_before_erase_edge;ScKeynodes::sc_event_before_erase_element;ScKeynodes::sc_event_before_change_link_content.
Use them as eventClassAddr for CreateElementaryEventSubscription.
The table of description (parameters of callback function named on pictures, if there are no parameter name on picture, then it's would have an empty value):
Note
Here context is pointer to object of ScAgentContext class.
| Class | Description |
|---|---|
| ScElementaryEventSubscription |
Example C++ code:
|
| ScEventAfterGenerateConnector |
|
| ScEventAfterGenerateOutgoingArc |
|
| ScEventAfterGenerateIncomingArc |
|
| ScEventAfterGenerateEdge |
|
| ScEventBeforeEraseConnector |
|
| ScEventBeforeEraseOutgoingArc |
|
| ScEventBeforeEraseIncomingArc |
|
| ScEventBeforeEraseEdge |
|
| ScEventBeforeEraseElement |
Example C++ code:
|
| ScEventBeforeChangeLinkContent |
|
Frequently Asked Questions
- Whether a function is considered an agent if this function is subscribed to a sc-event and which is called after that sc-event occurs?
- Why can't you call the constructor of a subscription class to sc-event?
- Are
ScEventAfterGenerateIncomingArcorScEventAfterGenerateOutgoingArcevents trigger whenScEventAfterGenerateEdgeevent is triggered?
Whether a function is considered an agent if this function is subscribed to a sc-event and which is called after that sc-event occurs?
No, such functions are not agents. Agents have a strict specification. See C++ Agents API.
Why can't I call the constructor of a subscription class to sc-event?
First of all, it's not safe. We need more checks on input arguments because there are more of them. Secondly, it is correct from the OOP point of view. Constructors should not throw exceptions. Third, it is correct from the point of view of the architecture we use in the sc-machine. The ScAgentContext is a facade over all possible objects used by agents.
Are ScEventAfterGenerateIncomingArc or ScEventAfterGenerateOutgoingArc events trigger when ScEventAfterGenerateEdge event is triggered?
No, the ScEventAfterGenerateEdge event only occurs when sc-edges are generated.