summaryrefslogtreecommitdiff
path: root/libadhocutil/plugins.h
blob: d51ad2bc0223b2104578d6fc87842c71c9a7ee92 (plain)
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
223
224
225
226
227
228
#ifndef ADHOCUTIL_PLUGINS_H
#define ADHOCUTIL_PLUGINS_H

#include <memory>
#include <functional>
#include <optional>
#include <typeinfo>
#include <set>
#include <map>
#include <algorithm>
#include "visibility.h"
#include "unique.h"

namespace std {
	DLL_PUBLIC
	std::ostream &
	operator<<(std::ostream & s, const std::type_info & t);
}

namespace AdHoc {
	/// Thrown when no matching plugin can be found.
	class NoSuchPluginException : public std::runtime_error {
		public:
			/// Constructor taking name and type of plugin requested.
			NoSuchPluginException(const std::string &, const std::type_info &);
	};

	/// Base class for all plugin implementations.
	class DLL_PUBLIC AbstractPluginImplementation {
		public:
			virtual ~AbstractPluginImplementation() = 0;
	};

	/// Base class for untyped plugins.
	class DLL_PUBLIC Plugin {
		public:
			/// Constructor taking name, filename and line of install.
			Plugin(const std::string &, const std::string &, int);
			virtual ~Plugin() = default;

			/// Get the plugin type from the subclass.
			virtual const std::type_info & type() const = 0;

			/// Get the abstract base plugin implementation.
			virtual std::shared_ptr<AbstractPluginImplementation> instance() const = 0;

			/// The name the plugin was installed with.
			const std::string name;
			/// The filename the plugin was installed in.
			const std::string filename;
			/// The line of file the plugin was installed in.
			const int lineno;
	};
	typedef std::shared_ptr<const Plugin> PluginPtr;

	/// Thrown when a plugin with the same name and base is loaded into a manager.
	class DuplicatePluginException : public std::runtime_error {
		public:
			/// Constructor taking the original and offending plugin.
			DuplicatePluginException(PluginPtr p1, PluginPtr p2);
	};

	/// Thrown when a resolver function is added a second time.
	class DuplicateResolverException : public std::runtime_error {
		public:
			/// Constuctor taking resolver type.
			DuplicateResolverException(const std::type_info &);
	};

	/// Thrown when an attempt to load a library fails.
	class LoadLibraryException : public std::runtime_error {
		public:
			/// Constuctor taking syscall error details.
			LoadLibraryException(const std::string & f, const char * msg);
	};

	template <typename T>
	/// Typed plugin and handle to implementation.
	class DLL_PUBLIC PluginOf : public Plugin {
		public:
			/// Constructor taking an instance and name, filename and line of install for Plugin.
			PluginOf(const std::shared_ptr<T> & t, const std::string & n, const std::string & f, int l);
			~PluginOf() = default;

			/// Get the type of this plugin.
			const std::type_info & type() const override;
			/// Get the implementation of this plugin.
			std::shared_ptr<T> implementation() const;

			std::shared_ptr<AbstractPluginImplementation> instance() const override;

		private:
			std::shared_ptr<T> impl;
	};

	/// Container for loaded plugins.
	class DLL_PUBLIC PluginManager {
		public:
			/// Callback definition to resolve a plugin type and name to a potential library
			/// containing an implementation.
			typedef std::function<std::optional<std::string> (const std::type_info &, const std::string &)> PluginResolver;

			PluginManager();
			virtual ~PluginManager() = default;

			/// Install a plugin.
			void add(const PluginPtr &);
			/// Uninstall a plugin.
			void remove(const std::string &, const std::type_info &);
			/// Get a specific plugin.
			PluginPtr get(const std::string &, const std::type_info &) const;
			/// Get all plugins.
			std::set<PluginPtr> getAll() const;
			/// Get all plugins of a specific type.
			std::set<PluginPtr> getAll(const std::type_info &) const;

			/**
			 * Install a plugin.
			 * @param i Implementation instance.
			 * @param n Name of plugin.
			 * @param f Filename of plugin.
			 * @param l Line number.
			 */
			template<typename T> void add(const std::shared_ptr<T> & i, const std::string & n, const std::string & f, int l);

			/**
			 * Create and install a plugin
			 * @tparam T Base type of plugin
			 * @tparam I Implementation type of plugin
			 * @tparam Args Constructor arguments types
			 * @param n Name of plugin.
			 * @param f Filename of plugin.
			 * @param l Line number.
			 * @param args Arguments to construct an instance of I with.
			 */
			template<typename T, typename I, typename ... Args> void create(const std::string & n, const std::string & f, int l, const Args & ... args)
			{
				add<T>(std::make_shared<I>(args...), n, f, l);
			}

			/**
			 * Uninstall a plugin.
			 * @param n Name of plugin.
			 */
			template<typename T> void remove(const std::string & n);

			/**
			 * Get a specific plugin.
			 * @param n Name of plugin.
			 */
			template<typename T> std::shared_ptr<const PluginOf<T>> get(const std::string & n) const;

			/**
			 * Get the implementation from specific plugin.
			 * @param n Name of plugin.
			 */
			template<typename T> std::shared_ptr<T> getImplementation(const std::string & n) const;

			/**
			 * Get all plugins of a given time.
			 */
			template<typename T> std::set<std::shared_ptr<const PluginOf<T>>> getAll() const;

			/**
			 * The number of installed plugins.
			 */
			size_t count() const;

			/**
			 * Add a type plugin resolver function.
			 * @param t The resolver type.
			 * @param f The resolver function.
			 */
			void addResolver(const std::type_info & t, const PluginResolver & f);

			/**
			 * Add a type plugin resolver function.
			 * @param f The resolver function.
			 */
			template<typename T> void addResolver(const PluginResolver & f);

			/**
			 * Remove a type plugin resolver function.
			 * @param t The resolver type.
			 */
			void removeResolver(const std::type_info & t);

			/**
			 * Remove a type plugin resolver function.
			 */
			template<typename T> void removeResolver();

			/**
			 * The number of installed plugins.
			 */
			size_t countResolvers() const;

			/**
			 * Get the default plugin manager instance.
			 */
			static PluginManager * getDefault();

		private:
			static void loadLibrary(const std::string &);

			class PluginStore;
			std::unique_ptr<PluginStore> plugins;
			class TypePluginResolvers;
			std::unique_ptr<TypePluginResolvers> resolvers;
	};
}

#define NAMEDPLUGIN(Name, Implementation, Base) \
	namespace MAKE_UNIQUE(__plugin__) { \
		static void InstallPlugin() __attribute__((constructor(102))); \
		void InstallPlugin() { \
			::AdHoc::PluginManager::getDefault()->add<Base>(std::make_shared<Implementation>(), Name, __FILE__, __LINE__); \
		} \
		static void UninstallPlugin() __attribute__((destructor(102))); \
		void UninstallPlugin() { \
			::AdHoc::PluginManager::getDefault()->remove<Base>(Name); \
		} \
	}
#define PLUGIN(Implementation, Base) \
	NAMEDPLUGIN(#Implementation, Implementation, Base)

#endif