Skip to content

Commit 8e6adc4

Browse files
committed
refactor CommandHandler to have a fge::CalleeUniquePtr and allow it to be more universal
1 parent 7955a8d commit 8e6adc4

2 files changed

Lines changed: 39 additions & 41 deletions

File tree

includes/FastEngine/C_commandHandler.hpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@
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

3329
namespace fge
3430
{
@@ -37,16 +33,16 @@ class CommandHandler;
3733
class Scene;
3834
class 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

163166
private:
164-
fge::CommandHandler::CommandDataType g_cmdData;
167+
CommandDataType g_cmdData;
165168
std::unordered_map<std::string_view, std::size_t> g_cmdDataMap;
166169
};
167170

sources/C_commandHandler.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@
1717
#include "FastEngine/C_commandHandler.hpp"
1818
#include <limits>
1919

20-
#define _FGE_CMD_RESERVESIZE 30
21-
2220
namespace fge
2321
{
2422

2523
CommandHandler::CommandHandler()
2624
{
27-
this->g_cmdData.reserve(_FGE_CMD_RESERVESIZE);
25+
this->g_cmdData.reserve(FGE_COMMAND_DEFAULT_RESERVE_SIZE);
2826
}
2927

30-
bool CommandHandler::addCmd(std::string_view name, fge::CommandHandler* handle, fge::CommandFunction cmdfunc)
28+
bool CommandHandler::addCmd(std::string_view name, fge::CommandFunction cmdfunc)
3129
{
3230
auto it = this->g_cmdDataMap.find(name);
3331

3432
if (it == this->g_cmdDataMap.end())
3533
{
36-
auto& cmdData =
37-
this->g_cmdData.emplace_back(fge::CommandHandler::CommandData({handle, cmdfunc, std::string(name)}));
34+
auto& cmdData = this->g_cmdData.emplace_back(std::move(cmdfunc), name);
3835
this->g_cmdDataMap[cmdData._name] = this->g_cmdData.size() - 1;
3936
return true;
4037
}
@@ -47,8 +44,7 @@ void CommandHandler::delCmd(std::string_view name)
4744

4845
if (it != this->g_cmdDataMap.end())
4946
{
50-
this->g_cmdData.erase(this->g_cmdData.begin() +
51-
static_cast<fge::CommandHandler::CommandDataType::difference_type>(it->second));
47+
this->g_cmdData.erase(this->g_cmdData.begin() + static_cast<CommandDataType::difference_type>(it->second));
5248
this->g_cmdDataMap.erase(it);
5349

5450
for (std::size_t i = 0; i < this->g_cmdData.size(); ++i)
@@ -58,14 +54,13 @@ void CommandHandler::delCmd(std::string_view name)
5854
}
5955
}
6056

61-
bool CommandHandler::replaceCmd(std::string_view name, fge::CommandHandler* handle, fge::CommandFunction cmdfunc)
57+
bool CommandHandler::replaceCmd(std::string_view name, fge::CommandFunction cmdfunc)
6258
{
6359
auto it = this->g_cmdDataMap.find(name);
6460

6561
if (it != this->g_cmdDataMap.end())
6662
{
67-
this->g_cmdData[it->second]._handle = handle;
68-
this->g_cmdData[it->second]._func = cmdfunc;
63+
this->g_cmdData[it->second]._func = std::move(cmdfunc);
6964
return true;
7065
}
7166
return false;
@@ -78,22 +73,22 @@ void CommandHandler::clearCmd()
7873
}
7974

8075
fge::Property
81-
CommandHandler::callCmd(std::string_view name, fge::Object* caller, fge::Property const& arg, fge::Scene* caller_scene)
76+
CommandHandler::callCmd(std::string_view name, fge::Object* caller, fge::Property const& arg, fge::Scene* callerScene)
8277
{
8378
auto it = this->g_cmdDataMap.find(name);
8479

8580
if (it != this->g_cmdDataMap.end())
8681
{
87-
return (this->g_cmdData[it->second]._handle->*this->g_cmdData[it->second]._func)(caller, arg, caller_scene);
82+
return this->g_cmdData[it->second]._func->call(caller, arg, callerScene);
8883
}
8984
return {};
9085
}
9186
fge::Property
92-
CommandHandler::callCmd(std::size_t index, fge::Object* caller, fge::Property const& arg, fge::Scene* caller_scene)
87+
CommandHandler::callCmd(std::size_t index, fge::Object* caller, fge::Property const& arg, fge::Scene* callerScene)
9388
{
9489
if (index < this->g_cmdData.size())
9590
{
96-
return (this->g_cmdData[index]._handle->*this->g_cmdData[index]._func)(caller, arg, caller_scene);
91+
return this->g_cmdData[index]._func->call(caller, arg, callerScene);
9792
}
9893
return {};
9994
}

0 commit comments

Comments
 (0)