10 #include "boost/algorithm/string/erase.hpp" 11 #include "boost/algorithm/string/predicate.hpp" 12 #include "boost/circular_buffer.hpp" 19 namespace ba = boost::algorithm;
21 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) ) 22 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) ) 27 unsigned long totalRefCount(
const C& toolList )
31 [&](
unsigned long count,
const IAlgTool* tool ) {
return count + tool->
refCount(); } );
36 void remove( C&
c,
typename C::const_reference i ) {
59 error() <<
"Unable to initialize the Service" <<
endmsg;
65 error() <<
"Unable to set base properties" <<
endmsg;
100 info() <<
"Removing all tools created by ToolSvc" <<
endmsg;
105 log <<
" Tool List : ";
106 for (
const auto& iTool :
m_instancesTools ) {
log << iTool->name() <<
":" << iTool->refCount() <<
" "; }
128 unsigned long startRefCount = 0;
130 unsigned long startMinRefCount = 0;
132 while ( toolCount > 0 && endRefCount > 0 && ( endRefCount != startRefCount || endMinRefCount != startMinRefCount ) ) {
133 ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
134 debug() << toolCount <<
" tools left to finalize. Summed refCounts: " << endRefCount <<
endmsg;
135 debug() <<
"Will finalize tools with refCount <= " << endMinRefCount <<
endmsg;
137 startMinRefCount = endMinRefCount;
138 startRefCount = endRefCount;
139 unsigned long maxLoop = toolCount + 1;
144 unsigned long count = pTool->
refCount();
147 if ( count <= startMinRefCount ) {
148 ON_DEBUG debug() <<
" Performing finalization of " << toolName <<
" (refCount " << count <<
")" <<
endmsg;
156 finalizedTools.push_back( pTool );
160 ON_DEBUG debug() <<
" Delaying finalization of " << toolName <<
" (refCount " << count <<
")" <<
endmsg;
182 auto maxLoop = totalRefCount( finalizedTools ) + 1;
183 while ( --maxLoop > 0 && !finalizedTools.empty() ) {
184 IAlgTool* pTool = finalizedTools.front();
185 finalizedTools.pop_front();
192 finalizedTools.push_back( pTool );
200 error() <<
"Unable to finalize and delete the following tools : ";
201 for (
const auto& iTool :
m_instancesTools ) {
error() << iTool->name() <<
": " << iTool->refCount() <<
" "; }
206 if ( !finalizedTools.empty() ) {
207 error() <<
"Failed to delete the following " << finalizedTools.size()
208 <<
" finalized tools. Bug in ToolSvc::finalize()?: ";
209 for (
const auto& iTool : finalizedTools ) {
error() << iTool->name() <<
": " << iTool->refCount() <<
" "; }
234 if ( ba::ends_with( tooltype, s_PUBLIC ) ) {
236 return retrieve( ba::erase_tail_copy( tooltype, s_PUBLIC.
size() ), iid, tool,
this, createIf );
240 if ( tooltype.
empty() ) {
241 error() <<
"retrieve(): No Tool Type/Name given" <<
endmsg;
244 auto pos = tooltype.
find(
'/' );
245 if ( std::string::npos == pos ) {
return retrieve( tooltype, tooltype, iid, tool, parent, createIf ); }
246 return retrieve( tooltype.
substr( 0, pos ), tooltype.
substr( pos + 1 ), iid, tool, parent, createIf );
258 if (
toolname.empty() || ( std::string::npos != tooltype.
find(
'/' ) ) ) {
259 return retrieve( tooltype, iid, tool, parent, createIf );
263 if ( ba::ends_with(
toolname, s_PUBLIC ) ) {
265 return retrieve( tooltype, ba::erase_tail_copy(
toolname, s_PUBLIC.size() ), iid, tool,
this, createIf );
276 if ( !parent ) parent =
this;
282 [&](
const IAlgTool* i ) {
return i->
name() == fullname && i->
parent() == parent; } );
302 error() <<
"Tool " <<
toolname <<
" either does not implement the correct interface, or its version is incompatible" 322 if ( tool->type() == toolType ) tools.
push_back( tool->name() );
351 unsigned long count = tool->
refCount();
358 debug() <<
" Performing finalization of " << tool->
name() <<
" (refCount " << count <<
")" <<
endmsg;
362 debug() <<
"Performing finalization and deletion of " << tool->
name() <<
endmsg;
384 template <
typename T>
385 class ToolCreateGuard {
392 explicit ToolCreateGuard( T& tools ) : m_tools( tools ) {}
396 ToolCreateGuard( ToolCreateGuard&& ) noexcept =
default;
398 void create( const
std::
string& tooltype, const
std::
string& fullname, const
IInterface* parent ) {
400 if ( m_tool )
remove( m_tools, m_tool.
get() );
401 m_tool = AlgTool::Factory::create( tooltype, tooltype, fullname, parent );
403 if ( m_tool ) m_tools.push_back( m_tool.get() );
415 if ( m_tool )
remove( m_tools, m_tool.get() );
419 template <
typename C>
420 ToolCreateGuard<C> make_toolCreateGuard( C&
c ) {
421 return ToolCreateGuard<C>{
c};
441 error() <<
"create(): No Tool Type given" <<
endmsg;
446 if ( !parent ) parent =
this;
461 error() <<
"Tool " << fullname <<
" already exists with the same parent" <<
endmsg;
462 if ( parent ==
this )
463 error() <<
"... In addition, the parent is the ToolSvc: public tools cannot be cloned!" <<
endmsg;
472 toolguard.create( tooltype, fullname, parent );
473 if (
UNLIKELY( !toolguard.get() ) ) {
474 error() <<
"Cannot create tool " << tooltype <<
" (No factory found)" <<
endmsg;
479 fatal() <<
"Exception with tag=" << Exception.
tag() <<
" is caught whilst instantiating tool '" << tooltype <<
"'" 487 fatal() <<
"Standard std::exception is caught whilst instantiating tool '" << tooltype <<
"'" <<
endmsg;
494 fatal() <<
"UNKNOWN Exception is caught whilst instantiating tool '" << tooltype <<
"'" <<
endmsg;
502 AlgTool* mytool = dynamic_cast<AlgTool*>( toolguard.get() );
506 error() <<
"Error setting properties for tool '" << fullname <<
"'" <<
endmsg;
514 sc = toolguard->sysInitialize();
518 error() <<
"GaudiException with tag=" << Exception.
tag() <<
" caught whilst initializing tool '" << fullname <<
"'" 522 error() <<
"Standard std::exception caught whilst initializing tool '" << fullname <<
"'" <<
endmsg 526 error() <<
"UNKNOWN Exception caught whilst initializing tool '" << fullname <<
"'" <<
endmsg;
532 error() <<
"Error initializing tool '" << fullname <<
"'" <<
endmsg;
538 sc = toolguard->sysStart();
541 error() <<
"Error starting tool '" << fullname <<
"'" <<
endmsg;
567 if ( !parent ) {
return this->
name() +
"." +
toolname; }
571 if ( named_parent ) {
572 auto fullname = named_parent->name() +
"." +
toolname;
576 error() <<
"Private Tools only allowed for components implementing INamedInterface" <<
endmsg;
587 [&](
const IAlgTool* tool ) {
return tool->
name() == fullname; } );
588 return i !=
std::end( m_instancesTools );
602 sc = itool->sysFinalize();
606 error() <<
"GaudiException with tag=" << Exception.
tag() <<
" caught whilst finalizing tool '" << toolName <<
"'" 610 error() <<
"Standard std::exception caught whilst finalizing tool '" << toolName <<
"'" <<
endmsg 614 error() <<
"UNKNOWN Exception caught whilst finalizing tool '" << toolName <<
"'" <<
endmsg;
662 if (
UNLIKELY( !iTool->sysStart().isSuccess() ) ) {
664 error() << iTool->name() <<
" failed to start()" <<
endmsg;
669 error() <<
"One or more AlgTools failed to start()" <<
endmsg;
686 if (
UNLIKELY( !iTool->sysStop().isSuccess() ) ) {
688 error() << iTool->name() <<
" failed to stop()" <<
endmsg;
693 error() <<
"One or more AlgTools failed to stop()" <<
endmsg;
Gaudi::StateMachine::State m_targetState
Service state.
StatusCode initialize() override
Define general base for Gaudi exception.
Gaudi::StateMachine::State m_state
Service state.
StatusCode finalize() override
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
virtual const std::string & tag() const
name tag for the exception, or exception type
constexpr static const auto SUCCESS
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
virtual const std::string & name() const =0
Retrieve the name of the instance.
#define DECLARE_COMPONENT(type)
const std::string & name() const override
Retrieve name of the service.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
This class is used for returning status codes from appropriate routines.
virtual StatusCode registerAlgTool(const IAlgTool &)=0
Definition of the basic interface.
virtual unsigned long refCount() const =0
Current reference count.
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
const StatusCode & ignore() const
Ignore/check StatusCode.
virtual unsigned long release()=0
Release Interface instance.
constexpr static const auto FAILURE
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.