| The Gaudi Framework v32r1 (f65d50dc)

Bootstrap.cpp
Go to the documentation of this file.
1 #include <iostream>
2
3 #include "GaudiKernel/Bootstrap.h"
4 #include "GaudiKernel/System.h"
5
6 #include "GaudiKernel/IAlgorithm.h"
7 #include "GaudiKernel/IAppMgrUI.h"
8 #include "GaudiKernel/IClassManager.h"
9 #include "GaudiKernel/IInterface.h"
10 #include "GaudiKernel/IService.h"
11 #include "GaudiKernel/ISvcLocator.h"
12
13 #include "GaudiKernel/ObjectFactory.h"
14 #include "GaudiKernel/Service.h"
15 #include <Gaudi/Algorithm.h>
16
17 #include "GaudiKernel/IEventProcessor.h"
18 #include "GaudiKernel/IJobOptionsSvc.h"
19
20 #include "RVersion.h"
21
22 namespace Gaudi {
23
39 class BootSvcLocator : public implements<ISvcLocator> {
40 public:
41 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
42 StatusCode getService( const Gaudi::Utils::TypeNameString& typeName, const InterfaceID& iid,
43 IInterface*& pinterface ) override;
44 StatusCode getService( const Gaudi::Utils::TypeNameString& typeName, IService*& svc,
45 const bool createIf = true ) override;
46 #endif
47 const std::list<IService*>& getServices() const override;
48 bool existsService( const std::string& name ) const override;
49
51 SmartIF<IService>& service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf = true ) override;
52 };
53 } // namespace Gaudi
54
55 static SmartIF<ISvcLocator> s_svclocInstance;
56 static SmartIF<IAppMgrUI> s_appmgrInstance;
57
58 //------------------------------------------------------------------------------
59 IAppMgrUI* Gaudi::createApplicationMgr( const std::string& dllname, const std::string& factname )
60 //------------------------------------------------------------------------------
61 {
62 // Allow not for multiple AppManagers: If already instantiated then just
63 // return it
64 if ( !s_appmgrInstance ) {
65 s_appmgrInstance = createApplicationMgrEx( dllname, factname );
66 s_svclocInstance = s_appmgrInstance;
67 }
68 return s_appmgrInstance.get();
69 }
70
71 //------------------------------------------------------------------------------
72 IAppMgrUI* Gaudi::createApplicationMgrEx( const std::string& dllname, const std::string& factname )
73 //------------------------------------------------------------------------------
74 {
75 // Create an instance of the application Manager
76 auto iif = make_SmartIF( Gaudi::createInstance( "ApplicationMgr", factname, dllname ) );
77 // Locate few interfaces of the Application Manager
78 return iif ? iif.as<IAppMgrUI>().get() : nullptr;
79 }
80
81 //------------------------------------------------------------------------------
82 ISvcLocator* Gaudi::svcLocator()
83 //------------------------------------------------------------------------------
84 //
85 // A dual-stage bootstrap mechanism is used to ensure an orderly startup
86 // of the ApplicationMgr. If this function is called before the singleton
87 // ApplicationMgr instance exists, a BootSvcLocator singleton instance is
88 // created. This responds to any subsequent requests for services by
89 // returning StatusCode::FAILURE, unless the ApplicationMgr singleton
90 // instance has been created in the interim. In this case, the BootSvcLocator
91 // forwards the request to the ApplicationMgr instance. The motivation for
92 // this is to handle static object instances where the constructor attempts
93 // to locate services and would otherwise instantiate the ApplicationMgr
94 // instance in an unorderly manner. This logic requires that the
95 // ApplicationMgr instance is created explicitly.
96
97 {
98 if ( !s_svclocInstance ) {
99 IAppMgrUI* iappmgr = createApplicationMgr();
100 if ( iappmgr ) {
101 s_svclocInstance = iappmgr;
102 if ( s_svclocInstance ) return s_svclocInstance;
103 }
104 //---Reverted change to create a Minimal SvcLocator in case is requested before AppMgr is created
105 // if( 0 == s_appmgrInstance ) {
106 // s_svclocInstance = new BootSvcLocator();
107 //} else {
108 // StatusCode sc = s_appmgrInstance->queryInterface( ISvcLocator::interfaceID(),
109 // pp_cast<void>(&s_svclocInstance) );
110 // if( sc.isSuccess() ) {
111 // return s_svclocInstance;
112 // }
113 //}
114 }
115 return s_svclocInstance;
116 }
117
118 //------------------------------------------------------------------------------
119 ISvcLocator* Gaudi::setInstance( ISvcLocator* newInstance )
120 //------------------------------------------------------------------------------
121 {
122 ISvcLocator* oldInstance = s_svclocInstance.get();
123 s_svclocInstance = newInstance;
124 s_appmgrInstance = s_svclocInstance;
125 return oldInstance;
126 }
127
128 //------------------------------------------------------------------------------
129 IAppMgrUI* Gaudi::setInstance( IAppMgrUI* newInstance )
130 //------------------------------------------------------------------------------
131 {
132 IAppMgrUI* oldInstance = s_appmgrInstance.get();
133 s_appmgrInstance = newInstance;
134 s_svclocInstance = s_appmgrInstance;
135 return oldInstance;
136 }
137
138 //------------------------------------------------------------------------------
139 IInterface* Gaudi::createInstance( const std::string& name, const std::string& factname, const std::string& dllname )
140 //------------------------------------------------------------------------------
141 {
142
143 IInterface* ii = ObjFactory::create( factname, nullptr ).release();
144 if ( ii ) return ii;
145 IService* is = Service::Factory::create( factname, name, nullptr ).release();
146 if ( is ) return is;
147 IAlgorithm* ia = Algorithm::Factory::create( factname, name, nullptr ).release();
148 if ( ia ) return ia;
149
150 void* libHandle = nullptr;
151 if ( System::loadDynamicLib( dllname, &libHandle ) ) {
152 ii = ObjFactory::create( factname, nullptr ).release();
153 if ( ii ) return ii;
154 is = Service::Factory::create( factname, name, nullptr ).release();
155 if ( is ) return is;
156 ia = Algorithm::Factory::create( factname, name, nullptr ).release();
157 if ( ia ) return ia;
158 } else {
159 // DLL library not loaded. Try in the local module
160 std::cout << System::getLastErrorString() << std::endl;
161 std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
162 }
163 return nullptr;
164 }
165
166 //------------------------------------------------------------------------------
167 IAppMgrUI* Gaudi::createApplicationMgr( const std::string& dllname ) {
168 //------------------------------------------------------------------------------
169 return createApplicationMgr( dllname, "ApplicationMgr" );
170 }
171
172 //------------------------------------------------------------------------------
173 IAppMgrUI* Gaudi::createApplicationMgr() {
174 //------------------------------------------------------------------------------
175 return createApplicationMgr( "GaudiCoreSvc", "ApplicationMgr" );
176 }
177
178 //=======================================================================
179 // BootSvcLocator
180 //=======================================================================
181
182 static std::list<IService*> s_bootServices;
183 static SmartIF<IService> s_bootService;
184 static SmartIF<IInterface> s_bootInterface;
185
186 using Gaudi::BootSvcLocator;
187
188 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
189 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName, const InterfaceID& iid,
190 IInterface*& pinterface ) {
191 StatusCode sc = StatusCode::FAILURE;
192 if ( s_appmgrInstance ) {
193 sc = s_svclocInstance->getService( typeName, iid, pinterface );
194 } else {
195 pinterface = s_bootInterface.get();
196 }
197 return sc;
198 }
199 StatusCode Gaudi::BootSvcLocator::getService( const Gaudi::Utils::TypeNameString& typeName, IService*& svc,
200 const bool createIf ) {
201 StatusCode sc = StatusCode::FAILURE;
202 if ( s_appmgrInstance ) {
203 sc = s_svclocInstance->getService( typeName, svc, createIf );
204 } else {
205 svc = s_bootService.get();
206 }
207 return sc;
208 }
209 #endif
210
211 const std::list<IService*>& Gaudi::BootSvcLocator::getServices() const {
212 return s_appmgrInstance ? s_svclocInstance->getServices() : s_bootServices;
213 }
214 bool Gaudi::BootSvcLocator::existsService( const std::string& name ) const {
215 return s_appmgrInstance && s_svclocInstance->existsService( name );
216 }
217
218 SmartIF<IService>& Gaudi::BootSvcLocator::service( const Gaudi::Utils::TypeNameString& typeName, const bool createIf ) {
219 return s_appmgrInstance ? s_svclocInstance->service( typeName, createIf ) : s_bootService;
220 }
221
222 #ifdef GAUDI_HASCLASSVISIBILITY
223 # pragma GCC visibility push( default )
224 #endif
225 // Python bootstrap helpers
226 extern "C" {
227 #define PyHelper( x ) py_bootstrap_##x
228 IInterface* PyHelper( createApplicationMgr )() { return Gaudi::createApplicationMgr(); }
229 IInterface* PyHelper( getService )( IInterface* app, char* name ) {
230 auto svcloc = SmartIF<ISvcLocator>( app );
231 return svcloc ? svcloc->service<IInterface>( name ).get() : nullptr;
232 }
233 bool PyHelper( setProperty )( IInterface* p, char* name, char* value ) {
234 auto prop = SmartIF<IProperty>( p );
235 return prop && prop->setProperty( name, value ).isSuccess();
236 }
237 const char* PyHelper( getProperty )( IInterface* p, char* name ) {
238 auto prop = SmartIF<IProperty>( p );
239 return prop ? prop->getProperty( name ).toString().c_str() : nullptr;
240 }
241 bool PyHelper( configureApp )( IInterface* app ) {
242 auto ui = SmartIF<IAppMgrUI>( app );
243 return ui && ui->configure().isSuccess();
244 }
245 bool PyHelper( addPropertyToCatalogue )( IInterface* p, char* comp, char* name, char* value ) {
246 auto jos = SmartIF<IJobOptionsSvc>( p );
247 return jos && jos->addPropertyToCatalogue( comp, Gaudi::Property<std::string>( name, value ) ).isSuccess();
248 }
249 int PyHelper( ROOT_VERSION_CODE )() { return ROOT_VERSION_CODE; }
250
251 #define PyFSMHelper( s ) \
252 bool py_bootstrap_fsm_##s( IInterface* i ) { \
253 auto fsm = SmartIF<IStateful>( i ); \
254 return fsm && fsm->s().isSuccess(); \
255 }
256
257 PyFSMHelper( configure ) PyFSMHelper( initialize ) PyFSMHelper( start ) PyFSMHelper( stop ) PyFSMHelper( finalize )
258 PyFSMHelper( terminate )
259
260 bool py_bootstrap_app_run( IInterface* i, int maxevt ) {
261 auto ep = SmartIF<IEventProcessor>( i );
262 return ep && ep->executeRun( maxevt ).isSuccess();
263 }
264 }
265 #ifdef GAUDI_HASCLASSVISIBILITY
266 # pragma GCC visibility pop
267 #endif
System.h
PyHelper
#define PyHelper(x)
Definition: Bootstrap.cpp:227
ROOT_VERSION_CODE
int PyHelper() ROOT_VERSION_CODE()
Definition: Bootstrap.cpp:249
PyFSMHelper
#define PyFSMHelper(s)
Definition: Bootstrap.cpp:251
SmartIF< IService >
implements
Base class used to implement the interfaces.
Definition: implements.h:9
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
IEventProcessor.h
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::BootSvcLocator::getServices
const std::list< IService * > & getServices() const override
Definition: Bootstrap.cpp:211
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
IAppMgrUI.h
IAlgorithm.h
std::endl
T endl(T... args)
ObjectFactory.h
addPropertyToCatalogue
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:245
Bootstrap.h
Gaudi::createApplicationMgrEx
GAUDI_API IAppMgrUI * createApplicationMgrEx(const std::string &dllname, const std::string &factname)
getProperty
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:237
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
IOTest.start
def start
Definition: IOTest.py:97
std::string
STL class.
Service.h
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
InterfaceID
Interface ID class.
Definition: IInterface.h:29
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:10
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
Algorithm.h
Gaudi::BootSvcLocator::service
SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Returns a smart pointer to a service.
Definition: Bootstrap.cpp:218
IService
General service interface definition.
Definition: IService.h:18
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
ISvcLocator::getService
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:35
setProperty
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:233
std::list< IService * >
IAlgorithm
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
IService.h
IAppMgrUI
Application Manager User Interface.
Definition: IAppMgrUI.h:20
Gaudi::setInstance
GAUDI_API ISvcLocator * setInstance(ISvcLocator *newInstance)
Set new instance of service locator.
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:332
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
Gaudi::BootSvcLocator::getService
StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface) override
Definition: Bootstrap.cpp:189
Gaudi::BootSvcLocator
A dual-stage boostrap mechanism is used to ensure an orderly startup of the ApplicationMgr.
Definition: Bootstrap.cpp:39
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
make_SmartIF
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:140
ISvcLocator::existsService
virtual bool existsService(const std::string &name) const =0
Check the existence of a service given a service name.
ISvcLocator::getServices
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:262
maxevt
int maxevt
Definition: Bootstrap.cpp:260
Gaudi::BootSvcLocator::existsService
bool existsService(const std::string &name) const override
Definition: Bootstrap.cpp:214
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
IClassManager.h
IInterface.h
std::cout
ISvcLocator.h
configureApp
bool PyHelper() configureApp(IInterface *app)
Definition: Bootstrap.cpp:241
getService
IInterface *PyHelper() getService(IInterface *app, char *name)
Definition: Bootstrap.cpp:229
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::createInstance
GAUDI_API IInterface * createInstance(const std::string &name, const std::string &factname, const std::string &ddlname)
Gaudi::createApplicationMgr
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
System::loadDynamicLib
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:135
IJobOptionsSvc.h