|
| 1 | +#ifndef DESIGN_PATTERN_FACTORY_HPP |
| 2 | +#define DESIGN_PATTERN_FACTORY_HPP |
| 3 | + |
| 4 | +#include <memory> |
| 5 | +#include <map> |
| 6 | +#include <iostream> |
| 7 | +#include <typeinfo> |
| 8 | +#include <cxxabi.h> |
| 9 | + |
| 10 | +#include "util/color.hpp" |
| 11 | +#include "exception.hpp" |
| 12 | + |
| 13 | +namespace design_patterns { |
| 14 | +namespace creational { |
| 15 | + |
| 16 | +class factory_exception : public design_pattern_exception { |
| 17 | +public: |
| 18 | + explicit factory_exception(const std::string& msg):design_pattern_exception(msg) { }; |
| 19 | +}; |
| 20 | + |
| 21 | +class factory_create_exception : public factory_exception { |
| 22 | +public: |
| 23 | + explicit factory_create_exception(const std::string& tag) : |
| 24 | + factory_exception("Unknown type under name '" + tag + "'"){}; |
| 25 | +}; |
| 26 | + |
| 27 | +template<class BaseType, class... Args> |
| 28 | +struct map_holder { |
| 29 | + static std::map<std::string, |
| 30 | + std::unique_ptr<BaseType>(*)(Args&& ...)> functions; |
| 31 | +}; |
| 32 | + |
| 33 | +template<class BaseType, class... Args> |
| 34 | +std::map<std::string, std::unique_ptr<BaseType>(*)(Args&& ...)>map_holder< |
| 35 | + BaseType, |
| 36 | + Args...>::functions; |
| 37 | + |
| 38 | +template<class T> |
| 39 | +class factory { |
| 40 | +public: |
| 41 | + |
| 42 | + factory(const factory&) = default; |
| 43 | + void operator=(factory const&) = delete; |
| 44 | + |
| 45 | + static factory& get_instance() |
| 46 | + { |
| 47 | + static factory instance; |
| 48 | + return instance; |
| 49 | + } |
| 50 | + |
| 51 | + template<class ...Args> |
| 52 | + unsigned long registered() const |
| 53 | + { |
| 54 | + return map_holder<T, Args&& ...>::functions.size(); |
| 55 | + } |
| 56 | + |
| 57 | + const std::string name() const |
| 58 | + { |
| 59 | + return demangle(typeid(*this).name()); |
| 60 | + } |
| 61 | + |
| 62 | + template<class TDerived, typename ...Args> |
| 63 | + bool register_type(const std::string& name) |
| 64 | + { |
| 65 | + |
| 66 | + if (map_holder<T, Args&& ...>::functions.count(name)==0) { |
| 67 | + |
| 68 | + map_holder<T, Args&& ...>::functions[name] = |
| 69 | + &create_func<TDerived, Args...>; |
| 70 | + |
| 71 | + std::cout << "[OK]: " << (*this).name() << ": " |
| 72 | + << FBLU(demangle(typeid(TDerived).name())) |
| 73 | + << " type registered under name " << FGRN(name) |
| 74 | + << std::endl; |
| 75 | + /** |
| 76 | + std::cout << "Ptr: " |
| 77 | + << &MapHolder<T, Args...>::functions[name] |
| 78 | + << std::endl; |
| 79 | + */ |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + std::cout << "[ERROR]: Type already registered: " << name << |
| 84 | + std::endl; |
| 85 | + return false; |
| 86 | + } |
| 87 | + /** |
| 88 | + * Create instance by a registered name |
| 89 | + * @param name The name of the registered class type |
| 90 | + * @return An instance of the requested class name, or null if not |
| 91 | + * found. |
| 92 | + */ |
| 93 | + template<typename ...Args> |
| 94 | + std::unique_ptr<T> create(const std::string& name, Args&& ...args) |
| 95 | + { |
| 96 | + try { |
| 97 | + auto el = map_holder<T, Args&& ...>::functions.at(name); |
| 98 | + return el(std::forward<Args>(args)...); |
| 99 | + } |
| 100 | + catch (std::exception& ex) { |
| 101 | + throw factory_create_exception(name); |
| 102 | + } |
| 103 | + /** |
| 104 | + auto it = MapHolder<T, Args&&...>::functions.find(name); |
| 105 | + if (it!=MapHolder<T, Args&&...>::functions.end()) { |
| 106 | + std::cout << "[OK] factory::create() " << name << std::endl; |
| 107 | + std::cout << "Ptr: " << &(*it).second << std::endl; |
| 108 | + return (*it).second(std::forward<Args>(args)...); |
| 109 | + } |
| 110 | + std::cout << "[ERROR]: factory::create() " << name << std::endl; |
| 111 | + return std::unique_ptr<T>(nullptr); |
| 112 | + */ |
| 113 | + } |
| 114 | +protected: |
| 115 | + static const std::string demangle(const char* name) |
| 116 | + { |
| 117 | + int status = -4; |
| 118 | + char* res = abi::__cxa_demangle(name, nullptr, nullptr, &status); |
| 119 | + const char* const demangled_name = (status==0) ? res : name; |
| 120 | + std::string ret_val(demangled_name); |
| 121 | + free(res); |
| 122 | + return ret_val; |
| 123 | + } |
| 124 | + |
| 125 | +private: |
| 126 | + factory() = default; |
| 127 | + |
| 128 | + template<class TDerived, typename ...Args> |
| 129 | + static std::unique_ptr<T> create_func(Args&& ...args) |
| 130 | + { |
| 131 | + std::cout << "[INFO]: factory::create_func() " |
| 132 | + << typeid(TDerived).name() << std::endl; |
| 133 | + return std::make_unique<TDerived>(std::forward<Args>(args)...); |
| 134 | + } |
| 135 | + |
| 136 | +}; |
| 137 | + |
| 138 | +template<typename T, typename ...Args> |
| 139 | +std::unique_ptr<T> make(const std::string& name, Args&& ...args) |
| 140 | +{ |
| 141 | + auto fac = factory<T>::get_instance(); |
| 142 | + return fac.create(name, std::forward<Args>(args)...); |
| 143 | +} |
| 144 | + |
| 145 | +} |
| 146 | +} |
| 147 | + |
| 148 | +#endif //DESIGN_PATTERN_FACTORY_HPP |
0 commit comments