fire v0.19.0
Framework for sImulation and Reconstruction of Events
Factory.h
1#ifndef FIRE_FACTORY_FACTORY_H
2#define FIRE_FACTORY_FACTORY_H
3
4#include <memory> // for the unique_ptr default
5#include <string> // for the keys in the library map
6#include <unordered_map> // for the library of prototypes
7
8#include <boost/core/demangle.hpp> // for demangling
9
10#include "fire/exception/Exception.h"
11
20namespace fire::factory {
21
42void loadLibrary(const std::string& libname);
43
228template <typename Prototype,
229 typename PrototypePtr = std::unique_ptr<Prototype>,
230 typename... PrototypeConstructorArgs>
231class Factory {
232 public:
239 using PrototypeMaker = PrototypePtr (*)(PrototypeConstructorArgs...);
240
241 public:
251 static Factory& get() {
252 static Factory the_factory;
253 return the_factory;
254 }
255
274 template<typename DerivedType>
275 uint64_t declare() {
276 std::string full_name{boost::core::demangle(typeid(DerivedType).name())};
277 library_[full_name] = &maker<DerivedType>;
278 return reinterpret_cast<std::uintptr_t>(&library_);
279 }
280
298 PrototypePtr make(const std::string& full_name,
299 PrototypeConstructorArgs... maker_args) {
300 auto lib_it{library_.find(full_name)};
301 if (lib_it == library_.end()) {
302 throw Exception("Factory","An object named " + full_name +
303 " has not been declared.",false);
304 }
305 return lib_it->second(maker_args...);
306 }
307
309 Factory(Factory const&) = delete;
310
312 void operator=(Factory const&) = delete;
313
314 private:
334 template <typename DerivedType>
335 static PrototypePtr maker(PrototypeConstructorArgs... args) {
336 return PrototypePtr(new DerivedType(std::forward<PrototypeConstructorArgs>(args)...));
337 }
338
340 Factory() = default;
341
344}; // Factory
345
346} // namespace fire::factory
347
348#endif // FACTORY_FACTORY_H
Standard base exception class with some useful output information.
Definition: Exception.h:18
Factory to dynamically create objects derived from a specific prototype class.
Definition: Factory.h:231
static Factory & get()
get the factory instance
Definition: Factory.h:251
Factory(Factory const &)=delete
delete the copy constructor
uint64_t declare()
register a new object to be constructible
Definition: Factory.h:275
std::unordered_map< std::string, PrototypeMaker > library_
library of possible objects to create
Definition: Factory.h:343
static PrototypePtr maker(PrototypeConstructorArgs... args)
make a new DerivedType returning a PrototypePtr
Definition: Factory.h:335
PrototypePtr make(const std::string &full_name, PrototypeConstructorArgs... maker_args)
make a new object by name
Definition: Factory.h:298
PrototypePtr(*)(PrototypeConstructorArgs...) PrototypeMaker
the signature of a function that can be used by this factory to dynamically create a new object.
Definition: Factory.h:239
Factory()=default
private constructor to prevent creation
void operator=(Factory const &)=delete
delete the assignment operator
used to isolate base templated Factory class
Definition: Factory.h:20
void loadLibrary(const std::string &libname)
load a library by name
Definition: Factory.cxx:9