10 #ifndef COMMAND_FUNC_H
11 #define COMMAND_FUNC_H
38 #define return_cmd_error(errcode) return CommandCost(errcode);
48 template <Commands Tcmd>
79 #if defined(__GNUC__) && !defined(__clang__)
89 # pragma GCC diagnostic push
90 # pragma GCC diagnostic ignored "-Wcast-function-type"
91 # define SILENCE_GCC_FUNCTION_POINTER_CAST
94 template<Commands TCmd,
typename T,
bool THasTile>
struct CommandHelper;
115 template <
Commands Tcmd,
typename Tret,
typename... Targs>
121 if constexpr (std::is_same_v<Tret, CommandCost>) {
124 return std::get<0>(ret);
132 ExtractCommandCost(ret) = cost;
152 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, std::tuple<Targs...>>>) {
154 TileIndex tile = std::get<0>(std::make_tuple(args...));
161 if (counter.IsTopLevel() || !(flags &
DC_EXEC)) {
162 InternalDoBefore(counter.IsTopLevel(),
true);
164 InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(),
true);
166 if (ExtractCommandCost(res).Failed() || !(flags &
DC_EXEC))
return res;
171 InternalDoBefore(counter.IsTopLevel(),
false);
173 InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(),
false);
183 static inline bool Post(
StringID err_message, Targs... args) {
return Post<CommandCallback>(err_message,
nullptr, std::forward<Targs>(args)...); }
189 template <
typename Tcallback>
190 static inline bool Post(Tcallback *callback, Targs... args) {
return Post((
StringID)0, callback, std::forward<Targs>(args)...); }
195 static inline bool Post(Targs... args) {
return Post<CommandCallback>((
StringID)0,
nullptr, std::forward<Targs>(args)...); }
207 template <
typename Tcallback>
208 static bool Post(
StringID err_message, Tcallback *callback, Targs... args)
210 return InternalPost(err_message, callback,
true,
false, std::forward_as_tuple(args...));
222 template <
typename Tcallback>
225 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, decltype(args)>>) {
231 return InternalPost(err_message, callback, my_cmd,
true, location, std::move(args));
243 auto args_tuple = std::forward_as_tuple(args...);
246 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, decltype(args_tuple)>>) {
247 tile = std::get<0>(args_tuple);
263 template <
typename Tcallback>
264 static Tret
Unsafe(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool estimate_only,
TileIndex location, std::tuple<Targs...> args)
266 return Execute(err_message,
reinterpret_cast<CommandCallback *
>(callback), my_cmd, estimate_only,
false, location, std::move(args));
274 if constexpr (std::is_same_v<ClientID, T>) {
280 template<
class Ttuple,
size_t... Tindices>
281 static inline void SetClientIds(Ttuple &values, std::index_sequence<Tindices...>)
287 template <
template <
typename...>
typename Tt,
typename T1,
typename... Ts>
290 return std::apply([](
auto &&,
const auto&... args) {
return std::tie(args...); }, tuple);
293 template <
typename Tcallback>
294 static bool InternalPost(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool network_command, std::tuple<Targs...> args)
298 if constexpr (std::is_same_v<
TileIndex, std::tuple_element_t<0, decltype(args)>>) {
299 tile = std::get<0>(args);
302 return InternalPost(err_message, callback, my_cmd, network_command, tile, std::move(args));
305 template <
typename Tcallback>
306 static bool InternalPost(
StringID err_message, Tcallback *callback,
bool my_cmd,
bool network_command,
TileIndex tile, std::tuple<Targs...> args)
311 auto [err, estimate_only, only_sending] = InternalPostBefore(Tcmd, GetCommandFlags<Tcmd>(), tile, err_message, network_command);
312 if (err)
return false;
315 if (!network_command && GetCommandFlags<Tcmd>() &
CMD_CLIENT_ID)
SetClientIds(args, std::index_sequence_for<Targs...>{});
317 Tret res = Execute(err_message,
reinterpret_cast<CommandCallback *
>(callback), my_cmd, estimate_only, network_command, tile, args);
318 InternalPostResult(ExtractCommandCost(res), tile, estimate_only, only_sending, err_message, my_cmd);
320 if (!estimate_only && !only_sending && callback !=
nullptr) {
321 if constexpr (std::is_same_v<Tcallback, CommandCallback>) {
323 callback(Tcmd, ExtractCommandCost(res), tile);
324 }
else if constexpr (std::is_same_v<Tcallback, CommandCallbackData>) {
326 if constexpr (std::is_same_v<Tret, CommandCost>) {
332 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res));
335 if constexpr (std::is_same_v<Tret, CommandCost>) {
336 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd, res), args));
338 std::apply(callback, std::tuple_cat(std::make_tuple(Tcmd), res, args));
343 return ExtractCommandCost(res).Succeeded();
350 if constexpr (std::is_same_v<ClientID, T>) {
358 template<
class Ttuple,
size_t... Tindices>
361 return (ClientIdIsSet(std::get<Tindices>(values)) && ...);
364 template<
class Ttuple>
365 static inline Money ExtractAdditionalMoney(Ttuple &values)
367 if constexpr (std::is_same_v<std::tuple_element_t<1, Tret>,
Money>) {
368 return std::get<1>(values);
374 static Tret Execute(
StringID err_message,
CommandCallback *callback,
bool my_cmd,
bool estimate_only,
bool network_command,
TileIndex tile, std::tuple<Targs...> args)
378 assert(counter.IsTopLevel());
381 constexpr
CommandFlags cmd_flags = GetCommandFlags<Tcmd>();
385 assert(AllClientIdsSet(args, std::index_sequence_for<Targs...>{}));
389 if (!InternalExecutePrepTest(cmd_flags, tile, cur_company)) {
398 auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(ExtractCommandCost(res), cmd_flags, estimate_only, network_command, cur_company);
401 cur_company.Restore();
409 cur_company.Restore();
425 Money additional_money{};
426 if constexpr (!std::is_same_v<Tret, CommandCost>) {
427 additional_money = ExtractAdditionalMoney(res2);
430 if constexpr (std::is_same_v<Tret, CommandCost>) {
431 return InternalExecuteProcessResult(Tcmd, cmd_flags, res, res2, additional_money, tile, cur_company);
433 std::get<0>(res2) = InternalExecuteProcessResult(Tcmd, cmd_flags, ExtractCommandCost(res), ExtractCommandCost(res2), additional_money, tile, cur_company);
446 template <
Commands Tcmd,
typename Tret,
typename... Targs>
458 static inline bool Post(
StringID err_message,
TileIndex location, Targs... args) {
return Post<CommandCallback>(err_message,
nullptr, location, std::forward<Targs>(args)...); }
465 template <
typename Tcallback>
466 static inline bool Post(Tcallback *callback,
TileIndex location, Targs... args) {
return Post((
StringID)0, callback, location, std::forward<Targs>(args)...); }
472 static inline bool Post(
TileIndex location, Targs... args) {
return Post<CommandCallback>((
StringID)0,
nullptr, location, std::forward<Targs>(args)...); }
483 template <
typename Tcallback>
486 return CommandHelper<Tcmd, Tret(*)(
DoCommandFlag, Targs...),
true>::InternalPost(err_message, callback,
true,
false, location, std::forward_as_tuple(args...));
490 #ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
491 # pragma GCC diagnostic pop
494 template <Commands Tcmd>