-
Notifications
You must be signed in to change notification settings - Fork 47
/
StaticModule.h
71 lines (57 loc) · 1.74 KB
/
StaticModule.h
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
#pragma once
#include <functional>
#include "imodule.h"
/**
* greebo: Use a StaticModule to define a static RegisterableModule,
* which enlists itself automatically during application startup.
*
* The template parameter must be a RegisterModule class and
* is automatically registered with the ModuleRegistry by
* the StaticModule constructor.
*
* Since immediate registering is not desired, the constructor
* adds the incoming modules to a static std::list
* for a later routine to add the modules to the registry.
*
* Note: does NOT hold actual shared_ptrs of the RegisterableModule.
*
* Usage: StaticModule<RegisterableModule> myStaticModule;
*/
namespace module
{
namespace internal
{
/**
* Static container holding the modules registered by
* the StaticModule<T> helper. They will be picked up
* and acquired by the ModuleRegistry, after which point
* this list will be cleared.
*/
typedef std::function<RegisterableModulePtr()> ModuleCreationFunc;
class StaticModuleList :
private std::list<ModuleCreationFunc>
{
public:
~StaticModuleList();
static void Add(const ModuleCreationFunc& creationFunc);
// Creates all pre-registered modules and submits them to the registry
static void RegisterModules();
private:
static StaticModuleList& Instance();
};
}
template <class ModuleType>
class StaticModule
{
static_assert(std::is_base_of<RegisterableModule, ModuleType>::value, "ModuleType must be of type RegisterableModule");
public:
StaticModule()
{
// Add a creator to the list in the backend, it will be called by the registry later
internal::StaticModuleList::Add([]()->RegisterableModulePtr
{
return std::make_shared<ModuleType>();
});
}
};
} // namespace module