plugin.hpp 3.24 KB
Newer Older
Steffen Vogel's avatar
Steffen Vogel committed
1
2
3
4
5
/** Loadable / plugin support.
 *
 * @file
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @author Daniel Krebs <github@daniel-krebs.net>
Steffen Vogel's avatar
Steffen Vogel committed
6
 * @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
Steffen Vogel's avatar
Steffen Vogel committed
7
8
 * @license GNU General Public License (version 3)
 *
9
 * VILLAScommon
Steffen Vogel's avatar
Steffen Vogel committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#pragma once

27
#include <iostream>
Steffen Vogel's avatar
Steffen Vogel committed
28
29
30
#include <list>
#include <string>
#include <jansson.h>
31
#include <spdlog/fmt/ostr.h>
Steffen Vogel's avatar
Steffen Vogel committed
32
33

#include <villas/log.hpp>
Steffen Vogel's avatar
Steffen Vogel committed
34
#include <villas/common.hpp>
Steffen Vogel's avatar
Steffen Vogel committed
35
36

namespace villas {
Steffen Vogel's avatar
Steffen Vogel committed
37
namespace plugin {
Steffen Vogel's avatar
Steffen Vogel committed
38

Steffen Vogel's avatar
Steffen Vogel committed
39
40
/* Forward declarations */
class Plugin;
Steffen Vogel's avatar
Steffen Vogel committed
41

Steffen Vogel's avatar
Steffen Vogel committed
42
43
template<typename T = Plugin>
using List = std::list<T *>;
Steffen Vogel's avatar
Steffen Vogel committed
44

Steffen Vogel's avatar
Steffen Vogel committed
45
class Registry {
Steffen Vogel's avatar
Steffen Vogel committed
46

Steffen Vogel's avatar
Steffen Vogel committed
47
48
protected:
	static List<> *plugins;
Steffen Vogel's avatar
Steffen Vogel committed
49

Steffen Vogel's avatar
Steffen Vogel committed
50
51
public:

52
	static Logger
Steffen Vogel's avatar
Steffen Vogel committed
53
	getLogger()
54
55
56
	{
		return logging.get("plugin:registry");
	}
Steffen Vogel's avatar
Steffen Vogel committed
57

58
59
	static void
	add(Plugin *p)
Steffen Vogel's avatar
Steffen Vogel committed
60
61
62
63
64
65
66
	{
		if (plugins == nullptr)
			plugins = new List<>;

		plugins->push_back(p);
	}

67
68
	static void
	remove(Plugin *p)
Steffen Vogel's avatar
Steffen Vogel committed
69
70
71
72
73
74
75
76
77
78
	{
		plugins->remove(p);
	}

	template<typename T = Plugin>
	static T *
	lookup(const std::string &name)
	{
		for (Plugin *p : *plugins) {
			T *t = dynamic_cast<T *>(p);
79
			if (!t || t->getName() != name)
Steffen Vogel's avatar
Steffen Vogel committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
				continue;

			return t;
		}

		return nullptr;
	}

	template<typename T = Plugin>
	static List<T>
	lookup()
	{
		List<T> list;

		for (Plugin *p : *plugins) {
			T *t = dynamic_cast<T *>(p);
			if (t)
				list.push_back(t);
		}

		return list;
	}
102
103
104
105

	template<typename T = Plugin>
	static void
	dumpList();
Steffen Vogel's avatar
Steffen Vogel committed
106
107
108
109
110
111
};

class Loader {

public:
	int load(const std::string &path);
Steffen Vogel's avatar
Steffen Vogel committed
112
113
	int unload();

Steffen Vogel's avatar
Steffen Vogel committed
114
	virtual int parse(json_t *json);
Steffen Vogel's avatar
Steffen Vogel committed
115
};
Steffen Vogel's avatar
Steffen Vogel committed
116

Steffen Vogel's avatar
Steffen Vogel committed
117
class Plugin {
Steffen Vogel's avatar
Steffen Vogel committed
118

Steffen Vogel's avatar
Steffen Vogel committed
119
	friend plugin::Registry;
Steffen Vogel's avatar
Steffen Vogel committed
120

Steffen Vogel's avatar
Steffen Vogel committed
121
public:
122
	Plugin();
Steffen Vogel's avatar
Steffen Vogel committed
123
	virtual ~Plugin();
Steffen Vogel's avatar
Steffen Vogel committed
124

Steffen Vogel's avatar
Steffen Vogel committed
125
126
127
	// copying a plugin doesn't make sense, so explicitly deny it
	Plugin(Plugin const&) = delete;
	void operator=(Plugin const&) = delete;
Steffen Vogel's avatar
Steffen Vogel committed
128

Steffen Vogel's avatar
Steffen Vogel committed
129
130
	virtual void dump();

131
132
133
134
	/// Get plugin name
	virtual std::string
	getName() const = 0;

135
136
137
138
	/// Get plugin type
	virtual std::string
	getType() const = 0;

139
140
141
	// Get plugin description
	virtual std::string
	getDescription() const = 0;
Steffen Vogel's avatar
Steffen Vogel committed
142

143
144
145
146
147
148
149
	/** Custom formatter for spdlog */
	template<typename OStream>
	friend OStream &operator<<(OStream &os, const class Plugin &p)
	{
		return os << p.getName();
	}

Steffen Vogel's avatar
Steffen Vogel committed
150
protected:
Steffen Vogel's avatar
Steffen Vogel committed
151
152
	std::string path;

153
	virtual
154
	Logger
Steffen Vogel's avatar
Steffen Vogel committed
155
	getLogger()
156
	{
157
158
		auto name = fmt::format("{}:{}", getType(), getName());
		return logging.get(name);
159
	}
Steffen Vogel's avatar
Steffen Vogel committed
160
161
};

162
163
164
165
template<typename T = Plugin>
void
Registry::dumpList()
{
166
167
	getLogger()->info("Available plugins:");

168
169
170
	for (Plugin *p : *plugins) {
		T *t = dynamic_cast<T *>(p);
		if (t)
171
			getLogger()->info(" - {}: {}", *p, p->getDescription());
172
173
174
	}
}

175
176
} /* namespace plugin */
} /* namespace villas */