10 #ifndef COMMAND_FUNC_H
11 #define COMMAND_FUNC_H
38 #define return_cmd_error(errcode) return CommandCost(errcode);
47 template <Commands Tcmd>
78 #if defined(__GNUC__) && !defined(__clang__)
88 # pragma GCC diagnostic push
89 # pragma GCC diagnostic ignored "-Wcast-function-type"
90 # define SILENCE_GCC_FUNCTION_POINTER_CAST
93 template<Commands TCmd,
typename T,
bool THasTile>
struct CommandHelper;
114 template <
Commands Tcmd,
typename Tret,
typename... Targs>
120 if constexpr (std::is_same_v<Tret, CommandCost>) {
123 return std::get<0>(ret);
131 ExtractCommandCost(ret) = cost;
151 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, std::tuple<Targs...>>>) {
153 TileIndex tile = std::get<0>(std::make_tuple(args...));
160 if (counter.IsTopLevel() || !(flags &
DC_EXEC)) {
161 InternalDoBefore(counter.IsTopLevel(),
true);
163 InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(),
true);
165 if (ExtractCommandCost(res).Failed() || !(flags &
DC_EXEC))
return res;
170 InternalDoBefore(counter.IsTopLevel(),
false);
172 InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(),
false);
182 static inline bool Post(
StringID err_message, Targs... args) {
return Post<CommandCallback>(err_message,
nullptr, std::forward<Targs>(args)...); }
188 template <
typename Tcallback>
189 static inline bool Post(Tcallback *callback, Targs... args) {
return Post((
StringID)0, callback, std::forward<Targs>(args)...); }
194 static inline bool Post(Targs... args) {
return Post<CommandCallback>((
StringID)0,
nullptr, std::forward<Targs>(args)...); }
206 template <
typename Tcallback>
207 static bool Post(
StringID err_message, Tcallback *callback, Targs... args)
209 return InternalPost(err_message, callback,
true,
false, std::forward_as_tuple(args...));
220 template <
typename Tcallback>
221 static bool PostFromNet(
StringID err_message, Tcallback *callback,
bool my_cmd, std::tuple<Targs...> args)
223 return InternalPost(err_message, callback, my_cmd,
true, std::move(args));
235 auto args_tuple = std::forward_as_tuple(args...);
250 template <
typename Tcallback>
251 static Tret
Unsafe(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool estimate_only,
TileIndex location, std::tuple<Targs...> args)
253 return Execute(err_message,
reinterpret_cast<CommandCallback *
>(
reinterpret_cast<void(*)()
>(callback)), my_cmd, estimate_only,
false, location, std::move(args));
261 if constexpr (std::is_same_v<ClientID, T>) {
267 template<
class Ttuple,
size_t... Tindices>
268 static inline void SetClientIds(Ttuple &values, std::index_sequence<Tindices...>)
274 template <
template <
typename...>
typename Tt,
typename T1,
typename... Ts>
277 return std::apply([](
auto &&,
const auto&... args) {
return std::tie(args...); }, tuple);
280 template <
typename Tcallback>
281 static bool InternalPost(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool network_command, std::tuple<Targs...> args)
285 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, decltype(args)>>) {
286 tile = std::get<0>(args);
289 return InternalPost(err_message, callback, my_cmd, network_command, tile, std::move(args));
292 template <
typename Tcallback>
293 static bool InternalPost(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool network_command,
TileIndex tile, std::tuple<Targs...> args)
298 auto [err, estimate_only, only_sending] = InternalPostBefore(Tcmd, GetCommandFlags<Tcmd>(), tile, err_message, network_command);
299 if (err)
return false;
302 if (!network_command && GetCommandFlags<Tcmd>() &
CMD_CLIENT_ID)
SetClientIds(args, std::index_sequence_for<Targs...>{});
304 Tret res = Execute(err_message,
reinterpret_cast<CommandCallback *
>(
reinterpret_cast<void(*)()
>(callback)), my_cmd, estimate_only, network_command, tile, args);
305 InternalPostResult(ExtractCommandCost(res), tile, estimate_only, only_sending, err_message, my_cmd);
307 if (!estimate_only && !only_sending && callback !=
nullptr) {
308 if constexpr (std::is_same_v<Tcallback, CommandCallback>) {
310 callback(Tcmd, ExtractCommandCost(res), tile);
311 }
else if constexpr (std::is_same_v<Tcallback, CommandCallbackData>) {
313 if constexpr (std::is_same_v<Tret, CommandCost>) {
319 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res));
322 if constexpr (std::is_same_v<Tret, CommandCost>) {
323 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd, res), args));
325 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res, args));
330 return ExtractCommandCost(res).Succeeded();
337 if constexpr (std::is_same_v<ClientID, T>) {
345 template<
class Ttuple,
size_t... Tindices>
348 return (ClientIdIsSet(std::get<Tindices>(values)) && ...);
351 template<
class Ttuple>
352 static inline Money ExtractAdditionalMoney([[maybe_unused]] Ttuple &values)
354 if constexpr (std::is_same_v<std::tuple_element_t<1, Tret>,
Money>) {
355 return std::get<1>(values);
361 static Tret Execute(
StringID err_message,
CommandCallback *callback,
bool,
bool estimate_only,
bool network_command,
TileIndex tile, std::tuple<Targs...> args)
365 assert(counter.IsTopLevel());
368 constexpr
CommandFlags cmd_flags = GetCommandFlags<Tcmd>();
372 assert(AllClientIdsSet(args, std::index_sequence_for<Targs...>{}));
376 if (!InternalExecutePrepTest(cmd_flags, tile, cur_company)) {
385 auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(ExtractCommandCost(res), cmd_flags, estimate_only, network_command, cur_company);
388 cur_company.Restore();
396 cur_company.Restore();
412 Money additional_money{};
413 if constexpr (!std::is_same_v<Tret, CommandCost>) {
414 additional_money = ExtractAdditionalMoney(res2);
417 if constexpr (std::is_same_v<Tret, CommandCost>) {
418 return InternalExecuteProcessResult(Tcmd, cmd_flags, res, res2, additional_money, tile, cur_company);
420 std::get<0>(res2) = InternalExecuteProcessResult(Tcmd, cmd_flags, ExtractCommandCost(res), ExtractCommandCost(res2), additional_money, tile, cur_company);
433 template <
Commands Tcmd,
typename Tret,
typename... Targs>
437 static inline bool Post(
StringID err_message, Targs... args) =
delete;
438 template <
typename Tcallback>
439 static inline bool Post(Tcallback *callback, Targs... args) =
delete;
440 static inline bool Post(Targs... args) =
delete;
441 template <
typename Tcallback>
442 static bool Post(
StringID err_message, Tcallback *callback, Targs... args) =
delete;
450 static inline bool Post(
StringID err_message,
TileIndex location, Targs... args) {
return Post<CommandCallback>(err_message,
nullptr, location, std::forward<Targs>(args)...); }
457 template <
typename Tcallback>
458 static inline bool Post(Tcallback *callback,
TileIndex location, Targs... args) {
return Post((
StringID)0, callback, location, std::forward<Targs>(args)...); }
464 static inline bool Post(
TileIndex location, Targs... args) {
return Post<CommandCallback>((
StringID)0,
nullptr, location, std::forward<Targs>(args)...); }
474 template <
typename Tcallback>
477 return CommandHelper<Tcmd, Tret(*)(
DoCommandFlag, Targs...),
true>::InternalPost(err_message, callback,
true,
false, location, std::forward_as_tuple(args...));
481 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
482 # pragma GCC diagnostic pop
485 template <Commands Tcmd>