The Gaudi Framework  v33r0 (d5ea422b)
ISvcLocator.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_ISVCLOCATOR_H
12 #define GAUDIKERNEL_ISVCLOCATOR_H 1
13 
14 // Include files
15 #include "GaudiKernel/IInterface.h"
17 #include "GaudiKernel/SmartIF.h"
19 #include <list>
20 #include <string>
21 
22 // Forward class declaration
23 class IService;
24 
35 class GAUDI_API ISvcLocator : virtual public IInterface {
36 public:
39 
40 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
41 
46  const bool createIf = true ) {
47  SmartIF<IService>& s = service( typeName, createIf );
48  svc = s.get();
49  if ( svc ) {
50  svc->addRef(); // Needed to maintain the correct reference counting.
51  return StatusCode::SUCCESS;
52  }
53  return StatusCode::FAILURE;
54  }
55 
62  IInterface*& pinterface ) {
63  auto svc = service( typeName, false );
64  return svc ? svc->queryInterface( iid, (void**)&pinterface ) : StatusCode::FAILURE;
65  }
66 
72 // virtual StatusCode getService( const Gaudi::Utils::TypeNameString& name,
73 // IService*& svc,
74 // bool createIf ) = 0;
75 #endif
76 
78  virtual const std::list<IService*>& getServices() const = 0;
79 
81  virtual bool existsService( const std::string& name ) const = 0;
82 
83 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
84  template <class T>
86  StatusCode service( const Gaudi::Utils::TypeNameString& name, T*& svc, bool createIf = true ) {
87  if ( createIf ) {
88  IService* s;
89  StatusCode sc = getService( name, s, true );
90  if ( !sc.isSuccess() ) return sc; // Must check if initialization was OK!
91  }
92  return getService( name, T::interfaceID(), (IInterface*&)svc );
93  }
94 
96  template <class T>
97  StatusCode service( const std::string& type, const std::string& name, T*& svc, bool createIf = true ) {
98  return service( type + "/" + name, svc, createIf );
99  }
100 #endif
101 
103  virtual SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) = 0;
104 
106  template <typename T>
107  inline SmartIF<T> service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) {
108  return SmartIF<T>{service( typeName, createIf )};
109  }
110 
111  // try to access a different interface of the _current_ serviceLocator...
112  template <typename IFace>
114  return SmartIF<IFace>{this};
115  }
116 };
117 
118 #endif // GAUDI_ISVCLOCATOR_H
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
SmartIF< T > service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)
Returns a smart pointer to the requested interface of a service.
Definition: ISvcLocator.h:107
StatusCode service(const std::string &type, const std::string &name, T *&svc, bool createIf=true)
Templated method to access a service by type and name.
Definition: ISvcLocator.h:97
SmartIF< IFace > as()
Definition: ISvcLocator.h:113
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
Interface ID class.
Definition: IInterface.h:39
Helper class to parse a string of format "type/name".
General service interface definition.
Definition: IService.h:28
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, IService *&svc, const bool createIf=true)
Get a reference to the service given a service name.
Definition: ISvcLocator.h:45
bool isSuccess() const
Definition: StatusCode.h:361
#define DeclareInterfaceID(iface, major, minor)
Macro to declare the interface ID when using the new mechanism of extending and implementing interfac...
Definition: IInterface.h:23
virtual StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface)
Get a specific interface pointer given a service name and interface id.
Definition: ISvcLocator.h:61
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:97
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
#define GAUDI_API
Definition: Kernel.h:81
IInterface *PyHelper() getService(IInterface *app, char *name)
Definition: Bootstrap.cpp:239
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:257