2 #include "GaudiKernel/MsgStream.h"
3 #include "GaudiKernel/Service.h"
4 #include "GaudiKernel/ISvcLocator.h"
5 #include "GaudiKernel/IAlgorithm.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/AlgTool.h"
8 #include "GaudiKernel/IHistorySvc.h"
17 #pragma warning(disable:177)
19 #include "boost/lambda/bind.hpp"
21 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
22 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
28 namespace bl = boost::lambda;
53 error() <<
"Unable to initialize the Service" <<
endmsg;
59 error() <<
"Unable to set base properties" <<
endmsg;
95 info() <<
"Removing all tools created by ToolSvc" <<
endmsg;
100 log <<
" Tool List : ";
103 log << (*iTool)->name() <<
":" <<
refCountTool( *iTool ) <<
" ";
125 unsigned long startRefCount = 0;
127 unsigned long startMinRefCount = 0;
129 while ( toolCount > 0 &&
131 (endRefCount != startRefCount || endMinRefCount != startMinRefCount) ) {
132 ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
133 debug() << toolCount <<
" tools left to finalize. Summed refCounts: "
135 debug() <<
"Will finalize tools with refCount <= "
136 << endMinRefCount <<
endmsg;
138 startMinRefCount = endMinRefCount;
139 startRefCount = endRefCount;
140 unsigned long maxLoop = toolCount + 1;
147 std::string toolName = pTool->
name();
148 if ( count <= startMinRefCount ) {
149 ON_DEBUG debug() <<
" Performing finalization of " << toolName
150 <<
" (refCount " << count <<
")" <<
endmsg;
158 finalizedTools.push_back(pTool);
163 <<
" (refCount " << count <<
")" <<
endmsg;
186 while ( --maxLoop > 0 && finalizedTools.size() > 0 ) {
187 IAlgTool* pTool = finalizedTools.front();
188 finalizedTools.pop_front();
194 <<
" (refCount " << count <<
")" <<
endmsg;
196 finalizedTools.push_back(pTool);
204 error() <<
"Unable to finalize and delete the following tools : ";
213 if ( finalizedTools.size() > 0 ) {
214 error() <<
"Failed to delete the following " << finalizedTools.size()
215 <<
" finalized tools. Bug in ToolSvc::finalize()?: ";
216 for ( ListTools::const_iterator iTool = finalizedTools.begin();
217 iTool != finalizedTools.end(); ++iTool ) {
244 const std::string s_PUBLIC =
":PUBLIC" ;
257 if ( tooltype.empty() ) {
258 error() <<
"retrieve(): No Tool Type/Name given" <<
endmsg;
264 const std::string::size_type pos = tooltype.find ( s_PUBLIC ) ;
265 if ( std::string::npos != pos )
269 return retrieve ( std::string( tooltype , 0 , pos ) ,
270 iid , tool , parent , createIf ) ;
274 const std::string::size_type pos = tooltype.find(
'/');
275 if( std::string::npos == pos ) {
276 return retrieve ( tooltype , tooltype , iid , tool , parent , createIf );
278 const std::string newtype ( tooltype , 0 , pos ) ;
279 const std::string newname ( tooltype , pos + 1 , std::string::npos ) ;
280 return retrieve ( newtype , newname , iid , tool , parent , createIf ) ;
296 if( toolname.empty() || (std::string::npos != tooltype.find(
'/')) )
297 {
return retrieve ( tooltype , iid , tool , parent , createIf ) ; }
301 const std::string::size_type pos = toolname.find ( s_PUBLIC ) ;
302 if ( std::string::npos != pos )
306 return retrieve ( tooltype , std::string( toolname , 0 , pos ) ,
307 iid , tool , parent , createIf ) ;
320 const std::string fullname =
nameTool( toolname, parent );
324 ListTools::const_iterator it;
326 if( (*it)->name() == fullname ) {
337 warning() <<
"Tool " << toolname
338 <<
" not found and creation not requested" <<
endmsg;
342 sc =
create( tooltype, toolname, parent, itool );
350 error() <<
"Tool " << toolname
351 <<
" either does not implement the correct interface, or its version is incompatible"
373 std::vector<std::string> tools;
376 if (tool->type() == toolType) {
377 tools.push_back( tool->name() );
418 log <<
MSG::DEBUG <<
" Performing finalization of " << tool->
name()
419 <<
" (refCount " << count <<
")" <<
endmsg;
441 const std::string &
toolname = tooltype;
442 return create( tooltype, toolname, parent, tool);
448 class ToolCreateGuard {
457 m_list.remove(m_tool);
462 m_list.push_back(m_tool);
465 ToolCreateGuard& operator=(
IAlgTool* tool) {
503 error() <<
"create(): No Tool Type given" <<
endmsg;
508 if ( 0 == parent ) parent =
this;
516 const std::string fullname =
nameTool(toolname, parent);
518 error() <<
"Tool " << fullname <<
" already exists" <<
endmsg;
523 toolguard = AlgTool::Factory::create(tooltype, tooltype, fullname, parent);
525 error() <<
"Cannot create tool " << tooltype <<
" (No factory found)" <<
endmsg;
531 fatal() <<
"Exception with tag=" << Exception.
tag()
532 <<
" is caught whilst instantiating tool '" << tooltype <<
"'" <<
endmsg;
538 catch(
const std::exception& Exception ) {
540 fatal() <<
"Standard std::exception is caught whilst instantiating tool '"
541 << tooltype <<
"'" <<
endmsg;
549 fatal() <<
"UNKNOWN Exception is caught whilst instantiating tool '"
550 << tooltype <<
"'" <<
endmsg;
562 error() <<
"Error setting properties for tool '"
563 << fullname <<
"'" <<
endmsg;
571 sc = toolguard->sysInitialize();
577 <<
"GaudiException with tag=" << Exception.
tag()
578 <<
" caught whilst initializing tool '" << fullname <<
"'" << endmsg
582 catch(
const std::exception & Exception )
585 <<
"Standard std::exception caught whilst initializing tool '"
586 << fullname <<
"'" << endmsg << Exception.what() <<
endmsg;
592 <<
"UNKNOWN Exception caught whilst initializing tool '"
593 << fullname <<
"'" <<
endmsg;
599 error() <<
"Error initializing tool '" << fullname <<
"'" <<
endmsg;
605 sc = toolguard->sysStart();
608 error() <<
"Error starting tool '" << fullname <<
"'" <<
endmsg;
616 tool = toolguard.release();
645 std::string fullname =
"";
646 if ( parent == 0 ) {
return this->
name() +
"." +
toolname; }
662 <<
"Private Tools only allowed for components implementing INamedInterface"
672 for ( ListTools::const_iterator it = m_instancesTools.begin();
673 it != m_instancesTools.end(); ++it ) {
674 if ( (*it)->name() == fullname ) {
return true; }
685 const std::string toolName = itool->name();
690 sc = itool->sysFinalize();
696 <<
"GaudiException with tag=" << Exception.
tag()
697 <<
" caught whilst finalizing tool '" << toolName <<
"'" <<
endmsg
701 catch(
const std::exception & Exception )
704 <<
"Standard std::exception caught whilst finalizing tool '"
705 << toolName <<
"'" <<
endmsg << Exception.what() <<
endmsg;
711 <<
"UNKNOWN Exception caught whilst finalizing tool '"
712 << toolName <<
"'" <<
endmsg;
724 unsigned long count = 0;
725 for ( ListTools::const_iterator iTool = toolList.begin();
726 iTool != toolList.end(); ++iTool ) {
727 count += refCountTool( *iTool );
742 unsigned long count = 0;
762 std::vector<IToolSvc::Observer*>::iterator
i =
780 if (
UNLIKELY(!(*iTool)->sysStart().isSuccess())) {
782 error() << (*iTool)->name() <<
" failed to start()" <<
endmsg;
788 error() <<
"One or more AlgTools failed to start()" <<
endmsg;
809 if (
UNLIKELY(!(*iTool)->sysStop().isSuccess())) {
811 error() << (*iTool)->name() <<
" failed to stop()" <<
endmsg;
817 error() <<
"One or more AlgTools failed to stop()" <<
endmsg;