forked from leameow/factory_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.h
More file actions
62 lines (50 loc) · 1.8 KB
/
factory.h
File metadata and controls
62 lines (50 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <map>
#include <memory>
#define DECLARE_FACTORY(Base, KeyType) \
template<> std::map<KeyType, std::shared_ptr<AbstractFactory<Base>>> FactoryStore<Base, KeyType>::_factories = {};\
template<> class FactoryKeyType<Base> { public: typedef KeyType Type; };
#define DECLARE_FACTORY_2(Base, KeyType) \
template<> std::map<KeyType, std::shared_ptr<AbstractFactory<Base>>> FactoryStore<Base, KeyType>::_factories = {};
#define REGISTER_FACTORY(Base, CType, Key) static FactoryRegisterer<Base, CType, FactoryKeyType<Base>::Type> Base##CType##_factory_registerer(Key);
#define REGISTER_FACTORY_2(Base, Type, KeyType, Key) static FactoryRegisterer<Base, Type, KeyType> Base##Type##KeyType##_factory_registerer(Key);
template<typename T>
class AbstractFactory {
public:
virtual std::shared_ptr<T> build()=0;
};
template<typename>
class FactoryKeyType {};
template<typename T, typename K = typename FactoryKeyType<T>::Type>
class FactoryStore {
public:
static std::shared_ptr<T> build(K type) {
return _factories[type]->build();
}
static void register_factory(K key, std::shared_ptr<AbstractFactory<T>> factory) {
_factories[key] = factory;
}
private:
static std::map<K, std::shared_ptr<AbstractFactory<T>>> _factories;
};
template<typename T>
class Factory {
public:
template<typename K>
static std::shared_ptr<T> build(K type) {
return FactoryStore<T,K>::build(type);
};
};
template<typename T, typename I>
class SimpleFactory : public AbstractFactory<T> {
public:
virtual std::shared_ptr<T> build() {
return std::make_shared<I>();
};
};
template<typename T, typename I, typename K>
class FactoryRegisterer {
public:
FactoryRegisterer(K key) {
FactoryStore<T,K>::register_factory(key, std::make_shared<SimpleFactory<T, I>>());
}
};