DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
AbstractFactory.hxx
Go to the documentation of this file.
1
8
9#ifndef TPGLIBS_ABSTRACTFACTORY_HXX_
10#define TPGLIBS_ABSTRACTFACTORY_HXX_
11
12namespace tpglibs {
13
14template<typename T>
15std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::s_single_factory = nullptr;
16
17template<typename T>
20{
21 static name_creator_map s_creators;
22 return s_creators;
23}
24
25template<typename T>
26void
27AbstractFactory<T>::register_creator(const std::string& processor_name, create_processor_func creator)
28{
29 name_creator_map& creators = get_creators();
30 auto it = creators.find(processor_name);
31
32 if (it == creators.end()) {
33 creators[processor_name] = creator;
34 return;
35 }
36 throw std::runtime_error("Attempted to overwrite a creator in factory with " + processor_name);
37}
38
39template<typename T>
40std::shared_ptr<T>
41AbstractFactory<T>::create_processor(const std::string& processor_name)
42{
43 name_creator_map& creators = get_creators();
44 auto it = creators.find(processor_name);
45
46 if (it != creators.end()) {
47 return it->second();
48 }
49
50 throw std::runtime_error("Factory failed to find " + processor_name);
52
53template<typename T>
54std::shared_ptr<AbstractFactory<T>>
56{
57 if (s_single_factory == nullptr) {
58 s_single_factory = std::make_shared<AbstractFactory<T>>();
59 }
60
61 return s_single_factory;
63
64} // namespace tpglibs
65
66#endif // TPGLIBS_ABSTRACTFACTORY_HXX_
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.
std::function< std::shared_ptr< T >()> create_processor_func
Function that creates shared_ptrs of type T.
static std::shared_ptr< AbstractFactory< T > > get_instance()
Singleton get instance function.
std::shared_ptr< T > create_processor(const std::string &processor_name)
Create the requested processor.
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 > > s_single_factory
Singleton instance.