13#ifndef RTCTK_COMPONENTFRAMEWORK_TYPEDEVENTSERVICE_HPP
14#define RTCTK_COMPONENTFRAMEWORK_TYPEDEVENTSERVICE_HPP
38 template <
typename EventType>
40 std::lock_guard
guard(m_mutex);
42 const std::string
tpc_name = EventType::TOPIC_NAME;
44 if (m_hl_publishers.count(
tpc_name) == 0) {
52 template <
typename EventType>
54 std::lock_guard
guard(m_mutex);
56 const std::string
tpc_name = EventType::TOPIC_NAME;
58 if (m_hl_publishers.count(
tpc_name)) {
59 if (m_hl_publishers.at(
tpc_name) >= 1) {
63 if (m_hl_publishers.at(
tpc_name) == 0) {
70 template <
typename EventType>
72 std::lock_guard
guard(m_mutex);
75#pragma GCC diagnostic push
76#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
77 const std::string
tpc_name = EventType::TOPIC_NAME;
79#pragma GCC diagnostic pop
90 std::map<std::string, unsigned> m_hl_publishers;
91 std::map<std::string, std::unique_ptr<EventPublisherIf>> m_ll_publishers;
103 template <
typename EventType>
105 std::lock_guard
guard(m_mutex);
107 const std::string
tpc_name = EventType::TOPIC_NAME;
109 if (m_hl_subscribers.count(
tpc_name) == 0) {
110 m_hl_subscribers[
tpc_name].insert(
id);
113 m_hl_subscribers[
tpc_name].insert(
id);
117 template <
typename EventType>
119 std::lock_guard
guard(m_mutex);
124 const std::string
tpc_name = EventType::TOPIC_NAME;
126 if (m_hl_subscribers.count(
tpc_name)) {
127 m_hl_subscribers.at(
tpc_name).erase(
id);
129 if (m_hl_subscribers.at(
tpc_name).empty()) {
136 template <
typename EventType>
138 std::lock_guard
guard(m_mutex);
140 const std::string
tpc_name = EventType::TOPIC_NAME;
143 if (m_callbacks.count(
tpc_name) == 0) {
145 std::lock_guard
guard(m_mutex);
147 if (m_callbacks.count(
tpc_name) != 0) {
149 for (
const auto& [
k,
obj] :
val) {
160 template <
typename EventType>
162 std::lock_guard
guard(m_mutex);
168 template <
typename EventType>
170 const std::string
tpc_name = EventType::TOPIC_NAME;
178 if (m_callbacks.at(
tpc_name).empty()) {
180 m_ll_subscribers.at(
tpc_name)->Unsubscribe();
189 virtual ~FuncObjBase() =
default;
194 template <
typename EventType>
195 class FuncObj :
public FuncObjBase {
197 static_assert(std::is_base_of_v<AbstractEvent, EventType>,
198 "EventType must extend AbstractEvent");
200 explicit FuncObj(std::function<
void(
const EventType&)>
cb) : m_cb(
cb) {
207#pragma GCC diagnostic push
208#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
210#pragma GCC diagnostic pop
219 EventServiceIf& m_es;
222 std::map<std::string, std::set<uintptr_t>> m_hl_subscribers;
223 std::map<std::string, std::unique_ptr<EventSubscriberIf>> m_ll_subscribers;
232 std::map<std::string, std::map<std::string, std::map<uintptr_t, std::shared_ptr<FuncObjBase>>>>
243template <
typename EventType>
244class TypedEventPublisher;
246template <
typename EventType>
247class TypedEventSubscriber;
267 template <
typename EventType>
269 return std::unique_ptr<TypedEventPublisher<EventType>>(
278 template <
typename EventType>
280 return std::unique_ptr<TypedEventSubscriber<EventType>>(
299template <
typename EventType>
306 m_man.UnregisterPublisher<EventType>();
317 m_man.Publish(sample);
325 detail::TypedEventPublisherManager& m_man;
338template <
typename EventType>
345 m_man.UnregisterSubscriber<EventType>(
reinterpret_cast<uintptr_t
>(
this));
358 void Subscribe(std::function<
void(
const EventType&)> cb) {
359 m_man.Subscribe<EventType>(
reinterpret_cast<uintptr_t
>(
this), cb);
368 m_man.Unsubscribe<EventType>(
reinterpret_cast<uintptr_t
>(
this));
373 m_man.RegisterSubscriber<EventType>(
reinterpret_cast<uintptr_t
>(
this));
376 detail::TypedEventSubscriberManager& m_man;
Interface class for providing pub/sub facilities for JSON events.
Definition eventServiceIf.hpp:29
virtual std::unique_ptr< EventSubscriberIf > MakeSubscriber(const std::string &topic)=0
Creates a new subscriber for a specified topic.
virtual std::unique_ptr< EventPublisherIf > MakePublisher(const std::string &topic)=0
Creates a new publisher for a specified topic.
An event publisher that is aware of event types.
Definition typedEventService.hpp:300
friend TypedEventService
Definition typedEventService.hpp:303
~TypedEventPublisher()
Definition typedEventService.hpp:305
void Publish(const EventType &sample)
Publishes an event of a specific type.
Definition typedEventService.hpp:316
A high-level event service API that is aware of event types.
Definition typedEventService.hpp:257
std::unique_ptr< TypedEventPublisher< EventType > > MakePublisher()
Creates a new publisher for a specified event type.
Definition typedEventService.hpp:268
TypedEventService(EventServiceIf &es)
Definition typedEventService.hpp:259
std::unique_ptr< TypedEventSubscriber< EventType > > MakeSubscriber()
Creates a new subscriber for a specified event type.
Definition typedEventService.hpp:279
An event subscriber that is aware of event types.
Definition typedEventService.hpp:339
~TypedEventSubscriber()
Definition typedEventService.hpp:344
friend TypedEventService
Definition typedEventService.hpp:342
void Unsubscribe()
Unsubscribes from a specified event type.
Definition typedEventService.hpp:367
void Subscribe(std::function< void(const EventType &)> cb)
Subscribes to an event of a specific type.
Definition typedEventService.hpp:358
Definition typedEventService.hpp:33
void Publish(const EventType &sample)
Definition typedEventService.hpp:71
void UnregisterPublisher()
Definition typedEventService.hpp:53
TypedEventPublisherManager(EventServiceIf &es)
Definition typedEventService.hpp:35
void RegisterPublisher()
Definition typedEventService.hpp:39
Definition typedEventService.hpp:98
void UnregisterSubscriber(uintptr_t id)
Definition typedEventService.hpp:118
void Unsubscribe(uintptr_t id)
Definition typedEventService.hpp:161
TypedEventSubscriberManager(EventServiceIf &es)
Definition typedEventService.hpp:100
void RegisterSubscriber(uintptr_t id)
Definition typedEventService.hpp:104
void Subscribe(uintptr_t id, std::function< void(const EventType &)> cb)
Definition typedEventService.hpp:137
Framework-provided event definitions.
Low-level interface of the event service.
Defines the JSON payload type JsonPayload.
Definition commandReplier.cpp:22
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23