PineForge unreleased
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
scratch_errors.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <cstdint>
6#include <stdexcept>
7#include <string>
8#include <system_error>
9
10namespace pineforge::detail {
11
12// Closed vocabulary: layout and cohort capacity labels are not byte regions.
27
28inline const char* scratch_resource_name(ScratchResource resource) noexcept {
29 switch (resource) {
30 case ScratchResource::layout: return "layout";
31 case ScratchResource::sequence_data: return "sequence_data";
32 case ScratchResource::sequence_index: return "sequence_index";
33 case ScratchResource::erased_data: return "erased_data";
34 case ScratchResource::erased_index: return "erased_index";
35 case ScratchResource::consumed_data: return "consumed_data";
36 case ScratchResource::consumed_index: return "consumed_index";
37 case ScratchResource::cohort: return "cohort";
38 case ScratchResource::exit_rank: return "exit_rank";
39 case ScratchResource::successor_roots: return "successor_roots";
40 case ScratchResource::cohort_ids_total: return "cohort_ids_total";
41 case ScratchResource::cohort_orders_live: return "cohort_orders_live";
42 }
43 return "layout";
44}
45
46// Locale-independent decimal grouping, shared by all count/byte refusals.
47inline std::string scratch_limit_number(std::uint64_t limit) {
48 char digits[26]; // 20 decimal digits and six separators fit.
49 auto* end = digits + sizeof digits;
50 auto* at = end;
51 unsigned group = 0;
52 do {
53 if (group == 3) {
54 *--at = ',';
55 group = 0;
56 }
57 *--at = static_cast<char>('0' + limit % 10);
58 limit /= 10;
59 ++group;
60 } while (limit != 0);
61 return {at, end};
62}
63
64inline std::string scratch_limit_message(const char* prefix, std::uint64_t limit,
65 const char* suffix) {
66 std::string message(prefix);
67 message += scratch_limit_number(limit);
68 message += suffix;
69 return message;
70}
71
72inline std::string scratch_count_limit_message(ScratchResource resource, std::uint64_t limit) {
73 switch (resource) {
75 return scratch_limit_message("more than ", limit,
76 " distinct order ids in one run; reuse ids, e.g. build the id from a modulo of bar_index");
78 return scratch_limit_message("more than ", limit,
79 " retired order-id keys in one run (each retired order uses 2); reuse ids, e.g. a modulo of bar_index");
81 return scratch_limit_message("more than ", limit,
82 " keys for partially filled exits in one run; reuse ids, e.g. a modulo of bar_index");
84 return scratch_limit_message("scratch capacity exceeded: more than ", limit,
85 " exit rank ids in one run; reuse ids");
86 default:
87 return scratch_limit_message("scratch capacity exceeded: more than ", limit,
88 " keys in one run; reuse ids");
89 }
90}
91
92inline std::string scratch_byte_limit_message(ScratchResource resource, std::uint64_t limit) {
93 switch (resource) {
95 return scratch_limit_message("order ids use more than ", limit,
96 " bytes in one run; use shorter ids or reuse ids");
98 return scratch_limit_message("retired order ids use more than ", limit,
99 " bytes in one run; use shorter ids or reuse ids");
101 return scratch_limit_message("partially filled exits use more than ", limit,
102 " bytes in one run; use shorter ids or reuse ids");
104 return scratch_limit_message("scratch capacity exceeded: exit rank ids use more than ", limit,
105 " bytes in one run; use shorter ids or reuse ids");
106 default:
107 return scratch_limit_message("scratch capacity exceeded: ids use more than ", limit,
108 " bytes in one run; use shorter ids or reuse ids");
109 }
110}
111
112inline std::string scratch_resident_cohort_limit_message(std::uint64_t limit) {
113 return scratch_limit_message("more than ", limit,
114 " order ids active at once (placed and not yet closed or cancelled); "
115 "close or cancel orders sooner, or reuse ids");
116}
117
118inline std::string scratch_object_limit_message(std::uint64_t limit,
119 std::uint64_t live,
120 std::uint64_t cap) {
121 std::string message = scratch_limit_message("more than ", limit,
122 " store objects live at once; ");
123 message += scratch_limit_number(limit);
124 message += " is the most allowed in this store; attempted live count ";
125 message += scratch_limit_number(live);
126 message += ", approved maximum ";
127 message += scratch_limit_number(cap);
128 return message;
129}
130
131class ScratchCapacityError final : public std::runtime_error, public RunFailureInfo {
132public:
134 const char* operation = "scratch capacity exceeded")
135 : std::runtime_error(operation),
137 {{"resource", scratch_resource_name(resource)}}) {}
138 ScratchCapacityError(ScratchResource resource, const std::string& message)
139 : std::runtime_error(message),
141 {{"resource", scratch_resource_name(resource)}}) {}
142};
143
144// Preserve errno and the existing system_error English; only table scratch
145// failures use this code. The report sink retains report_sink_failed.
146class AuxiliaryScratchIoError final : public std::system_error, public RunFailureInfo {
147public:
148 AuxiliaryScratchIoError(int error, const char* operation)
149 : std::system_error(error, std::generic_category(), operation),
151 explicit AuxiliaryScratchIoError(const std::system_error& error)
152 : std::system_error(error),
154};
155
156[[noreturn]] inline void raise_scratch_io_error(int error, const char* operation) {
157 throw AuxiliaryScratchIoError(error, operation);
158}
159
160} // namespace pineforge::detail
RunFailureInfo(RunFailureCode code, const RunFailureArgs &args) noexcept
AuxiliaryScratchIoError(const std::system_error &error)
AuxiliaryScratchIoError(int error, const char *operation)
ScratchCapacityError(ScratchResource resource, const std::string &message)
ScratchCapacityError(ScratchResource resource, const char *operation="scratch capacity exceeded")
std::string scratch_object_limit_message(std::uint64_t limit, std::uint64_t live, std::uint64_t cap)
std::string scratch_count_limit_message(ScratchResource resource, std::uint64_t limit)
std::string scratch_byte_limit_message(ScratchResource resource, std::uint64_t limit)
std::string scratch_limit_message(const char *prefix, std::uint64_t limit, const char *suffix)
const char * scratch_resource_name(ScratchResource resource) noexcept
void raise_scratch_io_error(int error, const char *operation)
std::string scratch_limit_number(std::uint64_t limit)
std::string scratch_resident_cohort_limit_message(std::uint64_t limit)