-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryFactory.cpp
More file actions
46 lines (35 loc) · 1.13 KB
/
Copy pathQueryFactory.cpp
File metadata and controls
46 lines (35 loc) · 1.13 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
/**
* Query Factory
*
* Based on C++ Object Factory
* http://www.codeproject.com/Articles/567242/AplusC-b-bplusObjectplusFactory
*/
#include "QueryFactory.hpp"
QueryRegistrar::QueryRegistrar(string name, function<QueryBase*(void)> classFactoryFunction)
{
// register the class factory function
QueryFactory::Instance()->RegisterFactoryFunction(name, classFactoryFunction);
}
QueryFactory * QueryFactory::Instance()
{
static QueryFactory factory;
return &factory;
}
void QueryFactory::RegisterFactoryFunction(string name, function<QueryBase*(void)> classFactoryFunction)
{
// register the class factory function
factoryFunctionRegistry[name] = classFactoryFunction;
}
shared_ptr<QueryBase> QueryFactory::Create(string name)
{
QueryBase * instance = nullptr;
// find name in the registry and call factory method.
auto it = factoryFunctionRegistry.find(name);
if(it != factoryFunctionRegistry.end())
instance = it->second();
// wrap instance in a shared ptr and return
if(instance != nullptr)
return std::shared_ptr<QueryBase>(instance);
else
return nullptr;
}