Repository navigation
Expand file tree
/
Copy pathoptions.cpp
More file actions
218 lines (194 loc) · 7.54 KB
/
Copy pathoptions.cpp
File metadata and controls
218 lines (194 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "engine/framework/runtime/options.h"
#include <chrono>
#include <cmath>
#include <limits>
#include <random>
#include <stdexcept>
namespace engine::runtime {
namespace {
int parse_int_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const long long out = std::stoll(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an integer");
}
if (out < std::numeric_limits<int>::min() || out > std::numeric_limits<int>::max()) {
throw std::runtime_error(std::string(key) + " is outside int range");
}
return static_cast<int>(out);
}
float parse_float_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const float out = std::stof(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be a float");
}
return out;
}
uint32_t parse_u32_value(const std::string & value, std::string_view key) {
if (!value.empty() && value.front() == '-') {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
size_t parsed = 0;
const unsigned long out = std::stoul(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
if (out > std::numeric_limits<uint32_t>::max()) {
throw std::runtime_error(std::string(key) + " is outside uint32 range");
}
return static_cast<uint32_t>(out);
}
uint64_t parse_u64_value(const std::string & value, std::string_view key) {
if (!value.empty() && value.front() == '-') {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
size_t parsed = 0;
const unsigned long long out = std::stoull(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an unsigned integer");
}
if (out > std::numeric_limits<uint64_t>::max()) {
throw std::runtime_error(std::string(key) + " is outside uint64 range");
}
return static_cast<uint64_t>(out);
}
int64_t parse_i64_value(const std::string & value, std::string_view key) {
size_t parsed = 0;
const int64_t out = std::stoll(value, &parsed);
if (parsed != value.size()) {
throw std::runtime_error(std::string(key) + " must be an integer");
}
return out;
}
size_t checked_mib_to_bytes(int64_t mb, std::string_view key) {
constexpr int64_t kMiB = 1024 * 1024;
if (mb > static_cast<int64_t>(std::numeric_limits<size_t>::max() / kMiB)) {
throw std::runtime_error(std::string(key) + " is too large");
}
return static_cast<size_t>(mb) * static_cast<size_t>(kMiB);
}
} // namespace
std::optional<OptionMatch> find_option_match(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
std::optional<OptionMatch> match;
for (const std::string_view key : keys) {
const auto it = options.find(std::string(key));
if (it != options.end() && !it->second.empty()) {
if (match.has_value()) {
if (match->value != it->second) {
throw std::runtime_error(
"conflicting option values for " + match->key + " and " + std::string(key));
}
continue;
}
match = OptionMatch{std::string(key), it->second};
}
}
return match;
}
std::optional<std::string> find_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
const auto match = find_option_match(options, keys);
return match.has_value() ? std::optional<std::string>(match->value) : std::nullopt;
}
bool parse_bool_option(const std::string & value, std::string_view key) {
if (value == "true" || value == "1" || value == "yes" || value == "on") {
return true;
}
if (value == "false" || value == "0" || value == "no" || value == "off") {
return false;
}
throw std::runtime_error(std::string(key) + " must be true/false, yes/no, on/off, or 1/0");
}
std::optional<int> parse_int_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_int_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<float> parse_float_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_float_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<float> parse_finite_float_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
const float value = parse_float_value(match->value, match->key);
if (!std::isfinite(value)) {
throw std::runtime_error(match->key + " must be a finite float");
}
return value;
}
return std::nullopt;
}
std::optional<uint32_t> parse_u32_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_u32_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<uint64_t> parse_u64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_u64_value(match->value, match->key);
}
return std::nullopt;
}
std::optional<int64_t> parse_i64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys) {
if (const auto match = find_option_match(options, keys)) {
return parse_i64_value(match->value, match->key);
}
return std::nullopt;
}
int64_t parse_positive_i64_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys,
int64_t fallback) {
if (const auto match = find_option_match(options, keys)) {
const int64_t value = parse_i64_value(match->value, match->key);
if (value <= 0) {
throw std::runtime_error(match->key + " must be positive");
}
return value;
}
return fallback;
}
size_t parse_size_mb_option(
const std::unordered_map<std::string, std::string> & options,
std::initializer_list<std::string_view> keys,
size_t fallback_bytes) {
if (const auto match = find_option_match(options, keys)) {
const int64_t mb = parse_i64_value(match->value, match->key);
if (mb <= 0) {
throw std::runtime_error(match->key + " must be positive");
}
return checked_mib_to_bytes(mb, match->key);
}
return fallback_bytes;
}
uint64_t random_u64_seed() {
std::random_device random_device;
const uint64_t hi = static_cast<uint64_t>(random_device()) << 32U;
const uint64_t lo = static_cast<uint64_t>(random_device());
const auto now = std::chrono::high_resolution_clock::now().time_since_epoch().count();
return hi ^ lo ^ static_cast<uint64_t>(now);
}
uint32_t random_u32_seed() {
return static_cast<uint32_t>(random_u64_seed());
}
} // namespace engine::runtime