1818#define _FGE_C_COMMANDHANDLER_HPP_INCLUDED
1919
2020#include " FastEngine/fge_extern.hpp"
21+ #include " FastEngine/C_callback.hpp"
2122#include " FastEngine/C_property.hpp"
2223#include < string>
2324#include < unordered_map>
2425#include < vector>
2526
26- /* *
27- * \def FGE_CMD_FUNC
28- * \ingroup objectControl
29- * \brief Helper to case a command function
30- */
31- #define FGE_CMD_FUNC (x ) static_cast <fge::CommandFunction>(x)
27+ #define FGE_COMMAND_DEFAULT_RESERVE_SIZE 16
3228
3329namespace fge
3430{
@@ -37,16 +33,16 @@ class CommandHandler;
3733class Scene ;
3834class Object ;
3935
40- using CommandFunction = fge::Property (CommandHandler::*)( fge::Object* caller,
41- fge::Property const & arg,
42- fge::Scene* caller_scene) ;
36+ using CommandFunction = fge::CalleeUniquePtr<fge:: Property, fge::Object*, fge::Property const &, fge::Scene*>;
37+ using CommandStaticHelpers =
38+ fge::CallbackStaticHelpers<fge::Property, CommandFunction, fge::Object*, fge::Property const &, fge::Scene*> ;
4339
4440/* *
4541 * \class CommandHandler
4642 * \ingroup objectControl
47- * \brief CommandHandler is a class that can be used to handle commands
43+ * \brief CommandHandler can implement functions attached to an Object that can be called by others with a name
4844 *
49- * A command is a well-defined function signature that can be attributed to an name (string) and
45+ * A command is a well-defined function signature that can be attributed to a name (string) and
5046 * be called by another object with ease.
5147 *
5248 * A command is also indexed to avoid sending the command name on a network communication.
@@ -60,26 +56,34 @@ class FGE_API CommandHandler
6056 */
6157 struct CommandData
6258 {
63- fge::CommandHandler* _handle;
59+ inline CommandData (fge::CommandFunction cmdfunc, std::string_view name) :
60+ _func(std::move(cmdfunc)),
61+ _name(name)
62+ {}
63+
6464 fge::CommandFunction _func;
6565 std::string _name;
6666 };
6767
68- using CommandDataType = std::vector<fge::CommandHandler:: CommandData>;
68+ using CommandDataType = std::vector<CommandData>;
6969
7070 CommandHandler ();
71+ CommandHandler (CommandHandler const & r) = delete ;
72+ CommandHandler (CommandHandler&& r) noexcept = delete ;
73+
74+ CommandHandler& operator =(CommandHandler const & r) = delete ;
75+ CommandHandler& operator =(CommandHandler&& r) noexcept = delete ;
7176
7277 /* *
7378 * \brief Add a new command to the handler
7479 *
7580 * An object should inherit from CommandHandler and add commands to it.
7681 *
7782 * \param name The name of the command
78- * \param handle The object that will handle the command
79- * \param cmdfunc The function pointer of the command
83+ * \param cmdfunc The command
8084 * \return \b true if the command was added, \b false otherwise
8185 */
82- bool addCmd (std::string_view name, fge::CommandHandler* handle, fge:: CommandFunction cmdfunc);
86+ bool addCmd (std::string_view name, fge::CommandFunction cmdfunc);
8387 /* *
8488 * \brief Delete a command from the handler
8589 *
@@ -90,11 +94,10 @@ class FGE_API CommandHandler
9094 * \brief Replace a command from the handler
9195 *
9296 * \param name The name of the command
93- * \param handle The new object that will handle the command
94- * \param cmdfunc The new function pointer of the command
97+ * \param cmdfunc The command
9598 * \return \b true if the command was replaced, \b false otherwise
9699 */
97- bool replaceCmd (std::string_view name, fge::CommandHandler* handle, fge:: CommandFunction cmdfunc);
100+ bool replaceCmd (std::string_view name, fge::CommandFunction cmdfunc);
98101
99102 /* *
100103 * \brief Clear all commands from the handler
@@ -107,21 +110,21 @@ class FGE_API CommandHandler
107110 * \param name The name of the command
108111 * \param caller The object that call the command
109112 * \param arg The arguments of the command
110- * \param caller_scene The scene that contains the caller
113+ * \param callerScene The scene that contains the caller
111114 * \return A Property containing the result of the command
112115 */
113116 fge::Property
114- callCmd (std::string_view name, fge::Object* caller, fge::Property const & arg, fge::Scene* caller_scene );
117+ callCmd (std::string_view name, fge::Object* caller, fge::Property const & arg, fge::Scene* callerScene );
115118 /* *
116119 * \brief Call a command by its index
117120 *
118121 * \param index The index of the command
119122 * \param caller The object that call the command
120123 * \param arg The arguments of the command
121- * \param caller_scene The scene that contains the caller
124+ * \param callerScene The scene that contains the caller
122125 * \return A Property containing the result of the command
123126 */
124- fge::Property callCmd (std::size_t index, fge::Object* caller, fge::Property const & arg, fge::Scene* caller_scene );
127+ fge::Property callCmd (std::size_t index, fge::Object* caller, fge::Property const & arg, fge::Scene* callerScene );
125128
126129 /* *
127130 * \brief Get the index of a command by its name
@@ -144,7 +147,7 @@ class FGE_API CommandHandler
144147 * \param name The name of the command
145148 * \return The command or nullptr if the command doesn't exist
146149 */
147- [[nodiscard]] fge::CommandHandler:: CommandData const * getCmd (std::string_view name) const ;
150+ [[nodiscard]] CommandData const * getCmd (std::string_view name) const ;
148151
149152 /* *
150153 * \brief Get the number of commands
@@ -158,10 +161,10 @@ class FGE_API CommandHandler
158161 *
159162 * \return The commands list
160163 */
161- [[nodiscard]] fge::CommandHandler:: CommandDataType const & getCmdList () const ;
164+ [[nodiscard]] CommandDataType const & getCmdList () const ;
162165
163166private:
164- fge::CommandHandler:: CommandDataType g_cmdData;
167+ CommandDataType g_cmdData;
165168 std::unordered_map<std::string_view, std::size_t > g_cmdDataMap;
166169};
167170
0 commit comments