From 4ce378b6b88a5ac8646043dd033192c811be9fe6 Mon Sep 17 00:00:00 2001 From: Alexey Rybalchenko Date: Thu, 16 May 2019 14:27:14 +0200 Subject: [PATCH] Add DeleteProperty --- fairmq/options/FairMQProgOptions.cxx | 45 ++++++++++------------------ fairmq/options/FairMQProgOptions.h | 39 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/fairmq/options/FairMQProgOptions.cxx b/fairmq/options/FairMQProgOptions.cxx index 310a4fc6..e50675d5 100644 --- a/fairmq/options/FairMQProgOptions.cxx +++ b/fairmq/options/FairMQProgOptions.cxx @@ -119,7 +119,7 @@ ValInfo ConvertVarValToValInfo(const po::variable_value& v) return {info.first, info.second, origin}; } catch (out_of_range& oor) { - return {string("[unidentified]"), string("[unidentified]"), origin}; + return {string("[unidentified_type]"), string("[unidentified_type]"), origin}; } }; @@ -179,21 +179,6 @@ FairMQProgOptions::FairMQProgOptions() ParseDefaults(); } -FairMQProgOptions::~FairMQProgOptions() -{ -} - -int FairMQProgOptions::ParseAll(const vector& cmdLineArgs, bool allowUnregistered) -{ - vector argv(cmdLineArgs.size()); - - transform(cmdLineArgs.begin(), cmdLineArgs.end(), argv.begin(), [](const string& str) { - return str.c_str(); - }); - - return ParseAll(argv.size(), const_cast(argv.data()), allowUnregistered); -} - int FairMQProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUnregistered) { ParseCmdLine(argc, argv, allowUnregistered); @@ -372,24 +357,24 @@ void FairMQProgOptions::UpdateMQValues() fChannelKeyMap[portRangeMaxKey] = ChannelKey{p.first, index, "portRangeMax"}; fChannelKeyMap[autoBindKey] = ChannelKey{p.first, index, "autoBind"}; - UpdateVarMap(typeKey, channel.GetType()); - UpdateVarMap(methodKey, channel.GetMethod()); - UpdateVarMap(addressKey, channel.GetAddress()); - UpdateVarMap(transportKey, channel.GetTransportName()); - UpdateVarMap(sndBufSizeKey, channel.GetSndBufSize()); - UpdateVarMap(rcvBufSizeKey, channel.GetRcvBufSize()); - UpdateVarMap(sndKernelSizeKey, channel.GetSndKernelSize()); - UpdateVarMap(rcvKernelSizeKey, channel.GetRcvKernelSize()); - UpdateVarMap(lingerKey, channel.GetLinger()); - UpdateVarMap(rateLoggingKey, channel.GetRateLogging()); - UpdateVarMap(portRangeMinKey, channel.GetPortRangeMin()); - UpdateVarMap(portRangeMaxKey, channel.GetPortRangeMax()); - UpdateVarMap(autoBindKey, channel.GetAutoBind()); + SetVarMapValue(typeKey, channel.GetType()); + SetVarMapValue(methodKey, channel.GetMethod()); + SetVarMapValue(addressKey, channel.GetAddress()); + SetVarMapValue(transportKey, channel.GetTransportName()); + SetVarMapValue(sndBufSizeKey, channel.GetSndBufSize()); + SetVarMapValue(rcvBufSizeKey, channel.GetRcvBufSize()); + SetVarMapValue(sndKernelSizeKey, channel.GetSndKernelSize()); + SetVarMapValue(rcvKernelSizeKey, channel.GetRcvKernelSize()); + SetVarMapValue(lingerKey, channel.GetLinger()); + SetVarMapValue(rateLoggingKey, channel.GetRateLogging()); + SetVarMapValue(portRangeMinKey, channel.GetPortRangeMin()); + SetVarMapValue(portRangeMaxKey, channel.GetPortRangeMax()); + SetVarMapValue(autoBindKey, channel.GetAutoBind()); index++; } - UpdateVarMap("chans." + p.first + ".numSockets", index); + SetVarMapValue("chans." + p.first + ".numSockets", index); } } diff --git a/fairmq/options/FairMQProgOptions.h b/fairmq/options/FairMQProgOptions.h index 709d0939..26d83215 100644 --- a/fairmq/options/FairMQProgOptions.h +++ b/fairmq/options/FairMQProgOptions.h @@ -52,13 +52,19 @@ class FairMQProgOptions public: FairMQProgOptions(); - virtual ~FairMQProgOptions(); + virtual ~FairMQProgOptions() {} struct PropertyNotFoundException : std::runtime_error { using std::runtime_error::runtime_error; }; - int ParseAll(const std::vector& cmdLineArgs, bool allowUnregistered); - // parse command line. - // default parser for the mq-configuration file (JSON) is called if command line key mq-config is called + int ParseAll(const std::vector& cmdArgs, bool allowUnregistered) + { + std::vector argv(cmdArgs.size()); + transform(cmdArgs.begin(), cmdArgs.end(), argv.begin(), [](const std::string& str) { + return str.c_str(); + }); + return ParseAll(argv.size(), const_cast(argv.data()), allowUnregistered); + } + int ParseAll(const int argc, char const* const* argv, bool allowUnregistered = true); FairMQChannelMap GetFairMQMap() const; @@ -100,6 +106,8 @@ class FairMQProgOptions std::regex re(q); std::map result; + std::lock_guard lock(fMtx); + for (const auto& m : fVarMap) { if (std::regex_search(m.first, re)) { result.emplace(m.first, m.second.value()); @@ -109,7 +117,6 @@ class FairMQProgOptions return result; } - // Given a key, convert the variable value to string std::string GetStringValue(const std::string& key); template @@ -117,8 +124,7 @@ class FairMQProgOptions { std::unique_lock lock(fMtx); - // update variable map - UpdateVarMap::type>(key, val); + SetVarMapValue::type>(key, val); if (key == "channel-config") { ParseChannelsFromCmdLine(); @@ -128,7 +134,6 @@ class FairMQProgOptions lock.unlock(); - //if (std::is_same::value || std::is_same::value)//if one wants to restrict type fEvents.Emit::type>(key, val); fEvents.Emit(key, GetStringValue(key)); } @@ -142,20 +147,30 @@ class FairMQProgOptions void SetProperties(const std::map& input) { + std::lock_guard lock(fMtx); + std::map& vm = fVarMap; for (const auto& m : input) { vm[m.first].value() = m.second; } + + // TODO: call subscriptions here (after unlock) + } + + void DeleteProperty(const std::string& key) + { + std::lock_guard lock(fMtx); + + std::map& vm = fVarMap; + vm.erase(key); } template void Subscribe(const std::string& subscriber, std::function func) { std::lock_guard lock(fMtx); - static_assert(!std::is_same::value || !std::is_same::value, "In template member FairMQProgOptions::Subscribe(key,Lambda) the types const char* or char* for the calback signatures are not supported."); - fEvents.Subscribe(subscriber, func); } @@ -239,9 +254,9 @@ class FairMQProgOptions void UpdateChannelInfo(); - // helper to modify the value of variable map after calling boost::program_options::store + // modify the value of variable map after calling boost::program_options::store template - void UpdateVarMap(const std::string& key, const T& val) + void SetVarMapValue(const std::string& key, const T& val) { std::map& vm = fVarMap; vm[key].value() = boost::any(val);