add CountProperty() to plugin APIs, throw is GetProperty doesn't find key

This commit is contained in:
Alexey Rybalchenko 2018-03-19 13:59:43 +01:00 committed by Mohammad Al-Turany
parent 243352d717
commit 95112dac02
5 changed files with 35 additions and 16 deletions

View File

@ -84,6 +84,7 @@ class Plugin
// device config API
// see <fairmq/PluginServices.h> for docs
auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
template<typename T>
auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
template<typename T>

View File

@ -165,6 +165,9 @@ class PluginServices
auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice->UnsubscribeFromStateChange(subscriber); }
// Config API
struct PropertyNotFoundError : std::runtime_error { using std::runtime_error::runtime_error; };
auto PropertyExists(const std::string& key) const -> bool { return fConfig->Count(key) > 0; }
/// @brief Set config property
/// @param key
@ -195,14 +198,24 @@ class PluginServices
/// TODO Currently, if a non-existing key is requested and a default constructed object is returned.
/// This behaviour will be changed in the future to throw an exception in that case to provide a proper sentinel.
template<typename T>
auto GetProperty(const std::string& key) const -> T { return fConfig->GetValue<T>(key); }
auto GetProperty(const std::string& key) const -> T {
if (PropertyExists(key)) {
return fConfig->GetValue<T>(key);
}
throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
}
/// @brief Read config property as string
/// @param key
/// @return config property value converted to string
///
/// If a type is not supported, the user can provide support by overloading the ostream operator for this type
auto GetPropertyAsString(const std::string& key) const -> std::string { return fConfig->GetStringValue(key); }
auto GetPropertyAsString(const std::string& key) const -> std::string {
if (PropertyExists(key)) {
return fConfig->GetStringValue(key);
}
throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
}
auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fConfig->GetChannelInfo(); }

View File

@ -17,6 +17,7 @@
#include "FairMQLogger.h"
#include "FairProgOptionsHelper.h"
#include <fairmq/Tools.h>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
@ -26,6 +27,7 @@
#include <iostream>
#include <fstream>
#include <mutex>
#include <exception>
namespace po = boost::program_options;
namespace fs = boost::filesystem;
@ -61,21 +63,14 @@ class FairProgOptions
std::unique_lock<std::mutex> lock(fConfigMutex);
T val = T();
try
if (fVarMap.count(key))
{
if (fVarMap.count(key))
{
val = fVarMap[key].as<T>();
}
else
{
LOG(error) << "Config has no key: " << key;
}
val = fVarMap[key].as<T>();
}
catch (std::exception& e)
else
{
LOG(error) << "Exception thrown for the key '" << key << "'";
LOG(error) << e.what();
LOG(warn) << "Config has no key: " << key << ". Returning default constructed object.";
}
return val;

View File

@ -182,8 +182,14 @@ auto DDS::FillChannelContainers() -> void
}
// save properties that will have multiple values arriving (with only some of them to be used)
vector<string> iValues = GetProperty<vector<string>>("dds-i");
vector<string> inValues = GetProperty<vector<string>>("dds-i-n");
vector<string> iValues;
if (PropertyExists("dds-i")) {
iValues = GetProperty<vector<string>>("dds-i");
}
vector<string> inValues;
if (PropertyExists("dds-i-n")) {
inValues = GetProperty<vector<string>>("dds-i-n");
}
for (const auto& vi : iValues) {
size_t pos = vi.find(":");

View File

@ -292,6 +292,8 @@ int64_t FairMQSocketSHM::Send(vector<FairMQMessagePtr>& msgVec, const int flags)
return nbytes;
}
}
return -1;
}
@ -365,6 +367,8 @@ int64_t FairMQSocketSHM::Receive(vector<FairMQMessagePtr>& msgVec, const int fla
return nbytes;
}
}
return -1;
}
void FairMQSocketSHM::Close()