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 <stdexcept>
24#include <string>
25#include <string_view>
26#include <variant>
27#include <vector>
28
29namespace chess::uci {
30
31using std::string;
32using std::string_view;
33
38struct Option {
39 Option() = default;
40
41 virtual ~Option();
42
43 Option(const Option&) = default;
44 Option(Option&&) = default;
45 Option& operator=(const Option&) = default;
46 Option& operator=(Option&&) = default;
47
49 [[nodiscard]] virtual auto get_name() const noexcept -> string_view = 0;
50
54 [[nodiscard]] virtual auto get_declaration_string() const -> string = 0;
55
57 [[nodiscard]] virtual auto get_type() const noexcept -> string_view = 0;
58
60 [[nodiscard]] virtual auto get_help() const noexcept -> string_view = 0;
61
65 [[nodiscard]] virtual auto has_value() const noexcept -> bool { return true; }
66
68 using Variant = std::variant<bool, int, string_view>;
69
73 [[nodiscard]] virtual auto get_value_variant() const -> Variant = 0;
74
78 [[nodiscard]] virtual auto get_default_value_variant() const -> Variant = 0;
79
83 virtual void handle_setvalue(string_view arguments) = 0;
84};
85
90struct BoolOption final : Option {
91 using Value = bool;
92 using Callback = std::function<void(bool)>;
93
96 string name, bool defaultValue,
97 string helpString,
98 Callback&& changeCallback = [](bool) { });
99
103 [[nodiscard]] auto get_value() const noexcept -> bool { return value; }
104
106 void set_value(bool newValue);
107
108 [[nodiscard]] auto get_value_variant() const -> Variant override { return value; }
109
111 [[nodiscard]] auto get_default_value() const noexcept -> bool { return optionDefault; }
112
113 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return optionDefault; }
114
115 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
116
117 [[nodiscard]] auto get_declaration_string() const -> string override;
118
119 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Toggle"; }
120
121 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
122
123 void handle_setvalue(string_view arguments) override;
124
125private:
126 string optionName;
127
128 bool optionDefault { true };
129
130 bool value { optionDefault };
131
132 string help;
133
134 Callback onChange { [](bool) { } };
135};
136
141struct IntOption final : Option {
142 using Value = int;
143 using Callback = std::function<void(int)>;
144
147 string name,
148 int minValue, int maxValue,
149 int defaultValue,
150 string helpString,
151 Callback&& changeCallback = [](int) { });
152
156 [[nodiscard]] auto get_value() const noexcept -> int { return value; }
157
159 void set_value(int newValue);
160
161 [[nodiscard]] auto get_value_variant() const -> Variant override { return value; }
162
164 [[nodiscard]] auto get_default_value() const noexcept -> int { return optionDefault; }
165
166 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return optionDefault; }
167
168 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
169
170 [[nodiscard]] auto get_declaration_string() const -> string override;
171
172 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Integer"; }
173
174 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
175
176 void handle_setvalue(string_view arguments) override;
177
178private:
179 string optionName;
180
181 int optionMin { 0 };
182 int optionMax { 100 };
183
184 int optionDefault { 0 };
185
186 int value { optionDefault };
187
188 string help;
189
190 Callback onChange { [](int) { } };
191};
192
197struct ComboOption final : Option {
198 using Value = string_view;
199 using Callback = std::function<void(string_view)>;
200
203 string name,
204 std::vector<string> values,
205 string defaultValue,
206 string helpString,
207 Callback&& changeCallback = [](string_view) { });
208
209 [[nodiscard]] auto get_value() const noexcept -> string_view { return value; } // cppcheck-suppress returnByReference
210
212 void set_value(string_view newValue);
213
214 [[nodiscard]] auto get_value_variant() const -> Variant override { return get_value(); }
215
217 [[nodiscard]] auto get_default_value() const noexcept -> string_view { return optionDefault; } // cppcheck-suppress returnByReference
218
219 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return get_default_value(); }
220
221 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
222
223 [[nodiscard]] auto get_declaration_string() const -> string override;
224
225 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Multichoice"; }
226
227 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
228
229 void handle_setvalue(string_view arguments) override;
230
231private:
232 string optionName;
233
234 std::vector<string> possibleValues;
235
236 string optionDefault;
237
238 string value { optionDefault };
239
240 string help;
241
242 Callback onChange { [](string_view) { } };
243};
244
249struct StringOption final : Option {
250 using Value = string_view;
251 using Callback = std::function<void(string_view)>;
252
255 string name,
256 string defaultValue,
257 string helpString,
258 Callback&& changeCallback = [](string_view) { });
259
260 [[nodiscard]] auto get_value() const noexcept -> string_view
261 {
262 if (value == "<empty>")
263 return { };
264
265 return value;
266 }
267
269 void set_value(string_view newValue);
270
271 [[nodiscard]] auto get_value_variant() const -> Variant override { return get_value(); }
272
273 [[nodiscard]] auto get_default_value_variant() const -> Variant override { return string_view { }; }
274
275 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
276
277 [[nodiscard]] auto get_declaration_string() const -> string override;
278
279 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "String"; }
280
281 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
282
283 void handle_setvalue(string_view arguments) override;
284
285private:
286 string optionName;
287
288 string value;
289
290 string help;
291
292 Callback onChange { [](string_view) { } };
293};
294
299struct Action final : Option {
300 using Value = void;
301 using Callback = std::function<void()>;
302
305 string name,
306 Callback&& action,
307 string helpString);
308
309 [[nodiscard]] auto get_value_variant() const -> Variant override { throw_value_error(); }
310 [[nodiscard]] auto get_default_value_variant() const -> Variant override { throw_value_error(); }
311
312 [[nodiscard]] auto get_name() const noexcept -> string_view override { return optionName; }
313
314 [[nodiscard]] auto get_declaration_string() const -> string override;
315
316 [[nodiscard]] auto get_type() const noexcept -> string_view override { return "Button"; }
317
318 [[nodiscard]] auto get_help() const noexcept -> string_view override { return help; }
319
320 [[nodiscard]] auto has_value() const noexcept -> bool override { return false; }
321
322 void handle_setvalue(string_view arguments) override;
323
324private:
325 [[noreturn]] static void throw_value_error()
326 {
327 throw std::logic_error { "get_value_variant() called on option of Action type" };
328 }
329
330 string optionName;
331
332 Callback callback;
333
334 string help;
335};
336
337} // namespace chess::uci
auto get_type() const noexcept -> string_view override
Definition Options.hpp:316
Action(string name, Callback &&action, string helpString)
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:310
auto get_declaration_string() const -> string override
auto has_value() const noexcept -> bool override
Definition Options.hpp:320
std::function< void()> Callback
Definition Options.hpp:301
auto get_help() const noexcept -> string_view override
Definition Options.hpp:318
auto get_value_variant() const -> Variant override
Definition Options.hpp:309
auto get_name() const noexcept -> string_view override
Definition Options.hpp:312
void handle_setvalue(string_view arguments) override
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:113
auto get_help() const noexcept -> string_view override
Definition Options.hpp:121
std::function< void(bool)> Callback
Definition Options.hpp:92
auto get_value() const noexcept -> bool
Definition Options.hpp:103
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:111
void handle_setvalue(string_view arguments) override
auto get_value_variant() const -> Variant override
Definition Options.hpp:108
auto get_declaration_string() const -> string override
auto get_name() const noexcept -> string_view override
Definition Options.hpp:115
auto get_type() const noexcept -> string_view override
Definition Options.hpp:119
auto get_type() const noexcept -> string_view override
Definition Options.hpp:225
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:219
auto get_name() const noexcept -> string_view override
Definition Options.hpp:221
auto get_help() const noexcept -> string_view override
Definition Options.hpp:227
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:199
void set_value(string_view newValue)
auto get_value_variant() const -> Variant override
Definition Options.hpp:214
auto get_value() const noexcept -> string_view
Definition Options.hpp:209
auto get_default_value() const noexcept -> string_view
Definition Options.hpp:217
void handle_setvalue(string_view arguments) override
void handle_setvalue(string_view arguments) override
std::function< void(int)> Callback
Definition Options.hpp:143
void set_value(int newValue)
auto get_name() const noexcept -> string_view override
Definition Options.hpp:168
auto get_value() const noexcept -> int
Definition Options.hpp:156
auto get_help() const noexcept -> string_view override
Definition Options.hpp:174
auto get_value_variant() const -> Variant override
Definition Options.hpp:161
auto get_default_value() const noexcept -> int
Definition Options.hpp:164
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:172
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:166
Option & operator=(Option &&)=default
std::variant< bool, int, string_view > Variant
Definition Options.hpp:68
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:65
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 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:279
void set_value(string_view newValue)
auto get_declaration_string() const -> string override
std::function< void(string_view)> Callback
Definition Options.hpp:251
auto get_value_variant() const -> Variant override
Definition Options.hpp:271
auto get_value() const noexcept -> string_view
Definition Options.hpp:260
auto get_name() const noexcept -> string_view override
Definition Options.hpp:275
void handle_setvalue(string_view arguments) override
auto get_default_value_variant() const -> Variant override
Definition Options.hpp:273
auto get_help() const noexcept -> string_view override
Definition Options.hpp:281