diff --git a/src/command_func.h b/src/command_func.h index e1f958f5bd..2a1c3dc37b 100644 --- a/src/command_func.h +++ b/src/command_func.h @@ -79,7 +79,7 @@ private: }; -template struct CommandHelper; +template struct CommandHelper; class CommandHelperBase { protected: @@ -100,7 +100,7 @@ protected: * @tparam Targs The command parameter types. */ template -struct CommandHelper : protected CommandHelperBase { +struct CommandHelper : protected CommandHelperBase { public: /** * This function executes a given command with the parameters from the #CommandProc parameter list. @@ -342,7 +342,56 @@ protected: } }; +/** + * Overload for #CommandHelper that exposes additional \c Post variants + * for commands that don't take a TileIndex themselves. + * @tparam Tcmd The command-id to execute. + * @tparam Targs The command parameter types. + */ +template +struct CommandHelper : CommandHelper +{ + /* Import Post overloads from our base class. */ + using CommandHelper::Post; + + /** + * Shortcut for Post when not using an error message. + * @param err_message Message prefix to show on error + * @param location Tile location for user feedback. + * @param args Parameters for the command + */ + static inline bool Post(StringID err_message, TileIndex location, Targs... args) { return Post(err_message, nullptr, location, std::forward(args)...); } + /** + * Shortcut for Post when not using a callback. + * @param callback A callback function to call after the command is finished + * @param location Tile location for user feedback. + * @param args Parameters for the command + */ + static inline bool Post(CommandCallback *callback, TileIndex location, Targs... args) { return Post((StringID)0, callback, location, std::forward(args)...); } + /** + * Shortcut for Post when not using a callback or an error message. + * @param location Tile location for user feedback. + * @param args Parameters for the command* + */ + static inline bool Post(TileIndex location, Targs... args) { return Post((StringID)0, nullptr, location, std::forward(args)...); } + + /** + * Post variant that takes a TileIndex (for error window location and text effects) for + * commands that don't take a TileIndex by themselves. + * @param err_message Message prefix to show on error + * @param callback A callback function to call after the command is finished + * @param location Tile location for user feedback. + * @param args Parameters for the command + * @return \c true if the command succeeded, else \c false. + */ + static inline bool Post(StringID err_message, CommandCallback *callback, TileIndex location, Targs... args) + { + return CommandHelper::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...)); + } +}; + + template -using Command = CommandHelper::ProcType>; +using Command = CommandHelper::ProcType, std::is_same_v::Args>>>; #endif /* COMMAND_FUNC_H */