ATLAS Offline Software
AsgToolConfig.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 #ifndef ASG_TOOLS_ASG_TOOL_CONFIG_H
10 #define ASG_TOOLS_ASG_TOOL_CONFIG_H
11 
13 #include <AsgTools/ToolHandle.h>
14 
15 namespace asg
16 {
17  class AsgTool;
18 
20 
22  {
23  //
24  // public interface
25  //
26 
32  public:
33  AsgToolConfig () = default;
34 
35 
41  public:
42  explicit AsgToolConfig (const std::string& val_typeAndName);
43 
44 
50  public:
51  explicit AsgToolConfig (const AsgComponentConfig& val_config);
52 
53 
80  public:
81  template<typename T> ::StatusCode
82  makeTool (ToolHandle<T>& toolHandle,
83  std::shared_ptr<void>& cleanup,
84  bool allowNestedName = false) const;
85 
86 
104  public:
105  template<typename T> ::StatusCode
106  makePrivateTool (ToolHandle<T>& toolHandle) const;
107 
108 
109 
110  //
111  // private interface
112  //
113  };
114 
115 
116 
117  //
118  // template methods
119  //
120 
121 #ifdef XAOD_STANDALONE
122 
123  template<typename T> ::StatusCode AsgToolConfig ::
124  makeTool (ToolHandle<T>& toolHandle,
125  std::shared_ptr<void>& cleanup,
126  bool allowNestedName) const
127  {
128  using namespace msgComponentConfig;
129 
130  std::string prefix = toolHandle.parentName() + ".";
131 
132  std::unique_ptr<T> tool;
133  ANA_CHECK (makeComponentExpert (tool, "new %1% (\"%2%\")", allowNestedName, prefix));
134  ANA_CHECK (tool->initialize());
135 
136  std::shared_ptr<T> mycleanup (tool.release());
137  toolHandle = mycleanup.get();
138  cleanup = mycleanup;
139 
140  ANA_MSG_DEBUG ("Created component of type " << type());
141  return StatusCode::SUCCESS;
142  }
143 
144  template<typename T> ::StatusCode AsgToolConfig ::
145  makePrivateTool (ToolHandle<T>& toolHandle) const
146  {
147  using namespace msgComponentConfig;
148 
149  INamedInterface *parentNamed = toolHandle.parent();
150  if (parentNamed == nullptr)
151  {
152  ANA_MSG_ERROR ("need to pass private ToolHandle into makePrivateTool");
153  return StatusCode::FAILURE;
154  }
155  AsgComponent *parentComponent = dynamic_cast<AsgComponent*>(parentNamed);
156  if (parentComponent == nullptr)
157  {
158  ANA_MSG_ERROR ("parent \"" << parentNamed->name() << "\" for makePrivateTool() does not inherit from AsgComponent");
159  return StatusCode::FAILURE;
160  }
161 
162  std::shared_ptr<void> cleanup;
163  if (makeTool (toolHandle, cleanup).isFailure())
164  return StatusCode::FAILURE;
165  parentComponent->addCleanup (cleanup);
166  return StatusCode::SUCCESS;
167  }
168 
169 #else
170 
171  template<typename T> ::StatusCode AsgToolConfig ::
172  makeTool (ToolHandle<T>& toolHandle,
173  std::shared_ptr<void>& /*cleanup*/,
174  bool allowNestedName) const
175  {
176  using namespace msgComponentConfig;
177 
178  std::string prefix = toolHandle.parentName() + ".";
179 
180  ANA_CHECK (configureComponentExpert (prefix, allowNestedName));
181  toolHandle.setTypeAndName (type() + "/" + name());
182  ANA_CHECK (toolHandle.retrieve());
183 
184  ANA_MSG_DEBUG ("Created component of type " << type());
185  return StatusCode::SUCCESS;
186  }
187 
188  template<typename T> ::StatusCode AsgToolConfig ::
189  makePrivateTool (ToolHandle<T>& toolHandle) const
190  {
191  using namespace msgComponentConfig;
192 
193  // This is safe, because the `makeTool` function in athena does
194  // not use the cleanup argument.
195  std::shared_ptr<void> cleanup;
196  return makeTool (toolHandle, cleanup);
197  }
198 
199 #endif
200 
201 }
202 
203 #endif
asg::AsgComponentConfig
an object that stores the configuration for an AsgComponent and is able to create one from it
Definition: AsgComponentConfig.h:34
AsgComponentConfig.h
asg
Definition: DataHandleTestTool.h:28
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
asg::AsgToolConfig::makePrivateTool
::StatusCode makePrivateTool(ToolHandle< T > &toolHandle) const
make a private tool with the given configuration
asg::AsgToolConfig
an object that can create a AsgTool
Definition: AsgToolConfig.h:22
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
asg::AsgComponentConfig::type
const std::string & type() const noexcept
the type of the component
Definition: AsgComponentConfig.cxx:74
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
asg::AsgComponentConfig::configureComponentExpert
StatusCode configureComponentExpert(const std::string &prefix, bool nestedNames) const
add component configuration to configuration service (expert only)
Definition: AsgComponentConfig.cxx:365
asg::AsgToolConfig::AsgToolConfig
AsgToolConfig()=default
standard constructor
asg::AsgComponentConfig::name
const std::string & name() const noexcept
the name of the component
Definition: AsgComponentConfig.cxx:90
ToolHandle.h
asg::AsgToolConfig::makeTool
::StatusCode makeTool(ToolHandle< T > &toolHandle, std::shared_ptr< void > &cleanup, bool allowNestedName=false) const
make a tool with the given configuration
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288