The Gaudi Framework  v33r1 (b1225454)
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
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
an helper to share the implementation of service() among the various kernel base classes
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:25
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
SmartIF< T > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
const std::string & requesterName() const
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string requesterName)
STL namespace.
ServiceLocatorHelper(ISvcLocator &svcLoc, std::string loggedName, std::string requesterName)
ServiceLocatorHelper(ISvcLocator &svcLoc, const INamedInterface &requester)
STL class.
StatusCode getService(std::string_view name, bool createIf, const InterfaceID &iid, void **ppSvc) const
Interface ID class.
Definition: IInterface.h:39
ServiceLocatorHelper(ISvcLocator &svcLoc, const MsgStream &log, std::string requesterName)
MsgStream & log() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:47
IInterface compliant class extending IInterface with the name() method.
ISvcLocator * serviceLocator() const
#define GAUDI_API
Definition: Kernel.h:81