1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/common/include/villas/plugin.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
3.1 KiB
C++
Raw Permalink Normal View History

/* Loadable / plugin support.
2018-08-21 00:25:44 +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
*/
2018-08-21 00:25:44 +02:00
#pragma once
#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>
#include <list>
#include <string>
2019-10-27 20:23:47 +01:00
#include <villas/common.hpp>
#include <villas/config.hpp>
#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
// Forward declarations
2018-08-23 13:22:46 +02:00
class Plugin;
class Registry;
extern Registry *registry;
2018-08-21 00:25:44 +02:00
template <typename T = Plugin> using List = std::list<T *>;
2018-08-21 00:25:44 +02:00
class SubRegistry {
public:
virtual List<> lookup() = 0;
};
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:
List<> plugins;
List<SubRegistry> registries;
2018-08-21 00:25:44 +02:00
2018-08-23 13:22:46 +02:00
public:
Logger getLogger() { return Log::get("plugin:registry"); }
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
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
friend plugin::Registry;
2018-08-21 00:25:44 +02:00
2021-11-03 17:12:11 -04:00
protected:
Logger logger;
2021-11-03 17:12:11 -04:00
2018-08-23 13:22:46 +02:00
public:
Plugin();
2021-11-03 17:12:11 -04:00
virtual ~Plugin();
2018-08-21 00:25:44 +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
virtual void dump();
2018-08-23 13:22:46 +02:00
// Get plugin name
virtual std::string getName() const = 0;
// Get plugin type
virtual std::string getType() const = 0;
2021-06-21 16:07:46 -04:00
// Get plugin description
virtual std::string getDescription() const = 0;
2021-11-03 17:12:11 -04:00
virtual Logger getLogger() {
if (!logger) {
auto name = fmt::format("{}:{}", getType(), getName());
logger = Log::get(name);
}
2021-11-03 17:12:11 -04:00
return logger;
}
friend std::ostream &operator<<(std::ostream &os, const class Plugin &p) {
return os << p.getName();
}
2018-08-21 00:25:44 +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
}
} // namespace plugin
} // namespace villas
#ifndef FMT_LEGACY_OSTREAM_FORMATTER
template <>
class fmt::formatter<villas::plugin::Plugin> : public fmt::ostream_formatter {};
#endif