Source · fairmq/
Plugin.hpublic header
Defines class fair::mq::Plugin.
Described in Plugins and control, custom-controller: control from code, Write a plugin, Overview.
1/********************************************************************************2 * Copyright (C) 2017-2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *3 * *4 * This software is distributed under the terms of the *5 * GNU Lesser General Public Licence (LGPL) version 3, *6 * copied verbatim in the file "LICENSE" *7 ********************************************************************************/8 9#ifndef FAIR_MQ_PLUGIN_H10#define FAIR_MQ_PLUGIN_H11 12#include <fairmq/tools/Version.h>13#include <fairmq/PluginServices.h>14 15#include <boost/dll/alias.hpp>16#include <boost/optional.hpp>17#include <boost/program_options.hpp>18 19#include <functional>20#include <unordered_map>21#include <ostream>22#include <memory>23#include <string>24#include <tuple>25#include <utility>26 27namespace fair::mq28{29 30/**31 * @class Plugin Plugin.h <fairmq/Plugin.h>32 * @brief Base class for FairMQ plugins33 *34 * The plugin base class encapsulates the plugin metadata.35 */36class Plugin37{38 public:39 using ProgOptions = boost::optional<boost::program_options::options_description>;40 41 using Version = tools::Version;42 43 Plugin() = delete;44 Plugin(std::string name,45 Version version,46 std::string maintainer,47 std::string homepage,48 PluginServices* pluginServices);49 50 Plugin(const Plugin&) = delete;51 Plugin(Plugin&&) = delete;52 Plugin& operator=(const Plugin&) = delete;53 Plugin& operator=(Plugin&&) = delete;54 55 virtual ~Plugin();56 57 auto GetName() const -> const std::string& { return fkName; }58 auto GetVersion() const -> const Version { return fkVersion; }59 auto GetMaintainer() const -> const std::string& { return fkMaintainer; }60 auto GetHomepage() const -> const std::string& { return fkHomepage; }61 62 friend auto operator==(const Plugin& lhs, const Plugin& rhs) -> bool { return std::make_tuple(lhs.GetName(), lhs.GetVersion()) == std::make_tuple(rhs.GetName(), rhs.GetVersion()); }63 friend auto operator!=(const Plugin& lhs, const Plugin& rhs) -> bool { return !(lhs == rhs); }64 friend auto operator<<(std::ostream& os, const Plugin& p) -> std::ostream&65 {66 return os << "'" << p.GetName() << "', "67 << "version '" << p.GetVersion() << "', "68 << "maintainer '" << p.GetMaintainer() << "', "69 << "homepage '" << p.GetHomepage() << "'";70 }71 static auto NoProgramOptions() -> ProgOptions { return boost::none; }72 73 // device control API74 // see <fairmq/PluginServices.h> for docs75 using DeviceState = fair::mq::PluginServices::DeviceState;76 using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;77 auto ToDeviceState(const std::string& state) const -> DeviceState { return PluginServices::ToDeviceState(state); }78 auto ToDeviceStateTransition(const std::string& transition) const -> DeviceStateTransition { return PluginServices::ToDeviceStateTransition(transition); }79 auto ToStr(DeviceState state) const -> std::string { return PluginServices::ToStr(state); }80 auto ToStr(DeviceStateTransition transition) const -> std::string { return PluginServices::ToStr(transition); }81 auto GetCurrentDeviceState() const -> DeviceState { return fPluginServices->GetCurrentDeviceState(); }82 auto TakeDeviceControl() -> void { fPluginServices->TakeDeviceControl(fkName); };83 auto StealDeviceControl() -> void { fPluginServices->StealDeviceControl(fkName); };84 auto ReleaseDeviceControl() -> void85 {86 if (fPluginServices->GetDeviceController() == fkName) {87 fPluginServices->ReleaseDeviceControl(fkName);88 }89 };90 auto ChangeDeviceState(const DeviceStateTransition next) -> bool { return fPluginServices->ChangeDeviceState(fkName, next); }91 auto SubscribeToDeviceStateChange(std::function<void(DeviceState)> callback) -> void { fPluginServices->SubscribeToDeviceStateChange(fkName, callback); }92 auto UnsubscribeFromDeviceStateChange() -> void { fPluginServices->UnsubscribeFromDeviceStateChange(fkName); }93 94 auto GetNumberOfConnectedPeers(const std::string& channelName, int index = 0) -> unsigned long { return fPluginServices->GetNumberOfConnectedPeers(channelName, index); }95 96 // device config API97 // see <fairmq/PluginServices.h> for docs98 auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }99 100 template<typename T>101 T GetProperty(const std::string& key) const { return fPluginServices->GetProperty<T>(key); }102 template<typename T>103 T GetProperty(const std::string& key, const T& ifNotFound) const { return fPluginServices->GetProperty(key, ifNotFound); }104 std::string GetPropertyAsString(const std::string& key) const { return fPluginServices->GetPropertyAsString(key); }105 std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const { return fPluginServices->GetPropertyAsString(key, ifNotFound); }106 fair::mq::Properties GetProperties(const std::string& q) const { return fPluginServices->GetProperties(q); }107 fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesStartingWith(q); };108 std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fPluginServices->GetPropertiesAsString(q); }109 std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesAsStringStartingWith(q); };110 111 auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fPluginServices->GetChannelInfo(); }112 auto GetPropertyKeys() const -> std::vector<std::string> { return fPluginServices->GetPropertyKeys(); }113 114 template<typename T>115 auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }116 void SetProperties(const fair::mq::Properties& props) { fPluginServices->SetProperties(props); }117 template<typename T>118 bool UpdateProperty(const std::string& key, T val) { return fPluginServices->UpdateProperty(key, val); }119 bool UpdateProperties(const fair::mq::Properties& input) { return fPluginServices->UpdateProperties(input); }120 121 void DeleteProperty(const std::string& key) { fPluginServices->DeleteProperty(key); }122 123 template<typename T>124 auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }125 template<typename T>126 auto UnsubscribeFromPropertyChange() -> void { fPluginServices->UnsubscribeFromPropertyChange<T>(fkName); }127 auto SubscribeToPropertyChangeAsString(std::function<void(const std::string& key, std::string newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChangeAsString(fkName, callback); }128 auto UnsubscribeFromPropertyChangeAsString() -> void { fPluginServices->UnsubscribeFromPropertyChangeAsString(fkName); }129 130 auto CycleLogConsoleSeverityUp() -> void { fPluginServices->CycleLogConsoleSeverityUp(); }131 auto CycleLogConsoleSeverityDown() -> void { fPluginServices->CycleLogConsoleSeverityDown(); }132 auto CycleLogVerbosityUp() -> void { fPluginServices->CycleLogVerbosityUp(); }133 auto CycleLogVerbosityDown() -> void { fPluginServices->CycleLogVerbosityDown(); }134 135 private:136 const std::string fkName;137 const Version fkVersion;138 const std::string fkMaintainer;139 const std::string fkHomepage;140 PluginServices* fPluginServices;141}; /* class Plugin */142 143} // namespace fair::mq144 145#define REGISTER_FAIRMQ_PLUGIN(KLASS, NAME, VERSION, MAINTAINER, HOMEPAGE, PROGOPTIONS) \146static auto Make_##NAME##_Plugin(fair::mq::PluginServices* pluginServices) -> std::unique_ptr<fair::mq::Plugin> \147{ \148 return std::make_unique<KLASS>(std::string{#NAME}, VERSION, std::string{MAINTAINER}, std::string{HOMEPAGE}, pluginServices); \149} \150BOOST_DLL_ALIAS(Make_##NAME##_Plugin, make_##NAME##_plugin) \151BOOST_DLL_ALIAS(PROGOPTIONS, get_##NAME##_plugin_progoptions)152 153#endif /* FAIR_MQ_PLUGIN_H */