Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
reject_commands.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "cli_endpoint.hpp"
4#include <algorithm>
5#include <format>
6#include <string>
7#include <string_view>
8#include <vector>
9
12namespace slick::sim::cli {
13
17template <typename Set>
18bool toggle_id(Set &ids, const Command &command, std::string &out) {
19 const auto id = command.arg_view();
20 if (id.empty()) {
21 out = std::format("{} needs an ID", to_string(command.command));
22 return false;
23 }
24 const bool on = command.flag != 0;
25 bool changed;
26 if (on) {
27 changed = ids.emplace(id).second;
28 } else {
29 auto it = ids.find(id);
30 changed = it != ids.end();
31 if (changed) {
32 ids.erase(it);
33 }
34 }
35 out = std::format("{} {} {}{}", to_string(command.command), on ? "on" : "off", id, changed ? "" : " (unchanged)");
36 return true;
37}
38
40template <typename Set>
41void list_ids(const Set &ids, std::string_view name, std::string &out) {
42 std::vector<std::string_view> sorted(ids.begin(), ids.end());
43 std::sort(sorted.begin(), sorted.end());
44 out += name;
45 out += ':';
46 if (sorted.empty()) {
47 out += " (none)";
48 }
49 for (auto id : sorted) {
50 out += ' ';
51 out += id;
52 }
53}
54
59template <typename Set, typename OnNewOrders>
60bool serve_rejects(const Command &command, Set &reject_logon, Set &reject_new_orders, OnNewOrders &&on_new_orders,
61 std::string &out) {
62 switch (command.command) {
64 return toggle_id(reject_logon, command, out);
66 if (!toggle_id(reject_new_orders, command, out)) {
67 return false;
68 }
69 on_new_orders(command.arg_view(), command.flag != 0);
70 return true;
73 out += '\n';
75 return true;
76 default:
77 return refuse_unserved(command, out);
78 }
79}
80
81} // namespace slick::sim::cli
The CLI commands that switch a gateway's business rejects per ID: REJECT_LOGON, REJECT_NEW_ORDERS and...
void list_ids(const Set &ids, std::string_view name, std::string &out)
"<name>: a b c", sorted, or "<name>: (none)".
bool refuse_unserved(const Command &command, std::string &out)
A handler's answer to a command it doesn't serve: says so in out, false.
bool serve_rejects(const Command &command, Set &reject_logon, Set &reject_new_orders, OnNewOrders &&on_new_orders, std::string &out)
Serves the reject commands against a gateway's two lists.
bool toggle_id(Set &ids, const Command &command, std::string &out)
Adds the command's ID to ids (flag 1) or removes it (flag 0), and says so in out.
@ REJECT_LOGON
A venue. flag 1 refuses the logons of ID arg from now on, 0 stops.
@ LIST_REJECTS
A venue. The IDs both of the above apply to.
@ REJECT_NEW_ORDERS
A venue. flag 1 rejects every new order of ID arg, 0 stops.
constexpr const char * to_string(FeedState state) noexcept
One command, as the CLI server hands it to the thread that runs it.
std::string_view arg_view() const noexcept