Commit 2776170c authored by Nils Krumnack's avatar Nils Krumnack
Browse files

introduce AsgToolConfig::makePrivateTool()

The goal is to ensure there is a proper cleanup of the tool created
(within AnalysisBase), so that the AsgToolConfig-based approach can be
used to replace AnaToolHandle.
parent e230b6bd
......@@ -225,6 +225,23 @@ namespace asg
EXPECT_EQ (19, tool->getArray()[0]->getPropertyInt());
EXPECT_EQ (22, tool->getArray()[1]->getPropertyInt());
}
TEST (AsgToolConfigTest, makePrivateTool)
{
const std::string name1 = makeUniqueName();
AsgToolConfig config1 ("asg::UnitTestTool1A/" + name1);
std::shared_ptr<void> cleanup1;
ToolHandle<IUnitTestTool1> tool1;
ASSERT_SUCCESS (config1.makeTool (tool1, cleanup1));
EXPECT_EQ ("ToolSvc." + name1, tool1->name());
AsgToolConfig config2 ("asg::UnitTestTool1A/myPrivateTool");
ToolHandle<IUnitTestTool1> tool2 ("", &*tool1);
ASSERT_SUCCESS (config2.makePrivateTool (tool2));
EXPECT_EQ ("ToolSvc." + name1 + ".myPrivateTool", tool2->name());
}
}
ATLAS_GOOGLE_TEST_MAIN
......@@ -64,10 +64,10 @@ namespace asg
/// the config.
///
/// \par Guarantee
/// strong
/// basic
/// \par Failures
/// configuration errors\n
/// algorithm creation/initialization errors
/// tool creation/initialization errors
public:
template<typename T> ::StatusCode
makeTool (ToolHandle<T>& toolHandle,
......@@ -75,6 +75,28 @@ namespace asg
bool allowNestedName = false) const;
/// \brief make a private tool with the given configuration
///
/// This requires a parent pointer to be set on the ToolHandle to
/// work, and for the parent to derive from AsgComponent in
/// AnalysisBase. That allows to attach the cleanup of the tool
/// to the parent component.
///
/// For practical purposes, when using an AsgToolConfig to create
/// a private tool owned by a parent tool/algorithm inside the
/// initialize of the tool/algorithm, this is the preferred way of
/// doing so.
///
/// \par Guarantee
/// basic
/// \par Failures
/// configuration errors\n
/// tool creation/initialization errors
public:
template<typename T> ::StatusCode
makePrivateTool (ToolHandle<T>& toolHandle) const;
//
// private interface
......@@ -110,6 +132,31 @@ namespace asg
return StatusCode::SUCCESS;
}
template<typename T> ::StatusCode AsgToolConfig ::
makePrivateTool (ToolHandle<T>& toolHandle) const
{
using namespace msgComponentConfig;
INamedInterface *parentNamed = toolHandle.parent();
if (parentNamed == nullptr)
{
ANA_MSG_ERROR ("need to pass private ToolHandle into makePrivateTool");
return StatusCode::FAILURE;
}
AsgComponent *parentComponent = dynamic_cast<AsgComponent*>(parentNamed);
if (parentComponent == nullptr)
{
ANA_MSG_ERROR ("parent \"" << parentNamed->name() << "\" for makePrivateTool() does not inherit from AsgComponent");
return StatusCode::FAILURE;
}
std::shared_ptr<void> cleanup;
if (makeTool (toolHandle, cleanup).isFailure())
return StatusCode::FAILURE;
parentComponent->addCleanup (cleanup);
return StatusCode::SUCCESS;
}
#else
template<typename T> ::StatusCode AsgToolConfig ::
......@@ -129,6 +176,17 @@ namespace asg
return StatusCode::SUCCESS;
}
template<typename T> ::StatusCode AsgToolConfig ::
makePrivateTool (ToolHandle<T>& toolHandle) const
{
using namespace msgComponentConfig;
// This is safe, because the `makeTool` function in athena does
// not use the cleanup argument.
std::shared_ptr<void> cleanup;
return makeTool (toolHandle, cleanup);
}
#endif
}
......
......@@ -44,6 +44,8 @@ public:
const std::string& name() const;
/// Return the tool's parent tool name
const std::string& parentName() const;
/// Return a pointer to the tool's parent
INamedInterface *parent() const noexcept;
/// Change the handle's type and name
void setTypeAndName(const std::string& typeAndName);
......
......@@ -54,3 +54,7 @@ const std::string& ToolHandleBase::parentName() const {
static const std::string toolSvcName = "ToolSvc";
return toolSvcName;
}
INamedInterface *ToolHandleBase ::parent() const noexcept {
return m_parent;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment