Skip to content

Commit

Permalink
feat: Add get_async_cmd helper
Browse files Browse the repository at this point in the history
  • Loading branch information
shmocz committed Sep 19, 2023
1 parent b7f3744 commit 3e02d0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/command/is_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ struct ISCommand {

template <typename MessageT>
std::pair<std::string, iservice_cmd::handler_t> get_cmd(
std::function<void(ISCommand<MessageT>*)> fn) {
std::function<void(ISCommand<MessageT>*)> fn, bool async = false) {
return {MessageT().GetTypeName(), [=](iservice_cmd* c) {
ISCommand<MessageT> Q(c);
if (async) {
Q.async();
}
try {
dprintf("exec {} ", MessageT().GetTypeName());
fn(&Q);
Expand All @@ -96,6 +99,12 @@ std::pair<std::string, iservice_cmd::handler_t> get_cmd(
}};
}

template <typename MessageT>
std::pair<std::string, iservice_cmd::handler_t> get_async_cmd(
std::function<void(ISCommand<MessageT>*)> fn) {
return get_cmd(fn, true);
}

///
/// Unpacks the message stored in cmd->result() to appropriate message type, and
/// returns pointer to the result and instance of the message.
Expand Down
7 changes: 3 additions & 4 deletions src/commands_yr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <map>
#include <stdexcept>

using ra2yrcpp::command::get_async_cmd;
using ra2yrcpp::command::get_cmd;
using ra2yrcpp::command::message_result;

Expand Down Expand Up @@ -187,9 +188,8 @@ auto mission_clicked() {
}

auto add_event() {
return get_cmd<ra2yrproto::commands::AddEvent>([](auto* Q) {
return get_async_cmd<ra2yrproto::commands::AddEvent>([](auto* Q) {
auto args = Q->command_data();
Q->async();

put_gameloop_command(Q, [args](auto* it) {
const auto frame_delay = args.frame_delay();
Expand Down Expand Up @@ -247,7 +247,7 @@ auto add_event() {
}

auto place_query() {
return get_cmd<ra2yrproto::commands::PlaceQuery>([](auto* Q) {
return get_async_cmd<ra2yrproto::commands::PlaceQuery>([](auto* Q) {
auto args = Q->command_data();
if (static_cast<unsigned int>(args.coordinates().size()) >
cfg::PLACE_QUERY_MAX_LENGTH) {
Expand All @@ -256,7 +256,6 @@ auto place_query() {
args.coordinates().size() - cfg::PLACE_QUERY_MAX_LENGTH);
wrprintf("truncated place query to size {}", args.coordinates().size());
}
Q->async();

put_gameloop_command(Q, [args](auto* it) {
auto [C, cmd, fn] = *it;
Expand Down

0 comments on commit 3e02d0a

Please sign in to comment.