The Gaudi Framework  v32r2 (46d42edc)
Application.cpp
Go to the documentation of this file.
1 #include <Gaudi/Application.h>
11 #include <GaudiKernel/Property.h>
12 #include <gsl/span>
13 
14 #define GAUDI_ASSERT_THROW_NAME( cond, msg, name ) \
15  if ( !cond ) throw GaudiException{msg, name, StatusCode::FAILURE};
16 
17 #define GAUDI_ASSERT_THROW( cond, msg ) \
18  if ( !cond ) throw GaudiException{msg, this->name(), StatusCode::FAILURE};
19 
20 namespace {
21  void consumeOptions( SmartIF<IProperty> prop, Gaudi::Application::Options& options ) {
22  GAUDI_ASSERT_THROW_NAME( prop, "invalid IProperty", "Gaudi::Application" );
24  const auto prefix_size = prefix.size();
25 
26  auto opt = options.upper_bound( prefix );
27  while ( opt != end( options ) && std::string_view( opt->first ).substr( 0, prefix_size ) == prefix ) {
28  GAUDI_ASSERT_THROW_NAME( prop->setProperty( opt->first.substr( prefix_size ), opt->second ),
29  "failure setting " + opt->first + " to " + opt->second, "Gaudi::Application" );
30  // drop processed option and increase iterator
31  opt = options.erase( opt );
32  }
33  }
34 } // namespace
35 
36 Gaudi::Application::Factory::ReturnType Gaudi::Application::create( std::string_view type, Options opts ) {
37  if ( type == "Gaudi::Application" ) { return std::make_unique<Application>( std::move( opts ) ); }
38  return Factory::create( type, std::move( opts ) );
39 }
40 
42  // # Prepare the application
43  // - instantiate
45  GAUDI_ASSERT_THROW_NAME( app, "failure instantiating ApplicationMgr", "Gaudi::Application" );
46 
47  // - main configuration
48  consumeOptions( app.as<IProperty>(), options );
49  // - start minimal services
50  GAUDI_ASSERT_THROW_NAME( app->configure(), "failure creating basic services", "Gaudi::Application" );
51 
52  consumeOptions( SmartIF<IMessageSvc>{app}.as<IProperty>(), options );
53 
54  // - prepare job configuration
55  {
56  auto sloc = app.as<ISvcLocator>();
57  auto jos = sloc->service<IJobOptionsSvc>( "JobOptionsSvc" );
58  std::for_each( begin( options ), end( options ), [&jos]( const auto& item ) {
59  std::string_view name = item.first;
60  const auto sep_pos = name.find_last_of( '.' );
61  std::string_view client = name.substr( 0, sep_pos );
62  name.remove_prefix( sep_pos + 1 );
63  jos->addPropertyToCatalogue( std::string{client}, Gaudi::Property<std::string>{std::string{name}, item.second} );
64  } );
65  }
66 }
67 
68 Gaudi::Application::~Application() { app->terminate().ignore(); }
69 
71  auto prop = app.as<IProperty>();
72  auto processor = app.as<IEventProcessor>();
73 
75  evtMax.assign( prop->getProperty( "EvtMax" ) );
76 
77  // - get ready to process events
78  if ( app->initialize() ) {
79  if ( app->start() ) {
80  // - main processing loop
81  if ( !processor->executeRun( evtMax ) ) setAppReturnCode( prop, Gaudi::ReturnCode::GenericFailure );
82 
83  app->stop().ignore();
84  } else
86  app->finalize().ignore();
87  } else
89  return getAppReturnCode( prop );
90 }
91 
92 namespace {
93  struct c_opt_t {
94  char* key;
95  char* value;
96  };
97 } // namespace
98 
99 #ifdef GAUDI_HASCLASSVISIBILITY
100 # pragma GCC visibility push( default )
101 #endif
102 
103 extern "C" {
104 // helper to invoke the factory from Python
105 Gaudi::Application* _py_Gaudi__Application__create( const char* name, const c_opt_t* options, long n ) {
107  gsl::span py_opts{options, n};
108  for ( auto& opt : py_opts ) { opts[opt.key] = opt.value; }
109  return Gaudi::Application::create( name, std::move( opts ) ).release();
110 }
111 int _py_Gaudi__Application__run( Gaudi::Application* self ) { return self->run(); }
113 }
114 
115 #ifdef GAUDI_HASCLASSVISIBILITY
116 # pragma GCC visibility pop
117 #endif
int getAppReturnCode(const SmartIF< IProperty > &appmgr)
Get the application (current) return code.
Definition: AppReturnCode.h:69
Gaudi::Application * _py_Gaudi__Application__create(const char *name, const c_opt_t *options, long n)
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 GAUDI_ASSERT_THROW_NAME(cond, msg, name)
Definition: Application.cpp:14
virtual int run()
Implement the application main logic:
Definition: Application.cpp:70
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:107
constexpr int GenericFailure
Definition: AppReturnCode.h:18
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
Main interface for the JobOptions service.
string prefix
Definition: gaudirun.py:333
def end
Definition: IOTest.py:113
def create(cls, appType, opts)
Definition: __init__.py:86
StatusCode setAppReturnCode(SmartIF< IProperty > &appmgr, int value, bool force=false)
Set the application return code.
Definition: AppReturnCode.h:49
T move(T... args)
Gaudi application entry point.
Definition: __init__.py:62
Application(Options opts)
Construct and configure the application from the provided options.
Definition: Application.cpp:41
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
int _py_Gaudi__Application__run(Gaudi::Application *self)
virtual ~Application()
Definition: Application.cpp:68
The IEventProcessor is the interface to process events.
AttribStringParser::Iterator begin(const AttribStringParser &parser)
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
T for_each(T... args)
void _py_Gaudi__Application__delete(Gaudi::Application *self)
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
std::map< std::string, std::string > Options
Definition: Application.h:19