Back to home page

EIC code displayed by LXR

 
 

    


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

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 #include "GaudiKernel/DataObjID.h"
0013 #include "GaudiKernel/ToolVisitor.h"
0014 #include <functional>
0015 #include <string>
0016 #include <type_traits>
0017 #include <vector>
0018 
0019 #ifndef USAGE_IS_THREAD_SAFE
0020 #  define USAGE_IS_THREAD_SAFE
0021 #endif
0022 
0023 class IAlgTool;
0024 
0025 /** Helper class to be used in conjunction with the recursive tool visitor to renounce certain inputs.
0026  */
0027 class RenounceToolInputsVisitor {
0028 public:
0029   /** Helper class interface to optionally log renounce operations.
0030    */
0031   struct ILogger {
0032     virtual ~ILogger()                                                        = default;
0033     virtual void renounce( std::string_view tool_name, std::string_view key ) = 0;
0034   };
0035 
0036   /** A do-nothing helper class which implements the logger interface.
0037    */
0038   struct NoLogger : ILogger {
0039     void renounce( std::string_view, std::string_view ) override {}
0040   };
0041 
0042   class Logger final : public ILogger {
0043     std::function<void( std::string_view, std::string_view )> m_func;
0044 
0045   public:
0046     template <typename F,
0047               typename = std::enable_if_t<std::is_invocable_r_v<void, F, std::string_view, std::string_view>>>
0048     Logger( F func ) : m_func( std::move( func ) ) {}
0049     void renounce( std::string_view tool_name, std::string_view key ) override { m_func( tool_name, key ); }
0050   };
0051   /** @brief Create a logger from a function.
0052    * usage:
0053    * @verbatim
0054    * auto logger=RenounceToolInputsVisitor::createLogger( [this]( std::string_view tool_name, std::string_view key )
0055    * { this->msg(MSG::INFO) << " Renounce " << tool_name << " . " << key << endmsg;
0056    *                                                       });
0057    * @endverbatim
0058    */
0059   static Logger createLogger( std::function<void( std::string_view, std::string_view )> func ) {
0060     return Logger{ std::move( func ) };
0061   }
0062 
0063   /** construct the renounce visitor helper object
0064    * @param input_keys a list of input keys to be renounced.
0065    * @parame logger optional helper object to log inputs which are renounced.
0066    * By default this renounce visitor will renounce inputs with keys found in the inputs_names list, and it operates
0067    * silently.
0068    */
0069   RenounceToolInputsVisitor( std::vector<DataObjID> input_keys, ILogger& logger = s_noLogger )
0070       : m_renounceKeys( std::move( input_keys ) )
0071       , m_logger( &logger ) // @TODO possible source of use after delete
0072   {}
0073 
0074   void operator()( IAlgTool* );
0075 
0076 private:
0077   std::vector<DataObjID> m_renounceKeys;
0078   ILogger*               m_logger;
0079 
0080   static NoLogger s_noLogger USAGE_IS_THREAD_SAFE;
0081 };