-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfactories.h
More file actions
222 lines (202 loc) · 5.48 KB
/
Copy pathfactories.h
File metadata and controls
222 lines (202 loc) · 5.48 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* factories.h - Assorted template factory, singleton factory and dispatcher class declarations.
Mike Jackson, The University of Edinburgh & Michael Chappell, FMRIB Image Analysis Group & IBME
QuBIc Group
Copyright (C) 2015 University of Oxford */
#pragma once
#include <iostream>
#include <map>
#include <string>
#include <vector>
/**
* Template factory class.
*
* Manages a map from names to function pointers. Each function
* pointer is assumed to point to a function that returns a pointer to
* an object of type T.
*/
template <class T> class TemplateFactory
{
public:
/**
* Function pointer to a zero argument function that returns a
* pointer to an object of type T.
*/
typedef T *(*Function)(void);
/** Constructor. */
TemplateFactory();
/** Destructor. */
~TemplateFactory();
/**
* Add a mapping from a given name to a function pointer.
* @param name Name by which the function pointer can be
* accessed.
* @param function Function pointer.
*/
void Add(const std::string &name, Function function);
/**
* Invoke the function pointer with the given name and return a
* pointer to an object.
* @param name
* @return pointer, or NULL if name is
* not known.
*/
T *Create(const std::string &name);
/**
* Get the list of names.
* @return list of names.
*/
std::vector<std::string> GetNames();
/**
* Is there a function pointer with the given name?
* @param name
* @return true if so, false otherwise.
*/
bool HasName(const std::string &name);
private:
/** Map from names to function pointers. */
std::map<std::string, Function> functionMap_;
};
template <class T> TemplateFactory<T>::TemplateFactory()
{
// No-op.
}
template <class T> TemplateFactory<T>::~TemplateFactory()
{
functionMap_.clear();
}
template <class T> std::vector<std::string> TemplateFactory<T>::GetNames()
{
std::vector<std::string> names;
for (typename std::map<std::string, Function>::iterator it = functionMap_.begin();
it != functionMap_.end(); ++it)
{
names.push_back(it->first);
}
return names;
}
template <class T> bool TemplateFactory<T>::HasName(const std::string &name)
{
return (functionMap_.find(name) != functionMap_.end());
}
template <class T> void TemplateFactory<T>::Add(const std::string &name, Function function)
{
functionMap_[name] = function;
}
template <class T> T *TemplateFactory<T>::Create(const std::string &name)
{
if (functionMap_.count(name))
{
return functionMap_[name]();
}
return NULL;
}
/**
* Singleton template factory class.
*
* Maintains a singleton instance of a \ref TemplateFactory.
*/
template <class T> class SingletonFactory : public TemplateFactory<T>
{
public:
/**
* Returns pointer to singleton instance of this class.
* @return instance.
*/
static SingletonFactory *GetInstance();
/**
* Delete the singleton instance.
*/
static void Destroy();
private:
/** Singleton instance of this class. */
static SingletonFactory *singleton_;
/** Constructor. */
SingletonFactory();
};
template <class T> SingletonFactory<T>::SingletonFactory()
{
// No-op.
}
template <class T> void SingletonFactory<T>::Destroy()
{
if (singleton_ != NULL)
{
delete singleton_;
singleton_ = NULL;
}
}
template <class T> SingletonFactory<T> *SingletonFactory<T>::singleton_ = NULL;
template <class T> SingletonFactory<T> *SingletonFactory<T>::GetInstance()
{
if (singleton_ == NULL)
{
singleton_ = new SingletonFactory<T>();
}
return singleton_;
}
/**
* Template class for registration of classes with factories.
* Assumes T supports a GetInstance function which returns an object
* which supports an Add(name, Function) function where Function
* is a function pointer.
* Assumes U supports a NewInstance function that conforms to the type
* suppoorted by T's Add function.
*/
template <class T, class U> class FactoryRegistration
{
public:
/**
* Constructor.
* @param name Name by which U's function will be registered,
*/
explicit FactoryRegistration(std::string name)
{
(T::GetInstance())->Add(name, &U::NewInstance);
}
};
/**
* Simple dispatcher class.
*
* Manages a map from names to function pointers. Each function
* pointer is assumed to point to a function that carries out some
* action.
*/
class Dispatcher
{
public:
/**
* Function pointer to a zero argument function that does some
* action, but returns no result.
*/
typedef void (*Function)(void);
/** Constructor. */
Dispatcher();
/** Destructor. */
~Dispatcher();
/**
* Add a mapping from a given name to a function pointer.
* @param name Name by which the function pointer can be
* accessed.
* @param function Function pointer.
*/
void Add(const std::string &name, Function function);
/**
* Invoke the function pointer with the given name.
* @param name
*/
void Dispatch(const std::string &name);
/**
* Get the list of names.
* @return list of names.
*/
std::vector<std::string> GetNames();
/**
* Is there a function pointer with the given name?
* @param name
* @return true if so, false otherwise.
*/
bool HasName(const std::string &name);
private:
/** Map from names to function pointers. */
std::map<std::string, Function> functionMap_;
};