Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
sequenced_message.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <charconv>
4#include <cstdint>
5#include <nlohmann/json.hpp>
6#include <string>
7#include <string_view>
8
10
25public:
26 explicit SequencedMessage(nlohmann::json &message) {
27 message["sequence_num"] = 0;
28 prefix_ = message.dump();
29
30 static constexpr std::string_view key = "\"sequence_num\":";
31 const auto at = prefix_.find(key);
32 if (at == std::string::npos) [[unlikely]] {
33 // Nothing to stamp. Every fan-out message carries the field, but sending
34 // the document unchanged is the safe answer if one ever does not.
35 sequenced_ = false;
36 return;
37 }
38
39 const auto value_at = at + key.size();
40 auto end = value_at;
41 while (end < prefix_.size() && prefix_[end] >= '0' && prefix_[end] <= '9') {
42 ++end;
43 }
44 suffix_ = prefix_.substr(end);
45 prefix_.resize(value_at);
46 }
47
50 std::string_view render(uint64_t sequence_num) {
51 if (!sequenced_) [[unlikely]] {
52 return prefix_;
53 }
54 char digits[20];
55 const auto result = std::to_chars(digits, digits + sizeof(digits), sequence_num);
56 buffer_.assign(prefix_);
57 buffer_.append(digits, static_cast<size_t>(result.ptr - digits));
58 buffer_.append(suffix_);
59 return buffer_;
60 }
61
62private:
65 std::string prefix_;
66 std::string suffix_;
69 std::string buffer_;
70 bool sequenced_ = true;
71};
72
73} // end namespace slick::sim::md_publisher
std::string_view render(uint64_t sequence_num)
The payload for one subscriber.