The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
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"
16
18
20
21MyAlgorithm::MyAlgorithm( const std::string& name, ISvcLocator* ploc ) : TestAlg( name, ploc ) {
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
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
63 m_myUnusedToolHandle.disable();
64
65 // m_wrongIfaceTool is being retrieved via the wrong interface.
66 // we expect the retrieve() to throw an exception.
67 try {
68 if ( m_wrongIfaceTool.retrieve().isFailure() ) {
69 error() << "unable to retrieve " << m_wrongIfaceTool.typeAndName() << " (unexpected)" << endmsg;
70 m_wrongIfaceTool.disable();
71 }
72 } catch ( GaudiException& ex ) {
73 info() << "unable to retrieve " << m_wrongIfaceTool.typeAndName() << " (expected) with exception: " << ex.what()
74 << endmsg;
75 m_wrongIfaceTool.disable();
76 }
77
78 if ( m_privateToolsOnly ) {
79 // disable all public tools
80 m_myPubToolHandle.disable();
81 m_myConstToolHandle.disable();
83 m_myCopiedToolHandle.disable();
85 }
86
87 info() << "....initialization done" << endmsg;
88
90}
91
92//------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 info() << "executing...." << endmsg;
96
97 if ( !m_privateToolsOnly ) { m_publicTool->doIt(); }
98 m_privateTool->doIt();
100 m_privateOtherInterface->doItAgain();
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
142
143 return TestAlg::finalize();
144}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition Algorithm.h:286
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
const std::string & name() const override
The identifying name of the algorithm object.
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Define general base for Gaudi exception.
const char * what() const override
method from std::exception
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
virtual StatusCode releaseTool(IAlgTool *tool)=0
Release the tool.
StatusCode retrieveTool(std::string_view type, T *&tool, const IInterface *parent=nullptr, bool createIf=true)
Retrieve specified tool sub-type with tool dependent part of the name automatically assigned.
Definition IToolSvc.h:147
Trivial Algorithm for tutorial purposes.
Definition MyAlgorithm.h:21
PublicToolHandle< IMyTool > m_myCopiedToolHandle
Definition MyAlgorithm.h:63
PublicToolHandle< const IMyTool > m_myCopiedConstToolHandle2
Definition MyAlgorithm.h:62
StatusCode finalize() override
StatusCode initialize() override
Three mandatory member functions of any algorithm.
IMyTool * m_privateToolWithName
Definition MyAlgorithm.h:41
IMyTool * m_publicTool
Definition MyAlgorithm.h:39
Gaudi::Property< std::string > m_privateToolType
Definition MyAlgorithm.h:34
PublicToolHandle< const IMyTool > m_myConstToolHandle
Definition MyAlgorithm.h:59
PublicToolHandle< const IMyTool > m_myCopiedConstToolHandle
Definition MyAlgorithm.h:61
ToolHandle< IMyTool > m_myPrivToolHandle
Definition MyAlgorithm.h:47
StatusCode execute() override
MyAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor of this form must be provided or inherited from the base class.
IMyOtherTool * m_privateOtherInterface
Definition MyAlgorithm.h:43
ToolHandle< IMyTool > m_legacyToolHandle
Definition MyAlgorithm.h:45
IMyTool * m_privateTool
Definition MyAlgorithm.h:38
Gaudi::Property< bool > m_privateToolsOnly
Definition MyAlgorithm.h:36
ToolHandle< IAlgTool > m_myUnusedToolHandle
Definition MyAlgorithm.h:52
ToolHandle< IMyTool > m_undefinedToolHandle
Definition MyAlgorithm.h:54
ToolHandle< IWrongTool > m_wrongIfaceTool
Definition MyAlgorithm.h:57
PublicToolHandle< IMyTool > m_myPubToolHandle
Definition MyAlgorithm.h:48
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
constexpr static const auto SUCCESS
Definition StatusCode.h:99
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
Simple algorithm useful as base class for tests.
Definition TestAlg.h:19
StatusCode initialize() override
Definition TestAlg.h:21