summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2021-11-05 23:56:33 +0100
committerMichael Lutz <michi@icosahedron.de>2021-12-16 22:28:32 +0100
commitde45a8729c0f1ab965e4adf4e6ad7095a4a678c8 (patch)
tree746d4c60e6d49a5a7dd45ab7b573377948b85c19
parent4f3ea3907e23428461c626b6c861d834b358bc9f (diff)
downloadopenttd-de45a8729c0f1ab965e4adf4e6ad7095a4a678c8.tar.xz
Codechange: Add DoCommand::Post specialization for commands that take no TileIndex.
This adds a new Post overloads that still take a TileIndex which is used to place any error windows or text effects.
-rw-r--r--src/command_func.h55
1 files changed, 52 insertions, 3 deletions
diff --git a/src/command_func.h b/src/command_func.h
index e1f958f5b..2a1c3dc37 100644
--- a/src/command_func.h
+++ b/src/command_func.h
@@ -79,7 +79,7 @@ private:
};
-template<Commands TCmd, typename T> struct CommandHelper;
+template<Commands TCmd, typename T, bool THasTile> struct CommandHelper;
class CommandHelperBase {
protected:
@@ -100,7 +100,7 @@ protected:
* @tparam Targs The command parameter types.
*/
template <Commands Tcmd, typename... Targs>
-struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...)> : protected CommandHelperBase {
+struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true> : 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 <Commands Tcmd, typename... Targs>
+struct CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), false> : CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>
+{
+ /* Import Post overloads from our base class. */
+ using CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>::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<Targs>(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<Targs>(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<Targs>(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<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...));
+ }
+};
+
+
template <Commands Tcmd>
-using Command = CommandHelper<Tcmd, typename CommandTraits<Tcmd>::ProcType>;
+using Command = CommandHelper<Tcmd, typename CommandTraits<Tcmd>::ProcType, std::is_same_v<TileIndex, std::tuple_element_t<0, typename CommandTraits<Tcmd>::Args>>>;
#endif /* COMMAND_FUNC_H */