The Gaudi Framework  master (37c0b60a)
ToolVisitor.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <GaudiKernel/AlgTool.h>
13 
14 namespace {
15  void reverseAppend( const std::vector<IAlgTool*>& src, std::vector<IAlgTool*>& dest, const std::regex& reject_filter,
16  bool use_filter ) {
17  dest.reserve( dest.size() + src.size() );
18  std::copy_if( src.rbegin(), src.rend(), std::back_inserter( dest ), [&]( const auto& i ) {
19  return !use_filter || !std::regex_match( i->name(), reject_filter );
20  } ); // @TODO include type ?
21  }
22 } // namespace
23 
24 const std::regex ToolVisitor::s_noFilter{};
25 
26 void ToolVisitor::recursiveVisit( const std::vector<IAlgTool*>& tools, IVisitor const& visitor,
27  const std::regex& reject_filter ) {
28  bool use_filter = ( &reject_filter != &ToolVisitor::s_noFilter ) &&
29  ( !std::regex_match( "", reject_filter ) ); // @TODO remove empty string match-test ?
31  // reverse tool lost to process tools in given order
32  reverseAppend( tools, stack, reject_filter, use_filter );
33 
34  // keep track of tools which have been visited to prevent infinite loops in case of circular tool dependencies.
35  std::set<IAlgTool*> visited;
36  while ( !stack.empty() ) {
37  auto* a_tool = stack.back();
38  stack.pop_back();
39  if ( a_tool ) {
40  if ( visited.insert( a_tool ).second ) {
41  visitor.visit( a_tool );
42  // also visit all child tools
43  if ( auto* tool_impl = dynamic_cast<AlgTool*>( a_tool ); tool_impl ) {
44  reverseAppend( tool_impl->tools(), stack, reject_filter, use_filter );
45  }
46  }
47  // @TODO warn about cicular tool dependencies ?
48  }
49  }
50 }
std::vector< IAlgTool * >
std::vector::size
T size(T... args)
std::back_inserter
T back_inserter(T... args)
std::regex_match
T regex_match(T... args)
std::vector::back
T back(T... args)
std::copy_if
T copy_if(T... args)
std::vector::pop_back
T pop_back(T... args)
std::regex
std::vector::rend
T rend(T... args)
gaudirun.dest
dest
Definition: gaudirun.py:224
AlgTool
Definition: AlgTool.h:62
std::set::insert
T insert(T... args)
std::vector::empty
T empty(T... args)
AlgTool.h
ToolVisitor.h
std::set
STL class.
std::vector::rbegin
T rbegin(T... args)