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