mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 07:48:12 +00:00
LibChess: Return Commands by pointer from FooCommand::from_string()
This removes this slightly silly pattern: `make<FooCommand>(FooCommand::from_string(s))` Also, let's propagate OOM here, even if nobody reacts to it yet.
This commit is contained in:
parent
b904435866
commit
6576bbc098
3 changed files with 76 additions and 75 deletions
|
@ -9,12 +9,12 @@
|
|||
|
||||
namespace Chess::UCI {
|
||||
|
||||
UCICommand UCICommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<UCICommand>> UCICommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "uci");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return UCICommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) UCICommand);
|
||||
}
|
||||
|
||||
DeprecatedString UCICommand::to_deprecated_string() const
|
||||
|
@ -22,15 +22,15 @@ DeprecatedString UCICommand::to_deprecated_string() const
|
|||
return "uci\n";
|
||||
}
|
||||
|
||||
DebugCommand DebugCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<DebugCommand>> DebugCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "debug");
|
||||
VERIFY(tokens.size() == 2);
|
||||
if (tokens[1] == "on")
|
||||
return DebugCommand(Flag::On);
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) DebugCommand(Flag::On));
|
||||
if (tokens[1] == "off")
|
||||
return DebugCommand(Flag::Off);
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) DebugCommand(Flag::Off));
|
||||
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -44,12 +44,12 @@ DeprecatedString DebugCommand::to_deprecated_string() const
|
|||
}
|
||||
}
|
||||
|
||||
IsReadyCommand IsReadyCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<IsReadyCommand>> IsReadyCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "isready");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return IsReadyCommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) IsReadyCommand);
|
||||
}
|
||||
|
||||
DeprecatedString IsReadyCommand::to_deprecated_string() const
|
||||
|
@ -57,7 +57,7 @@ DeprecatedString IsReadyCommand::to_deprecated_string() const
|
|||
return "isready\n";
|
||||
}
|
||||
|
||||
SetOptionCommand SetOptionCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<SetOptionCommand>> SetOptionCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "setoption");
|
||||
|
@ -75,13 +75,13 @@ SetOptionCommand SetOptionCommand::from_string(StringView command)
|
|||
in_value = true;
|
||||
continue;
|
||||
}
|
||||
name.append(part);
|
||||
name.append(' ');
|
||||
TRY(name.try_append(part));
|
||||
TRY(name.try_append(' '));
|
||||
continue;
|
||||
}
|
||||
if (in_value) {
|
||||
value.append(part);
|
||||
value.append(' ');
|
||||
TRY(value.try_append(part));
|
||||
TRY(value.try_append(' '));
|
||||
continue;
|
||||
}
|
||||
if (part == "name") {
|
||||
|
@ -92,7 +92,7 @@ SetOptionCommand SetOptionCommand::from_string(StringView command)
|
|||
|
||||
VERIFY(!name.is_empty());
|
||||
|
||||
return SetOptionCommand(name.to_deprecated_string().trim_whitespace(), value.to_deprecated_string().trim_whitespace());
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) SetOptionCommand(name.to_deprecated_string().trim_whitespace(), value.to_deprecated_string().trim_whitespace()));
|
||||
}
|
||||
|
||||
DeprecatedString SetOptionCommand::to_deprecated_string() const
|
||||
|
@ -108,7 +108,7 @@ DeprecatedString SetOptionCommand::to_deprecated_string() const
|
|||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
PositionCommand PositionCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<PositionCommand>> PositionCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens.size() >= 3);
|
||||
|
@ -121,9 +121,9 @@ PositionCommand PositionCommand::from_string(StringView command)
|
|||
|
||||
Vector<Move> moves;
|
||||
for (size_t i = 3; i < tokens.size(); ++i) {
|
||||
moves.append(Move(tokens[i]));
|
||||
TRY(moves.try_append(Move(tokens[i])));
|
||||
}
|
||||
return PositionCommand(fen, moves);
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) PositionCommand(fen, moves));
|
||||
}
|
||||
|
||||
DeprecatedString PositionCommand::to_deprecated_string() const
|
||||
|
@ -144,46 +144,46 @@ DeprecatedString PositionCommand::to_deprecated_string() const
|
|||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
GoCommand GoCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<GoCommand>> GoCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "go");
|
||||
|
||||
GoCommand go_command;
|
||||
auto go_command = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GoCommand));
|
||||
for (size_t i = 1; i < tokens.size(); ++i) {
|
||||
if (tokens[i] == "searchmoves") {
|
||||
VERIFY_NOT_REACHED();
|
||||
} else if (tokens[i] == "ponder") {
|
||||
go_command.ponder = true;
|
||||
go_command->ponder = true;
|
||||
} else if (tokens[i] == "wtime") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.wtime = tokens[i].to_int().value();
|
||||
go_command->wtime = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "btime") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.btime = tokens[i].to_int().value();
|
||||
go_command->btime = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "winc") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.winc = tokens[i].to_int().value();
|
||||
go_command->winc = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "binc") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.binc = tokens[i].to_int().value();
|
||||
go_command->binc = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "movestogo") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.movestogo = tokens[i].to_int().value();
|
||||
go_command->movestogo = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "depth") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.depth = tokens[i].to_int().value();
|
||||
go_command->depth = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "nodes") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.nodes = tokens[i].to_int().value();
|
||||
go_command->nodes = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "mate") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.mate = tokens[i].to_int().value();
|
||||
go_command->mate = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "movetime") {
|
||||
VERIFY(i++ < tokens.size());
|
||||
go_command.movetime = tokens[i].to_int().value();
|
||||
go_command->movetime = tokens[i].to_int().value();
|
||||
} else if (tokens[i] == "infinite") {
|
||||
go_command.infinite = true;
|
||||
go_command->infinite = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -230,12 +230,12 @@ DeprecatedString GoCommand::to_deprecated_string() const
|
|||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
StopCommand StopCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<StopCommand>> StopCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "stop");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return StopCommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) StopCommand);
|
||||
}
|
||||
|
||||
DeprecatedString StopCommand::to_deprecated_string() const
|
||||
|
@ -243,22 +243,22 @@ DeprecatedString StopCommand::to_deprecated_string() const
|
|||
return "stop\n";
|
||||
}
|
||||
|
||||
IdCommand IdCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<IdCommand>> IdCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "id");
|
||||
StringBuilder value;
|
||||
for (size_t i = 2; i < tokens.size(); ++i) {
|
||||
if (i != 2)
|
||||
value.append(' ');
|
||||
TRY(value.try_append(' '));
|
||||
|
||||
value.append(tokens[i]);
|
||||
TRY(value.try_append(tokens[i]));
|
||||
}
|
||||
|
||||
if (tokens[1] == "name") {
|
||||
return IdCommand(Type::Name, value.to_deprecated_string());
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) IdCommand(Type::Name, value.to_deprecated_string()));
|
||||
} else if (tokens[1] == "author") {
|
||||
return IdCommand(Type::Author, value.to_deprecated_string());
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) IdCommand(Type::Author, value.to_deprecated_string()));
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -277,12 +277,12 @@ DeprecatedString IdCommand::to_deprecated_string() const
|
|||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
UCIOkCommand UCIOkCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<UCIOkCommand>> UCIOkCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "uciok");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return UCIOkCommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) UCIOkCommand);
|
||||
}
|
||||
|
||||
DeprecatedString UCIOkCommand::to_deprecated_string() const
|
||||
|
@ -290,12 +290,12 @@ DeprecatedString UCIOkCommand::to_deprecated_string() const
|
|||
return "uciok\n";
|
||||
}
|
||||
|
||||
ReadyOkCommand ReadyOkCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<ReadyOkCommand>> ReadyOkCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "readyok");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return ReadyOkCommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) ReadyOkCommand);
|
||||
}
|
||||
|
||||
DeprecatedString ReadyOkCommand::to_deprecated_string() const
|
||||
|
@ -303,12 +303,12 @@ DeprecatedString ReadyOkCommand::to_deprecated_string() const
|
|||
return "readyok\n";
|
||||
}
|
||||
|
||||
BestMoveCommand BestMoveCommand::from_string(StringView command)
|
||||
ErrorOr<NonnullOwnPtr<BestMoveCommand>> BestMoveCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "bestmove");
|
||||
VERIFY(tokens.size() == 2);
|
||||
return BestMoveCommand(Move(tokens[1]));
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) BestMoveCommand(Move(tokens[1])));
|
||||
}
|
||||
|
||||
DeprecatedString BestMoveCommand::to_deprecated_string() const
|
||||
|
@ -320,7 +320,7 @@ DeprecatedString BestMoveCommand::to_deprecated_string() const
|
|||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
InfoCommand InfoCommand::from_string([[maybe_unused]] StringView command)
|
||||
ErrorOr<NonnullOwnPtr<InfoCommand>> InfoCommand::from_string([[maybe_unused]] StringView command)
|
||||
{
|
||||
// FIXME: Implement this.
|
||||
VERIFY_NOT_REACHED();
|
||||
|
@ -333,12 +333,12 @@ DeprecatedString InfoCommand::to_deprecated_string() const
|
|||
return "info";
|
||||
}
|
||||
|
||||
QuitCommand QuitCommand::from_string([[maybe_unused]] StringView command)
|
||||
ErrorOr<NonnullOwnPtr<QuitCommand>> QuitCommand::from_string(StringView command)
|
||||
{
|
||||
auto tokens = command.split_view(' ');
|
||||
VERIFY(tokens[0] == "quit");
|
||||
VERIFY(tokens.size() == 1);
|
||||
return QuitCommand();
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) QuitCommand);
|
||||
}
|
||||
|
||||
DeprecatedString QuitCommand::to_deprecated_string() const
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -38,15 +39,15 @@ public:
|
|||
Info,
|
||||
Option,
|
||||
};
|
||||
virtual DeprecatedString to_deprecated_string() const = 0;
|
||||
|
||||
virtual ~Command() = default;
|
||||
|
||||
protected:
|
||||
explicit Command(Type type)
|
||||
: Core::Event(to_underlying(type))
|
||||
{
|
||||
}
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const = 0;
|
||||
|
||||
virtual ~Command() = default;
|
||||
};
|
||||
|
||||
class UCICommand : public Command {
|
||||
|
@ -56,7 +57,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static UCICommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<UCICommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
@ -74,7 +75,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static DebugCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<DebugCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -91,7 +92,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static IsReadyCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<IsReadyCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
@ -105,7 +106,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static SetOptionCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<SetOptionCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -126,7 +127,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static PositionCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<PositionCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -145,7 +146,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static GoCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<GoCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -170,7 +171,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static StopCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<StopCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
@ -189,7 +190,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static IdCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<IdCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -208,7 +209,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static UCIOkCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<UCIOkCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
@ -220,7 +221,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static ReadyOkCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<ReadyOkCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
@ -233,7 +234,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static BestMoveCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<BestMoveCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -250,7 +251,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static InfoCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<InfoCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
|
||||
|
@ -275,7 +276,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
static QuitCommand from_string(StringView command);
|
||||
static ErrorOr<NonnullOwnPtr<QuitCommand>> from_string(StringView command);
|
||||
|
||||
virtual DeprecatedString to_deprecated_string() const override;
|
||||
};
|
||||
|
|
|
@ -89,31 +89,31 @@ NonnullOwnPtr<Command> Endpoint::read_command()
|
|||
dbgln_if(UCI_DEBUG, "{} Received UCI Command: {}", class_name(), line);
|
||||
|
||||
if (line == "uci") {
|
||||
return make<UCICommand>(UCICommand::from_string(line));
|
||||
return UCICommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("debug"sv)) {
|
||||
return make<DebugCommand>(DebugCommand::from_string(line));
|
||||
return DebugCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("isready"sv)) {
|
||||
return make<IsReadyCommand>(IsReadyCommand::from_string(line));
|
||||
return IsReadyCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("setoption"sv)) {
|
||||
return make<SetOptionCommand>(SetOptionCommand::from_string(line));
|
||||
return SetOptionCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("position"sv)) {
|
||||
return make<PositionCommand>(PositionCommand::from_string(line));
|
||||
return PositionCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("go"sv)) {
|
||||
return make<GoCommand>(GoCommand::from_string(line));
|
||||
return GoCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("stop"sv)) {
|
||||
return make<StopCommand>(StopCommand::from_string(line));
|
||||
return StopCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("id"sv)) {
|
||||
return make<IdCommand>(IdCommand::from_string(line));
|
||||
return IdCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("uciok"sv)) {
|
||||
return make<UCIOkCommand>(UCIOkCommand::from_string(line));
|
||||
return UCIOkCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("readyok"sv)) {
|
||||
return make<ReadyOkCommand>(ReadyOkCommand::from_string(line));
|
||||
return ReadyOkCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("bestmove"sv)) {
|
||||
return make<BestMoveCommand>(BestMoveCommand::from_string(line));
|
||||
return BestMoveCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("info"sv)) {
|
||||
return make<InfoCommand>(InfoCommand::from_string(line));
|
||||
return InfoCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
} else if (line.starts_with("quit"sv)) {
|
||||
return make<QuitCommand>(QuitCommand::from_string(line));
|
||||
return QuitCommand::from_string(line).release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
dbgln("command line: {}", line);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue