mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:57:45 +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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue