The Gaudi Framework  v29r0 (ff2e7097)
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  BootSvcLocator() = default;
44  ~BootSvcLocator() override = default;
45 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
47  IInterface*& pinterface ) override;
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 //------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------
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
166  std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
167  }
168  return nullptr;
169 }
170 
171 //------------------------------------------------------------------------------
173 {
174  //------------------------------------------------------------------------------
175  return createApplicationMgr( dllname, "ApplicationMgr" );
176 }
177 
178 //------------------------------------------------------------------------------
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 
194 
195 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
197  IInterface*& pinterface )
198 {
200  if ( s_appmgrInstance ) {
201  sc = s_svclocInstance->getService( typeName, iid, pinterface );
202  } else {
203  pinterface = s_bootInterface.get();
204  }
205  return sc;
206 }
208  const bool createIf )
209 {
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 
221 {
222  return s_appmgrInstance ? s_svclocInstance->getServices() : s_bootServices;
223 }
225 {
226  return s_appmgrInstance && s_svclocInstance->existsService( name );
227 }
228 
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
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 }
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 }
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
#define PyHelper(x)
Definition: Bootstrap.cpp:239
int PyHelper() ROOT_VERSION_CODE()
Definition: Bootstrap.cpp:266
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:319
#define PyFSMHelper(s)
Definition: Bootstrap.cpp:268
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
bool existsService(const std::string &name) const override
Definition: Bootstrap.cpp:224
~BootSvcLocator() override=default
T endl(T...args)
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:261
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:220
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:251
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:229
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
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:246
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.
StatusCode getService(const Gaudi::Utils::TypeNameString &typeName, const InterfaceID &iid, IInterface *&pinterface) override
Definition: Bootstrap.cpp:196
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:291
virtual StatusCode executeRun(int maxevt)=0
Process the maxevt events as a Run (beginRun() and endRun() called)
int maxevt
Definition: Bootstrap.cpp:279
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:256
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:147