6#ifndef FRAMEWORK_FACTORY_H
7#define FRAMEWORK_FACTORY_H
13#include <unordered_map>
176template <
typename Prototype,
typename PrototypePtr,
177 typename... PrototypeConstructorArgs>
207 template <
typename DerivedType>
208 uint64_t
declare(
const std::string& derived_type_name) {
210 return reinterpret_cast<std::uintptr_t
>(&
library_);
230 [[nodiscard]]
virtual std::optional<PrototypePtr>
make(
231 const std::string& full_name, PrototypeConstructorArgs... maker_args) {
232 auto lib_it{
library_.find(full_name)};
236 return lib_it->second(maker_args...);
271 template <
typename DerivedType>
272 static PrototypePtr
maker(PrototypeConstructorArgs... args) {
274 new DerivedType(std::forward<PrototypeConstructorArgs>(args)...));
278 std::unordered_map<std::string, PrototypeMaker>
library_;
294template <
typename Prototype,
typename PrototypePtr,
295 typename... PrototypeConstructorArgs>
297 :
public Factory<Prototype, PrototypePtr, PrototypeConstructorArgs...> {
300 Factory<Prototype, PrototypePtr, PrototypeConstructorArgs...>;
320 [[nodiscard]] std::optional<PrototypePtr>
make(
321 const std::string& full_name,
322 PrototypeConstructorArgs... maker_args)
override {
340 template <
class UnaryFunction>
374#define DECLARE_FACTORY(...) \
375 struct Factory : public ::framework::Factory<__VA_ARGS__> { \
376 static Factory& get(); \
392#define DECLARE_FACTORY_WITH_WAREHOUSE(...) \
393 struct Factory : public ::framework::FactoryWithWarehouse<__VA_ARGS__> { \
394 static Factory& get(); \
408#define DEFINE_FACTORY(classtype) \
409 classtype::Factory& classtype::Factory::get() { \
410 static classtype::Factory the_factory; \
411 return the_factory; \
422#define _CONCAT_INTERNAL(a, b) a##b
432#define _CONCAT(a, b) _CONCAT_INTERNAL(a, b)
441#define UNIQUE(a) _CONCAT(a, __COUNTER__)
466#define FACTORY_REGISTRATION(prototype, derived) \
468 auto UNIQUE(v) = ::prototype::Factory::get().declare<::derived>(#derived); \
A Factory with a warehouse to hold created objects.
FactoryWithWarehouse(FactoryWithWarehouse const &)=delete
delete the copy constructor
FactoryWithWarehouse()=default
default constructor that does nothing
void operator=(FactoryWithWarehouse const &)=delete
delete the assignment operator
void apply(UnaryFunction f) const
Apply the input UnaryFunction to each entry in the warehouse.
std::optional< PrototypePtr > make(const std::string &full_name, PrototypeConstructorArgs... maker_args) override
make a new object by name
std::vector< PrototypePtr > warehouse_
warehouse of objects that have already been created
Factory to dynamically create objects derived from a specific prototype class.
void operator=(Factory const &)=delete
delete the assignment operator
PrototypePtr(*)(PrototypeConstructorArgs...) PrototypeMaker
the signature of a function that can be used by this factory to dynamically create a new object.
Factory(Factory const &)=delete
delete the copy constructor
std::unordered_map< std::string, PrototypeMaker > library_
library of possible objects to create
virtual ~Factory()=default
default destructor that is virtual for Warehouse override
uint64_t declare(const std::string &derived_type_name)
register a new object to be constructible
virtual std::optional< PrototypePtr > make(const std::string &full_name, PrototypeConstructorArgs... maker_args)
make a new object by name
static PrototypePtr maker(PrototypeConstructorArgs... args)
make a new DerivedType returning a PrototypePtr
Factory()=default
default constructor that does nothing
All classes in the ldmx-sw project use this namespace.