ATLAS Offline Software
AlgFactory.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
2 #ifndef L1TopoInterfaces_AlgFactory
3 #define L1TopoInterfaces_AlgFactory
4 
6 
7 #include <string>
8 #include <vector>
9 #include <map>
10 #include <memory>
11 
12 namespace TCS {
13 
14  class ConfigurableAlg;
15 
16  class AlgFactory {
17  public:
19  typedef std::unique_ptr<ConfigurableAlg> (*Creator)(const std::string & methodName);
20 
21  static const AlgFactory& instance();
22  static AlgFactory& mutable_instance ATLAS_NOT_THREAD_SAFE();
23 
25  bool Register( const std::string &algType, Creator creator );
26 
28  ConfigurableAlg* create(const std::string & algType, const std::string & algName);
29 
31  ConfigurableAlg* algorithm(const std::string & algName);
32 
33  std::vector<std::string> getAllClassNames() const;
34  void printAlgorithmNames() const;
35 
36  private:
37  AlgFactory() = default; //<! singleton created via instance()
38  AlgFactory(const AlgFactory&) = delete;
39  AlgFactory& operator=(const AlgFactory&) = delete;
40 
41  std::map<std::string, Creator> m_callMap;
42  std::map<std::string, std::unique_ptr<TCS::ConfigurableAlg>> m_algs;
43  };
44 
45 }
46 
47 // Macro to register algorithm in arbitrary namespace
48 #define REGISTER_ALG_NS(NS,CLASS) \
49  namespace { \
50  std::unique_ptr<TCS::ConfigurableAlg> Create##NS__##CLASS(const std::string & methodName) { \
51  auto alg = std::make_unique<NS::CLASS>(methodName); \
52  alg->setClassName( std::string(#NS) + "__" + #CLASS ); \
53  alg->msg().setName( alg->fullname() ); \
54  return alg; \
55  } \
56  \
57  const bool success = TCS::AlgFactory::mutable_instance().Register( std::string(#NS) + "__" + #CLASS, Create##NS__##CLASS); \
58  }
59 
60 
61 // Macro to register algorithm in TCS namespace
62 #define REGISTER_ALG_TCS(CLASS) \
63  namespace { \
64  std::unique_ptr<TCS::ConfigurableAlg> Create##CLASS(const std::string & methodName) { \
65  auto alg = std::make_unique<TCS::CLASS>(methodName); \
66  alg->setClassName( #CLASS ); \
67  alg->msg().setName( alg->fullname() ); \
68  return alg; \
69  } \
70  \
71  const bool success = TCS::AlgFactory::mutable_instance().Register( #CLASS, Create##CLASS); \
72  }
73 
74 
75 #endif
TCS::AlgFactory::m_callMap
std::map< std::string, Creator > m_callMap
Definition: AlgFactory.h:41
xAOD::JetAlgorithmType::algName
const std::string & algName(ID id)
Converts a JetAlgorithmType::ID into a string.
Definition: JetContainerInfo.cxx:67
TCS::AlgFactory::printAlgorithmNames
void printAlgorithmNames() const
Definition: AlgFactory.cxx:77
TCS::AlgFactory::getAllClassNames
std::vector< std::string > getAllClassNames() const
Definition: AlgFactory.cxx:56
TCS::AlgFactory::operator=
AlgFactory & operator=(const AlgFactory &)=delete
no assign
TCS::AlgFactory::instance
static const AlgFactory & instance()
read-only access
Definition: AlgFactory.cxx:7
TCS::AlgFactory::ATLAS_NOT_THREAD_SAFE
static AlgFactory &mutable_instance ATLAS_NOT_THREAD_SAFE()
non-const access
TCS::AlgFactory::algorithm
ConfigurableAlg * algorithm(const std::string &algName)
Retrieve algorithm by name.
Definition: AlgFactory.cxx:66
TCS::AlgFactory::AlgFactory
AlgFactory()=default
TCS::AlgFactory::m_algs
std::map< std::string, std::unique_ptr< TCS::ConfigurableAlg > > m_algs
Definition: AlgFactory.h:42
TCS::ConfigurableAlg
Definition: ConfigurableAlg.h:30
TCS::AlgFactory::Register
bool Register(const std::string &algType, Creator creator)
Register creator function for algorithm type.
Definition: AlgFactory.cxx:46
TCS::AlgFactory::AlgFactory
AlgFactory(const AlgFactory &)=delete
no copy
TCS::AlgFactory::create
ConfigurableAlg * create(const std::string &algType, const std::string &algName)
Create algorithm of given type and name.
Definition: AlgFactory.cxx:21
TCS::AlgFactory::Creator
std::unique_ptr< ConfigurableAlg >(* Creator)(const std::string &methodName)
function type to create an algorithm
Definition: AlgFactory.h:19
TCS
Definition: Global/GlobalSimulation/src/IO/Decision.h:18
checker_macros.h
Define macros for attributes used to control the static checker.
TCS::AlgFactory
Definition: AlgFactory.h:16