Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:36

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 
0013 #include "GaudiKernel/IAlgTool.h"
0014 #include <functional>
0015 #include <regex>
0016 #include <type_traits>
0017 
0018 /** Helper class to visit a list of tools and all their child tools.
0019  */
0020 class ToolVisitor {
0021   struct IVisitor {
0022     virtual ~IVisitor()                   = default;
0023     virtual void visit( IAlgTool* ) const = 0;
0024   };
0025 
0026 public:
0027   /** Visit the given tools and their child tools.
0028    * @param tools: list of top level tools to be visited
0029    * @param visitor: a helper class which will be called for each visited tool
0030    * @param reject_filter: an optional regular expression to filter out tools by name i.e. they and their child tools
0031    * are not visited.
0032    *
0033    * usage:
0034    * @verbatim
0035    *      auto visitor=[this](const IAlgTool *tool) { this->msg(MSG::DEBUG)  << tool->name() << endmsg; };
0036    *      ToolVisitor::visit(tools(), visitor);
0037    * @verbatim
0038    */
0039   template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<void, Callable, IAlgTool*>>>
0040   static void visit( const std::vector<IAlgTool*>& tools, Callable& callable,
0041                      const std::regex& reject_filter = s_noFilter ) {
0042     class Visitor final : public IVisitor {
0043       Callable* m_func;
0044 
0045     public:
0046       Visitor( Callable* f ) : m_func{ f } {}
0047       void visit( IAlgTool* alg_tool ) const override { std::invoke( *m_func, alg_tool ); }
0048     };
0049     recursiveVisit( tools, Visitor{ &callable }, reject_filter );
0050   }
0051 
0052 private:
0053   static void             recursiveVisit( const std::vector<IAlgTool*>& tools, IVisitor const& visitor,
0054                                           const std::regex& reject_filter );
0055   static const std::regex s_noFilter;
0056 };