summaryrefslogtreecommitdiff
path: root/libadhocutil/plugins.impl.h
blob: 6f8da057c6f40de5c3dc9d3488ad83402850f075 (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
#ifndef ADHOCUTIL_PLUGINS_IMPL_H
#define ADHOCUTIL_PLUGINS_IMPL_H

#include "plugins.h"

namespace AdHoc {
	template <typename T>
	PluginOf<T>::PluginOf(const std::shared_ptr<T> & t, const std::string & n, const std::string & f, int l) :
		Plugin(n, f, l),
		impl(t)
	{
	}

	/// Get the type of this plugin.
	template <typename T>
	const std::type_info &
	PluginOf<T>::type() const
	{
		return typeid(T);
	}

	/// Get the implementation of this plugin.
	template <typename T>
	std::shared_ptr<T>
	PluginOf<T>::implementation() const
	{
		return impl;
	}

	template <typename T>
	std::shared_ptr<AbstractPluginImplementation>
	PluginOf<T>::instance() const
	{
		return impl;
	}

	template <typename T>
	void
	PluginManager::add(const std::shared_ptr<T> & i, const std::string & n, const std::string & f, int l)
	{
		add(std::make_shared<PluginOf<T>>(i, n, f, l));
	}

	template <typename T>
	void
	PluginManager::remove(const std::string & n)
	{
		remove(n, typeid(T));
	}

	template <typename T>
	std::shared_ptr<const PluginOf<T>>
	PluginManager::get(const std::string & n) const
	{
		return std::dynamic_pointer_cast<const PluginOf<T>>(get(n, typeid(T)));
	}

	template <typename T>
	std::shared_ptr<T>
	PluginManager::getImplementation(const std::string & n) const
	{
		return std::static_pointer_cast<T>(get<T>(n)->implementation());
	}

	template <typename T>
	std::set<std::shared_ptr<const PluginOf<T>>>
	PluginManager::getAll() const
	{
		std::set<std::shared_ptr<const PluginOf<T>>> all;
		for(const auto & p : getAll(typeid(T))) {
			if (auto tp = std::dynamic_pointer_cast<const PluginOf<T>>(p)) {
				all.insert(tp);
			}
		}
		return all;
	}

	template<typename T>
	void
	PluginManager::addResolver(const PluginResolver & f)
	{
		addResolver(typeid(T), f);
	}

	template<typename T>
	void
	PluginManager::removeResolver()
	{
		removeResolver(typeid(T));
	}
}

#define INSTANTIATEPLUGINOF(...) \
	template class AdHoc::PluginOf<__VA_ARGS__>; \
	template void AdHoc::PluginManager::add<__VA_ARGS__>(const std::shared_ptr<__VA_ARGS__> &, const std::string &, const std::string &, int); \
	template void AdHoc::PluginManager::remove<__VA_ARGS__>(const std::string &); \
	template std::shared_ptr<const AdHoc::PluginOf<__VA_ARGS__>> AdHoc::PluginManager::get<__VA_ARGS__>(const std::string &) const; \
	template std::shared_ptr<__VA_ARGS__> AdHoc::PluginManager::getImplementation<__VA_ARGS__>(const std::string &) const; \
	template std::set<std::shared_ptr<const AdHoc::PluginOf<__VA_ARGS__>>> AdHoc::PluginManager::getAll<__VA_ARGS__>() const; \
	template void AdHoc::PluginManager::addResolver<__VA_ARGS__>(const AdHoc::PluginManager::PluginResolver & f); \
	template void AdHoc::PluginManager::removeResolver<__VA_ARGS__>(); \

#define PLUGINRESOLVER(T, F) \
	namespace MAKE_UNIQUE(__plugin__) { \
		static void InstallResolver() __attribute__((constructor(102))); \
		void InstallResolver() { \
			::AdHoc::PluginManager::getDefault()->addResolver<T>(F); \
		} \
		static void UninstallResolver() __attribute__((destructor(102))); \
		void UninstallResolver() { \
			::AdHoc::PluginManager::getDefault()->removeResolver<T>(); \
		} \
	}

#endif