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 is a refusal resource, not a tenth byte region.
25
26inline const char* scratch_resource_name(ScratchResource resource) noexcept {
27 switch (resource) {
28 case ScratchResource::layout: return "layout";
29 case ScratchResource::sequence_data: return "sequence_data";
30 case ScratchResource::sequence_index: return "sequence_index";
31 case ScratchResource::erased_data: return "erased_data";
32 case ScratchResource::erased_index: return "erased_index";
33 case ScratchResource::consumed_data: return "consumed_data";
34 case ScratchResource::consumed_index: return "consumed_index";
35 case ScratchResource::cohort: return "cohort";
36 case ScratchResource::exit_rank: return "exit_rank";
37 case ScratchResource::successor_roots: return "successor_roots";
38 }
39 return "layout";
40}
41
42// Locale-independent decimal grouping, shared by all count/byte refusals.
43inline std::string scratch_limit_number(std::uint64_t limit) {
44 char digits[26]; // 20 decimal digits and six separators fit.
45 auto* end = digits + sizeof digits;
46 auto* at = end;
47 unsigned group = 0;
48 do {
49 if (group == 3) {
50 *--at = ',';
51 group = 0;
52 }
53 *--at = static_cast<char>('0' + limit % 10);
54 limit /= 10;
55 ++group;
56 } while (limit != 0);
57 return {at, end};
58}
59
60inline std::string scratch_limit_message(const char* prefix, std::uint64_t limit,
61 const char* suffix) {
62 std::string message(prefix);
63 message += scratch_limit_number(limit);
64 message += suffix;
65 return message;
66}
67
68inline std::string scratch_count_limit_message(ScratchResource resource, std::uint64_t limit) {
69 switch (resource) {
71 return scratch_limit_message("more than ", limit,
72 " distinct order ids in one run; reuse ids, e.g. build the id from a modulo of bar_index");
74 return scratch_limit_message("more than ", limit,
75 " retired order-id keys in one run (each retired order uses 2); reuse ids, e.g. a modulo of bar_index");
77 return scratch_limit_message("more than ", limit,
78 " keys for partially filled exits in one run; reuse ids, e.g. a modulo of bar_index");
80 return scratch_limit_message("scratch capacity exceeded: more than ", limit,
81 " exit rank ids in one run; reuse ids");
82 default:
83 return scratch_limit_message("scratch capacity exceeded: more than ", limit,
84 " keys in one run; reuse ids");
85 }
86}
87
88inline std::string scratch_byte_limit_message(ScratchResource resource, std::uint64_t limit) {
89 switch (resource) {
91 return scratch_limit_message("order ids use more than ", limit,
92 " bytes in one run; use shorter ids or reuse ids");
94 return scratch_limit_message("retired order ids use more than ", limit,
95 " bytes in one run; use shorter ids or reuse ids");
97 return scratch_limit_message("partially filled exits use more than ", limit,
98 " bytes in one run; use shorter ids or reuse ids");
100 return scratch_limit_message("scratch capacity exceeded: exit rank ids use more than ", limit,
101 " bytes in one run; use shorter ids or reuse ids");
102 default:
103 return scratch_limit_message("scratch capacity exceeded: ids use more than ", limit,
104 " bytes in one run; use shorter ids or reuse ids");
105 }
106}
107
108inline std::string scratch_resident_cohort_limit_message(std::uint64_t limit) {
109 return scratch_limit_message("more than ", limit,
110 " order ids active at once (placed and not yet closed or cancelled); "
111 "close or cancel orders sooner, or reuse ids");
112}
113
114class ScratchCapacityError final : public std::runtime_error, public RunFailureInfo {
115public:
117 const char* operation = "scratch capacity exceeded")
118 : std::runtime_error(operation),
120 {{"resource", scratch_resource_name(resource)}}) {}
121 ScratchCapacityError(ScratchResource resource, const std::string& message)
122 : std::runtime_error(message),
124 {{"resource", scratch_resource_name(resource)}}) {}
125};
126
127// Preserve errno and the existing system_error English; only table scratch
128// failures use this code. The report sink retains report_sink_failed.
129class AuxiliaryScratchIoError final : public std::system_error, public RunFailureInfo {
130public:
131 AuxiliaryScratchIoError(int error, const char* operation)
132 : std::system_error(error, std::generic_category(), operation),
134 explicit AuxiliaryScratchIoError(const std::system_error& error)
135 : std::system_error(error),
137};
138
139[[noreturn]] inline void raise_scratch_io_error(int error, const char* operation) {
140 throw AuxiliaryScratchIoError(error, operation);
141}
142
143} // 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_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)