Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
broker_registry.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <slick/stream_buffer_multiplexer.hpp>
5#include <nlohmann/json.hpp>
6#include <cstdint>
7#include <memory>
8#include <string>
9#include <string_view>
10#include <vector>
11
12namespace slick::trader {
13
14using json = nlohmann::json;
15
24class BrokerRegistry {
25public:
27 using Factory = std::shared_ptr<IBroker> (*)(
28 const json& config,
29 slick::stream_buffer_multiplexer& event_mux,
30 slick::queue<uint8_t>& control_queue,
31 slick::queue<uint8_t>& md_request_queue,
32 slick::queue<uint8_t>& order_request_queue);
33
34 static BrokerRegistry& instance();
35
39 bool add(std::string_view key, Factory factory);
40
42 Factory find(std::string_view key) const noexcept;
43
48 std::shared_ptr<IBroker> create(std::string_view key, const json& config,
49 slick::stream_buffer_multiplexer& event_mux,
50 slick::queue<uint8_t>& control_queue,
51 slick::queue<uint8_t>& md_request_queue,
52 slick::queue<uint8_t>& order_request_queue,
53 std::string& error) const;
54
57 std::vector<std::string> keys() const;
58
59private:
60 BrokerRegistry() = default;
61
62 struct Entry {
63 std::string key; // lowercased
64 Factory factory;
65 };
66 std::vector<Entry> entries_;
67};
68
69} // namespace slick::trader
70
79#define SLICK_TRADER_REGISTER_BROKER(key, BrokerType) \
80 namespace { \
81 ::std::shared_ptr<::slick::trader::IBroker> slick_trader_make_##BrokerType( \
82 const ::nlohmann::json& config, \
83 ::slick::stream_buffer_multiplexer& event_mux, \
84 ::slick::queue<uint8_t>& control_queue, \
85 ::slick::queue<uint8_t>& md_request_queue, \
86 ::slick::queue<uint8_t>& order_request_queue) { \
87 return ::std::make_shared<BrokerType>( \
88 config, event_mux, control_queue, md_request_queue, order_request_queue); \
89 } \
90 [[maybe_unused]] const bool slick_trader_registered_##BrokerType = \
91 ::slick::trader::BrokerRegistry::instance().add(key, &slick_trader_make_##BrokerType); \
92 } \
93 static_assert(true, "require a trailing semicolon")
static BrokerRegistry & instance()
std::shared_ptr< IBroker >(*)( const json &config, slick::stream_buffer_multiplexer &event_mux, slick::queue< uint8_t > &control_queue, slick::queue< uint8_t > &md_request_queue, slick::queue< uint8_t > &order_request_queue) Factory
A plain function pointer, not std::function - no allocation, no <functional>.
Factory find(std::string_view key) const noexcept
nullptr when no adapter for this key was built into the binary.
bool add(std::string_view key, Factory factory)
key is matched case-insensitively.
std::vector< std::string > keys() const
Registered keys, lowercased, in registration order - for the startup log and for the error on an unkn...
std::shared_ptr< IBroker > create(std::string_view key, const json &config, slick::stream_buffer_multiplexer &event_mux, slick::queue< uint8_t > &control_queue, slick::queue< uint8_t > &md_request_queue, slick::queue< uint8_t > &order_request_queue, std::string &error) const
Builds the broker registered under key.
nlohmann::json json