-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryFactory.hpp
More file actions
44 lines (32 loc) · 1.1 KB
/
Copy pathQueryFactory.hpp
File metadata and controls
44 lines (32 loc) · 1.1 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
#ifndef QUERY_FACTORY_H
#define QUERY_FACTORY_H
#include "QueryBase.hpp"
#include <memory>
#include <string>
#include <map>
#include <functional>
using namespace std;
// A helper class to register a factory function
class QueryRegistrar {
public:
QueryRegistrar(string className, function<QueryBase*(void)> classFactoryFunction);
};
// A preprocessor define used by derived classes
#define REGISTER_QUERY_CLASS(NAME, TYPE) static QueryRegistrar Queryregistrar(NAME, [](void) -> QueryBase * { return new TYPE();});
// The factory - implements singleton pattern!
class QueryFactory
{
public:
// Get the single instance of the factory
static QueryFactory * Instance();
// register a factory function to create an instance of className
void RegisterFactoryFunction(string name, function<QueryBase*(void)> classFactoryFunction);
// create an instance of a registered class
shared_ptr<QueryBase> Create(string name);
private:
// a private ctor
QueryFactory(){}
// the registry of factory functions
map<string, function<QueryBase*(void)>> factoryFunctionRegistry;
};
#endif