| The Gaudi Framework v27r1

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