Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
14 #include "GaudiKernel/Service.h"
15 #include <Gaudi/Algorithm.h>
16 
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 )
43  IInterface*& pinterface ) override;
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 //------------------------------------------------------------------------------
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 //------------------------------------------------------------------------------
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
161  std::cout << "Gaudi::Bootstrap: Not found DLL " << dllname << std::endl;
162  }
163  return nullptr;
164 }
165 
166 //------------------------------------------------------------------------------
168  //------------------------------------------------------------------------------
169  return createApplicationMgr( dllname, "ApplicationMgr" );
170 }
171 
172 //------------------------------------------------------------------------------
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 
187 
188 #if !defined( GAUDI_V22_API ) || defined( G22_NEW_SVCLOCATOR )
190  IInterface*& pinterface ) {
192  if ( s_appmgrInstance ) {
193  sc = s_svclocInstance->getService( typeName, iid, pinterface );
194  } else {
195  pinterface = s_bootInterface.get();
196  }
197  return sc;
198 }
200  const bool createIf ) {
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 
212  return s_appmgrInstance ? s_svclocInstance->getServices() : s_bootServices;
213 }
215  return s_appmgrInstance && s_svclocInstance->existsService( name );
216 }
217 
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
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 }
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 }
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
#define PyHelper(x)
Definition: Bootstrap.cpp:227
int PyHelper() ROOT_VERSION_CODE()
Definition: Bootstrap.cpp:249
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:352
#define PyFSMHelper(s)
Definition: Bootstrap.cpp:251
bool isSuccess() const
Definition: StatusCode.h:267
bool existsService(const std::string &name) const override
Definition: Bootstrap.cpp:214
T endl(T...args)
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:245
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:211
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:237
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:76
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
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:218
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
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
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:233
start
Definition: IOTest.py:97
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:189
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:39
constexpr static const auto FAILURE
Definition: StatusCode.h:86
SmartIF< IFace > make_SmartIF(IFace *iface)
Definition: SmartIF.h:140
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:273
virtual StatusCode executeRun(int maxevt)=0
Process the maxevt events as a Run.
int maxevt
Definition: Bootstrap.cpp:260
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
virtual StatusCode configure()=0
Configure the job.
bool PyHelper() configureApp(IInterface *app)
Definition: Bootstrap.cpp:241
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:146