Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
cl_ord_id_index.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cstddef>
5#include <cstdint>
6#include <functional>
7#include <unordered_map>
8
10
17template <typename Key, typename Hash = std::hash<Key>, typename Eq = std::equal_to<Key>>
19public:
21 template <typename K>
22 [[nodiscard]] uint64_t find(const K &id) const noexcept {
23 const auto it = ids_.find(id);
24 return it == ids_.end() || !it->second.held ? 0 : it->second.order;
25 }
26
29 [[nodiscard]] bool claim(const Key &id, uint64_t order_num = 0) {
30 const auto [it, inserted] = ids_.try_emplace(id, Entry{order_num});
31 if (!inserted && (order_num == 0 || it->second.order != order_num)) {
32 return false;
33 }
34 ++it->second.claims;
35 return true;
36 }
37
40 template <typename K>
41 [[nodiscard]] bool claim(const K &id, uint64_t order_num = 0) {
42 return claim(Key(id), order_num);
43 }
44
46 template <typename K>
47 void release(const K &id) noexcept {
48 if (const auto it = ids_.find(id); it != ids_.end() && it->second.claims > 0) {
49 --it->second.claims;
50 erase_if_unused(it);
51 }
52 }
53
55 template <typename K>
56 void assign(const K &id, uint64_t order_num) {
57 assert(order_num != 0);
58 auto &entry = ids_[Key(id)];
59 // Assigning an id another order holds would leave that order unfindable: the
60 // caller must `forget` it first, as `rename` does.
61 assert(!entry.held || entry.order == order_num);
62 entry.order = order_num;
63 entry.held = true;
64 }
65
67 template <typename K>
68 void forget(const K &id, uint64_t order_num) noexcept {
69 if (const auto it = ids_.find(id); it != ids_.end() && it->second.order == order_num) {
70 it->second.held = false;
71 erase_if_unused(it);
72 }
73 }
74
76 template <typename K>
77 void rename(const K &from, const K &to, uint64_t order_num) {
78 forget(from, order_num);
79 assign(to, order_num);
80 }
81
83 [[nodiscard]] size_t size() const noexcept { return ids_.size(); }
84 [[nodiscard]] bool empty() const noexcept { return ids_.empty(); }
85
86private:
87 struct Entry {
88 uint64_t order = 0;
89 uint32_t claims = 0;
90 bool held = false;
91 };
92 using Map = std::unordered_map<Key, Entry, Hash, Eq>;
93
94 void erase_if_unused(typename Map::iterator it) noexcept {
95 if (it->second.claims == 0 && !it->second.held) {
96 ids_.erase(it);
97 }
98 }
99
100 Map ids_;
101};
102
103} // namespace slick::sim::order_gateway
One session's ClOrdIDs, each mapped to the working order that holds it.
size_t size() const noexcept
How many ids are taken - by a working order, by a request in flight, or both.
bool claim(const Key &id, uint64_t order_num=0)
Claims id for a new order (order_num 0) or for a replace of order_num.
void rename(const K &from, const K &to, uint64_t order_num)
A replace renamed order_num from from to to.
bool claim(const K &id, uint64_t order_num=0)
The same, by the key's lookup form.
void forget(const K &id, uint64_t order_num) noexcept
order_num no longer holds id: it is done, or was renamed.
uint64_t find(const K &id) const noexcept
The working order named id, or 0 when no order holds it (a request may).
void release(const K &id) noexcept
The request that claimed id was answered.
void assign(const K &id, uint64_t order_num)
id now names order_num.