Skip to content
Snippets Groups Projects
Commit dddeca32 authored by Gerhard Raven's avatar Gerhard Raven
Browse files

Modernize GaudiPluginService

   - prfer boost::algorithm::trim over in-situ implementation
   - make static const
   - prefer aggregate initialization
parent 891bd4e4
Branches
Tags
1 merge request!7C++11 modernization changes
......@@ -39,34 +39,8 @@ namespace {
#endif
#include <algorithm>
#include "boost/algorithm/string/trim.hpp"
namespace {
// string trimming functions taken from
// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
constexpr struct is_space_t {
bool operator()(int i) const { return std::isspace(i); }
} is_space {};
// trim from start
static inline std::string &ltrim(std::string &s) {
s.erase(s.begin(),
std::find_if_not(s.begin(), s.end(), is_space ) );
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if_not(s.rbegin(), s.rend(), is_space).base(),
s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
}
namespace {
/// Helper function used to set values in FactoryInfo data members only
......@@ -198,7 +172,7 @@ namespace Gaudi { namespace PluginService {
while (!factories.eof()) {
++lineCount;
std::getline(factories, line);
trim(line);
boost::algorithm::trim(line);
// skip empty lines and lines starting with '#'
if (line.empty() || line[0] == '#') continue;
// look for the separator
......@@ -250,9 +224,7 @@ namespace Gaudi { namespace PluginService {
type, rtype, className, props) ).first;
} else {
// do not replace an existing factory with a new one
if (!entry->second.ptr) {
entry->second.ptr = factory;
}
if (!entry->second.ptr) entry->second.ptr = factory;
factoryInfoSetHelper(entry->second.type, type, "type", id);
factoryInfoSetHelper(entry->second.rtype, rtype, "return type", id);
factoryInfoSetHelper(entry->second.className, className, "class", id);
......@@ -284,32 +256,24 @@ namespace Gaudi { namespace PluginService {
logger().warning("cannot load " + f->second.library +
" for factory " + id);
char *dlmsg = dlerror();
if (dlmsg)
logger().warning(dlmsg);
if (dlmsg) logger().warning(dlmsg);
return nullptr;
}
f = facts.find(id); // ensure that the iterator is valid
}
if (f->second.type == type) {
return f->second.ptr;
} else {
logger().warning("found factory " + id + ", but of wrong type: " +
demangle(f->second.type) + " instead of " + demangle(type));
}
if (f->second.type == type) return f->second.ptr;
logger().warning("found factory " + id + ", but of wrong type: " +
demangle(f->second.type) + " instead of " + demangle(type));
}
return nullptr; // factory not found
}
const Registry::FactoryInfo& Registry::getInfo(const std::string& id) const {
REG_SCOPE_LOCK
static FactoryInfo unknown("unknown");
static const FactoryInfo unknown("unknown");
const FactoryMap &facts = factories();
auto f = facts.find(id);
if (f != facts.end())
{
return f->second;
}
return unknown; // factory not found
return (f != facts.end()) ? f->second : unknown;
}
Registry&
......@@ -319,10 +283,7 @@ namespace Gaudi { namespace PluginService {
REG_SCOPE_LOCK
FactoryMap &facts = factories();
auto f = facts.find(id);
if (f != facts.end())
{
f->second.properties[k] = v;
}
if (f != facts.end()) f->second.properties[k] = v;
return *this;
}
......@@ -348,12 +309,8 @@ namespace Gaudi { namespace PluginService {
}
static std::unique_ptr<Logger> s_logger(new Logger);
Logger& logger() {
return *s_logger;
}
void setLogger(Logger* logger) {
s_logger.reset(logger);
}
Logger& logger() { return *s_logger; }
void setLogger(Logger* logger) { s_logger.reset(logger); }
} // namespace Details
......@@ -370,8 +327,8 @@ namespace Gaudi { namespace PluginService {
int Debug() {
using namespace Details;
switch (logger().level()) {
case Logger::Debug: return 2; break;
case Logger::Info: return 1; break;
case Logger::Debug: return 2;
case Logger::Info: return 1;
default: return 0;
}
}
......
......@@ -10,9 +10,7 @@ cgaudi_pluginsvc_t
cgaudi_pluginsvc_instance()
{
static Registry& cxxreg = Registry::instance();
cgaudi_pluginsvc_t reg;
reg.registry = (void*)(&cxxreg);
return reg;
return { (void*)(&cxxreg) };
}
int
......@@ -40,43 +38,35 @@ cgaudi_pluginsvc_get_factory_at(cgaudi_pluginsvc_t self, int n)
std::transform(reg->factories().begin(),reg->factories().end(),
std::back_inserter(keys),
select1st );
const char *key = keys[n].c_str();
cgaudi_factory_t fac;
fac.registry = self;
fac.id = key;
return fac;
return { self, keys[n].c_str() };
}
const char*
cgaudi_factory_get_library(cgaudi_factory_t self)
{
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(self.id);
return fi.library.c_str();
return reg.getInfo(self.id).library.c_str();
}
const char*
cgaudi_factory_get_type(cgaudi_factory_t self)
{
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(self.id);
return fi.type.c_str();
return reg.getInfo(self.id).type.c_str();
}
const char*
cgaudi_factory_get_rtype(cgaudi_factory_t self)
{
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(self.id);
return fi.rtype.c_str();
return reg.getInfo(self.id).rtype.c_str();
}
const char*
cgaudi_factory_get_classname(cgaudi_factory_t self)
{
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(self.id);
return fi.className.c_str();
return reg.getInfo(self.id).className.c_str();
}
int
......@@ -91,10 +81,7 @@ cgaudi_factory_get_property_size(cgaudi_factory_t self)
cgaudi_property_t
cgaudi_factory_get_property_at(cgaudi_factory_t self, int n)
{
cgaudi_property_t cprop;
cprop.registry = self.registry;
cprop.id = self.id;
cprop.key = nullptr;
cgaudi_property_t cprop{ self.registry, self.id, nullptr };
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(cprop.id);
if (n<(long)fi.properties.size()) {
......@@ -115,7 +102,6 @@ cgaudi_property_get_value(cgaudi_property_t self)
{
Registry &reg = Registry::instance();
const Registry::FactoryInfo& fi = reg.getInfo(self.id);
Registry::KeyType key = self.key;
Registry::Properties::const_iterator prop = fi.properties.find(key);
auto prop = fi.properties.find(self.key);
return prop != fi.properties.end() ? prop->second.c_str() : nullptr;
}
......@@ -96,34 +96,27 @@ int main(int argc, char* argv[]) {
// handle output option
std::unique_ptr<std::ostream> output_file;
if (output_opt != "-") {
output_file = std::unique_ptr<std::ostream>(new std::ofstream(output_opt.c_str()));
output_file.reset( new std::ofstream(output_opt.c_str()) );
}
std::ostream &output = (output_file ? *output_file : std::cout);
// loop over the list of libraries passed on the command line
for (char* lib: libs) {
if (dlopen(lib, RTLD_LAZY | RTLD_LOCAL)) {
std::set<key_type> factories = reg.loadedFactories();
std::set<key_type>::const_iterator f;
for (f = factories.begin(); f != factories.end(); ++f) {
if (loaded.find(*f) == loaded.end())
{
for (auto f = factories.begin(); f != factories.end(); ++f) {
if (loaded.find(*f) == loaded.end()) {
output << lib << ":" << *f << std::endl;
loaded[*f] = lib;
}
else
} else
std::cerr << "WARNING: factory '" << *f
<< "' already found in " << loaded[*f]
<< std::endl;
}
} else {
std::cerr << "ERROR: failed to load " << lib << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment