DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
AbstractFactory.hxx
Go to the documentation of this file.
1/* @file: AbstractFactory.hxx
2 *
3 * This is part of the DUNE DAQ Application Framework, copyright 2023.
4 * Licensing/copyright details are in the COPYING file that you should have
5 * received with this code.
6 */
7
8#ifndef TRIGGERALGS_ABSTRACT_FACTORY_HXX_
9#define TRIGGERALGS_ABSTRACT_FACTORY_HXX_
10
12
13namespace triggeralgs {
14
15template <typename T>
16std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::s_single_factory = nullptr;
17
18template <typename T>
20 static creation_map s_makers;
21 return s_makers;
22}
23
24template <typename T>
25void AbstractFactory<T>::register_creator(const std::string alg_name, maker_creator creator)
26{
27 creation_map& makers = get_makers();
28 auto it = makers.find(alg_name);
29
30 if (it == makers.end()) {
31 makers[alg_name] = creator;
32 return;
33 }
34 throw FactoryOverwrite(ERS_HERE, alg_name);
35 return;
37
38template <typename T>
39std::unique_ptr<T> AbstractFactory<T>::build_maker(const std::string& alg_name)
40{
41 creation_map& makers = get_makers();
42 auto it = makers.find(alg_name);
43
44 if (it != makers.end()) {
45 TLOG() << "[AF] Factory building " << alg_name << ".";
46 return it->second();
47 }
48
49 throw FactoryNotFound(ERS_HERE, alg_name);
50 return nullptr;
51}
52
53template <typename T>
54std::shared_ptr<AbstractFactory<T>> AbstractFactory<T>::get_instance()
55{
56 if (s_single_factory == nullptr) {
57 s_single_factory = std::make_shared<AbstractFactory<T>>();
58 }
59 return s_single_factory;
60}
61
62} // namespace triggeralgs
63
64#endif // TRIGGERALGS_ABSTRACT_FACTORY_HXX_
#define ERS_HERE
static creation_map & get_makers()
static std::shared_ptr< AbstractFactory< T > > get_instance()
std::unordered_map< std::string, maker_creator > creation_map
static std::shared_ptr< AbstractFactory< T > > s_single_factory
static void register_creator(const std::string alg_name, maker_creator creator)
std::unique_ptr< T > build_maker(const std::string &alg_name)
std::function< std::unique_ptr< T >()> maker_creator
#define TLOG(...)
Definition macro.hpp:22
FactoryNotFound
Definition Issues.hpp:23