The Gaudi Framework  master (37c0b60a)
MyAlgorithm.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "MyAlgorithm.h"
12 #include "IMyTool.h"
13 #include <GaudiKernel/DataObject.h>
15 #include <GaudiKernel/IToolSvc.h>
16 
18 
20 
22  // Keep at least one old-style ToolHandle property to test compilation
23  declareProperty( "LegacyToolHandle", m_legacyToolHandle );
24  declareProperty( "UndefinedToolHandle", m_undefinedToolHandle );
25 
29 }
30 
32  StatusCode sc = TestAlg::initialize();
33  if ( !sc ) return sc;
34  info() << "initializing...." << endmsg;
35 
36  if ( !m_privateToolsOnly ) {
37  sc = toolSvc()->retrieveTool( "MyTool", m_publicTool );
38  if ( sc.isFailure() ) {
39  error() << "Error retrieving the public tool" << endmsg;
40  return sc;
41  }
42  }
43 
44  sc = toolSvc()->retrieveTool( "MyTool", m_privateTool, this );
45  if ( sc.isFailure() ) {
46  error() << "Error retrieving the private tool" << endmsg;
47  return sc;
48  }
49 
50  sc = toolSvc()->retrieveTool( m_privateToolType, "ToolWithName", m_privateToolWithName, this );
51  if ( sc.isFailure() ) {
52  error() << "Error retrieving the private tool with name" << endmsg;
53  return sc;
54  }
55 
56  sc = toolSvc()->retrieveTool( "MyTool", m_privateOtherInterface, this );
57  if ( sc.isFailure() ) {
58  error() << "Error retrieving the private tool with second interface" << endmsg;
59  return sc;
60  }
61 
62  // disable ToolHandle
64 
65  // m_wrongIfaceTool is being retrieved via the wrong interface.
66  // we expect the retrieve() to throw an exception.
67  try {
69  error() << "unable to retrieve " << m_wrongIfaceTool.typeAndName() << " (unexpected)" << endmsg;
71  }
72  } catch ( GaudiException& ex ) {
73  info() << "unable to retrieve " << m_wrongIfaceTool.typeAndName() << " (expected) with exception: " << ex.what()
74  << endmsg;
76  }
77 
78  if ( m_privateToolsOnly ) {
79  // disable all public tools
85  }
86 
87  info() << "....initialization done" << endmsg;
88 
89  return StatusCode::SUCCESS;
90 }
91 
92 //------------------------------------------------------------------------------
94  //------------------------------------------------------------------------------
95  info() << "executing...." << endmsg;
96 
97  if ( !m_privateToolsOnly ) { m_publicTool->doIt(); }
101 
102  info() << "tools created via ToolHandle<T>...." << endmsg;
103 
104  m_myPrivToolHandle->doIt();
105  if ( !m_privateToolsOnly ) {
106  m_myPubToolHandle->doIt();
107  m_myConstToolHandle->doIt();
108  }
109 
110  if ( !m_privateToolsOnly ) {
111  info() << "tools copied assigned via ToolHandle<T>...." << endmsg;
112 
114  m_myCopiedToolHandle->doIt();
116  }
117 
118  info() << "tools copied constructed via ToolHandle<T>...." << endmsg;
119 
120  // copy construct some handles
122  h1->doIt();
123 
124  if ( !m_privateToolsOnly ) {
127  h2->doIt();
128  h3->doIt();
129  }
130  return StatusCode::SUCCESS;
131 }
132 
133 //------------------------------------------------------------------------------
135  //------------------------------------------------------------------------------
136  info() << "finalizing...." << endmsg;
137 
138  if ( !m_privateToolsOnly ) { toolSvc()->releaseTool( m_publicTool ).ignore(); }
139  toolSvc()->releaseTool( m_privateTool ).ignore();
140  toolSvc()->releaseTool( m_privateToolWithName ).ignore();
141  toolSvc()->releaseTool( m_privateOtherInterface ).ignore();
142 
143  return TestAlg::finalize();
144 }
IMyOtherTool::doItAgain
virtual void doItAgain() const =0
Do something else.
ToolHandle::retrieve
StatusCode retrieve() const override
Retrieve the AlgTool.
Definition: ToolHandle.h:228
MyAlgorithm::m_privateToolWithName
IMyTool * m_privateToolWithName
Definition: MyAlgorithm.h:41
std::string
STL class.
Gaudi::Algorithm::toolSvc
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition: Algorithm.cpp:564
IMyTool.h
MyAlgorithm::m_myCopiedToolHandle
PublicToolHandle< IMyTool > m_myCopiedToolHandle
Definition: MyAlgorithm.h:63
BaseToolHandle::disable
void disable()
Definition: ToolHandle.h:109
IMyTool::doIt
virtual void doIt() const =0
Do something.
MyAlgorithm::m_undefinedToolHandle
ToolHandle< IMyTool > m_undefinedToolHandle
Definition: MyAlgorithm.h:54
MyAlgorithm::m_myCopiedConstToolHandle2
PublicToolHandle< const IMyTool > m_myCopiedConstToolHandle2
Definition: MyAlgorithm.h:62
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:31
Gaudi::TestSuite::TestAlg
Simple algorithm useful as base class for tests.
Definition: TestAlg.h:19
Gaudi::Algorithm::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Algorithm.h:304
MyAlgorithm
Definition: MyAlgorithm.h:21
MyAlgorithm::MyAlgorithm
MyAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor of this form must be provided or inherited from the base class.
Definition: MyAlgorithm.cpp:21
MyAlgorithm::m_myConstToolHandle
PublicToolHandle< const IMyTool > m_myConstToolHandle
Definition: MyAlgorithm.h:59
MyAlgorithm::m_publicTool
IMyTool * m_publicTool
Definition: MyAlgorithm.h:39
MyAlgorithm::m_privateToolsOnly
Gaudi::Property< bool > m_privateToolsOnly
Definition: MyAlgorithm.h:36
IDataProviderSvc.h
MyAlgorithm::m_myPrivToolHandle
ToolHandle< IMyTool > m_myPrivToolHandle
Definition: MyAlgorithm.h:47
IToolSvc.h
ToolHandle< IMyTool >
StatusCode
Definition: StatusCode.h:65
MyAlgorithm::m_myCopiedConstToolHandle
PublicToolHandle< const IMyTool > m_myCopiedConstToolHandle
Definition: MyAlgorithm.h:61
MyAlgorithm::m_myUnusedToolHandle
ToolHandle< IAlgTool > m_myUnusedToolHandle
Definition: MyAlgorithm.h:52
MyAlgorithm::m_legacyToolHandle
ToolHandle< IMyTool > m_legacyToolHandle
Definition: MyAlgorithm.h:45
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
MyAlgorithm::m_privateOtherInterface
IMyOtherTool * m_privateOtherInterface
Definition: MyAlgorithm.h:43
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
DataObject.h
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
MyAlgorithm::m_privateTool
IMyTool * m_privateTool
Definition: MyAlgorithm.h:38
MyAlgorithm::initialize
StatusCode initialize() override
Three mandatory member functions of any algorithm.
Definition: MyAlgorithm.cpp:31
MyAlgorithm::finalize
StatusCode finalize() override
Definition: MyAlgorithm.cpp:134
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:273
MyAlgorithm.h
MyAlgorithm::m_myPubToolHandle
PublicToolHandle< IMyTool > m_myPubToolHandle
Definition: MyAlgorithm.h:48
MyAlgorithm::m_privateToolType
Gaudi::Property< std::string > m_privateToolType
Definition: MyAlgorithm.h:34
MyAlgorithm::execute
StatusCode execute() override
Definition: MyAlgorithm.cpp:93
MyAlgorithm::m_wrongIfaceTool
ToolHandle< IWrongTool > m_wrongIfaceTool
Definition: MyAlgorithm.h:57
GaudiException::what
const char * what() const override
method from std::exception
Definition: GaudiException.h:110