From 7c9744760e74dca41877c7521feb0b6dbd005d1e Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 6 Jun 2019 16:18:10 +0200 Subject: [PATCH] Add UpdateProperty/ies() --- fairmq/DeviceRunner.cxx | 17 ++++++++--------- fairmq/Plugin.h | 3 +++ fairmq/PluginServices.h | 9 +++++---- fairmq/ProgOptions.cxx | 32 +++++++++++++++++++++++++++++++- fairmq/ProgOptions.h | 38 ++++++++++++++++++++++++++++---------- fairmq/ProgOptionsFwd.h | 3 +-- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/fairmq/DeviceRunner.cxx b/fairmq/DeviceRunner.cxx index 72a49dd9..dcdaedde 100644 --- a/fairmq/DeviceRunner.cxx +++ b/fairmq/DeviceRunner.cxx @@ -38,10 +38,9 @@ auto DeviceRunner::Run() -> int fEvents.Emit(*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(); diff --git a/fairmq/Plugin.h b/fairmq/Plugin.h index c3f12d0c..a9b08095 100644 --- a/fairmq/Plugin.h +++ b/fairmq/Plugin.h @@ -103,6 +103,9 @@ class Plugin template auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); } void SetProperties(const fair::mq::Properties& props) { fPluginServices->SetProperties(props); } + template + 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 auto SubscribeToPropertyChange(std::function callback) -> void { fPluginServices->SubscribeToPropertyChange(fkName, callback); } diff --git a/fairmq/PluginServices.h b/fairmq/PluginServices.h index 42041899..e745240d 100644 --- a/fairmq/PluginServices.h +++ b/fairmq/PluginServices.h @@ -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 + 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 diff --git a/fairmq/ProgOptions.cxx b/fairmq/ProgOptions.cxx index e6dea788..0355a72f 100644 --- a/fairmq/ProgOptions.cxx +++ b/fairmq/ProgOptions.cxx @@ -21,9 +21,10 @@ #include #include +#include #include #include -#include +#include #include // pair using namespace std; @@ -111,6 +112,7 @@ void ProgOptions::ParseAll(const vector& cmdArgs, bool allowUnregistered void ProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUnregistered) { + lock_guard 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 lock(fMtx); po::notify(fVarMap); } void ProgOptions::AddToCmdLineOptions(const po::options_description optDesc, bool /* visible */) { + lock_guard lock(fMtx); fAllOptions.add(optDesc); } @@ -298,6 +302,32 @@ void ProgOptions::SetProperties(const Properties& input) } } +bool ProgOptions::UpdateProperties(const Properties& input) +{ + unique_lock 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& 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(m.first, PropertyHelper::ConvertPropertyToString(m.second)); + } + + return true; +} + void ProgOptions::DeleteProperty(const string& key) { lock_guard lock(fMtx); diff --git a/fairmq/ProgOptions.h b/fairmq/ProgOptions.h index 91e9d3fe..beba33bf 100644 --- a/fairmq/ProgOptions.h +++ b/fairmq/ProgOptions.h @@ -9,23 +9,21 @@ #ifndef FAIR_MQ_PROGOPTIONS_H #define FAIR_MQ_PROGOPTIONS_H -#include -#include "FairMQLogger.h" #include "FairMQChannel.h" +#include "FairMQLogger.h" +#include +#include #include #include -#include #include -#include -#include #include -#include -#include +#include #include -#include -#include +#include +#include +#include namespace fair { @@ -90,10 +88,30 @@ class ProgOptions lock.unlock(); fEvents.Emit::type>(key, val); - fEvents.Emit(key, GetStringValue(key)); + fEvents.Emit(key, GetPropertyAsString(key)); + } + + template + bool UpdateProperty(const std::string& key, T val) + { + std::unique_lock lock(fMtx); + + if (fVarMap.count(key)) { + SetVarMapValue::type>(key, val); + + lock.unlock(); + + fEvents.Emit::type>(key, val); + fEvents.Emit(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); diff --git a/fairmq/ProgOptionsFwd.h b/fairmq/ProgOptionsFwd.h index 9e75d2fb..0d74cb4d 100644 --- a/fairmq/ProgOptionsFwd.h +++ b/fairmq/ProgOptionsFwd.h @@ -19,5 +19,4 @@ class ProgOptions; using FairMQProgOptions = fair::mq::ProgOptions; -#endif - +#endif /* FAIR_MQ_PROGOPTIONSFWD_H */