ifw-sup 5.1.0
 
Loading...
Searching...
No Matches
subsysFactory.hpp
Go to the documentation of this file.
1
7
8#ifndef SUP_SYSSUP_COMMON_SUBSYSFACTORY_HPP
9#define SUP_SYSSUP_COMMON_SUBSYSFACTORY_HPP
10
11// System headers
12#include <iostream>
13#include <memory>
14#include <string>
15#include <unordered_map>
16
17#include <cstdlib>
18#include <cxxabi.h>
19
21
22std::string Demangle(const char *name);
23
33 template <class Base, class... Args> class Factory {
34 public:
35
36 template <class ... T>
45 static std::unique_ptr<Base> Create(const std::string &s, T&&... args) {
46 const auto& obj_map = Data();
47 auto i = obj_map.find(s);
48 if (i == obj_map.end()) {
49 return nullptr;
50 } else {
51 return obj_map.at(s)(std::forward<T>(args)...);
52 }
53 }
54
55 template <class T> struct Registrar : Base {
56 friend T;
57
58 static bool RegisterT() {
59 const auto name = ifw::sup::syssup::common::Demangle(typeid(T).name());
60
61 Factory::Data()[name] = [](Args... args) -> std::unique_ptr<Base> {
62 return std::make_unique<T>(std::forward<Args>(args)...);
63 };
64 return true;
65 }
66 static bool registered;
67
68 private:
69 Registrar() : Base(Key{}) { (void)registered; }
70 };
71
72 friend Base;
73
74 private:
75 class Key {
76 Key(){};
77 template <class T> friend struct Registrar;
78 };
79 using FuncType = std::unique_ptr<Base> (*)(Args...);
80 Factory() = default;
81
82 static auto &Data() {
83 static std::unordered_map<std::string, FuncType> s;
84 return s;
85 }
86 };
87
88 template <class Base, class... Args>
89 template <class T>
90 bool Factory<Base, Args...>::Registrar<T>::registered =
92
93}
94
95#endif //SUP_SYSSUP_COMMON_SUBSYS_FACTORY_HPP
Definition subsysFactory.hpp:33
friend Base
Definition subsysFactory.hpp:72
static std::unique_ptr< Base > Create(const std::string &s, T &&... args)
create subsystem object instance
Definition subsysFactory.hpp:45
ActionMgr class source file.
Definition actionMgr.cpp:30
std::string Demangle(const char *name)
Definition subsysFactory.cpp:15
Definition subsysFactory.hpp:55
static bool RegisterT()
Definition subsysFactory.hpp:58
friend T
Definition subsysFactory.hpp:56
static bool registered
Definition subsysFactory.hpp:66