DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
AbstractFactory.hxx
Go to the documentation of this file.
1
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>
19 static name_creator_map s_creators;
20 return s_creators;
21}
22
23template <typename T>
24void AbstractFactory<T>::register_creator(const std::string& processor_name, create_processor_func creator) {
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}
35
36template <typename T>
37std::shared_ptr<T> AbstractFactory<T>::create_processor(const std::string& processor_name) {
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}
48
49template <typename T>
50std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::get_instance()
51{
52 if (s_single_factory == nullptr) {
53 s_single_factory = std::make_shared<AbstractFactory<T>>();
54 }
55
56 return s_single_factory;
57}
58
59} // namespace tpglibs
60
61#endif // TPGLIBS_ABSTRACTFACTORY_HXX_
std::unordered_map< std::string, create_processor_func > name_creator_map
Map from processor name to processor creation function.
static std::shared_ptr< AbstractFactory< T > > s_single_factory
Singleton instance.
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.