Add DeleteProperty

This commit is contained in:
Alexey Rybalchenko 2019-05-16 14:27:14 +02:00 committed by Dennis Klein
parent 29313bbec3
commit 4ce378b6b8
2 changed files with 42 additions and 42 deletions

View File

@ -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<string>& cmdLineArgs, bool allowUnregistered)
{
vector<const char*> argv(cmdLineArgs.size());
transform(cmdLineArgs.begin(), cmdLineArgs.end(), argv.begin(), [](const string& str) {
return str.c_str();
});
return ParseAll(argv.size(), const_cast<char**>(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<string>(typeKey, channel.GetType());
UpdateVarMap<string>(methodKey, channel.GetMethod());
UpdateVarMap<string>(addressKey, channel.GetAddress());
UpdateVarMap<string>(transportKey, channel.GetTransportName());
UpdateVarMap<int>(sndBufSizeKey, channel.GetSndBufSize());
UpdateVarMap<int>(rcvBufSizeKey, channel.GetRcvBufSize());
UpdateVarMap<int>(sndKernelSizeKey, channel.GetSndKernelSize());
UpdateVarMap<int>(rcvKernelSizeKey, channel.GetRcvKernelSize());
UpdateVarMap<int>(lingerKey, channel.GetLinger());
UpdateVarMap<int>(rateLoggingKey, channel.GetRateLogging());
UpdateVarMap<int>(portRangeMinKey, channel.GetPortRangeMin());
UpdateVarMap<int>(portRangeMaxKey, channel.GetPortRangeMax());
UpdateVarMap<bool>(autoBindKey, channel.GetAutoBind());
SetVarMapValue<string>(typeKey, channel.GetType());
SetVarMapValue<string>(methodKey, channel.GetMethod());
SetVarMapValue<string>(addressKey, channel.GetAddress());
SetVarMapValue<string>(transportKey, channel.GetTransportName());
SetVarMapValue<int>(sndBufSizeKey, channel.GetSndBufSize());
SetVarMapValue<int>(rcvBufSizeKey, channel.GetRcvBufSize());
SetVarMapValue<int>(sndKernelSizeKey, channel.GetSndKernelSize());
SetVarMapValue<int>(rcvKernelSizeKey, channel.GetRcvKernelSize());
SetVarMapValue<int>(lingerKey, channel.GetLinger());
SetVarMapValue<int>(rateLoggingKey, channel.GetRateLogging());
SetVarMapValue<int>(portRangeMinKey, channel.GetPortRangeMin());
SetVarMapValue<int>(portRangeMaxKey, channel.GetPortRangeMax());
SetVarMapValue<bool>(autoBindKey, channel.GetAutoBind());
index++;
}
UpdateVarMap<int>("chans." + p.first + ".numSockets", index);
SetVarMapValue<int>("chans." + p.first + ".numSockets", index);
}
}

View File

@ -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<std::string>& 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<std::string>& cmdArgs, bool allowUnregistered)
{
std::vector<const char*> argv(cmdArgs.size());
transform(cmdArgs.begin(), cmdArgs.end(), argv.begin(), [](const std::string& str) {
return str.c_str();
});
return ParseAll(argv.size(), const_cast<char**>(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<std::string, boost::any> result;
std::lock_guard<std::mutex> 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<typename T>
@ -117,8 +124,7 @@ class FairMQProgOptions
{
std::unique_lock<std::mutex> lock(fMtx);
// update variable map
UpdateVarMap<typename std::decay<T>::type>(key, val);
SetVarMapValue<typename std::decay<T>::type>(key, val);
if (key == "channel-config") {
ParseChannelsFromCmdLine();
@ -128,7 +134,6 @@ class FairMQProgOptions
lock.unlock();
//if (std::is_same<T, int>::value || std::is_same<T, std::string>::value)//if one wants to restrict type
fEvents.Emit<fair::mq::PropertyChange, typename std::decay<T>::type>(key, val);
fEvents.Emit<fair::mq::PropertyChangeAsString, std::string>(key, GetStringValue(key));
}
@ -142,20 +147,30 @@ class FairMQProgOptions
void SetProperties(const std::map<std::string, boost::any>& input)
{
std::lock_guard<std::mutex> lock(fMtx);
std::map<std::string, boost::program_options::variable_value>& 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<std::mutex> lock(fMtx);
std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
vm.erase(key);
}
template <typename T>
void Subscribe(const std::string& subscriber, std::function<void(typename fair::mq::PropertyChange::KeyType, T)> func)
{
std::lock_guard<std::mutex> lock(fMtx);
static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
"In template member FairMQProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
fEvents.Subscribe<fair::mq::PropertyChange, T>(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<typename T>
void UpdateVarMap(const std::string& key, const T& val)
void SetVarMapValue(const std::string& key, const T& val)
{
std::map<std::string, boost::program_options::variable_value>& vm = fVarMap;
vm[key].value() = boost::any(val);