mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-16 00:00:02 +01:00
178 lines
3.3 KiB
C++
178 lines
3.3 KiB
C++
/** Loadable / plugin support.
|
|
*
|
|
* @file
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
* @author Daniel Krebs <github@daniel-krebs.net>
|
|
* @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
|
|
* @license GNU General Public License (version 3)
|
|
*
|
|
* VILLAScommon
|
|
*
|
|
* 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
|
|
|
|
#include <iostream>
|
|
#include <list>
|
|
#include <string>
|
|
#include <jansson.h>
|
|
#include <spdlog/fmt/ostr.h>
|
|
|
|
#include <villas/log.hpp>
|
|
#include <villas/common.hpp>
|
|
|
|
namespace villas {
|
|
namespace plugin {
|
|
|
|
/* Forward declarations */
|
|
class Plugin;
|
|
|
|
template<typename T = Plugin>
|
|
using List = std::list<T *>;
|
|
|
|
class Registry {
|
|
|
|
protected:
|
|
static List<> *plugins;
|
|
|
|
public:
|
|
|
|
static Logger
|
|
getLogger()
|
|
{
|
|
return logging.get("plugin:registry");
|
|
}
|
|
|
|
static void
|
|
add(Plugin *p)
|
|
{
|
|
if (plugins == nullptr)
|
|
plugins = new List<>;
|
|
|
|
plugins->push_back(p);
|
|
}
|
|
|
|
static void
|
|
remove(Plugin *p)
|
|
{
|
|
plugins->remove(p);
|
|
}
|
|
|
|
template<typename T = Plugin>
|
|
static T *
|
|
lookup(const std::string &name)
|
|
{
|
|
for (Plugin *p : *plugins) {
|
|
T *t = dynamic_cast<T *>(p);
|
|
if (!t || t->getName() != name)
|
|
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);
|
|
}
|
|
|
|
/* Sort alphabetically */
|
|
list.sort([](const T *a, const T *b) {
|
|
return a->getName() < b->getName();
|
|
});
|
|
|
|
return list;
|
|
}
|
|
|
|
template<typename T = Plugin>
|
|
static void
|
|
dumpList();
|
|
};
|
|
|
|
class Plugin {
|
|
|
|
friend plugin::Registry;
|
|
|
|
protected:
|
|
Logger logger;
|
|
|
|
public:
|
|
Plugin();
|
|
|
|
virtual
|
|
~Plugin();
|
|
|
|
// copying a plugin doesn't make sense, so explicitly deny it
|
|
Plugin(Plugin const&) = delete;
|
|
void operator=(Plugin const&) = delete;
|
|
|
|
virtual
|
|
void dump();
|
|
|
|
/// Get plugin name
|
|
virtual
|
|
std::string getName() const = 0;
|
|
|
|
/// Get plugin type
|
|
virtual
|
|
std::string getType() const = 0;
|
|
|
|
// Get plugin description
|
|
virtual
|
|
std::string getDescription() const = 0;
|
|
|
|
virtual
|
|
Logger getLogger()
|
|
{
|
|
if (!logger) {
|
|
auto name = fmt::format("{}:{}", getType(), getName());
|
|
logger = logging.get(name);
|
|
}
|
|
|
|
return logger;
|
|
}
|
|
|
|
/** Custom formatter for spdlog */
|
|
template<typename OStream>
|
|
friend OStream &operator<<(OStream &os, const class Plugin &p)
|
|
{
|
|
return os << p.getName();
|
|
}
|
|
};
|
|
|
|
template<typename T = Plugin>
|
|
void
|
|
Registry::dumpList()
|
|
{
|
|
getLogger()->info("Available plugins:");
|
|
|
|
for (Plugin *p : *plugins) {
|
|
T *t = dynamic_cast<T *>(p);
|
|
if (t)
|
|
getLogger()->info(" - {}: {}", *p, p->getDescription());
|
|
}
|
|
}
|
|
|
|
} /* namespace plugin */
|
|
} /* namespace villas */
|