mirror of
https://github.com/xtjoeytx/GServer-v2.git
synced 2026-05-06 23:59:39 -04:00
50 lines
888 B
C++
50 lines
888 B
C++
#ifndef GS2EMU_COMMANDDISPATCHER_H
|
|
#define GS2EMU_COMMANDDISPATCHER_H
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
template<typename key_type, typename... Ts>
|
|
class CommandDispatcher
|
|
{
|
|
using fn_type = std::function<bool(Ts...)>;
|
|
using cmd_map_type = std::unordered_map<key_type, fn_type>;
|
|
|
|
public:
|
|
class Builder
|
|
{
|
|
cmd_map_type& m_builderCommands;
|
|
|
|
public:
|
|
Builder(cmd_map_type& cmds) : m_builderCommands(cmds) {}
|
|
|
|
void registerCommand(key_type key, fn_type fn)
|
|
{
|
|
m_builderCommands[key] = fn;
|
|
}
|
|
};
|
|
|
|
CommandDispatcher() {}
|
|
CommandDispatcher(std::function<void(Builder)> initfn)
|
|
{
|
|
initfn(Builder(m_commands));
|
|
}
|
|
|
|
bool execute(key_type key, Ts... args)
|
|
{
|
|
auto cmd_iter = m_commands.find(key);
|
|
if (cmd_iter == m_commands.end())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return cmd_iter->second(args...);
|
|
}
|
|
|
|
private:
|
|
cmd_map_type m_commands;
|
|
};
|
|
|
|
#endif
|