Public Types | Public Member Functions | Private Attributes | List of all members
WireCell::NamedFactoryRegistry< IType > Class Template Reference

#include <NamedFactory.h>

Public Types

typedef IType interface_type
 
typedef std::shared_ptr< IType > interface_ptr
 
typedef WireCell::INamedFactoryfactory_ptr
 
typedef std::unordered_map< std::string, factory_ptrfactory_lookup
 
typedef std::set< std::stringknown_type_set
 

Public Member Functions

 NamedFactoryRegistry ()
 
size_t hello (const std::string &classname)
 
known_type_set known_types () const
 
bool associate (const std::string &classname, factory_ptr factory)
 Register an existing factory by the "class" name of the instance it can create. More...
 
factory_ptr lookup_factory (const std::string &classname)
 Look up an existing factory by the name of the "class" it can create. More...
 
interface_ptr instance (const std::string &classname, const std::string &instname="", bool create=true, bool nullok=false)
 
std::vector< std::stringknown_classes ()
 

Private Attributes

Log::logptr_t l
 
factory_lookup m_lookup
 
known_type_set m_known_types
 

Detailed Description

template<class IType>
class WireCell::NamedFactoryRegistry< IType >

A registry of factories that produce instances which implement a given interface.

Definition at line 71 of file NamedFactory.h.

Member Typedef Documentation

template<class IType>
typedef std::unordered_map<std::string, factory_ptr> WireCell::NamedFactoryRegistry< IType >::factory_lookup

Definition at line 77 of file NamedFactory.h.

template<class IType>
typedef WireCell::INamedFactory* WireCell::NamedFactoryRegistry< IType >::factory_ptr

Definition at line 76 of file NamedFactory.h.

template<class IType>
typedef std::shared_ptr<IType> WireCell::NamedFactoryRegistry< IType >::interface_ptr

Definition at line 75 of file NamedFactory.h.

template<class IType>
typedef IType WireCell::NamedFactoryRegistry< IType >::interface_type

Definition at line 74 of file NamedFactory.h.

template<class IType>
typedef std::set<std::string> WireCell::NamedFactoryRegistry< IType >::known_type_set

Definition at line 78 of file NamedFactory.h.

Constructor & Destructor Documentation

template<class IType>
WireCell::NamedFactoryRegistry< IType >::NamedFactoryRegistry ( )
inline

Definition at line 80 of file NamedFactory.h.

80 : l(Log::logger("factory")) {}
logptr_t logger(std::string name)
Definition: Logging.cxx:71

Member Function Documentation

template<class IType>
bool WireCell::NamedFactoryRegistry< IType >::associate ( const std::string classname,
factory_ptr  factory 
)
inline

Register an existing factory by the "class" name of the instance it can create.

Definition at line 88 of file NamedFactory.h.

88  {
89  m_lookup[classname] = factory;
90  return true;
91  }
template<class IType>
size_t WireCell::NamedFactoryRegistry< IType >::hello ( const std::string classname)
inline

Definition at line 81 of file NamedFactory.h.

81  {
82  m_known_types.insert(classname);
83  return m_known_types.size();
84  }
template<class IType>
interface_ptr WireCell::NamedFactoryRegistry< IType >::instance ( const std::string classname,
const std::string instname = "",
bool  create = true,
bool  nullok = false 
)
inline

Return instance of give type and optional instance name. If create is true, create the instance if it does not exist. If nullok is true return nullptr if it does not exist else throw by default.

Definition at line 141 of file NamedFactory.h.

142  {
143  factory_ptr fac = lookup_factory(classname);
144  if (!fac) {
145  if (nullok) {
146  return nullptr;
147  }
148  l->error("no factory for class \"{}\" (instance \"{}\")", classname, instname);
149  std::cerr << "WireCell::NamedFactory: no factory for class \""
150  << classname << "\", (instance \"" << instname << "\"\n";
151 
152  THROW(FactoryException() << errmsg{"No factory for class " + classname});
153  }
155  std::string action = "";
156  if (create) {
157  iptr = fac->create(instname);
158  action = "create";
159  }
160  else {
161  iptr = fac->find(instname);
162  action = "find";
163  }
164  if (!iptr) {
165  if (nullok) {
166  return nullptr;
167  }
168  std::string msg = "NamedFactory: Failed to "+action+" instance \"" + instname;
169  msg += "\" of class \"" + classname + "\"";
170  l->error(msg);
171  THROW(FactoryException() << errmsg{msg});
172  }
173  interface_ptr uptype = std::dynamic_pointer_cast<interface_type>(iptr);
174  if (!uptype) {
175  if (nullok) {
176  return nullptr;
177  }
178  std::string msg = "NamedFactory: Failed to cast instance: " + instname;
179  msg += " of class " + classname;
180  msg += " c++ type: " + type(iptr);
181  msg += " to " + type(uptype);
182  l->error(msg);
183  THROW(FactoryException() << errmsg{msg});
184  }
185  return uptype;
186  }
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
factory_ptr lookup_factory(const std::string &classname)
Look up an existing factory by the name of the "class" it can create.
Definition: NamedFactory.h:95
WireCell::INamedFactory * factory_ptr
Definition: NamedFactory.h:76
std::shared_ptr< Interface > pointer
Definition: Interface.h:16
#define THROW(e)
Definition: Exceptions.h:25
std::string type(const T &t)
Definition: Type.h:20
std::shared_ptr< spdlog::logger > create(std::string logger_name, SinkArgs &&...sink_args)
Definition: spdlog.h:45
std::shared_ptr< IType > interface_ptr
Definition: NamedFactory.h:75
template<class IType>
std::vector<std::string> WireCell::NamedFactoryRegistry< IType >::known_classes ( )
inline

Return a collection of class names known to this factory registry. Note: linked/plugged shared libraries do not automatically register their factories.

Definition at line 191 of file NamedFactory.h.

191  {
192  std::vector<std::string> ret;
193  for (auto it : m_lookup) {
194  ret.push_back(it.first);
195  }
196  return ret;
197  }
template<class IType>
known_type_set WireCell::NamedFactoryRegistry< IType >::known_types ( ) const
inline

Definition at line 85 of file NamedFactory.h.

85 { return m_known_types; }
template<class IType>
factory_ptr WireCell::NamedFactoryRegistry< IType >::lookup_factory ( const std::string classname)
inline

Look up an existing factory by the name of the "class" it can create.

Definition at line 95 of file NamedFactory.h.

95  {
96  if (classname == "") {
97  l->error("no class name given for type \"{}\"",
98  demangle(typeid(IType).name()));
99  return nullptr;
100  }
101 
102  auto it = m_lookup.find(classname);
103  if (it != m_lookup.end()) {
104  return it->second;
105  }
106 
107  // cache miss, try plugin
108 
110 
111  std::string factory_maker = "make_" + classname + "_factory";
112  auto plugin = pm.find(factory_maker.c_str());
113  if (!plugin) {
114  l->error("no plugin for \"{}\"", classname);
115  return nullptr;
116  }
117 
118  typedef void* (*maker_function)();
119  maker_function mf;
120  if (!plugin->symbol(factory_maker.c_str(), mf)) {
121  l->error("no factory maker symbol for \"{}\"", classname);
122  return nullptr;
123  }
124 
125  void* fac_void_ptr = mf();
126 
127  if (!fac_void_ptr) {
128  l->error("no factory for \"{}\"", classname);
129  return nullptr;
130  }
131 
132  factory_ptr fptr = reinterpret_cast<factory_ptr>(fac_void_ptr);
133  m_lookup[classname] = fptr;
134  return fptr;
135  }
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
WireCell::INamedFactory * factory_ptr
Definition: NamedFactory.h:76
static PluginManager & instance()
Plugin * find(const std::string &symbol_name)
std::string demangle(const std::string &name)
Definition: Type.cxx:6

Member Data Documentation

template<class IType>
Log::logptr_t WireCell::NamedFactoryRegistry< IType >::l
private

Definition at line 72 of file NamedFactory.h.

template<class IType>
known_type_set WireCell::NamedFactoryRegistry< IType >::m_known_types
private

Definition at line 202 of file NamedFactory.h.

template<class IType>
factory_lookup WireCell::NamedFactoryRegistry< IType >::m_lookup
private

Definition at line 201 of file NamedFactory.h.


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