Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:33

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 #ifndef GAUDIALG_GAUDICOMMONIMP_H
0012 #define GAUDIALG_GAUDICOMMONIMP_H 1
0013 // ============================================================================
0014 // Include files
0015 // ============================================================================
0016 #include <algorithm>
0017 // ============================================================================
0018 // GaudiAlg
0019 // ============================================================================
0020 #include "GaudiAlg/GaudiCommon.h"
0021 #include "GaudiAlg/GetData.h"
0022 // ============================================================================
0023 /** @file
0024  *  The implementation of templated methods for class GaudiCommon
0025  *  @see    GaudiCommon
0026  *  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
0027  *  @author Chris Jones   Christopher.Rob.Jones@cern.ch
0028  *  @date   2004-01-19
0029  */
0030 // ============================================================================
0031 // Templated access to the data in Gaudi Transient Store
0032 // ============================================================================
0033 template <class PBASE>
0034 template <class TYPE>
0035 typename Gaudi::Utils::GetData<TYPE>::return_type
0036 GaudiCommon<PBASE>::get( IDataProviderSvc* service, std::string_view location, const bool useRootInTES ) const {
0037   // check the environment
0038   Assert( service, "get():: IDataProvider* points to NULL!" );
0039   // get the helper object:
0040   Gaudi::Utils::GetData<TYPE> getter;
0041   return getter( *this, service, this->fullTESLocation( location, useRootInTES ) );
0042 }
0043 // ============================================================================
0044 // Templated access to the data in Gaudi Transient Store, no check on the content
0045 // ============================================================================
0046 template <class PBASE>
0047 template <class TYPE>
0048 typename Gaudi::Utils::GetData<TYPE>::return_type
0049 GaudiCommon<PBASE>::getIfExists( IDataProviderSvc* service, std::string_view location, const bool useRootInTES ) const {
0050   // check the environment
0051   Assert( service, "get():: IDataProvider* points to NULL!" );
0052   // get the helper object:
0053   Gaudi::Utils::GetData<TYPE> getter;
0054   return getter( *this, service, this->fullTESLocation( location, useRootInTES ), false );
0055 }
0056 // ============================================================================
0057 // check the existence of objects in Gaudi Transient Store
0058 // ============================================================================
0059 template <class PBASE>
0060 template <class TYPE>
0061 bool GaudiCommon<PBASE>::exist( IDataProviderSvc* service, std::string_view location, const bool useRootInTES ) const {
0062   // check the environment
0063   Assert( service, "exist():: IDataProvider* points to NULL!" );
0064   // check the data object
0065   Gaudi::Utils::CheckData<TYPE> checker;
0066   return checker( service, this->fullTESLocation( location, useRootInTES ) );
0067 }
0068 // ============================================================================
0069 // get the existing object from Gaudi Event Transient store
0070 // or create new object register in in TES and return if object
0071 // does not exist
0072 // ============================================================================
0073 template <class PBASE>
0074 template <class TYPE, class TYPE2>
0075 typename Gaudi::Utils::GetData<TYPE>::return_type
0076 GaudiCommon<PBASE>::getOrCreate( IDataProviderSvc* service, std::string_view location, const bool useRootInTES ) const {
0077   // check the environment
0078   Assert( service, "getOrCreate():: svc points to NULL!" );
0079   // get the helper object
0080   Gaudi::Utils::GetOrCreateData<TYPE, TYPE2> getter;
0081   return getter( *this, service, this->fullTESLocation( location, useRootInTES ), location );
0082 }
0083 // ============================================================================
0084 // the useful method for location of tools.
0085 // ============================================================================
0086 template <class PBASE>
0087 template <class TOOL>
0088 TOOL* GaudiCommon<PBASE>::tool( std::string_view type, std::string_view name, const IInterface* parent,
0089                                 bool create ) const {
0090   // for empty names delegate to another method
0091   if ( name.empty() ) return tool<TOOL>( type, parent, create );
0092   Assert( this->toolSvc(), "tool():: IToolSvc* points to NULL!" );
0093   // get the tool from Tool Service
0094   TOOL*            Tool = nullptr;
0095   const StatusCode sc   = this->toolSvc()->retrieveTool( type, name, Tool, parent, create );
0096   if ( sc.isFailure() ) {
0097     Exception(
0098         std::string{ "tool():: Could not retrieve Tool '" }.append( type ).append( "'/'" ).append( name ).append( "'" ),
0099         sc );
0100   }
0101   if ( !Tool ) {
0102     Exception( std::string{ "tool():: Could not retrieve Tool '" }.append( type ).append( "'/'" ).append( name ).append(
0103         "'" ) );
0104   }
0105   // insert tool into list of tools
0106   PBASE::registerTool( Tool );
0107   m_managedTools.push_back( Tool );
0108   // return *VALID* located tool
0109   return Tool;
0110 }
0111 // ============================================================================
0112 // the useful method for location of tools.
0113 // ============================================================================
0114 template <class PBASE>
0115 template <class TOOL>
0116 TOOL* GaudiCommon<PBASE>::tool( std::string_view type, const IInterface* parent, bool create ) const {
0117   // check the environment
0118   Assert( PBASE::toolSvc(), "IToolSvc* points to NULL!" );
0119   // retrieve the tool from Tool Service
0120   TOOL*            Tool = nullptr;
0121   const StatusCode sc   = this->toolSvc()->retrieveTool( type, Tool, parent, create );
0122   if ( sc.isFailure() ) {
0123     Exception( std::string{ "tool():: Could not retrieve Tool '" }.append( type ).append( "'" ), sc );
0124   }
0125   if ( !Tool ) { Exception( std::string{ "tool():: Could not retrieve Tool '" }.append( type ).append( "'" ) ); }
0126   // add the tool into the list of known tools to be properly released
0127   PBASE::registerTool( Tool );
0128   m_managedTools.push_back( Tool );
0129   // return *VALID* located tool
0130   return Tool;
0131 }
0132 // ============================================================================
0133 // the useful method for location of services
0134 // ============================================================================
0135 template <class PBASE>
0136 template <class SERVICE>
0137 SmartIF<SERVICE> GaudiCommon<PBASE>::svc( std::string_view name, const bool create ) const {
0138   Assert( this->svcLoc(), "ISvcLocator* points to NULL!" );
0139   SmartIF<SERVICE> s;
0140   // check if we already have this service
0141   auto it = std::lower_bound( std::begin( m_services ), std::end( m_services ), name, GaudiCommon_details::svc_lt );
0142   if ( it != std::end( m_services ) && GaudiCommon_details::svc_eq( *it, name ) ) {
0143     // Try to get the requested interface
0144     s = *it;
0145     // check the results
0146     if ( !s ) {
0147       Exception( std::string{ "svc():: Could not retrieve Svc '" }.append( name ).append( "'" ), StatusCode::FAILURE );
0148     }
0149   } else {
0150     auto baseSvc = this->svcLoc()->service( name, create );
0151     // Try to get the requested interface
0152     s = baseSvc;
0153     // check the results
0154     if ( !baseSvc || !s ) {
0155       Exception( std::string{ "svc():: Could not retrieve Svc '" }.append( name ).append( "'" ), StatusCode::FAILURE );
0156     }
0157     // add the tool into list of known tools, to be properly released
0158     addToServiceList( baseSvc );
0159   }
0160   // return *VALID* located service
0161   return s;
0162 }
0163 // ============================================================================
0164 // Short-cut  to get a pointer to the UpdateManagerSvc
0165 // ============================================================================
0166 template <class PBASE>
0167 IUpdateManagerSvc* GaudiCommon<PBASE>::updMgrSvc() const {
0168   if ( !m_updMgrSvc ) { m_updMgrSvc = svc<IUpdateManagerSvc>( "UpdateManagerSvc", true ); }
0169   return m_updMgrSvc;
0170 }
0171 // ============================================================================
0172 // Assertion - throw exception, if condition is not fulfilled
0173 // ============================================================================
0174 template <class PBASE>
0175 void GaudiCommon<PBASE>::Assert( const bool ok, std::string_view msg, const StatusCode sc ) const {
0176   if ( !ok ) Exception( msg, sc );
0177 }
0178 // ============================================================================
0179 /** @def ALG_ERROR
0180  *  Small and simple macro to add into error message the file name
0181  *  and the line number for easy location of the problematic lines.
0182  *
0183  *  @code
0184  *
0185  *  if ( a < 0 ) { return ALG_ERROR( "'a' is negative" , 301 ) ; }
0186  *
0187  *  @endcode
0188  *
0189  *  @author Vanya BELYAEV Ivan.Belyaev@itep.ru
0190  *  @date 2002-10-29
0191  */
0192 // ============================================================================
0193 #define ALG_ERROR( message, code )                                                                                     \
0194   ( Error( message + std::string( " [ at line " ) + std::to_string( __LINE__ ) + std::string( " in file '" ) +         \
0195                std::string( __FILE__ ) + "']",                                                                         \
0196            code ) )
0197 
0198 // ============================================================================
0199 // The END
0200 // ============================================================================
0201 #endif // GAUDIALG_GAUDICOMMONIMP_H