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>
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}
34
35template <typename T>
36std::shared_ptr<T> AbstractFactory<T>::create_processor(const std::string& processor_name) {
37 name_creator_map& creators = get_creators();
38 auto it = creators.find(processor_name);
39
40 if (it != creators.end()) {
41 return it->second();
42 }
44 throw std::runtime_error("Factory failed to find " + processor_name);
45}
46
47template <typename T>
48std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::get_instance()
49{
50 if (s_single_factory == nullptr) {
51 s_single_factory = std::make_shared<AbstractFactory<T>>();
52 }
54 return s_single_factory;
55}
56
57} // namespace tpglibs
58
59#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.