Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
commands_registry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <functional>
5#include <memory>
7
8namespace CommandsRegistry {
9 using CommandFactory = std::function<std::unique_ptr<ICommand>()>;
10
11 inline std::unordered_map<std::string, CommandFactory> build_command_map(
12 const std::vector<std::pair<std::vector<std::string>, CommandFactory>>& commands
13 ) {
14 std::unordered_map<std::string, CommandFactory> map;
15 for (auto& cmd : commands) {
16 for (auto& alias : cmd.first) {
17 map[alias] = cmd.second;
18 }
19 }
20 return map;
21 }
22
23 inline std::unique_ptr<ICommand> get_command(
24 const std::string& name,
25 const std::vector<std::pair<std::vector<std::string>, CommandFactory>>& commands
26 ) {
27 static const auto internal_commands = build_command_map(commands);
28 auto it = internal_commands.find(name);
29 if (it != internal_commands.end()) {
30 return it->second();
31 }
32 return nullptr;
33 }
34
35} // namespace CommandsRegistry
std::unique_ptr< ICommand > get_command(const std::string &name, const std::vector< std::pair< std::vector< std::string >, CommandFactory > > &commands)
std::function< std::unique_ptr< ICommand >()> CommandFactory
std::unordered_map< std::string, CommandFactory > build_command_map(const std::vector< std::pair< std::vector< std::string >, CommandFactory > > &commands)