FastEngine 0.9.5
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_commandHandler.hpp
1/*
2 * Copyright 2026 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_COMMANDHANDLER_HPP_INCLUDED
18#define _FGE_C_COMMANDHANDLER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_callback.hpp"
22#include "FastEngine/C_property.hpp"
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27#define FGE_COMMAND_DEFAULT_RESERVE_SIZE 16
28
29namespace fge
30{
31
32class CommandHandler;
33class Scene;
34class Object;
35
36using CommandFunction = fge::CalleeUniquePtr<fge::Property, fge::Object*, fge::Property const&, fge::Scene*>;
37using CommandStaticHelpers =
38 fge::CallbackStaticHelpers<fge::Property, CommandFunction, fge::Object*, fge::Property const&, fge::Scene*>;
39
50class FGE_API CommandHandler
51{
52public:
57 struct CommandData
58 {
59 inline CommandData(fge::CommandFunction cmdfunc, std::string_view name) :
60 _func(std::move(cmdfunc)),
61 _name(name)
62 {}
63
64 fge::CommandFunction _func;
65 std::string _name;
66 };
67
68 using CommandDataType = std::vector<CommandData>;
69
70 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;
76
86 bool addCmd(std::string_view name, fge::CommandFunction cmdfunc);
92 void delCmd(std::string_view name);
100 bool replaceCmd(std::string_view name, fge::CommandFunction cmdfunc);
101
105 void clearCmd();
106
117 callCmd(std::string_view name, fge::Object* caller, fge::Property const& arg, fge::Scene* callerScene);
127 fge::Property callCmd(std::size_t index, fge::Object* caller, fge::Property const& arg, fge::Scene* callerScene);
128
135 [[nodiscard]] std::size_t getCmdIndex(std::string_view name) const;
142 [[nodiscard]] std::string_view getCmdName(std::size_t index) const;
143
150 [[nodiscard]] CommandData const* getCmd(std::string_view name) const;
151
157 [[nodiscard]] std::size_t getCmdSize() const;
158
164 [[nodiscard]] CommandDataType const& getCmdList() const;
165
166private:
167 CommandDataType g_cmdData;
168 std::unordered_map<std::string_view, std::size_t> g_cmdDataMap;
169};
170
171} // namespace fge
172
173#endif // _FGE_C_COMMANDHANDLER_HPP_INCLUDED
CommandHandler can implement functions attached to an Object that can be called by others with a name...
Definition C_commandHandler.hpp:51
void delCmd(std::string_view name)
Delete a command from the handler.
CommandDataType const & getCmdList() const
Get the commands list.
bool replaceCmd(std::string_view name, fge::CommandFunction cmdfunc)
Replace a command from the handler.
std::string_view getCmdName(std::size_t index) const
Get the name of a command by its index.
std::size_t getCmdSize() const
Get the number of commands.
fge::Property callCmd(std::string_view name, fge::Object *caller, fge::Property const &arg, fge::Scene *callerScene)
Call a command by its name.
CommandData const * getCmd(std::string_view name) const
Get a command by its name.
void clearCmd()
Clear all commands from the handler.
fge::Property callCmd(std::size_t index, fge::Object *caller, fge::Property const &arg, fge::Scene *callerScene)
Call a command by its index.
bool addCmd(std::string_view name, fge::CommandFunction cmdfunc)
Add a new command to the handler.
std::size_t getCmdIndex(std::string_view name) const
Get the index of a command by its name.
The Object class is the base class for all objects in the engine.
Definition C_object.hpp:102
A class that can store any type of data.
Definition C_property.hpp:54
A scene contain a collection of object and handle them.
Definition C_scene.hpp:465
This struct contain the data of a command, like the name and the function pointer.
Definition C_commandHandler.hpp:58