Skip to content

Commit 948de60

Browse files
committed
refactor: UCI engine base class now uses EngineCommand struct (#507)
1 parent 891326d commit 948de60

File tree

5 files changed

+189
-208
lines changed

5 files changed

+189
-208
lines changed

libbenbot/include/libbenbot/engine/CustomCommand.hpp

Lines changed: 0 additions & 68 deletions
This file was deleted.

libbenbot/include/libbenbot/engine/Engine.hpp

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <atomic>
2424
#include <filesystem>
2525
#include <functional>
26-
#include <libbenbot/engine/CustomCommand.hpp>
2726
#include <libbenbot/search/Thread.hpp>
2827
#include <libchess/game/Position.hpp>
2928
#include <libchess/uci/EngineBase.hpp>
@@ -38,11 +37,12 @@ struct GoCommandOptions;
3837

3938
namespace ben_bot {
4039

40+
namespace uci = chess::uci;
41+
4142
using chess::game::Position;
4243
using chess::moves::Move;
4344
using std::string_view;
44-
45-
namespace uci = chess::uci;
45+
using uci::EngineCommand;
4646

4747
/** The ``ben-bot`` UCI engine class.
4848
@ingroup libbenbot
@@ -87,7 +87,10 @@ class [[nodiscard]] Engine final : public uci::EngineBase {
8787

8888
[[nodiscard]] auto get_options() -> std::span<uci::Option*> override { return options; }
8989

90-
void handle_custom_command(string_view command, string_view opts) override;
90+
[[nodiscard]] auto get_custom_uci_commands() const noexcept -> CommandList override
91+
{
92+
return customCommands;
93+
}
9194

9295
void run_perft(string_view arguments) const;
9396

@@ -180,72 +183,58 @@ class [[nodiscard]] Engine final : public uci::EngineBase {
180183
&ttSize, &clearTT, &ponder, &threads, &moveOverhead, &logFile, &prettyPrintMode, &moveFormat
181184
};
182185

183-
/* ----- Custom commands ----- */
184-
185-
// clang-format off
186-
std::array<CustomCommand, 10uz> customCommands {
187-
CustomCommand {
188-
.name = "showpos",
189-
.action = [this](const string_view args){ print_current_position(args); },
186+
std::array<EngineCommand, 10uz> customCommands {
187+
EngineCommand {
188+
.name = "showpos",
189+
.action = [this](const string_view args) { print_current_position(args); },
190190
.description = "Prints the current position",
191-
.argsHelp = "[utf8]"
192-
},
193-
CustomCommand {
194-
.name = "makenull",
195-
.action = CustomCommand::void_cb([this]{ make_null_move(); }),
191+
.argsHelp = "[utf8]" },
192+
EngineCommand {
193+
.name = "makenull",
194+
.action = EngineCommand::void_cb([this] { make_null_move(); }),
196195
.description = "Play a null move on the internal board",
197-
.argsHelp = {}
198-
},
199-
CustomCommand {
200-
.name = "flip",
201-
.action = CustomCommand::void_cb([this]{ color_flip(); }),
196+
.argsHelp = { } },
197+
EngineCommand {
198+
.name = "flip",
199+
.action = EngineCommand::void_cb([this] { color_flip(); }),
202200
.description = "Color-flip the current position",
203-
.argsHelp = {}
204-
},
205-
CustomCommand {
206-
.name = "options",
207-
.action = CustomCommand::void_cb([this]{ print_options(); }),
201+
.argsHelp = { } },
202+
EngineCommand {
203+
.name = "options",
204+
.action = EngineCommand::void_cb([this] { print_options(); }),
208205
.description = "Dump current UCI option values",
209-
.argsHelp = {}
210-
},
211-
CustomCommand {
212-
.name = "perft",
206+
.argsHelp = { } },
207+
EngineCommand {
208+
.name = "perft",
213209
.action = [this](const string_view args) { run_perft(args); },
214210
.description = "Computes perft of the current position to the given depth",
215-
.argsHelp = "<N> [json]"
216-
},
217-
CustomCommand {
218-
.name = "bench",
219-
.action = [this](const string_view args){ run_bench(args); },
211+
.argsHelp = "<N> [json]" },
212+
EngineCommand {
213+
.name = "bench",
214+
.action = [this](const string_view args) { run_bench(args); },
220215
.description = "Runs a search and reports total nodes and NPS",
221-
.argsHelp = "[<depth>] [<epdPath>]"
222-
},
223-
CustomCommand {
224-
.name = "compiler",
225-
.action = CustomCommand::void_cb([]{ print_compiler_info(); }),
216+
.argsHelp = "[<depth>] [<epdPath>]" },
217+
EngineCommand {
218+
.name = "compiler",
219+
.action = EngineCommand::void_cb([] { print_compiler_info(); }),
226220
.description = "Print compiler info",
227-
.argsHelp = {}
228-
},
229-
CustomCommand {
230-
.name = "writeconfig",
221+
.argsHelp = { } },
222+
EngineCommand {
223+
.name = "writeconfig",
231224
.action = [this](const string_view args) { write_config_file(args); },
232225
.description = "Writes the engine's current state to a configuration file at the given path",
233-
.argsHelp = "<path>"
234-
},
235-
CustomCommand {
236-
.name = "readconfig",
226+
.argsHelp = "<path>" },
227+
EngineCommand {
228+
.name = "readconfig",
237229
.action = [this](const string_view args) { read_config_file(args); },
238230
.description = "Loads engine state from a configuration file at the given path",
239-
.argsHelp = "<path>"
240-
},
241-
CustomCommand {
242-
.name = "help",
243-
.action = [this] (const string_view args){ print_help(args); },
231+
.argsHelp = "<path>" },
232+
EngineCommand {
233+
.name = "help",
234+
.action = [this](const string_view args) { print_help(args); },
244235
.description = "Display this text",
245-
.argsHelp = "[--no-logo]"
246-
}
236+
.argsHelp = "[--no-logo]" }
247237
};
248-
// clang-format on
249238
};
250239

251240
} // namespace ben_bot

libbenbot/src/engine/Engine.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,6 @@ void Engine::go(const uci::GoCommandOptions& opts)
110110
searcher.start();
111111
}
112112

113-
// this function implements non-standard UCI commands that we support
114-
void Engine::handle_custom_command(
115-
const string_view command, const string_view opts)
116-
{
117-
if (const auto it = std::ranges::find(customCommands, command, &CustomCommand::name);
118-
it != customCommands.end()) {
119-
it->action(opts);
120-
return;
121-
}
122-
123-
info_string(std::format("Unknown UCI command: '{}'", command));
124-
info_string("Type help for a list of supported commands");
125-
}
126-
127113
void Engine::start_file_logger(const string_view arg)
128114
{
129115
if (arg.empty()) {

0 commit comments

Comments
 (0)