Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
Loading...
Searching...
No Matches
observer.hpp
Go to the documentation of this file.
1// Copyright 2026 Slick Quant
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
9#include <algorithm>
10#include <vector>
11#include <memory>
12
14
21public:
22 virtual ~IOrderBookObserver() = default;
23
25 virtual void onPriceLevelUpdate([[maybe_unused]] const PriceLevelUpdate& update) {
26 }
27
29 virtual void onOrderUpdate([[maybe_unused]] const OrderUpdate& update) {
30 }
31
33 virtual void onTrade([[maybe_unused]] const Trade& trade) {
34 }
35
37 virtual void onTopOfBookUpdate([[maybe_unused]] const TopOfBook& tob) {
38 }
39
42 virtual void onSnapshotBegin([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) {
43 }
44
46 virtual void onSnapshotEnd([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) {
47 }
48};
49
53public:
54 ObserverManager() = default;
55 ~ObserverManager() = default;
56
57 // Non-copyable, movable
60 ObserverManager(ObserverManager&&) noexcept = default;
61 ObserverManager& operator=(ObserverManager&&) noexcept = default;
62
65 void addObserver(std::shared_ptr<IOrderBookObserver> observer) {
66 if (observer) {
67 observers_.push_back(std::move(observer));
68 }
69 }
70
74 bool removeObserver(const std::shared_ptr<IOrderBookObserver>& observer) {
75 auto it = std::find(observers_.begin(), observers_.end(), observer);
76 if (it != observers_.end()) {
77 observers_.erase(it);
78 return true;
79 }
80 return false;
81 }
82
84 void clearObservers() noexcept {
85 observers_.clear();
86 }
87
89 [[nodiscard]] std::size_t observerCount() const noexcept {
90 return observers_.size();
91 }
92
94 void notifyPriceLevelUpdate(const PriceLevelUpdate& update) const {
95 for (const auto& observer : observers_) {
96 observer->onPriceLevelUpdate(update);
97 }
98 }
99
101 void notifyOrderUpdate(const OrderUpdate& update) const {
102 for (const auto& observer : observers_) {
103 observer->onOrderUpdate(update);
104 }
105 }
106
108 void notifyTrade(const Trade& trade) const {
109 for (const auto& observer : observers_) {
110 observer->onTrade(trade);
111 }
112 }
113
115 void notifyTopOfBookUpdate(const TopOfBook& tob) const {
116 for (const auto& observer : observers_) {
117 observer->onTopOfBookUpdate(tob);
118 }
119 }
120
122 void notifySnapshotBegin(SymbolId symbol, uint64_t seq_num, Timestamp timestamp) const {
123 for (const auto& observer : observers_) {
124 observer->onSnapshotBegin(symbol, seq_num, timestamp);
125 }
126 }
127
129 void notifySnapshotEnd(SymbolId symbol, uint64_t seq_num, Timestamp timestamp) const {
130 for (const auto& observer : observers_) {
131 observer->onSnapshotEnd(symbol, seq_num, timestamp);
132 }
133 }
134
135private:
136 std::vector<std::shared_ptr<IOrderBookObserver>> observers_;
137};
138
Base interface for orderbook observers Observers receive callbacks for orderbook events.
Definition observer.hpp:20
virtual void onSnapshotEnd(SymbolId symbol, uint64_t seq_num, Timestamp timestamp)
Called when snapshot processing completes.
Definition observer.hpp:46
virtual void onOrderUpdate(const OrderUpdate &update)
Called when an individual order is updated (L3 event)
Definition observer.hpp:29
virtual void onTopOfBookUpdate(const TopOfBook &tob)
Called when top-of-book changes.
Definition observer.hpp:37
virtual void onTrade(const Trade &trade)
Called when a trade occurs.
Definition observer.hpp:33
virtual void onPriceLevelUpdate(const PriceLevelUpdate &update)
Called when a price level is updated (L2 event)
Definition observer.hpp:25
virtual void onSnapshotBegin(SymbolId symbol, uint64_t seq_num, Timestamp timestamp)
Called when snapshot processing begins Followed by multiple onPriceLevelUpdate() or onOrderUpdate() c...
Definition observer.hpp:42
Observer manager for notifying multiple observers Uses type erasure to support both concept-based and...
Definition observer.hpp:52
void notifyPriceLevelUpdate(const PriceLevelUpdate &update) const
Notify all observers of price level update.
Definition observer.hpp:94
void notifyTrade(const Trade &trade) const
Notify all observers of trade.
Definition observer.hpp:108
std::size_t observerCount() const noexcept
Get number of observers.
Definition observer.hpp:89
ObserverManager(ObserverManager &&) noexcept=default
ObserverManager & operator=(const ObserverManager &)=delete
void clearObservers() noexcept
Clear all observers.
Definition observer.hpp:84
bool removeObserver(const std::shared_ptr< IOrderBookObserver > &observer)
Remove an observer.
Definition observer.hpp:74
ObserverManager(const ObserverManager &)=delete
void notifyTopOfBookUpdate(const TopOfBook &tob) const
Notify all observers of top-of-book update.
Definition observer.hpp:115
void notifySnapshotEnd(SymbolId symbol, uint64_t seq_num, Timestamp timestamp) const
Notify all observers that snapshot processing is complete.
Definition observer.hpp:129
void notifySnapshotBegin(SymbolId symbol, uint64_t seq_num, Timestamp timestamp) const
Notify all observers that snapshot processing is beginning.
Definition observer.hpp:122
void notifyOrderUpdate(const OrderUpdate &update) const
Notify all observers of order update.
Definition observer.hpp:101
#define SLICK_NAMESPACE_END
Definition config.hpp:127
#define SLICK_NAMESPACE_BEGIN
Definition config.hpp:126
uint16_t SymbolId
Symbol identifier - can be hash or sequential ID.
Definition types.hpp:27
uint64_t Timestamp
Timestamp in nanoseconds since epoch.
Definition types.hpp:30
Level 3 order update event Note: quantity = 0 implies the order should be deleted.
Definition events.hpp:67
Level 2 price level update event Note: quantity = 0 implies the price level should be deleted.
Definition events.hpp:21
Top-of-book snapshot - best bid and ask.
Definition events.hpp:142
Trade event - represents an executed trade.
Definition events.hpp:115