FinanceSim 0.1.0
Financial Simulation Library
Loading...
Searching...
No Matches
event_bus.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <functional>
5#include <unordered_map>
6#include <typeindex>
7#include <memory>
8#include "core/event.hpp"
9
10namespace financesim {
11
13using EventCallback = std::function<void(const EventPtr&)>;
14
16using SubscriptionId = std::size_t;
17
20class EventBus {
21public:
22 EventBus() = default;
23
24 // Non-copyable, non-movable (one per simulation)
25 EventBus(const EventBus&) = delete;
26 EventBus& operator=(const EventBus&) = delete;
27
30 template<typename EventType>
31 SubscriptionId subscribe(std::function<void(const EventType&)> callback) {
32 auto wrapper = [callback](const EventPtr& event) {
33 if (auto typed = std::dynamic_pointer_cast<const EventType>(event)) {
34 callback(*typed);
35 }
36 };
37
38 auto type_idx = std::type_index(typeid(EventType));
40 subscriptions_[type_idx].push_back({id, wrapper});
41 return id;
42 }
43
47 global_subscribers_.push_back({id, std::move(callback)});
48 return id;
49 }
50
53 for (auto& [type, subs] : subscriptions_) {
54 subs.erase(
55 std::remove_if(subs.begin(), subs.end(),
56 [id](const Subscription& s) { return s.id == id; }),
57 subs.end()
58 );
59 }
61 std::remove_if(global_subscribers_.begin(), global_subscribers_.end(),
62 [id](const Subscription& s) { return s.id == id; }),
64 );
65 }
66
68 template<typename EventType>
69 void publish(std::shared_ptr<const EventType> event) {
70 event_log_.push_back(event);
71
72 auto type_idx = std::type_index(typeid(EventType));
73 auto it = subscriptions_.find(type_idx);
74 if (it != subscriptions_.end()) {
75 for (const auto& sub : it->second) {
76 sub.callback(event);
77 }
78 }
79
80 for (const auto& sub : global_subscribers_) {
81 sub.callback(event);
82 }
83 }
84
86 const std::vector<EventPtr>& event_log() const { return event_log_; }
87
89 void clear_log() { event_log_.clear(); }
90
92 void reset() {
93 subscriptions_.clear();
94 global_subscribers_.clear();
95 event_log_.clear();
97 }
98
99private:
104
105 std::unordered_map<std::type_index, std::vector<Subscription>> subscriptions_;
106 std::vector<Subscription> global_subscribers_;
107 std::vector<EventPtr> event_log_;
109};
110
111} // namespace financesim
EventBus(const EventBus &)=delete
std::vector< Subscription > global_subscribers_
SubscriptionId subscribe_all(EventCallback callback)
Subscribe to all events.
Definition event_bus.hpp:45
EventBus & operator=(const EventBus &)=delete
SubscriptionId subscribe(std::function< void(const EventType &)> callback)
Definition event_bus.hpp:31
void reset()
Reset the bus (clear all subscriptions and log)
Definition event_bus.hpp:92
void clear_log()
Clear the event log.
Definition event_bus.hpp:89
SubscriptionId next_subscription_id_
std::unordered_map< std::type_index, std::vector< Subscription > > subscriptions_
void publish(std::shared_ptr< const EventType > event)
Publish an event to all subscribers.
Definition event_bus.hpp:69
void unsubscribe(SubscriptionId id)
Unsubscribe using subscription ID.
Definition event_bus.hpp:52
std::vector< EventPtr > event_log_
const std::vector< EventPtr > & event_log() const
Get all events in chronological order (for replay/debugging)
Definition event_bus.hpp:86
std::size_t SubscriptionId
Subscription handle for unsubscribing.
Definition event_bus.hpp:16
std::shared_ptr< const Event > EventPtr
Definition event.hpp:32
std::function< void(const EventPtr &)> EventCallback
Callback type for event subscribers.
Definition event_bus.hpp:13