Codechange: Use EnumBitSet for DoCommandFlags

This commit is contained in:
Rubidium
2025-02-13 23:35:52 +01:00
committed by rubidium42
parent f309b90a1d
commit c3d5e6d2a0
95 changed files with 871 additions and 873 deletions

View File

@@ -45,12 +45,12 @@ constexpr CommandFlags GetCommandFlags()
* @param cmd_flags Flags from GetCommandFlags
* @return flags for DoCommand
*/
static constexpr inline DoCommandFlag CommandFlagsToDCFlags(CommandFlags cmd_flags)
static constexpr inline DoCommandFlags CommandFlagsToDCFlags(CommandFlags cmd_flags)
{
DoCommandFlag flags = DC_NONE;
if (cmd_flags & CMD_NO_WATER) flags |= DC_NO_WATER;
if (cmd_flags & CMD_AUTO) flags |= DC_AUTO;
if (cmd_flags & CMD_ALL_TILES) flags |= DC_ALL_TILES;
DoCommandFlags flags = {};
if (cmd_flags & CMD_NO_WATER) flags.Set(DoCommandFlag::NoWater);
if (cmd_flags & CMD_AUTO) flags.Set(DoCommandFlag::Auto);
if (cmd_flags & CMD_ALL_TILES) flags.Set(DoCommandFlag::AllTiles);
return flags;
}
@@ -85,7 +85,7 @@ template <Commands TCmd, typename T, bool THasTile> struct CommandHelper;
class CommandHelperBase {
protected:
static void InternalDoBefore(bool top_level, bool test);
static void InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test);
static void InternalDoAfter(CommandCost &res, DoCommandFlags flags, bool top_level, bool test);
static std::tuple<bool, bool, bool> InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command);
static void InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd);
static bool InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup<CompanyID> &cur_company);
@@ -102,7 +102,7 @@ protected:
* @tparam Targs The command parameter types.
*/
template <Commands Tcmd, typename Tret, typename... Targs>
struct CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true> : protected CommandHelperBase {
struct CommandHelper<Tcmd, Tret(*)(DoCommandFlags, Targs...), true> : protected CommandHelperBase {
private:
/** Extract the \c CommandCost from a command proc result. */
static inline CommandCost &ExtractCommandCost(Tret &ret)
@@ -136,23 +136,23 @@ public:
* @see CommandProc
* @return the cost
*/
static Tret Do(DoCommandFlag flags, Targs... args)
static Tret Do(DoCommandFlags flags, Targs... args)
{
if constexpr (std::is_same_v<TileIndex, std::tuple_element_t<0, std::tuple<Targs...>>>) {
/* Do not even think about executing out-of-bounds tile-commands. */
TileIndex tile = std::get<0>(std::make_tuple(args...));
if (tile != 0 && (tile >= Map::Size() || (!IsValidTile(tile) && (flags & DC_ALL_TILES) == 0))) return MakeResult(CMD_ERROR);
if (tile != 0 && (tile >= Map::Size() || (!IsValidTile(tile) && !flags.Test(DoCommandFlag::AllTiles)))) return MakeResult(CMD_ERROR);
}
RecursiveCommandCounter counter{};
/* Only execute the test call if it's toplevel, or we're not execing. */
if (counter.IsTopLevel() || !(flags & DC_EXEC)) {
if (counter.IsTopLevel() || !flags.Test(DoCommandFlag::Execute)) {
InternalDoBefore(counter.IsTopLevel(), true);
Tret res = CommandTraits<Tcmd>::proc(flags & ~DC_EXEC, args...);
Tret res = CommandTraits<Tcmd>::proc(DoCommandFlags{flags}.Reset(DoCommandFlag::Execute), args...);
InternalDoAfter(ExtractCommandCost(res), flags, counter.IsTopLevel(), true); // Can modify res.
if (ExtractCommandCost(res).Failed() || !(flags & DC_EXEC)) return res;
if (ExtractCommandCost(res).Failed() || !flags.Test(DoCommandFlag::Execute)) return res;
}
/* Execute the command here. All cost-relevant functions set the expenses type
@@ -369,7 +369,7 @@ protected:
}
/* Test the command. */
DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
DoCommandFlags flags = CommandFlagsToDCFlags(cmd_flags);
Tret res = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags), args));
auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(ExtractCommandCost(res), cmd_flags, estimate_only, network_command, cur_company);
@@ -395,7 +395,7 @@ protected:
if (desync_log) LogCommandExecution(Tcmd, err_message, EndianBufferWriter<CommandDataBuffer>::FromValue(args), false);
/* Actually try and execute the command. */
Tret res2 = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags | DC_EXEC), args));
Tret res2 = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags | DoCommandFlag::Execute), args));
/* Convention: If the second result element is of type Money,
* this is the additional cash required for the command. */
@@ -421,7 +421,7 @@ protected:
* @tparam Targs The command parameter types.
*/
template <Commands Tcmd, typename Tret, typename... Targs>
struct CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), false> : CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true>
struct CommandHelper<Tcmd, Tret(*)(DoCommandFlags, Targs...), false> : CommandHelper<Tcmd, Tret(*)(DoCommandFlags, Targs...), true>
{
/* Do not allow Post without explicit location. */
static inline bool Post(StringID err_message, Targs... args) = delete;
@@ -464,7 +464,7 @@ struct CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), false> : CommandHel
template <typename Tcallback>
static inline bool Post(StringID err_message, Tcallback *callback, TileIndex location, Targs... args)
{
return CommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...));
return CommandHelper<Tcmd, Tret(*)(DoCommandFlags, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...));
}
};