BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Options.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <functional>
23#include <magic_enum/magic_enum.hpp>
24#include <ranges>
25#include <span>
26#include <stdexcept>
27#include <string>
28#include <string_view>
29#include <type_traits>
30#include <utility>
31#include <variant>
32#include <vector>
33
34namespace chess::uci {
35
36using std::string;
37using std::string_view;
38
43struct Option {
44 Option() = default;
45
46 virtual ~Option();
47
48 Option(const Option&) = default;
49 Option(Option&&) = default;
50 Option& operator=(const Option&) = default;
51 Option& operator=(Option&&) = default;
52
54 [[nodiscard]] virtual auto get_name() const noexcept -> string_view = 0;
55
59 [[nodiscard]] virtual auto get_declaration_string() const -> string = 0;
60
62 [[nodiscard]] virtual auto get_type() const noexcept -> string_view = 0;
63
65 [[nodiscard]] virtual auto get_help() const noexcept -> string_view = 0;
66
70 [[nodiscard]] virtual auto has_value() const noexcept -> bool { return true; }
71
73 using Variant = std::variant<bool, int, string_view>;
74
78 [[nodiscard]] virtual auto get_value_variant() const -> Variant = 0;
79
83 [[nodiscard]] virtual auto get_default_value_variant() const -> Variant = 0;
84
86 virtual void reset_to_default_value() = 0;
87
91 virtual void handle_setvalue(string_view arguments) = 0;
92};
93
98struct BoolOption final : Option {
99 using Value = bool;
100 using Callback = std::function<void(bool)>;
101
104 string name, bool defaultValue,
105 string helpString,
106 Callback&& changeCallback = [](bool) { });
107
111 [[nodiscard]] auto get_value() const noexcept -> bool { return value; }
112
114 void set_value(bool newValue);
115
116 [[nodiscard]] auto get_value_variant() const -> Variant override { return value; }
117
119 [[nodiscard]] auto get_default_value() const noexcept -> bool { return optionDefault; }
120
121 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return optionDefault; }
122
123 void reset_to_default_value() override { value = optionDefault; }
124
125 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
126
127 [[nodiscard]] auto get_declaration_string() const -> string override;
128
129 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Toggle"; }
130
131 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
132
133 void handle_setvalue(string_view arguments) override;
134
135private:
136 string optionName;
137
138 bool optionDefault { true };
139
140 bool value { optionDefault };
141
142 string help;
143
144 Callback onChange { [](bool) { } };
145};
146
151struct IntOption final : Option {
152 using Value = int;
153 using Callback = std::function<void(int)>;
154
157 string name,
158 int minValue, int maxValue,
159 int defaultValue,
160 string helpString,
161 Callback&& changeCallback = [](int) { });
162
166 [[nodiscard]] auto get_value() const noexcept -> int { return value; }
167
169 void set_value(int newValue);
170
171 [[nodiscard]] auto get_value_variant() const -> Variant override { return value; }
172
174 [[nodiscard]] auto get_default_value() const noexcept -> int { return optionDefault; }
175
176 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return optionDefault; }
177
178 void reset_to_default_value() override { value = optionDefault; }
179
180 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
181
182 [[nodiscard]] auto get_declaration_string() const -> string override;
183
184 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Integer"; }
185
186 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
187
188 void handle_setvalue(string_view arguments) override;
189
190private:
191 string optionName;
192
193 int optionMin { 0 };
194 int optionMax { 100 };
195
196 int optionDefault { 0 };
197
198 int value { optionDefault };
199
200 string help;
201
202 Callback onChange { [](int) { } };
203};
204
211 using Value = string_view;
212 using Callback = std::function<void(string_view)>;
213
216 string name,
217 std::vector<string> values,
218 string defaultValue,
219 string helpString,
220 Callback&& changeCallback = [](string_view) { });
221
222 [[nodiscard]] auto get_value() const noexcept -> string_view { return value; } // cppcheck-suppress returnByReference
223
225 void set_value(string_view newValue);
226
227 [[nodiscard]] auto get_value_variant() const -> Variant override { return get_value(); }
228
230 [[nodiscard]] auto get_default_value() const noexcept -> string_view { return optionDefault; } // cppcheck-suppress returnByReference
231
232 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return get_default_value(); }
233
234 void reset_to_default_value() override { value = optionDefault; }
235
236 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
237
238 [[nodiscard]] auto get_declaration_string() const -> string override;
239
240 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Multichoice"; }
241
242 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
243
244 void handle_setvalue(string_view arguments) override;
245
247 [[nodiscard]] auto get_possible_values() const noexcept
248 -> std::span<const string>
249 {
250 return possibleValues;
251 }
252
253private:
254 string optionName;
255
256 std::vector<string> possibleValues;
257
258 string optionDefault;
259
260 string value { optionDefault };
261
262 string help;
263
264 Callback onChange { [](string_view) { } };
265};
266
268template <typename T>
269concept Enum = std::is_enum_v<T>;
270
276template <Enum T>
277struct EnumOption final : ComboOption {
280 string name, T defaultValue, string helpString);
281
283 [[nodiscard]] auto get_enum_value() const -> T;
284};
285
290struct StringOption final : Option {
291 using Value = string_view;
292 using Callback = std::function<void(string_view)>;
293
296 string name,
297 string defaultValue,
298 string helpString,
299 Callback&& changeCallback = [](string_view) { });
300
301 [[nodiscard]] auto get_value() const noexcept -> string_view
302 {
303 if (value == "<empty>")
304 return { };
305
306 return value;
307 }
308
310 void set_value(string_view newValue);
311
312 [[nodiscard]] auto get_value_variant() const -> Variant override { return get_value(); }
313
314 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return optionDefault; }
315
316 void reset_to_default_value() override { value = optionDefault; }
317
318 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
319
320 [[nodiscard]] auto get_declaration_string() const -> string override;
321
322 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "String"; }
323
324 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
325
326 void handle_setvalue(string_view arguments) override;
327
328private:
329 string optionName;
330
331 string optionDefault;
332
333 string value { optionDefault };
334
335 string help;
336
337 Callback onChange { [](string_view) { } };
338};
339
344struct Action final : Option {
345 using Value = void;
346 using Callback = std::function<void()>;
347
350 string name,
351 Callback&& action,
352 string helpString);
353
354 [[nodiscard]] auto get_value_variant() const -> Variant override { throw_value_error(); }
355 [[nodiscard]] auto get_default_value_variant() const -> Variant override { throw_value_error(); }
356
358 void reset_to_default_value() override { }
359
360 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
361
362 [[nodiscard]] auto get_declaration_string() const -> string override;
363
364 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Button"; }
365
366 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
367
368 [[nodiscard]] auto has_value() const noexcept -> bool override { return false; }
369
370 void handle_setvalue(string_view arguments) override;
371
372private:
373 [[noreturn]] static void throw_value_error()
374 {
375 throw std::logic_error { "get_value_variant() called on option of Action type" };
376 }
377
378 string optionName;
379
380 Callback callback;
381
382 string help;
383};
384
385/*
386 ___ ,--,
387 ,---, ,--.'|_ ,--, ,--.'|
388 ,---.'| | | :,' ,--.'| | | :
389 | | : : : ' : | |, : : ' .--.--.
390 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
391 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
392 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
393. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
394' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
395| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
396| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
397 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
398 `----' `----' `--`---' ---`-' `--" `--" `--"
399
400 */
401
402namespace detail {
403 template <Enum T>
404 [[nodiscard]] auto get_all_value_strings()
405 -> std::vector<string>
406 {
407 return magic_enum::enum_names<T>()
408 | std::views::transform([](const string_view str) { return string { str }; })
409 | std::ranges::to<std::vector>();
410 }
411} // namespace detail
412
413template <Enum T>
415 string name, T defaultValue, string helpString)
416 : ComboOption {
417 std::move(name),
418 detail::get_all_value_strings<T>(),
419 std::string { magic_enum::enum_name(defaultValue) },
420 std::move(helpString)
421 }
422{
423}
424
425template <Enum T>
427{
428 return magic_enum::enum_cast<T>(get_value())
429 .value();
430}
431
432} // namespace chess::uci
auto get_all_value_strings() -> std::vector< string >
Definition Options.hpp:404
auto get_type() const noexcept -> string_view override
Definition Options.hpp:364
Action(string name, Callback &&action, string helpString)
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:355
auto get_declaration_string() const -> string override
auto has_value() const noexcept -> bool override
Definition Options.hpp:368
std::function< void()> Callback
Definition Options.hpp:346
auto get_help() const noexcept -> string_view override
Definition Options.hpp:366
auto get_value_variant() const -> Variant override
Definition Options.hpp:354
auto get_name() const noexcept -> string_view override
Definition Options.hpp:360
void reset_to_default_value() override
Definition Options.hpp:358
void handle_setvalue(string_view arguments) override
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:121
void reset_to_default_value() override
Definition Options.hpp:123
auto get_help() const noexcept -> string_view override
Definition Options.hpp:131
std::function< void(bool)> Callback
Definition Options.hpp:100
auto get_value() const noexcept -> bool
Definition Options.hpp:111
BoolOption(string name, bool defaultValue, string helpString, Callback &&changeCallback=[](bool) { })
void set_value(bool newValue)
auto get_default_value() const noexcept -> bool
Definition Options.hpp:119
void handle_setvalue(string_view arguments) override
auto get_value_variant() const -> Variant override
Definition Options.hpp:116
auto get_declaration_string() const -> string override
auto get_name() const noexcept -> string_view override
Definition Options.hpp:125
auto get_type() const noexcept -> string_view override
Definition Options.hpp:129
auto get_type() const noexcept -> string_view override
Definition Options.hpp:240
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:232
auto get_name() const noexcept -> string_view override
Definition Options.hpp:236
auto get_possible_values() const noexcept -> std::span< const string >
Definition Options.hpp:247
auto get_help() const noexcept -> string_view override
Definition Options.hpp:242
auto get_declaration_string() const -> string override
ComboOption(string name, std::vector< string > values, string defaultValue, string helpString, Callback &&changeCallback=[](string_view) { })
std::function< void(string_view)> Callback
Definition Options.hpp:212
void set_value(string_view newValue)
auto get_value_variant() const -> Variant override
Definition Options.hpp:227
auto get_value() const noexcept -> string_view
Definition Options.hpp:222
auto get_default_value() const noexcept -> string_view
Definition Options.hpp:230
void reset_to_default_value() override
Definition Options.hpp:234
void handle_setvalue(string_view arguments) override
EnumOption(string name, T defaultValue, string helpString)
Definition Options.hpp:414
auto get_enum_value() const -> T
Definition Options.hpp:426
void handle_setvalue(string_view arguments) override
std::function< void(int)> Callback
Definition Options.hpp:153
void set_value(int newValue)
void reset_to_default_value() override
Definition Options.hpp:178
auto get_name() const noexcept -> string_view override
Definition Options.hpp:180
auto get_value() const noexcept -> int
Definition Options.hpp:166
auto get_help() const noexcept -> string_view override
Definition Options.hpp:186
auto get_value_variant() const -> Variant override
Definition Options.hpp:171
auto get_default_value() const noexcept -> int
Definition Options.hpp:174
IntOption(string name, int minValue, int maxValue, int defaultValue, string helpString, Callback &&changeCallback=[](int) { })
auto get_declaration_string() const -> string override
auto get_type() const noexcept -> string_view override
Definition Options.hpp:184
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:176
Option & operator=(Option &&)=default
std::variant< bool, int, string_view > Variant
Definition Options.hpp:73
virtual auto get_declaration_string() const -> string=0
virtual auto get_help() const noexcept -> string_view=0
Option(const Option &)=default
virtual auto has_value() const noexcept -> bool
Definition Options.hpp:70
Option(Option &&)=default
Option & operator=(const Option &)=default
virtual auto get_name() const noexcept -> string_view=0
virtual auto get_default_value_variant() const -> Variant=0
virtual void reset_to_default_value()=0
virtual auto get_type() const noexcept -> string_view=0
virtual void handle_setvalue(string_view arguments)=0
virtual auto get_value_variant() const -> Variant=0
StringOption(string name, string defaultValue, string helpString, Callback &&changeCallback=[](string_view) { })
auto get_type() const noexcept -> string_view override
Definition Options.hpp:322
void set_value(string_view newValue)
auto get_declaration_string() const -> string override
void reset_to_default_value() override
Definition Options.hpp:316
std::function< void(string_view)> Callback
Definition Options.hpp:292
auto get_value_variant() const -> Variant override
Definition Options.hpp:312
auto get_value() const noexcept -> string_view
Definition Options.hpp:301
auto get_name() const noexcept -> string_view override
Definition Options.hpp:318
void handle_setvalue(string_view arguments) override
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:314
auto get_help() const noexcept -> string_view override
Definition Options.hpp:324