Health-GPS  1.2.2.0
Global Health Policy Simulation model (Health-GPS)
event_aggregator.h
Go to the documentation of this file.
1 #pragma once
2 #include <functional>
3 #include <stdexcept>
4 
5 #include "event_message.h"
6 #include <memory>
7 
8 namespace hgps {
9 
13 
17  EventHandlerIdentifier(std::string identifier) : identifier_{identifier} {
18  if (identifier.empty()) {
19  throw std::invalid_argument("Event handler identifier must not be empty.");
20  }
21  }
22 
25  const std::string str() const noexcept { return identifier_; }
26 
30  auto operator<=>(const EventHandlerIdentifier &other) const = default;
31 
32  private:
33  std::string identifier_;
34 };
35 
38  public:
40  EventSubscriber() = default;
41 
42  EventSubscriber(const EventSubscriber &) = delete;
46 
48  virtual ~EventSubscriber() = default;
49 
51  virtual void unsubscribe() const = 0;
52 
55  [[nodiscard]] virtual const EventHandlerIdentifier id() const noexcept = 0;
56 };
57 
60  public:
62  EventAggregator() = default;
63 
66  EventAggregator(EventAggregator &&other) = default;
67 
72 
73  EventAggregator(const EventAggregator &) = delete;
75 
77  virtual ~EventAggregator() = default;
78 
83  [[nodiscard]] virtual std::unique_ptr<EventSubscriber>
84  subscribe(EventType event_id,
85  std::function<void(std::shared_ptr<EventMessage> message)> &&handler) = 0;
86 
90  virtual bool unsubscribe(const EventSubscriber &subscriber) = 0;
91 
94  virtual void publish(std::unique_ptr<EventMessage> message) = 0;
95 
98  virtual void publish_async(std::unique_ptr<EventMessage> message) = 0;
99 };
100 } // namespace hgps
Defines the event aggregator interface type.
Definition: event_aggregator.h:59
virtual void publish(std::unique_ptr< EventMessage > message)=0
Publishes a message to all subscribers synchronous.
virtual std::unique_ptr< EventSubscriber > subscribe(EventType event_id, std::function< void(std::shared_ptr< EventMessage > message)> &&handler)=0
Subscribes a handler function to an event type identifier.
EventAggregator & operator=(const EventAggregator &)=delete
virtual void publish_async(std::unique_ptr< EventMessage > message)=0
Publishes a message to all subscribers asynchronous.
virtual bool unsubscribe(const EventSubscriber &subscriber)=0
Unsubscribes from an event notification.
EventAggregator(const EventAggregator &)=delete
EventAggregator()=default
Initialises a new instance of the EventAggregator class.
EventAggregator & operator=(EventAggregator &&other)=default
Replaces the EventAggregator contents with the other using move semantics.
virtual ~EventAggregator()=default
Destroys a EventAggregator instance.
EventAggregator(EventAggregator &&other)=default
Constructs the EventAggregator using move semantics.
Defines the event subscriber interface type.
Definition: event_aggregator.h:37
EventSubscriber & operator=(const EventSubscriber &)=delete
EventSubscriber(const EventSubscriber &)=delete
EventSubscriber()=default
Initialises a new instance of the EventSubscriber class.
EventSubscriber(EventSubscriber &&)=delete
virtual ~EventSubscriber()=default
Destroys a EventSubscriber instance.
virtual const EventHandlerIdentifier id() const noexcept=0
Gets the subscriber unique identifier.
EventSubscriber & operator=(EventSubscriber &&)=delete
virtual void unsubscribe() const =0
Unsubscribes from the event.
Top-level namespace for Health-GPS C++ API.
Definition: analysis_definition.h:8
EventType
Event Message type enumeration.
Definition: event_message.h:10
Defines the event handler identifier type.
Definition: event_aggregator.h:11
EventHandlerIdentifier(std::string identifier)
Initialises a new instance of the EventHandlerIdentifier structure.
Definition: event_aggregator.h:17
const std::string str() const noexcept
Gets the event handle identifier string representation.
Definition: event_aggregator.h:25
auto operator<=>(const EventHandlerIdentifier &other) const =default
Compare this instance with other EventHandlerIdentifier instance.