10 #include "common/str.h" 16 #include <unordered_map> 21 #define REGISTER_SUBCLASS_0(Base, Derived) \ 23 bool _registered_##Base##_##Derived ATTR_UNUSED = \ 24 SubclassRegistry<Base>::registerSubclass( \ 27 []() -> std::shared_ptr<Base> { \ 28 return std::make_shared<Derived>(); \ 32 #define REGISTER_SUBCLASS_1(Base, Derived, Arg1) \ 34 bool _registered_##Base##_##Derived ATTR_UNUSED = \ 35 SubclassRegistry<Base, Arg1>::registerSubclass( \ 38 [](Arg1 arg1) -> std::shared_ptr<Base> { \ 39 return std::make_shared<Derived>(arg1); \ 43 #define REGISTER_SUBCLASS_3(Base, Derived, Arg1, Arg2, Arg3) \ 45 bool _registered_##Base##_##Derived ATTR_UNUSED = \ 46 SubclassRegistry<Base, Arg1, Arg2, Arg3>::registerSubclass( \ 49 [](Arg1 arg1, Arg2 arg2, Arg3 arg3) -> std::shared_ptr<Base> { \ 50 return std::make_shared<Derived>(arg1, arg2, arg3); \ 54 template <
typename Base,
typename... Args>
57 using Ctor = std::function<std::shared_ptr<Base>(Args...)>;
64 std::type_index
const& type,
65 std::string
const& name,
67 : type(type), name(name), ctor(
std::move(ctor)) {}
71 std::type_index
const&
type,
72 std::string
const&
name,
74 auto& reg = registry();
75 if (reg.byType.find(type) == reg.byType.end()) {
76 reg.info.emplace_back(type, name, std::move(ctor));
77 reg.byType.emplace(type, reg.info.back());
84 auto& reg = registry();
86 auto it = reg.byName.find(lowerName);
87 if (it == reg.byName.end()) {
94 std::vector<SubclassInfo*> result;
95 for (
auto& it : registry().info) {
96 result.push_back(&it);
101 template <
typename... CArgs>
103 std::string
const&
name,
105 auto& reg = registry();
107 auto it = reg.byName.find(lowerName);
108 if (it == reg.byName.end()) {
111 return it->second.ctor(std::forward<CArgs>(args)...);
114 static std::string
name(std::type_index
const&
type) {
115 auto& reg = registry();
116 auto it = reg.byType.find(type);
117 if (it == reg.byType.end()) {
118 return std::string();
120 return it->second.name;
123 template <
typename Derived>
125 return name(
typeid(Derived));
130 std::list<SubclassInfo> info;
131 std::unordered_map<std::type_index, SubclassInfo&> byType;
133 std::unordered_map<std::string, SubclassInfo&> byName;
136 static Registry& registry() {
137 static Registry reg_;
std::string stringToLower(T &&str)
Definition: str.h:53
SubclassInfo(std::type_index const &type, std::string const &name, Ctor ctor)
Definition: registry.h:63
Definition: registry.h:58
static std::string name(std::type_index const &type)
Definition: registry.h:114
Ctor ctor
Definition: registry.h:61
std::string name
Definition: registry.h:60
static std::vector< SubclassInfo * > subclasses()
Definition: registry.h:93
Definition: registry.h:55
Main namespace for bot-related code.
Definition: areainfo.cpp:17
static std::string name()
Definition: registry.h:124
static std::shared_ptr< Base > create(std::string const &name, CArgs &&...args)
Definition: registry.h:102
std::function< std::shared_ptr< Base >(Args...)> Ctor
Definition: registry.h:57
static bool registerSubclass(std::type_index const &type, std::string const &name, Ctor ctor)
Definition: registry.h:70
static SubclassInfo * record(std::string const &name)
Definition: registry.h:83
std::type_index type
Definition: registry.h:59