-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdynamic_factory.h
More file actions
129 lines (106 loc) · 3.43 KB
/
dynamic_factory.h
File metadata and controls
129 lines (106 loc) · 3.43 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright(c) 2016 dragon jiang<jianlinlong@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _DYNAMIC_FACTORY_H_
#define _DYNAMIC_FACTORY_H_
#include <functional>
#include <map>
#include <memory>
//http://stackoverflow.com/questions/19036462/c-generic-object-factory-by-string-name
//https://gist.github.com/sacko87/3359911
//http://www.codeproject.com/Articles/567242/AplusC-b-bplusObjectplusFactory
template <typename Base_Type>
class Instance_Factory
{
public:
typedef Base_Type Base;
typedef std::function<Base*(void)> FN;
static Instance_Factory* instance()
{
static Instance_Factory factory;
return &factory;
}
/*
* @name class name
* @fn a creator which create instance of @name
*/
void register_function(const std::string& name, FN fn)
{
// register the class factory function
name_fn_map_[name] = fn;
}
/*
* create instance by class name
* @name class name
*/
std::shared_ptr<Base> create(const std::string& name)
{
Base * instance = nullptr;
// find name in the registry and call factory method.
auto it = name_fn_map_.find(name);
if (it != name_fn_map_.end())
instance = it->second();
// wrap instance in a shared ptr and return
if (instance != nullptr)
return std::shared_ptr<Base>(instance);
else
return nullptr;
}
private:
std::map<std::string, FN> name_fn_map_;
};
template<typename T, typename Base_Type>
class Registrar
{
typedef Base_Type Base;
public:
Registrar(const std::string& class_name)
{
// register the class factory function
Instance_Factory<Base>::instance()->register_function(class_name,
[](void) -> Base * { return new T(); });
}
};
/*
* @T instance type, which is inherit from @Base_Type
* @Base_Type paren type of @T
*/
#define REGISTRAR_CLASS_2(T, Base_Type) \
static Registrar<T, Base_Type> s___creator__##T(#T)
#if 0 //Demo Code -->create instance from string
#include "dynamic_factory.h"
int main(int argc, char *argv[])
{
struct X {
};
struct A : public X{
int a = 100;
};
struct B : public X{
int b = 999;
};
REGISTRAR_CLASS_2(A, X);
REGISTRAR_CLASS_2(B, X);
auto x1 = Instance_Factory<X>::instance()->create("A");
auto x2 = Instance_Factory<X>::instance()->create("B");
}
#endif
#endif