Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
instance_expansion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <nlohmann/json.hpp>
4#include <slick/logger.hpp>
5#include <format>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
15namespace slick::sim::exch {
16
27template <typename DefaultName>
28std::vector<nlohmann::json> expand_instances(const nlohmann::json &config, std::string_view list_key,
29 std::string_view venue, DefaultName &&default_name) {
30 std::vector<nlohmann::json> out;
31 const std::string key(list_key);
32 if (!config.contains(key)) {
33 auto &single = out.emplace_back(config);
34 if (!single.contains("name")) {
35 single["name"] = std::string(venue);
36 }
37 return out;
38 }
39
40 const auto &list = config[key];
41 if (!list.is_array() || list.empty()) {
42 throw std::runtime_error(std::format("{}: \"{}\" must be a non-empty array", venue, key));
43 }
44 nlohmann::json shared = config;
45 shared.erase(key);
46 for (size_t i = 0; i < list.size(); ++i) {
47 // RFC 7386 treats a non-object patch as a replacement, so a typo here would
48 // silently drop every shared key - or, for a string, throw from nlohmann a long
49 // way from the config line that caused it.
50 if (!list[i].is_object()) {
51 throw std::runtime_error(std::format("{}: \"{}\"[{}] must be an object", venue, key, i));
52 }
53 nlohmann::json instance = shared;
54 instance.merge_patch(list[i]);
55 if (!instance.contains("name")) {
56 instance["name"] = default_name(instance, i);
57 }
58 if (!instance.value("enabled", true)) {
59 LOG_INFO("{} {} is disabled, skipping", venue, instance["name"].get<std::string>());
60 continue;
61 }
62 out.push_back(std::move(instance));
63 }
64 return out;
65}
66
70template <typename T>
71void claim(std::unordered_map<T, std::string> &claimed, const T &value, const std::string &owner,
72 std::string_view what, std::string_view venue) {
73 auto [it, inserted] = claimed.emplace(value, owner);
74 if (inserted) {
75 return;
76 }
77 if (it->second == owner) {
78 throw std::runtime_error(std::format("{}: '{}' uses {} {} twice", venue, owner, what, value));
79 }
80 throw std::runtime_error(std::format("{}: '{}' and '{}' both use {} {}", venue, it->second, owner, what, value));
81}
82
84inline std::string group_key(const nlohmann::json &group) {
85 return std::format("{}:{}", group.at("address").get<std::string>(), group.at("port").get<uint16_t>());
86}
87
88} // namespace slick::sim::exch
Helpers for a venue whose one config entry describes several independent exchange instances,...
Definition exchange.hpp:30
std::vector< nlohmann::json > expand_instances(const nlohmann::json &config, std::string_view list_key, std::string_view venue, DefaultName &&default_name)
Expands a venue's config entry into one config per instance listed under list_key.
void claim(std::unordered_map< T, std::string > &claimed, const T &value, const std::string &owner, std::string_view what, std::string_view venue)
Records that instance owner uses value.
std::string group_key(const nlohmann::json &group)
"address:port" of a { "address", "port" } multicast group config, as claimed.