Add UpdateProperty/ies()

This commit is contained in:
Alexey Rybalchenko 2019-06-06 16:18:10 +02:00 committed by Dennis Klein
parent b905f517cc
commit 7c9744760e
6 changed files with 76 additions and 26 deletions

View File

@ -38,10 +38,9 @@ auto DeviceRunner::Run() -> int
fEvents.Emit<hooks::SetCustomCmdLineOptions>(*this);
////////////////////////
fPluginManager.ForEachPluginProgOptions(
[&](boost::program_options::options_description options) {
fConfig.AddToCmdLineOptions(options);
});
fPluginManager.ForEachPluginProgOptions([&](boost::program_options::options_description options) {
fConfig.AddToCmdLineOptions(options);
});
fConfig.AddToCmdLineOptions(fPluginManager.ProgramOptions());
////// CALL HOOK ///////
@ -80,11 +79,11 @@ auto DeviceRunner::Run() -> int
if (fPrintLogo) {
LOG(info) << std::endl
<< " ______ _ _______ _________ " << std::endl
<< " / ____/___ _(_)_______ |/ /_ __ \\ version " << FAIRMQ_GIT_VERSION << std::endl
<< " / /_ / __ `/ / ___/__ /|_/ /_ / / / build " << FAIRMQ_BUILD_TYPE << std::endl
<< " / __/ / /_/ / / / _ / / / / /_/ / " << FAIRMQ_REPO_URL << std::endl
<< " /_/ \\__,_/_/_/ /_/ /_/ \\___\\_\\ " << FAIRMQ_LICENSE << " © " << FAIRMQ_COPYRIGHT << std::endl;
<< " ______ _ _______ _________ " << std::endl
<< " / ____/___ _(_)_______ |/ /_ __ \\ version " << FAIRMQ_GIT_VERSION << std::endl
<< " / /_ / __ `/ / ___/__ /|_/ /_ / / / build " << FAIRMQ_BUILD_TYPE << std::endl
<< " / __/ / /_/ / / / _ / / / / /_/ / " << FAIRMQ_REPO_URL << std::endl
<< " /_/ \\__,_/_/_/ /_/ /_/ \\___\\_\\ " << FAIRMQ_LICENSE << " © " << FAIRMQ_COPYRIGHT << std::endl;
}
fConfig.PrintOptions();

View File

@ -103,6 +103,9 @@ class Plugin
template<typename T>
auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
void SetProperties(const fair::mq::Properties& props) { fPluginServices->SetProperties(props); }
template<typename T>
bool UpdateProperty(const std::string& key, T val) { return fPluginServices->UpdateProperty(key, val); }
bool UpdateProperties(const fair::mq::Properties& input) { return fPluginServices->UpdateProperties(input); }
template<typename T>
auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }

View File

@ -206,10 +206,11 @@ class PluginServices
"Supported state is ", DeviceState::InitializingDevice, ".")};
}
}
void SetProperties(const fair::mq::Properties& props)
{
fConfig.SetProperties(props);
}
void SetProperties(const fair::mq::Properties& props) { fConfig.SetProperties(props); }
template<typename T>
bool UpdateProperty(const std::string& key, T val) { return fConfig.UpdateProperty(key, val); }
bool UpdateProperties(const fair::mq::Properties& input) { return fConfig.UpdateProperties(input); }
struct InvalidStateError : std::runtime_error { using std::runtime_error::runtime_error; };
/// @brief Read config property

View File

@ -21,9 +21,10 @@
#include <boost/regex.hpp>
#include <algorithm>
#include <exception>
#include <iomanip>
#include <iostream>
#include <exception>
#include <sstream>
#include <utility> // pair
using namespace std;
@ -111,6 +112,7 @@ void ProgOptions::ParseAll(const vector<string>& cmdArgs, bool allowUnregistered
void ProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUnregistered)
{
lock_guard<mutex> lock(fMtx);
// clear the container because it was filled with default values and subsequent calls to store() do not overwrite the existing values
fVarMap.clear();
@ -128,11 +130,13 @@ void ProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUn
void ProgOptions::Notify()
{
lock_guard<mutex> lock(fMtx);
po::notify(fVarMap);
}
void ProgOptions::AddToCmdLineOptions(const po::options_description optDesc, bool /* visible */)
{
lock_guard<mutex> lock(fMtx);
fAllOptions.add(optDesc);
}
@ -298,6 +302,32 @@ void ProgOptions::SetProperties(const Properties& input)
}
}
bool ProgOptions::UpdateProperties(const Properties& input)
{
unique_lock<mutex> lock(fMtx);
for (const auto& m : input) {
if (fVarMap.count(m.first) == 0) {
LOG(debug) << "UpdateProperties failed, no property found with key '" << m.first << "'";
return false;
}
}
map<string, boost::program_options::variable_value>& vm = fVarMap;
for (const auto& m : input) {
vm[m.first].value() = m.second;
}
lock.unlock();
for (const auto& m : input) {
PropertyHelper::fEventEmitters.at(m.second.type())(fEvents, m.first, m.second);
fEvents.Emit<PropertyChangeAsString, string>(m.first, PropertyHelper::ConvertPropertyToString(m.second));
}
return true;
}
void ProgOptions::DeleteProperty(const string& key)
{
lock_guard<mutex> lock(fMtx);

View File

@ -9,23 +9,21 @@
#ifndef FAIR_MQ_PROGOPTIONS_H
#define FAIR_MQ_PROGOPTIONS_H
#include <fairmq/EventManager.h>
#include "FairMQLogger.h"
#include "FairMQChannel.h"
#include "FairMQLogger.h"
#include <fairmq/EventManager.h>
#include <fairmq/ProgOptionsFwd.h>
#include <fairmq/Properties.h>
#include <fairmq/Tools.h>
#include <fairmq/ProgOptionsFwd.h>
#include <boost/program_options.hpp>
#include <unordered_map>
#include <map>
#include <functional>
#include <string>
#include <vector>
#include <map>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
namespace fair
{
@ -90,10 +88,30 @@ class ProgOptions
lock.unlock();
fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetStringValue(key));
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
}
template<typename T>
bool UpdateProperty(const std::string& key, T val)
{
std::unique_lock<std::mutex> lock(fMtx);
if (fVarMap.count(key)) {
SetVarMapValue<typename std::decay<T>::type>(key, val);
lock.unlock();
fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetPropertyAsString(key));
return true;
} else {
LOG(debug) << "UpdateProperty failed, no property found with key '" << key << "'";
return false;
}
}
void SetProperties(const fair::mq::Properties& input);
bool UpdateProperties(const fair::mq::Properties& input);
void DeleteProperty(const std::string& key);
void AddChannel(const std::string& name, const FairMQChannel& channel);

View File

@ -19,5 +19,4 @@ class ProgOptions;
using FairMQProgOptions = fair::mq::ProgOptions;
#endif
#endif /* FAIR_MQ_PROGOPTIONSFWD_H */