DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
tpglibs::AbstractFactory< T > Class Template Reference

General singleton, abstract factory. More...

#include <AbstractFactory.hpp>

Public Member Functions

 AbstractFactory ()
 
 AbstractFactory (const AbstractFactory &)=delete
 
AbstractFactoryoperator= (const AbstractFactory &)=delete
 
virtual ~AbstractFactory ()=default
 
std::shared_ptr< T > create_processor (const std::string &processor_name)
 Create the requested processor.
 

Static Public Member Functions

static void register_creator (const std::string &processor_name, create_processor_func creator)
 Register the processor creation function to a given name.
 
static std::shared_ptr< AbstractFactory< T > > get_instance ()
 Singleton get instance function.
 

Static Protected Attributes

static std::shared_ptr< AbstractFactory< T > > s_single_factory = nullptr
 Singleton instance.
 

Private Types

using create_processor_func = std::function<std::shared_ptr<T>()>
 Function that creates shared_ptrs of type T.
 
using name_creator_map = std::unordered_map<std::string, create_processor_func>
 Map from processor name to processor creation function.
 

Static Private Member Functions

static name_creator_mapget_creators ()
 Returns singleton instance of creation map.
 

Detailed Description

template<typename T>
class tpglibs::AbstractFactory< T >

General singleton, abstract factory.

Definition at line 26 of file AbstractFactory.hpp.

Member Typedef Documentation

◆ create_processor_func

template<typename T >
using tpglibs::AbstractFactory< T >::create_processor_func = std::function<std::shared_ptr<T>()>
private

Function that creates shared_ptrs of type T.

Definition at line 28 of file AbstractFactory.hpp.

◆ name_creator_map

template<typename T >
using tpglibs::AbstractFactory< T >::name_creator_map = std::unordered_map<std::string, create_processor_func>
private

Map from processor name to processor creation function.

Definition at line 30 of file AbstractFactory.hpp.

Constructor & Destructor Documentation

◆ AbstractFactory() [1/2]

template<typename T >
tpglibs::AbstractFactory< T >::AbstractFactory ( )
inline

Definition at line 33 of file AbstractFactory.hpp.

33{}

◆ AbstractFactory() [2/2]

template<typename T >
tpglibs::AbstractFactory< T >::AbstractFactory ( const AbstractFactory< T > & )
delete

◆ ~AbstractFactory()

template<typename T >
virtual tpglibs::AbstractFactory< T >::~AbstractFactory ( )
virtualdefault

Member Function Documentation

◆ create_processor()

template<typename T >
std::shared_ptr< T > tpglibs::AbstractFactory< T >::create_processor ( const std::string & processor_name)

Create the requested processor.

Parameters
processor_nameName of the processor being created.
Returns
shared_ptr of the requested processor. nullptr if it doesn't exist/isn't registered.

Definition at line 37 of file AbstractFactory.hxx.

37 {
38 name_creator_map& creators = get_creators();
39 auto it = creators.find(processor_name);
40
41 if (it != creators.end()) {
42 return it->second();
43 }
44
45 throw std::runtime_error("Factory failed to find " + processor_name);
46 return nullptr;
47}
std::unordered_map< std::string, create_processor_func > name_creator_map
Map from processor name to processor creation function.
static name_creator_map & get_creators()
Returns singleton instance of creation map.

◆ get_creators()

template<typename T >
AbstractFactory< T >::name_creator_map & tpglibs::AbstractFactory< T >::get_creators ( )
staticprivate

Returns singleton instance of creation map.

Definition at line 18 of file AbstractFactory.hxx.

18 {
19 static name_creator_map s_creators;
20 return s_creators;
21}

◆ get_instance()

template<typename T >
std::shared_ptr< AbstractFactory< T > > tpglibs::AbstractFactory< T >::get_instance ( )
static

Singleton get instance function.

Definition at line 50 of file AbstractFactory.hxx.

51{
52 if (s_single_factory == nullptr) {
53 s_single_factory = std::make_shared<AbstractFactory<T>>();
54 }
55
56 return s_single_factory;
57}
static std::shared_ptr< AbstractFactory< T > > s_single_factory
Singleton instance.

◆ operator=()

template<typename T >
AbstractFactory & tpglibs::AbstractFactory< T >::operator= ( const AbstractFactory< T > & )
delete

◆ register_creator()

template<typename T >
void tpglibs::AbstractFactory< T >::register_creator ( const std::string & processor_name,
create_processor_func creator )
static

Register the processor creation function to a given name.

Parameters
processor_nameString name of the processor to be registered as.
creatorProcessor creation function.

Definition at line 24 of file AbstractFactory.hxx.

24 {
25 name_creator_map& creators = get_creators();
26 auto it = creators.find(processor_name);
27
28 if (it == creators.end()) {
29 creators[processor_name] = creator;
30 return;
31 }
32 throw std::runtime_error("Attempted to overwrite a creator in factory with " + processor_name);
33 return;
34}

Member Data Documentation

◆ s_single_factory

template<typename T >
std::shared_ptr< AbstractFactory< T > > tpglibs::AbstractFactory< T >::s_single_factory = nullptr
staticprotected

Singleton instance.

Definition at line 57 of file AbstractFactory.hpp.


The documentation for this class was generated from the following files: