2023-08-28 12:31:18 +02:00
|
|
|
/* Loadable / plugin support.
|
2018-08-21 00:25:44 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* Author: Daniel Krebs <github@daniel-krebs.net>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-02-29 19:34:27 +01:00
|
|
|
#include <fmt/ostream.h>
|
2020-06-11 14:19:39 +02:00
|
|
|
#include <iostream>
|
2018-08-21 00:25:44 +02:00
|
|
|
#include <jansson.h>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/common.hpp>
|
2023-09-25 10:52:55 +02:00
|
|
|
#include <villas/config.hpp>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/log.hpp>
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
namespace villas {
|
2018-08-23 13:22:46 +02:00
|
|
|
namespace plugin {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Forward declarations
|
2018-08-23 13:22:46 +02:00
|
|
|
class Plugin;
|
2022-02-25 09:52:09 -05:00
|
|
|
class Registry;
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
extern Registry *registry;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
template <typename T = Plugin> using List = std::list<T *>;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
class SubRegistry {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
virtual List<> lookup() = 0;
|
2022-02-25 09:52:09 -05:00
|
|
|
};
|
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
class Registry {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
List<> plugins;
|
|
|
|
List<SubRegistry> registries;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
public:
|
2024-07-29 12:35:42 +02:00
|
|
|
Logger getLogger() { return Log::get("plugin:registry"); }
|
2023-09-07 13:19:19 +02:00
|
|
|
|
|
|
|
void add(Plugin *p) { plugins.push_back(p); }
|
|
|
|
|
|
|
|
void addSubRegistry(SubRegistry *sr) { registries.push_back(sr); }
|
|
|
|
|
|
|
|
void remove(Plugin *p) { plugins.remove(p); }
|
|
|
|
|
|
|
|
// Get all plugins including sub-registries
|
|
|
|
List<> lookup() {
|
|
|
|
List<> all;
|
|
|
|
|
|
|
|
all.insert(all.end(), plugins.begin(), plugins.end());
|
|
|
|
|
|
|
|
for (auto r : registries) {
|
|
|
|
auto p = r->lookup();
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
all.insert(all.end(), p.begin(), p.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return all;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all plugins of specific type
|
|
|
|
template <typename T = Plugin> List<T> lookup() {
|
|
|
|
List<T> list;
|
|
|
|
|
|
|
|
for (Plugin *p : lookup()) {
|
|
|
|
T *t = dynamic_cast<T *>(p);
|
|
|
|
if (t)
|
|
|
|
list.push_back(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort alphabetically
|
|
|
|
list.sort(
|
|
|
|
[](const T *a, const T *b) { return a->getName() < b->getName(); });
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all plugins of specific type and name
|
|
|
|
template <typename T = Plugin> T *lookup(const std::string &name) {
|
|
|
|
for (T *p : lookup<T>()) {
|
|
|
|
if (p->getName() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T = Plugin> void dump();
|
2018-08-23 13:22:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Plugin {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
friend plugin::Registry;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2021-11-03 17:12:11 -04:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
Logger logger;
|
2021-11-03 17:12:11 -04:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
Plugin();
|
2021-11-03 17:12:11 -04:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
virtual ~Plugin();
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Copying a plugin doesn't make sense, so explicitly deny it
|
|
|
|
Plugin(Plugin const &) = delete;
|
|
|
|
void operator=(Plugin const &) = delete;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
virtual void dump();
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Get plugin name
|
|
|
|
virtual std::string getName() const = 0;
|
2020-06-11 18:08:08 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Get plugin type
|
|
|
|
virtual std::string getType() const = 0;
|
2021-06-21 16:07:46 -04:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Get plugin description
|
|
|
|
virtual std::string getDescription() const = 0;
|
2021-11-03 17:12:11 -04:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
virtual Logger getLogger() {
|
|
|
|
if (!logger) {
|
|
|
|
auto name = fmt::format("{}:{}", getType(), getName());
|
2024-07-29 12:35:42 +02:00
|
|
|
logger = Log::get(name);
|
2023-09-07 13:19:19 +02:00
|
|
|
}
|
2021-11-03 17:12:11 -04:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
return logger;
|
|
|
|
}
|
2023-01-10 15:15:20 +01:00
|
|
|
|
2023-09-25 10:52:55 +02:00
|
|
|
friend std::ostream &operator<<(std::ostream &os, const class Plugin &p) {
|
2023-09-07 13:19:19 +02:00
|
|
|
return os << p.getName();
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
};
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
template <typename T> void Registry::dump() {
|
|
|
|
getLogger()->info("Available plugins:");
|
|
|
|
|
|
|
|
for (Plugin *p : plugins) {
|
|
|
|
T *t = dynamic_cast<T *>(p);
|
|
|
|
if (t)
|
|
|
|
getLogger()->info(" - {}: {}", p->getName(), p->getDescription());
|
|
|
|
}
|
2020-06-11 14:19:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace plugin
|
|
|
|
} // namespace villas
|
2023-09-25 10:52:55 +02:00
|
|
|
|
|
|
|
#ifndef FMT_LEGACY_OSTREAM_FORMATTER
|
|
|
|
template <>
|
2024-02-29 19:34:27 +01:00
|
|
|
class fmt::formatter<villas::plugin::Plugin> : public fmt::ostream_formatter {};
|
2023-09-25 10:52:55 +02:00
|
|
|
#endif
|