Skip to content
Snippets Groups Projects
Commit c0e5c4f9 authored by Marco Clemencic's avatar Marco Clemencic
Browse files

Add support to ToolHandle for const tool interfaces

Allow use const tool interfaces as in
```.cpp
ToolHandle<const IMyTool> m_myTool{"MyTool", this};
```

See merge request !215
parents 8db3eb0e 1557d5e6
Branches
Tags
1 merge request!215Add support to ToolHandle for const tool interfaces
......@@ -11,6 +11,7 @@
#include <vector>
#include <stdexcept>
#include <iostream>
#include <type_traits>
class GAUDI_API GaudiHandleInfo {
protected:
......@@ -63,15 +64,15 @@ public:
protected:
/** The component type */
void setComponentType(const std::string& componentType) {
m_componentType = componentType;
}
/** The name of the parent */
void setParentName(const std::string& parent) {
m_parentName = parent;
}
/** The component type */
void setComponentType(const std::string& componentType) {
m_componentType = componentType;
}
/** The name of the parent */
void setParentName(const std::string& parent) {
m_parentName = parent;
}
private:
//
......@@ -268,19 +269,21 @@ public:
}
std::string getDefaultName() {
std::string defName = GaudiHandleBase::type();
if ( defName.empty() ) defName = getDefaultType();
return defName;
const auto defName = GaudiHandleBase::type();
return ( defName.empty() ? getDefaultType() : defName );
}
protected:
/** Retrieve the component. To be implemented by the derived class. It will pass the pointer */
virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
/** Release the component. Default implementation calls release() on the component.
Can be overridden by the derived class if something else if needed. */
Can be overridden by the derived class if something else is needed. */
virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
comp->release();
// const cast to support T being a const type
auto * c = const_cast< typename std::remove_const<T>::type * >(comp);
c->release();
return StatusCode::SUCCESS;
}
......
......@@ -29,6 +29,7 @@ protected:
{}
public:
virtual ~ToolHandleInfo() = default;
bool isPublic() const { return !m_parent; }
......@@ -40,19 +41,22 @@ public:
//
// Some helper functions
//
static std::string toolComponentType(const IInterface* parent) {
static std::string toolComponentType(const IInterface* parent)
{
return parent ? "PrivateTool" : "PublicTool";
}
static std::string toolParentName(const IInterface* parent) {
static std::string toolParentName(const IInterface* parent)
{
if (!parent) return "ToolSvc";
const INamedInterface* pNamed = dynamic_cast<const INamedInterface*>(parent);
return pNamed ? pNamed->name() : "";
}
protected:
const IInterface* m_parent;
bool m_createIf;
const IInterface* m_parent = nullptr;
bool m_createIf{true};
};
......@@ -73,13 +77,14 @@ protected:
virtual StatusCode i_retrieve(IAlgTool*&) const = 0;
public:
virtual ~BaseToolHandle() {}
StatusCode retrieve(IAlgTool*& tool) const {
return i_retrieve(tool);
}
virtual IAlgTool* get() const = 0;
virtual IAlgTool * get() const = 0;
virtual std::string typeAndName() const = 0;
};
......@@ -155,7 +160,8 @@ public:
public:
StatusCode initialize(const std::string& toolTypeAndName,
const IInterface* parent = nullptr, bool createIf = true){
const IInterface* parent = nullptr, bool createIf = true)
{
GaudiHandleBase::setTypeAndName(toolTypeAndName);
GaudiHandleBase::setComponentType(toolComponentType(parent));
......@@ -164,9 +170,7 @@ public:
m_parent = parent;
m_createIf = createIf;
StatusCode sc = m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
return sc;
return m_pToolSvc.initialize("ToolSvc", GaudiHandleBase::parentName());
}
/** Retrieve the AlgTool. Release existing tool if needed.
......@@ -189,22 +193,36 @@ public:
}
/** Do the real release of the AlgTool. */
StatusCode release( T* algTool ) const override {
return m_pToolSvc->releaseTool( algTool );
StatusCode release( T* algTool ) const override
{
return m_pToolSvc->releaseTool( nonConst(algTool) );
}
IAlgTool *get() const override {
return GaudiHandle<T>::get();
IAlgTool * get() const override
{
// const cast to support T being const
return nonConst( GaudiHandle<T>::get() );
}
std::string typeAndName() const override {
return GaudiHandleBase::typeAndName();
}
private:
template< class TOOL >
typename std::remove_const<TOOL>::type * nonConst( TOOL* algTool ) const
{
return const_cast< typename std::remove_const<TOOL>::type * >( algTool );
}
protected:
StatusCode i_retrieve(IAlgTool*& algTool) const override {
StatusCode i_retrieve(IAlgTool*& algTool) const override
{
return m_pToolSvc->retrieve( typeAndName(), IAlgTool::interfaceID(),
algTool,
ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
algTool,
ToolHandleInfo::parent(), ToolHandleInfo::createIf() );
}
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment