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 // device config API
// see <fairmq/PluginServices.h> for docs // see <fairmq/PluginServices.h> for docs
auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
template<typename T> template<typename T>
auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); } auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
template<typename T> template<typename T>

View File

@ -165,6 +165,9 @@ class PluginServices
auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice->UnsubscribeFromStateChange(subscriber); } auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice->UnsubscribeFromStateChange(subscriber); }
// Config API // 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 /// @brief Set config property
/// @param key /// @param key
@ -195,14 +198,24 @@ class PluginServices
/// TODO Currently, if a non-existing key is requested and a default constructed object is returned. /// 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. /// This behaviour will be changed in the future to throw an exception in that case to provide a proper sentinel.
template<typename T> 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 /// @brief Read config property as string
/// @param key /// @param key
/// @return config property value converted to string /// @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 /// 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(); } auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fConfig->GetChannelInfo(); }

View File

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

View File

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