The Gaudi Framework  v36r1 (3e2fb5a8)
ServiceLocatorHelper.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 #ifndef GAUDIKERNEL_SERVICELOCATORHELPER_H
12 #define GAUDIKERNEL_SERVICELOCATORHELPER_H
13 
14 #include <GaudiKernel/IService.h>
16 #include <GaudiKernel/SmartIF.h>
17 #include <GaudiKernel/StatusCode.h>
18 #include <string>
19 
20 class InterfaceID;
21 class MsgStream;
22 
28 public:
29  ServiceLocatorHelper( ISvcLocator& svcLoc, const INamedInterface& requester )
30  : m_svcLoc( svcLoc )
31  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requester.name() )
32  , // use requester msg level
33  m_requesterName( requester.name() ) {}
34  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string loggedName, std::string requesterName )
35  : m_svcLoc( svcLoc )
36  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), std::move( loggedName ) )
37  , // use requester msg level
38  m_requesterName( std::move( requesterName ) ) {}
39  ServiceLocatorHelper( ISvcLocator& svcLoc, std::string requesterName )
40  : m_svcLoc( svcLoc )
41  , m_msgLog( SmartIF<IMessageSvc>( &svcLoc ), requesterName )
42  , // use requester msg level
43  m_requesterName( std::move( requesterName ) ) {}
44 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
46  const MsgStream& log, // use requester msg level
47  std::string requesterName )
48  : m_svcLoc( svcLoc ), m_msgLog( log ), m_requesterName( std::move( requesterName ) ) {}
49 #endif
50 
51  StatusCode getService( std::string_view name, bool createIf, const InterfaceID& iid, void** ppSvc ) const {
52  return createIf ? createService( name, iid, ppSvc ) : locateService( name, iid, ppSvc, true );
53  }
54 
55  StatusCode locateService( std::string_view name, const InterfaceID& iid, void** ppSvc, bool quiet = false ) const;
56 
57  StatusCode createService( std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
58 
59  StatusCode createService( std::string_view type, std::string_view name, const InterfaceID& iid, void** ppSvc ) const;
60 
61  SmartIF<IService> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const;
62 
63  template <typename T>
64  SmartIF<T> service( std::string_view name, const bool quiet = false, const bool createIf = true ) const {
65  return service( name, quiet, createIf ).as<T>();
66  }
67 
68 private:
69  ISvcLocator* serviceLocator() const { return &m_svcLoc; }
70  MsgStream& log() const { return m_msgLog; }
71  const std::string& requesterName() const { return m_requesterName; }
75 };
76 #endif
ServiceLocatorHelper::service
SmartIF< T > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Definition: ServiceLocatorHelper.h:64
ServiceLocatorHelper::m_svcLoc
ISvcLocator & m_svcLoc
Definition: ServiceLocatorHelper.h:72
IService.h
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:24
ISvcLocator
Definition: ISvcLocator.h:46
ServiceLocatorHelper::m_requesterName
std::string m_requesterName
Definition: ServiceLocatorHelper.h:74
ServiceLocatorHelper::m_msgLog
MsgStream m_msgLog
Definition: ServiceLocatorHelper.h:73
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, const MsgStream &log, std::string requesterName)
Definition: ServiceLocatorHelper.h:45
ServiceLocatorHelper::log
MsgStream & log() const
Definition: ServiceLocatorHelper.h:70
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, const INamedInterface &requester)
Definition: ServiceLocatorHelper.h:29
StatusCode.h
ServiceLocatorHelper::requesterName
const std::string & requesterName() const
Definition: ServiceLocatorHelper.h:71
SmartIF.h
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string requesterName)
Definition: ServiceLocatorHelper.h:39
ServiceLocatorHelper::ServiceLocatorHelper
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string loggedName, std::string requesterName)
Definition: ServiceLocatorHelper.h:34
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
ServiceLocatorHelper
an helper to share the implementation of service() among the various kernel base classes
Definition: ServiceLocatorHelper.h:27
ServiceLocatorHelper::getService
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Definition: ServiceLocatorHelper.h:51
SmartIF
Definition: IConverter.h:25
MsgStream
Definition: MsgStream.h:34
gaudirun.type
type
Definition: gaudirun.py:154
INamedInterface
Definition: INamedInterface.h:25
std
STL namespace.
InterfaceID
Definition: IInterface.h:39
ServiceLocatorHelper::serviceLocator
ISvcLocator * serviceLocator() const
Definition: ServiceLocatorHelper.h:69
ISvcLocator.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81