Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
exchange_registry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <common/types.hpp>
4#include <memory>
5#include <string>
6#include <string_view>
7#include <vector>
8
9// Deliberately the forward header: this is included by main.cpp and by every
10// venue registration TU, none of which need the full nlohmann::json definition.
11#include <nlohmann/json_fwd.hpp>
12
13namespace slick::sim::exch {
14
15class Exchange;
16
25class ExchangeRegistry {
26public:
32 using Factory = std::vector<std::unique_ptr<Exchange>> (*)(const nlohmann::json &config);
33
34 static ExchangeRegistry &instance();
35
39 bool add(std::string_view key, Venue venue, Factory factory);
40
42 Factory find(std::string_view key) const noexcept;
43
46 std::vector<std::string> keys() const;
47
48private:
49 ExchangeRegistry() = default;
50
51 struct Entry {
52 std::string key;
53 Venue venue;
54 Factory factory;
55 };
56 std::vector<Entry> entries_;
57};
58
60template <typename T>
61concept MultiInstanceVenue = requires(const nlohmann::json &config) { T::create_instances(config); };
62
73template <typename T, typename Base = Exchange>
74std::vector<std::unique_ptr<Base>> make_exchanges(const nlohmann::json &config) {
75 if constexpr (MultiInstanceVenue<T>) {
76 return T::create_instances(config);
77 } else {
78 std::vector<std::unique_ptr<Base>> one;
79 one.push_back(std::make_unique<T>(config));
80 return one;
81 }
82}
83
84} // end namespace slick::sim::exch
85
100#define SLICK_SIM_REGISTER_VENUE(key, venue_enum, ExchangeType) \
101 namespace { \
102 ::std::vector<::std::unique_ptr<::slick::sim::exch::Exchange>> \
103 slick_sim_make_##ExchangeType(const ::nlohmann::json &config) { \
104 return ::slick::sim::exch::make_exchanges<ExchangeType>(config); \
105 } \
106 [[maybe_unused]] const bool slick_sim_registered_##ExchangeType = \
107 ::slick::sim::exch::ExchangeRegistry::instance().add( \
108 key, venue_enum, &slick_sim_make_##ExchangeType); \
109 } \
110 static_assert(true, "require a trailing semicolon")
Factory find(std::string_view key) const noexcept
nullptr when no adapter for this key was built into the binary.
std::vector< std::unique_ptr< Exchange > >(*)(const nlohmann::json &config) Factory
A plain function pointer, not std::function - no allocation, no <functional>.
std::vector< std::string > keys() const
Registered keys, in registration order - for the startup banner and for the error message on an unkno...
static ExchangeRegistry & instance()
bool add(std::string_view key, Venue venue, Factory factory)
key is matched case-insensitively, the same way to_venue matches a venue name.
A venue whose one config entry can describe several exchanges.
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
std::vector< std::unique_ptr< Base > > make_exchanges(const nlohmann::json &config)
The factory body SLICK_SIM_REGISTER_VENUE registers.