1#ifndef SIMCORE_FACTORY_H
2#define SIMCORE_FACTORY_H
5#include <boost/core/demangle.hpp>
8#include <unordered_map>
10#include "Framework/Exception/Exception.h"
195template <
typename Prototype,
typename PrototypePtr,
196 typename... PrototypeConstructorArgs>
240 template <
typename DerivedType>
242 std::string full_name{boost::core::demangle(
typeid(DerivedType).name())};
243 library_[full_name] = &maker<DerivedType>;
244 return reinterpret_cast<std::uintptr_t
>(&
library_);
265 PrototypePtr
make(
const std::string& full_name,
266 PrototypeConstructorArgs... maker_args) {
267 auto lib_it{
library_.find(full_name)};
269 EXCEPTION_RAISE(
"SimFactory",
"An object named " + full_name +
270 " has not been declared.");
272 warehouse_.emplace_back(lib_it->second(maker_args...));
282 template <
class UnaryFunction>
313 template <
typename DerivedType>
314 static PrototypePtr
maker(PrototypeConstructorArgs... args) {
316 new DerivedType(std::forward<PrototypeConstructorArgs>(args)...));
323 std::unordered_map<std::string, PrototypeMaker>
library_;
Factory to dynamically create objects derived from a specific prototype class.
uint64_t declare()
register a new object to be constructible
void operator=(Factory const &)=delete
delete the assignment operator
std::vector< PrototypePtr > warehouse_
warehouse of objects that have already been created
PrototypePtr(*)(PrototypeConstructorArgs...) PrototypeMaker
the signature of a function that can be used by this factory to dynamically create a new object.
void apply(UnaryFunction f) const
Apply the input UnaryFunction to each entry in the inventory.
static PrototypePtr maker(PrototypeConstructorArgs... args)
make a new DerivedType returning a PrototypePtr
static Factory & get()
get the factory instance
Factory(Factory const &)=delete
delete the copy constructor
PrototypePtr make(const std::string &full_name, PrototypeConstructorArgs... maker_args)
make a new object by name
Factory()=default
private constructor to prevent creation
std::unordered_map< std::string, PrototypeMaker > library_
library of possible objects to create