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

#include "plugins.h"

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

	template <typename T>
	PluginOf<T>::~PluginOf()
	{
		delete impl;
	}

	/// 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>
	const T *
	PluginOf<T>::implementation() const
	{
		return impl;
	}

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

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

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

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

	template <typename T>
	std::set<boost::shared_ptr<const PluginOf<T>>>
	PluginManager::getAll() const
	{
		std::set<boost::shared_ptr<const PluginOf<T>>> all;
		for(const auto & p : getAll(typeid(T))) {
			if (auto tp = boost::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 INSTANIATEPLUGINOF(T) \
	template class AdHoc::PluginOf<T>; \
	template void AdHoc::PluginManager::add<T>(const T *, const std::string &, const std::string &, int); \
	template void AdHoc::PluginManager::remove<T>(const std::string &); \
	template boost::shared_ptr<const AdHoc::PluginOf<T>> AdHoc::PluginManager::get<T>(const std::string &) const; \
	template const T * AdHoc::PluginManager::getImplementation<T>(const std::string &) const; \
	template std::set<boost::shared_ptr<const AdHoc::PluginOf<T>>> AdHoc::PluginManager::getAll<T>() const; \
	template void AdHoc::PluginManager::addResolver<T>(const AdHoc::PluginManager::PluginResolver & f); \
	template void AdHoc::PluginManager::removeResolver<T>(); \

#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