PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
native_order_identity.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <iosfwd>
5#include <mutex>
6#include <string>
7#include <string_view>
8#include <type_traits>
9#include <unordered_set>
10
12inline namespace native_order_v1 {
13
14// Stable native-v1 identity leaf. Request/core/event values live in
15// native_order_v4; do not duplicate these types there.
16
18public:
19 SessionKey() noexcept : ptr_(&empty_string()) {}
20 SessionKey(const std::string& s) : ptr_(intern(s)) {}
21 SessionKey(std::string_view s) : ptr_(intern(s)) {}
22 SessionKey(const char* s) : ptr_(s ? intern(s) : &empty_string()) {}
23 SessionKey(const char* s, std::size_t count) : ptr_(intern(std::string_view(s, count))) {}
24
25 SessionKey(const SessionKey&) noexcept = default;
26 SessionKey& operator=(const SessionKey&) noexcept = default;
27 SessionKey(SessionKey&&) noexcept = default;
28 SessionKey& operator=(SessionKey&&) noexcept = default;
29
30 SessionKey& operator=(const std::string& s) {
31 ptr_ = intern(s);
32 return *this;
33 }
34 SessionKey& operator=(std::string_view s) {
35 ptr_ = intern(s);
36 return *this;
37 }
38 SessionKey& operator=(const char* s) {
39 ptr_ = s ? intern(s) : &empty_string();
40 return *this;
41 }
42
43 void clear() noexcept { ptr_ = &empty_string(); }
44 bool empty() const noexcept { return ptr_->empty(); }
45 std::size_t size() const noexcept { return ptr_->size(); }
46 std::size_t length() const noexcept { return ptr_->length(); }
47 const char* c_str() const noexcept { return ptr_->c_str(); }
48 const char* data() const noexcept { return ptr_->data(); }
49
50 const std::string& string() const noexcept { return *ptr_; }
51 operator const std::string&() const noexcept { return *ptr_; }
52
53 char operator[](std::size_t i) const noexcept { return (*ptr_)[i]; }
54
55 SessionKey& operator+=(const std::string& extra) {
56 *this = SessionKey(*ptr_ + extra);
57 return *this;
58 }
59 SessionKey& operator+=(const char* extra) {
60 *this = SessionKey(*ptr_ + (extra ? extra : ""));
61 return *this;
62 }
63
64 bool operator==(const SessionKey& other) const noexcept { return ptr_ == other.ptr_; }
65 bool operator!=(const SessionKey& other) const noexcept { return ptr_ != other.ptr_; }
66 bool operator<(const SessionKey& other) const noexcept { return *ptr_ < *other.ptr_; }
67
68 bool operator==(const std::string& other) const noexcept { return *ptr_ == other; }
69 bool operator!=(const std::string& other) const noexcept { return *ptr_ != other; }
70 bool operator<(const std::string& other) const noexcept { return *ptr_ < other; }
71
72 bool operator==(std::string_view other) const noexcept { return *ptr_ == other; }
73 bool operator!=(std::string_view other) const noexcept { return *ptr_ != other; }
74
75 bool operator==(const char* other) const noexcept {
76 return other ? *ptr_ == other : ptr_->empty();
77 }
78 bool operator!=(const char* other) const noexcept { return !(*this == other); }
79
80 friend bool operator==(const std::string& a, const SessionKey& b) noexcept { return a == *b.ptr_; }
81 friend bool operator!=(const std::string& a, const SessionKey& b) noexcept { return a != *b.ptr_; }
82 friend bool operator<(const std::string& a, const SessionKey& b) noexcept { return a < *b.ptr_; }
83
84 friend bool operator==(const char* a, const SessionKey& b) noexcept {
85 return a ? a == *b.ptr_ : b.ptr_->empty();
86 }
87 friend bool operator!=(const char* a, const SessionKey& b) noexcept { return !(a == b); }
88
89 friend std::string operator+(const SessionKey& a, const std::string& b) { return a.string() + b; }
90 friend std::string operator+(const std::string& a, const SessionKey& b) { return a + b.string(); }
91 friend std::string operator+(const SessionKey& a, const char* b) { return a.string() + (b ? b : ""); }
92 friend std::string operator+(const char* a, const SessionKey& b) { return (a ? a : "") + b.string(); }
93 friend std::string operator+(const SessionKey& a, const SessionKey& b) { return a.string() + b.string(); }
94
95 friend std::ostream& operator<<(std::ostream& os, const SessionKey& key) {
96 return os << *key.ptr_;
97 }
98
99 friend void append(std::string& out, const SessionKey& value) {
100 const auto size = value.size();
101 out.append(reinterpret_cast<const char*>(&size), sizeof(size));
102 out.append(value.data(), value.size());
103 }
104
105private:
106 static const std::string& empty_string() {
107 static const std::string empty;
108 return empty;
109 }
110
111 static const std::string* intern(std::string_view s) {
112 if (s.empty()) return &empty_string();
113 static std::mutex mutex;
114 static std::unordered_set<std::string> pool;
115 std::lock_guard<std::mutex> lock(mutex);
116 auto it = pool.find(std::string(s));
117 if (it != pool.end()) return &*it;
118 auto [inserted, _] = pool.emplace(s);
119 return &*inserted;
120 }
121
122 const std::string* ptr_ = &empty_string();
123};
124
129
130inline bool operator==(const RunIdentity& a, const RunIdentity& b) {
131 return a.run_number == b.run_number && a.session_key == b.session_key;
132}
133inline bool operator!=(const RunIdentity& a, const RunIdentity& b) { return !(a == b); }
134
137 uint64_t incarnation = 0;
138};
139
140inline bool operator==(const RequestHandle& a, const RequestHandle& b) {
141 return a.incarnation == b.incarnation && a.run == b.run;
142}
143inline bool operator!=(const RequestHandle& a, const RequestHandle& b) { return !(a == b); }
144
145struct Birth {
146 uint64_t acceptance_ordinal = 0;
148};
149
150inline bool operator==(const Birth& a, const Birth& b) {
151 return a.acceptance_ordinal == b.acceptance_ordinal
152 && a.decision_time_lower_bound == b.decision_time_lower_bound;
153}
154inline bool operator!=(const Birth& a, const Birth& b) { return !(a == b); }
155
156inline bool point_eligible(const Birth& birth,
157 uint64_t point_ordinal,
158 int64_t effective_time_ms) noexcept {
159 return point_ordinal > birth.acceptance_ordinal
160 && effective_time_ms >= birth.decision_time_lower_bound;
161}
162
163static_assert(std::is_nothrow_move_constructible_v<RunIdentity>);
164static_assert(std::is_nothrow_move_assignable_v<RunIdentity>);
165static_assert(std::is_nothrow_move_constructible_v<RequestHandle>);
166static_assert(std::is_nothrow_move_assignable_v<RequestHandle>);
167static_assert(std::is_nothrow_move_constructible_v<Birth>);
168
169} // inline namespace native_order_v1
170} // namespace pineforge::native_order
friend std::ostream & operator<<(std::ostream &os, const SessionKey &key)
friend bool operator==(const char *a, const SessionKey &b) noexcept
bool operator!=(std::string_view other) const noexcept
bool operator<(const SessionKey &other) const noexcept
friend bool operator!=(const std::string &a, const SessionKey &b) noexcept
bool operator<(const std::string &other) const noexcept
friend std::string operator+(const std::string &a, const SessionKey &b)
bool operator!=(const std::string &other) const noexcept
friend bool operator==(const std::string &a, const SessionKey &b) noexcept
friend std::string operator+(const char *a, const SessionKey &b)
bool operator==(const SessionKey &other) const noexcept
friend std::string operator+(const SessionKey &a, const char *b)
bool operator!=(const SessionKey &other) const noexcept
friend bool operator<(const std::string &a, const SessionKey &b) noexcept
friend bool operator!=(const char *a, const SessionKey &b) noexcept
friend std::string operator+(const SessionKey &a, const std::string &b)
bool operator==(const std::string &other) const noexcept
friend void append(std::string &out, const SessionKey &value)
friend std::string operator+(const SessionKey &a, const SessionKey &b)
SessionKey & operator=(const SessionKey &) noexcept=default
SessionKey(const SessionKey &) noexcept=default
bool operator==(std::string_view other) const noexcept
bool operator==(const RunIdentity &a, const RunIdentity &b)
bool point_eligible(const Birth &birth, uint64_t point_ordinal, int64_t effective_time_ms) noexcept
bool operator!=(const RunIdentity &a, const RunIdentity &b)