Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
broker_registry.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <cctype>
4#include <exception>
5
6namespace slick::trader {
7
8namespace {
9
10std::string to_lower(std::string_view s) {
11 std::string out(s);
12 std::transform(out.begin(), out.end(), out.begin(),
13 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
14 return out;
15}
16
17} // namespace
18
19BrokerRegistry& BrokerRegistry::instance() {
20 static BrokerRegistry registry;
21 return registry;
22}
23
24bool BrokerRegistry::add(std::string_view key, Factory factory) {
25 // Runs during static initialisation, before the logger exists, so a duplicate is
26 // reported only through the return value.
27 auto lowered = to_lower(key);
28 if (factory == nullptr || find(lowered) != nullptr) {
29 return false;
30 }
31 entries_.push_back(Entry{std::move(lowered), factory});
32 return true;
33}
34
35BrokerRegistry::Factory BrokerRegistry::find(std::string_view key) const noexcept {
36 for (const auto& entry : entries_) {
37 if (entry.key.size() == key.size() &&
38 std::equal(entry.key.begin(), entry.key.end(), key.begin(), [](char a, unsigned char b) {
39 return a == static_cast<char>(std::tolower(b));
40 })) {
41 return entry.factory;
42 }
43 }
44 return nullptr;
45}
46
47std::shared_ptr<IBroker> BrokerRegistry::create(std::string_view key, const json& config,
48 slick::stream_buffer_multiplexer& event_mux,
49 slick::queue<uint8_t>& control_queue,
50 slick::queue<uint8_t>& md_request_queue,
51 slick::queue<uint8_t>& order_request_queue,
52 std::string& error) const {
53 const Factory factory = find(key);
54 if (factory == nullptr) {
55 error = "no adapter for '" + std::string(key) + "' is built into this binary";
56 return nullptr;
57 }
58 try {
59 auto broker = factory(config, event_mux, control_queue, md_request_queue, order_request_queue);
60 if (!broker) {
61 error = "its factory returned nothing";
62 }
63 return broker;
64 } catch (const std::exception& e) {
65 error = e.what();
66 } catch (...) {
67 error = "its constructor threw something that is not a std::exception";
68 }
69 return nullptr;
70}
71
72std::vector<std::string> BrokerRegistry::keys() const {
73 std::vector<std::string> result;
74 result.reserve(entries_.size());
75 for (const auto& entry : entries_) {
76 result.push_back(entry.key);
77 }
78 return result;
79}
80
81} // namespace slick::trader
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