-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinktimeplugin.hpp
More file actions
140 lines (126 loc) · 3.94 KB
/
Copy pathlinktimeplugin.hpp
File metadata and controls
140 lines (126 loc) · 3.94 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
/**
* @brief Link-time plug-in management
* @version 1.0.1
* @author Wolfram Rösler
* @date 2018-06-25
* @copyright MIT license
*/
#pragma once
#include <memory>
#include <vector>
/**
* Link-time plug-in management.
*
* Usage:
*
* 1. Define a base class for your plug-ins.
* 2. In this plug-in base class, add "public: using Base=x",
* where x is the name of the plug-in base class.
* 3. For every plug-in, derive a class from the base class.
* 4. For every such class, invoke REGISTER_PLUGIN(x), where x is the
* name of the derived plug-in class.
* 5. To retrieve a list of all plug-ins, invoke linktimeplugin::plugins<x>(),
* where x is the name of the plug-in base class. This function
* returns a pointer to an instance of every plug-in class.
*/
namespace linktimeplugin {
/*
* Base class for plug-in registrars. A registrar is an intermediate
* class that manages the registration of one plug-in class (which
* is derived from the common plug-in base class).
*/
template<typename BASE>
class RegistrarBase {
public:
// Ctor. Adds this object to the list of registrars.
RegistrarBase() noexcept {
try {
if (!registrars_) {
registrars_.reset(new std::vector<RegistrarBase<BASE>*>);
}
registrars_->push_back(this);
} catch(...) {}
}
// Rule of 5
virtual ~RegistrarBase() = default;
RegistrarBase(const RegistrarBase&) = delete;
RegistrarBase(RegistrarBase&&) = delete;
void operator=(const RegistrarBase&) = delete;
void operator=(RegistrarBase&&) = delete;
// Implemented by the derived registrar class.
virtual BASE& operator()() = 0;
// Returns all registrars.
static std::vector<BASE*> plugins() {
std::vector<BASE*> ret;
if (registrars_) {
for(auto r : *registrars_) {
ret.push_back(&(*r)());
}
}
return ret;
}
private:
// Pointers to the registrar objects (one per registered
// plug-in class).
static std::unique_ptr<std::vector<RegistrarBase<BASE>*>> registrars_;
};
/*
* Static member of the registrar base class.
* BASE is the plug-in base class.
*/
template<typename BASE>
std::unique_ptr<std::vector<RegistrarBase<BASE>*>> RegistrarBase<BASE>::registrars_;
/*
* Derived registrar class.
* PLUGIN is the plug-in class (derived from the plug-in base class).
*/
template<typename PLUGIN>
class Registrar : public RegistrarBase<typename PLUGIN::Base> {
PLUGIN plugin_;
typename PLUGIN::Base& operator()() override {
return plugin_;
}
};
/**
* Get pointers to instances of all registered plug-in classes.
*
* T is the plug-in base class.
*
* Example: (MyBase is the plug-in base class, DoSomething is a pure
* virtual function in the plug-in base class, implemented by the
* derived plug-in classes)
*
* for (auto& p : linktimeplugin::plugins<MyBase>()) {
* p->DoSomething();
* }
*/
template<typename T>
std::vector<T*> plugins() {
return RegistrarBase<T>::plugins();
}
}
/**
* Register one plug-in class.
* Use this once for every plug-in class that's derived from the
* plug-in base class.
*
* x is the name of the derived plug-in class.
*
* Example:
*
* // Base class
* class PluginBase {
* public:
* using Base = PluginBase;
* virtual void DoSomething() = 0;
* };
*
* // Plug-in class
* class Plugin: public PluginBase {
* void DoSomething() override { ... }
* };
*
* // Register the plug-in class
* REGISTER_PLUGIN(Plugin);
*/
#define REGISTER_PLUGIN(x) static linktimeplugin::Registrar<x> x##registrar