Gaudi::PluginService::Details::Registry Class Reference

In-memory database of the loaded factories. More...

#include </scratch/z5/marcocle/GaudiDocs/lhcb-release/996/GAUDI/GAUDI_v26r4/InstallArea/x86_64-slc6-gcc48-opt/include/Gaudi/Details/PluginServiceDetails.h>

Collaboration diagram for Gaudi::PluginService::Details::Registry:

Classes

struct  FactoryInfo
 

Public Types

typedef std::string KeyType
 
typedef std::map< KeyType, std::string > Properties
 Type used for the properties implementation. More...
 
typedef std::map< KeyType, FactoryInfoFactoryMap
 Type used for the database implementation. More...
 
typedef std::string KeyType
 
typedef std::map< KeyType, std::string > Properties
 Type used for the properties implementation. More...
 
typedef std::map< KeyType, FactoryInfoFactoryMap
 Type used for the database implementation. More...
 

Public Member Functions

template<typename F , typename T , typename I >
FactoryInfoadd (const I &id, typename F::FuncType ptr)
 Add a factory to the database. More...
 
void * get (const std::string &id, const std::string &type) const
 Retrieve the factory for the given id. More...
 
const FactoryInfogetInfo (const std::string &id) const
 Retrieve the FactoryInfo object for an id. More...
 
RegistryaddProperty (const std::string &id, const std::string &k, const std::string &v)
 Add a property to an already existing FactoryInfo object (via its id.) More...
 
std::set< KeyTypeloadedFactories () const
 Return a list of all the known and loaded factories. More...
 
const FactoryMapfactories () const
 Return the known factories (loading the list if not yet done). More...
 
template<typename F , typename T , typename I >
FactoryInfoadd (const I &id, typename F::FuncType ptr)
 Add a factory to the database. More...
 
void * get (const std::string &id, const std::string &type) const
 Retrieve the factory for the given id. More...
 
const FactoryInfogetInfo (const std::string &id) const
 Retrieve the FactoryInfo object for an id. More...
 
RegistryaddProperty (const std::string &id, const std::string &k, const std::string &v)
 Add a property to an already existing FactoryInfo object (via its id.) More...
 
std::set< KeyTypeloadedFactories () const
 Return a list of all the known and loaded factories. More...
 
const FactoryMapfactories () const
 Return the known factories (loading the list if not yet done). More...
 

Static Public Member Functions

static Registryinstance ()
 Retrieve the singleton instance of Registry. More...
 
static Registryinstance ()
 Retrieve the singleton instance of Registry. More...
 

Private Member Functions

 Registry ()
 Private constructor for the singleton pattern. More...
 
 Registry (const Registry &)
 Private copy constructor for the singleton pattern. More...
 
FactoryInfoadd (const std::string &id, void *factory, const std::string &type, const std::string &rtype, const std::string &className, const Properties &props=Properties())
 Add a factory to the database. More...
 
FactoryMapfactories ()
 Return the known factories (loading the list if not yet done). More...
 
void initialize ()
 Initialize the registry loading the list of factories from the .component files in the library search path. More...
 
 Registry ()
 Private constructor for the singleton pattern. More...
 
 Registry (const Registry &)
 Private copy constructor for the singleton pattern. More...
 
FactoryInfoadd (const std::string &id, void *factory, const std::string &type, const std::string &rtype, const std::string &className, const Properties &props=Properties())
 Add a factory to the database. More...
 
FactoryMapfactories ()
 Return the known factories (loading the list if not yet done). More...
 
void initialize ()
 Initialize the registry loading the list of factories from the .component files in the library search path. More...
 

Private Attributes

bool m_initialized
 Flag recording if the registry has been initialized or not. More...
 
FactoryMap m_factories
 Internal storage for factories. More...
 

Detailed Description

In-memory database of the loaded factories.

Definition at line 99 of file PluginServiceDetails.h.

Member Typedef Documentation

typedef std::map<KeyType, FactoryInfo> Gaudi::PluginService::Details::Registry::FactoryMap

Type used for the database implementation.

Definition at line 130 of file PluginServiceDetails.h.

typedef std::map<KeyType, FactoryInfo> Gaudi::PluginService::Details::Registry::FactoryMap

Type used for the database implementation.

Definition at line 130 of file PluginServiceDetails.h.

typedef std::string Gaudi::PluginService::Details::Registry::KeyType

Definition at line 101 of file PluginServiceDetails.h.

typedef std::string Gaudi::PluginService::Details::Registry::KeyType

Definition at line 101 of file PluginServiceDetails.h.

typedef std::map<KeyType, std::string> Gaudi::PluginService::Details::Registry::Properties

Type used for the properties implementation.

Definition at line 104 of file PluginServiceDetails.h.

typedef std::map<KeyType, std::string> Gaudi::PluginService::Details::Registry::Properties

Type used for the properties implementation.

Definition at line 104 of file PluginServiceDetails.h.

Constructor & Destructor Documentation

Gaudi::PluginService::Details::Registry::Registry ( )
private

Private constructor for the singleton pattern.

At construction time, the internal database of known factories is filled with the name of the libraries containing them, using the ".components" files in the LD_LIBRARY_PATH.

Definition at line 119 of file PluginService.cpp.

119 : m_initialized(false) {}
bool m_initialized
Flag recording if the registry has been initialized or not.
Gaudi::PluginService::Details::Registry::Registry ( const Registry )
inlineprivate

Private copy constructor for the singleton pattern.

Definition at line 176 of file PluginServiceDetails.h.

176 : m_initialized(false) {}
bool m_initialized
Flag recording if the registry has been initialized or not.
Gaudi::PluginService::Details::Registry::Registry ( )
private

Private constructor for the singleton pattern.

At construction time, the internal database of known factories is filled with the name of the libraries containing them, using the ".components" files in the LD_LIBRARY_PATH.

Gaudi::PluginService::Details::Registry::Registry ( const Registry )
inlineprivate

Private copy constructor for the singleton pattern.

Definition at line 176 of file PluginServiceDetails.h.

176 : m_initialized(false) {}
bool m_initialized
Flag recording if the registry has been initialized or not.

Member Function Documentation

template<typename F , typename T , typename I >
FactoryInfo& Gaudi::PluginService::Details::Registry::add ( const I &  id,
typename F::FuncType  ptr 
)
inline

Add a factory to the database.

Definition at line 137 of file PluginServiceDetails.h.

137  {
138  union { typename F::FuncType src; void* dst; } p2p;
139  p2p.src = ptr;
140  std::ostringstream o; o << id;
141  return add(o.str(), p2p.dst,
142  typeid(typename F::FuncType).name(),
143  typeid(typename F::ReturnType).name(),
144  demangle<T>());
145  }
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
template<typename F , typename T , typename I >
FactoryInfo& Gaudi::PluginService::Details::Registry::add ( const I &  id,
typename F::FuncType  ptr 
)
inline

Add a factory to the database.

Definition at line 137 of file PluginServiceDetails.h.

137  {
138  union { typename F::FuncType src; void* dst; } p2p;
139  p2p.src = ptr;
140  std::ostringstream o; o << id;
141  return add(o.str(), p2p.dst,
142  typeid(typename F::FuncType).name(),
143  typeid(typename F::ReturnType).name(),
144  demangle<T>());
145  }
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
Registry::FactoryInfo & Gaudi::PluginService::Details::Registry::add ( const std::string &  id,
void *  factory,
const std::string &  type,
const std::string &  rtype,
const std::string &  className,
const Properties props = Properties() 
)
private

Add a factory to the database.

Definition at line 213 of file PluginService.cpp.

216  {
218  FactoryMap &facts = factories();
219  auto entry = facts.find(id);
220  if (entry == facts.end())
221  {
222  // this factory was not known yet
223  entry = facts.emplace( id, FactoryInfo("unknown", factory,
224  type, rtype, className, props) ).first;
225  } else {
226  // do not replace an existing factory with a new one
227  if (!entry->second.ptr) entry->second.ptr = factory;
228  factoryInfoSetHelper(entry->second.type, type, "type", id);
229  factoryInfoSetHelper(entry->second.rtype, rtype, "return type", id);
230  factoryInfoSetHelper(entry->second.className, className, "class", id);
231  }
232 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
233  // add an alias for the factory using the Reflex convention
234  std::string old_name = old_style_name(id);
235  if (id != old_name)
236  add(old_name, factory, type, rtype, className, props)
237  .properties["ReflexName"] = "true";
238 #endif
239  return entry->second;
240  }
#define REG_SCOPE_LOCK
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
FactoryInfo & add(const I &id, typename F::FuncType ptr)
Add a factory to the database.
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
string type
Definition: gaudirun.py:151
FactoryInfo& Gaudi::PluginService::Details::Registry::add ( const std::string &  id,
void *  factory,
const std::string &  type,
const std::string &  rtype,
const std::string &  className,
const Properties props = Properties() 
)
private

Add a factory to the database.

Registry & Gaudi::PluginService::Details::Registry::addProperty ( const std::string &  id,
const std::string &  k,
const std::string &  v 
)

Add a property to an already existing FactoryInfo object (via its id.)

Definition at line 280 of file PluginService.cpp.

282  {
284  FactoryMap &facts = factories();
285  auto f = facts.find(id);
286  if (f != facts.end()) f->second.properties[k] = v;
287  return *this;
288  }
#define REG_SCOPE_LOCK
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
Registry& Gaudi::PluginService::Details::Registry::addProperty ( const std::string &  id,
const std::string &  k,
const std::string &  v 
)

Add a property to an already existing FactoryInfo object (via its id.)

const FactoryMap& Gaudi::PluginService::Details::Registry::factories ( ) const
inline

Return the known factories (loading the list if not yet done).

Definition at line 163 of file PluginServiceDetails.h.

163  {
164  if (!m_initialized) const_cast<Registry*>(this)->initialize();
165  return m_factories;
166  }
Registry()
Private constructor for the singleton pattern.
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
FactoryMap m_factories
Internal storage for factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
const FactoryMap& Gaudi::PluginService::Details::Registry::factories ( ) const
inline

Return the known factories (loading the list if not yet done).

Definition at line 163 of file PluginServiceDetails.h.

163  {
164  if (!m_initialized) const_cast<Registry*>(this)->initialize();
165  return m_factories;
166  }
Registry()
Private constructor for the singleton pattern.
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
FactoryMap m_factories
Internal storage for factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
FactoryMap& Gaudi::PluginService::Details::Registry::factories ( )
inlineprivate

Return the known factories (loading the list if not yet done).

Definition at line 186 of file PluginServiceDetails.h.

186  {
187  if (!m_initialized) initialize();
188  return m_factories;
189  }
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
FactoryMap m_factories
Internal storage for factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
FactoryMap& Gaudi::PluginService::Details::Registry::factories ( )
inlineprivate

Return the known factories (loading the list if not yet done).

Definition at line 186 of file PluginServiceDetails.h.

186  {
187  if (!m_initialized) initialize();
188  return m_factories;
189  }
void initialize()
Initialize the registry loading the list of factories from the .component files in the library search...
FactoryMap m_factories
Internal storage for factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
void* Gaudi::PluginService::Details::Registry::get ( const std::string &  id,
const std::string &  type 
) const

Retrieve the factory for the given id.

void * Gaudi::PluginService::Details::Registry::get ( const std::string &  id,
const std::string &  type 
) const

Retrieve the factory for the given id.

Definition at line 242 of file PluginService.cpp.

242  {
244  const FactoryMap &facts = factories();
245  auto f = facts.find(id);
246  if (f != facts.end())
247  {
248 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
249  const Properties& props = f->second.properties;
250  if (props.find("ReflexName") != props.end())
251  logger().warning("requesting factory via old name '" + id + "'"
252  "use '" + f->second.className + "' instead");
253 #endif
254  if (!f->second.ptr) {
255  if (!dlopen(f->second.library.c_str(), RTLD_LAZY | RTLD_GLOBAL)) {
256  logger().warning("cannot load " + f->second.library +
257  " for factory " + id);
258  char *dlmsg = dlerror();
259  if (dlmsg) logger().warning(dlmsg);
260  return nullptr;
261  }
262  f = facts.find(id); // ensure that the iterator is valid
263  }
264  if (f->second.type == type) return f->second.ptr;
265  logger().warning("found factory " + id + ", but of wrong type: " +
266  demangle(f->second.type) + " instead of " + demangle(type));
267  }
268  return nullptr; // factory not found
269  }
#define REG_SCOPE_LOCK
GAUDIPS_API Logger & logger()
Return the current logger instance.
std::map< KeyType, std::string > Properties
Type used for the properties implementation.
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
GAUDIPS_API std::string demangle(const std::type_info &id)
Return a canonical name for type_info object (implementation borrowed from GaudiKernel/System).
void warning(const std::string &msg)
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
string type
Definition: gaudirun.py:151
const Registry::FactoryInfo & Gaudi::PluginService::Details::Registry::getInfo ( const std::string &  id) const

Retrieve the FactoryInfo object for an id.

Definition at line 271 of file PluginService.cpp.

271  {
273  static const FactoryInfo unknown("unknown");
274  const FactoryMap &facts = factories();
275  auto f = facts.find(id);
276  return (f != facts.end()) ? f->second : unknown;
277  }
#define REG_SCOPE_LOCK
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
const FactoryInfo& Gaudi::PluginService::Details::Registry::getInfo ( const std::string &  id) const

Retrieve the FactoryInfo object for an id.

void Gaudi::PluginService::Details::Registry::initialize ( )
private

Initialize the registry loading the list of factories from the .component files in the library search path.

void Gaudi::PluginService::Details::Registry::initialize ( )
private

Initialize the registry loading the list of factories from the .component files in the library search path.

Definition at line 121 of file PluginService.cpp.

121  {
123  if (m_initialized) return;
124  m_initialized = true;
125 #ifdef WIN32
126  const char* envVar = "PATH";
127  const char sep = ';';
128 #else
129  const char* envVar = "LD_LIBRARY_PATH";
130  const char sep = ':';
131 #endif
132  char *search_path = ::getenv(envVar);
133  if (search_path) {
134  logger().debug(std::string("searching factories in ") + envVar);
135  std::string path(search_path);
136  std::string::size_type pos = 0;
137  std::string::size_type newpos = 0;
138  while (pos != std::string::npos) {
139  std::string dirName;
140  // get the next entry in the path
141  newpos = path.find(sep, pos);
142  if (newpos != std::string::npos) {
143  dirName = path.substr(pos, newpos - pos);
144  pos = newpos+1;
145  } else {
146  dirName = path.substr(pos);
147  pos = newpos;
148  }
149  logger().debug(std::string(" looking into ") + dirName);
150  // look for files called "*.components" in the directory
151  DIR *dir = opendir(dirName.c_str());
152  if (dir) {
153  struct dirent * entry;
154  while ((entry = readdir(dir))) {
155  std::string name(entry->d_name);
156  // check if the file name ends with ".components"
157  std::string::size_type extpos = name.find(".components");
158  if ((extpos != std::string::npos) &&
159  ((extpos+11) == name.size())) {
160  std::string fullPath = (dirName + '/' + name);
161  { // check if it is a regular file
162  struct stat buf;
163  stat(fullPath.c_str(), &buf);
164  if (!S_ISREG(buf.st_mode)) continue;
165  }
166  // read the file
167  logger().debug(std::string(" reading ") + name);
168  std::ifstream factories{fullPath};
169  std::string line;
170  int factoriesCount = 0;
171  int lineCount = 0;
172  while (!factories.eof()) {
173  ++lineCount;
174  std::getline(factories, line);
175  boost::algorithm::trim(line);
176  // skip empty lines and lines starting with '#'
177  if (line.empty() || line[0] == '#') continue;
178  // look for the separator
179  auto pos = line.find(':');
180  if (pos == std::string::npos) {
181  logger().warning( "failed to parse line "
182  + fullPath + ':'
183  + std::to_string(lineCount) );
184  continue;
185  }
186  const std::string lib(line, 0, pos);
187  const std::string fact(line, pos+1);
188  m_factories.emplace(fact, FactoryInfo(lib));
189 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
190  // add an alias for the factory using the Reflex convention
191  std::string old_name = old_style_name(fact);
192  if (fact != old_name) {
193  FactoryInfo old_info(lib);
194  old_info.properties["ReflexName"] = "true";
195  m_factories.emplace(old_name, old_info);
196  }
197 #endif
198  ++factoriesCount;
199  }
200  if (logger().level() <= Logger::Debug) {
201  logger().debug( " found " + std::to_string( factoriesCount )
202  + " factories" );
203  }
204  }
205  }
206  closedir(dir);
207  }
208  }
209  }
210  }
#define REG_SCOPE_LOCK
list path
Definition: __init__.py:15
GAUDIPS_API Logger & logger()
Return the current logger instance.
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
void warning(const std::string &msg)
FactoryMap m_factories
Internal storage for factories.
bool m_initialized
Flag recording if the registry has been initialized or not.
int line
Definition: ana.py:50
Registry & Gaudi::PluginService::Details::Registry::instance ( )
static

Retrieve the singleton instance of Registry.

Definition at line 113 of file PluginService.cpp.

113  {
115  static Registry r;
116  return r;
117  }
Registry()
Private constructor for the singleton pattern.
#define SINGLETON_LOCK
static Registry& Gaudi::PluginService::Details::Registry::instance ( )
static

Retrieve the singleton instance of Registry.

std::set< Registry::KeyType > Gaudi::PluginService::Details::Registry::loadedFactories ( ) const

Return a list of all the known and loaded factories.

Definition at line 290 of file PluginService.cpp.

290  {
292  const FactoryMap &facts = factories();
293  std::set<KeyType> l;
294  for (const auto& f : facts )
295  {
296  if (f.second.ptr) l.insert(f.first);
297  }
298  return l;
299  }
#define REG_SCOPE_LOCK
const FactoryMap & factories() const
Return the known factories (loading the list if not yet done).
dictionary l
Definition: gaudirun.py:422
std::map< KeyType, FactoryInfo > FactoryMap
Type used for the database implementation.
std::set<KeyType> Gaudi::PluginService::Details::Registry::loadedFactories ( ) const

Return a list of all the known and loaded factories.

Member Data Documentation

FactoryMap Gaudi::PluginService::Details::Registry::m_factories
private

Internal storage for factories.

Definition at line 199 of file PluginServiceDetails.h.

bool Gaudi::PluginService::Details::Registry::m_initialized
private

Flag recording if the registry has been initialized or not.

Definition at line 196 of file PluginServiceDetails.h.


The documentation for this class was generated from the following files: