Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
symbol_manager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <deque>
4#include <string_view>
5#include <unordered_map>
6#include <exchange/symbol.hpp>
8#include "types.hpp"
9
10namespace slick::sim {
11
15struct SymbolKey {
18
19 bool operator==(const SymbolKey &other) const noexcept {
20 return venue == other.venue && name == other.name;
21 }
22};
23
25 size_t operator()(const SymbolKey &key) const noexcept {
26 return std::hash<std::string_view>{}(key.name.view()) * 31u
27 + static_cast<size_t>(key.venue);
28 }
29};
30
32public:
35 return instance;
36 }
37
38 SymbolManager(const SymbolManager&) = delete;
42
44 exch::Symbol* getSymbol(std::string_view sym, Venue venue) const {
45 // The inline name avoids allocating and freeing a string on each lookup,
46 // including the lookup for every inbound order.
47 auto it = symbols_by_name_.find(SymbolKey{venue, sym});
48 if (it == symbols_by_name_.end()) {
49 return nullptr;
50 }
51 return it->second;
52 }
53
55 if (sid >= symbols_.size()) {
56 return nullptr;
57 }
58 return &symbols_[sid];
59 }
60
63 exch::Symbol* createSymbol(std::string_view sym, Venue venue) {
64 SymbolKey key{venue, sym};
65 if (auto it = symbols_by_name_.find(key); it != symbols_by_name_.end()) {
66 return it->second;
67 }
68
69 // Construct in place to avoid a move and the observer-registration update
70 // that Symbol's move constructor requires.
71 auto &symbol = symbols_.emplace_back();
72 symbol.id_ = static_cast<symid_t>(symbols_.size() - 1);
73 symbol.symbol_ = sym;
74 symbol.venue_ = venue;
75 symbols_by_name_.emplace(std::move(key), &symbol);
76 return &symbol;
77 }
78
79 size_t size() const noexcept { return symbols_.size(); }
80
81private:
82 SymbolManager() = default;
83 ~SymbolManager() = default;
84
88 std::deque<exch::Symbol> symbols_;
89 std::unordered_map<SymbolKey, exch::Symbol*, SymbolKeyHash> symbols_by_name_;
90};
91
92}
exch::Symbol * createSymbol(std::string_view sym, Venue venue)
Returns the existing instrument if this (venue, name) already has one, so a double create cannot stra...
SymbolManager & operator=(const SymbolManager &)=delete
static SymbolManager & instance()
exch::Symbol * getSymbol(std::string_view sym, Venue venue) const
A venue is required: see SymbolKey.
SymbolManager(const SymbolManager &)=delete
exch::Symbol * getSymbol(symid_t sid)
size_t size() const noexcept
SymbolManager(SymbolManager &&)=delete
SymbolManager & operator=(SymbolManager &&)=delete
uint16_t symid_t
Definition types.hpp:24
utils::fixed_string< sizeof(MarketDataUpdate::symbol)> symbol_name_t
An instrument name at its wire width.
size_t operator()(const SymbolKey &key) const noexcept
An instrument is identified by both venue and name.
bool operator==(const SymbolKey &other) const noexcept