1#ifndef FIRE_FACTORY_FACTORY_H
2#define FIRE_FACTORY_FACTORY_H
6#include <unordered_map>
8#include <boost/core/demangle.hpp>
10#include "fire/exception/Exception.h"
228template <
typename Prototype,
230 typename... PrototypeConstructorArgs>
274 template<
typename DerivedType>
276 std::string full_name{boost::core::demangle(
typeid(DerivedType).name())};
277 library_[full_name] = &maker<DerivedType>;
299 PrototypeConstructorArgs... maker_args) {
300 auto lib_it{
library_.find(full_name)};
302 throw Exception(
"Factory",
"An object named " + full_name +
303 " has not been declared.",
false);
305 return lib_it->second(maker_args...);
334 template <
typename DerivedType>
335 static PrototypePtr
maker(PrototypeConstructorArgs... args) {
336 return PrototypePtr(
new DerivedType(std::forward<PrototypeConstructorArgs>(args)...));
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