6#ifndef FRAMEWORK_FACTORY_H
7#define FRAMEWORK_FACTORY_H
14#include <unordered_map>
17#include "Framework/Exception/Exception.h"
179template <
typename Prototype,
typename PrototypePtr,
180 typename... PrototypeConstructorArgs>
210 template <
typename DerivedType>
211 uint64_t
declare(
const std::string& derived_type_name) {
213 return reinterpret_cast<std::uintptr_t
>(&
library_);
233 [[nodiscard]]
virtual std::optional<PrototypePtr>
make(
234 const std::string& full_name, PrototypeConstructorArgs... maker_args) {
235 auto lib_it{
library_.find(full_name)};
239 return lib_it->second(maker_args...);
274 template <
typename DerivedType>
275 static PrototypePtr
maker(PrototypeConstructorArgs... args) {
277 new DerivedType(std::forward<PrototypeConstructorArgs>(args)...));
281 std::unordered_map<std::string, PrototypeMaker>
library_;
297template <
typename Prototype,
typename PrototypePtr,
298 typename... PrototypeConstructorArgs>
300 :
public Factory<Prototype, PrototypePtr, PrototypeConstructorArgs...> {
303 Factory<Prototype, PrototypePtr, PrototypeConstructorArgs...>;
323 [[nodiscard]] std::optional<PrototypePtr>
make(
324 const std::string& full_name,
325 PrototypeConstructorArgs... maker_args)
override {
343 template <
class UnaryFunction>
377#define DECLARE_FACTORY(...) \
378 struct Factory : public ::framework::Factory<__VA_ARGS__> { \
379 static Factory& get(); \
395#define DECLARE_FACTORY_WITH_WAREHOUSE(...) \
396 struct Factory : public ::framework::FactoryWithWarehouse<__VA_ARGS__> { \
397 static Factory& get(); \
411#define DEFINE_FACTORY(classtype) \
412 classtype::Factory& classtype::Factory::get() { \
413 static classtype::Factory the_factory; \
414 return the_factory; \
425#define _CONCAT_INTERNAL(a, b) a##b
435#define _CONCAT(a, b) _CONCAT_INTERNAL(a, b)
444#define UNIQUE(a) _CONCAT(a, __COUNTER__)
469#define FACTORY_REGISTRATION(prototype, derived) \
471 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.