| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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