The Gaudi Framework  v36r1 (3e2fb5a8)
ConversionSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #define GAUDIKERNEL_CONVERSIONSVC_CPP
12 
14 #include "GaudiKernel/Converter.h"
15 #include "GaudiKernel/DataObject.h"
16 #include "GaudiKernel/IConverter.h"
19 #include "GaudiKernel/MsgStream.h"
20 #include "GaudiKernel/System.h"
21 
22 namespace {
23 
24  auto CnvTest = []( CLID clid ) { return [clid]( const auto& i ) { return i.clID() == clid; }; };
25 
26  enum CnvSvcAction {
27  CREATE_OBJ,
29  UPDATE_OBJ,
31  CREATE_REP,
33  UPDATE_REP,
35  };
36 } // namespace
37 
38 StatusCode ConversionSvc::makeCall( int typ, bool ignore_add, bool ignore_obj, bool update, IOpaqueAddress*& pAddress,
39  DataObject*& pObject ) {
40  if ( !pAddress && !ignore_add ) return Status::INVALID_ADDRESS;
41  if ( !pObject && !ignore_obj ) return Status::INVALID_OBJECT;
42  const CLID& obj_class =
43  ( pObject && !ignore_obj ) ? pObject->clID() : ( pAddress && !ignore_add ) ? pAddress->clID() : CLID_NULL;
44  IConverter* cnv = converter( obj_class );
45  if ( !cnv && pObject ) {
46  // Give it a try to autoload the class (dictionary) for which the converter is needed
47  loadConverter( pObject );
48  cnv = converter( obj_class );
49  }
50 
52  if ( cnv ) {
53  switch ( typ ) {
54  case CREATE_OBJ:
55  pObject = nullptr;
56  status = cnv->createObj( pAddress, pObject );
57  break;
58  case FILL_OBJ_REFS:
59  status = cnv->fillObjRefs( pAddress, pObject );
60  break;
61  case UPDATE_OBJ:
62  status = cnv->updateObj( pAddress, pObject );
63  break;
64  case UPDATE_OBJ_REFS:
65  status = cnv->updateObjRefs( pAddress, pObject );
66  break;
67  case CREATE_REP:
68  pAddress = nullptr;
69  status = cnv->createRep( pObject, pAddress );
70  break;
71  case FILL_REP_REFS:
72  status = cnv->fillRepRefs( pAddress, pObject );
73  break;
74  case UPDATE_REP:
75  status = cnv->updateRep( pAddress, pObject );
76  break;
77  case UPDATE_REP_REFS:
78  status = cnv->updateRepRefs( pAddress, pObject );
79  break;
80  default:
81  status = StatusCode::FAILURE;
82  break;
83  }
84  if ( status.isSuccess() && update ) { status = updateServiceState( pAddress ); }
85  return status;
86  }
87  status.ignore();
88  info() << "No converter for object ";
89  if ( pObject ) { msgStream() << System::typeinfoName( typeid( *pObject ) ); }
90  msgStream() << " CLID= " << obj_class << endmsg;
91  return Status::NO_CONVERTER;
92 }
93 
95 
97 
100  return makeCall( CREATE_OBJ, false, true, false, pAddress, refpObj );
101 }
102 
105  return makeCall( FILL_OBJ_REFS, false, true, true, pAddress, pObj );
106 }
107 
110  return makeCall( UPDATE_OBJ, false, true, false, pAddress, pObj );
111 }
112 
115  return makeCall( UPDATE_OBJ_REFS, false, true, true, pAddress, pObj );
116 }
117 
120  return makeCall( CREATE_REP, true, false, false, refpAddress, pObj );
121 }
122 
125  return makeCall( FILL_REP_REFS, true, false, false, pAddress, pObj );
126 }
127 
130  return makeCall( UPDATE_REP, true, false, false, pAddress, pObj );
131 }
132 
135  return makeCall( UPDATE_REP_REFS, true, false, false, pAddress, pObj );
136 }
137 
140  IConverter* cnv = nullptr;
141  auto i = std::find_if( m_workers.begin(), m_workers.end(), CnvTest( clid ) );
142  if ( i != m_workers.end() ) cnv = i->converter();
143  if ( !cnv ) {
144  StatusCode status = addConverter( clid );
145  if ( status.isSuccess() ) {
146  i = std::find_if( m_workers.begin(), m_workers.end(), CnvTest( clid ) );
147  if ( i != m_workers.end() ) cnv = i->converter();
148  }
149  }
150  return cnv;
151 }
152 
155  if ( !pDataSvc ) return StatusCode::SUCCESS; // Atlas does not use DataSvc
156  m_dataSvc = pDataSvc;
157  for ( auto& i : m_workers ) {
158  IConverter* cnv = i.converter();
159  if ( cnv && cnv->setDataProvider( m_dataSvc ).isFailure() ) { error() << "setting Data Provider" << endmsg; }
160  }
161  return StatusCode::SUCCESS;
162 }
163 
166 
170  for ( auto& i : m_workers ) {
171  auto* cnv = i.converter();
172  if ( cnv ) {
173  if ( cnv->setAddressCreator( m_addressCreator ).isFailure() ) { error() << "setting Address Creator" << endmsg; }
174  }
175  }
176  return StatusCode::SUCCESS;
177 }
178 
181 
184 
187 
190  // First look for the more specific converter
191  long typ = repSvcType();
192  IConverter* pConverter = createConverter( typ, clid, nullptr );
193  if ( pConverter ) {
194  StatusCode status = configureConverter( typ, clid, pConverter );
195  if ( status.isSuccess() ) {
196  status = initializeConverter( typ, clid, pConverter );
197  if ( status.isSuccess() ) {
198  status = activateConverter( typ, clid, pConverter );
199  if ( status.isSuccess() ) {
200  long conv_typ = pConverter->repSvcType();
201  const CLID& conv_clid = pConverter->objType();
202  typ = ( typ < 0xFF ) ? typ : typ & 0xFFFFFF00;
203  conv_typ = ( conv_typ < 0xFF ) ? conv_typ : conv_typ & 0xFFFFFF00;
204  if ( conv_typ == typ && conv_clid == clid ) { return addConverter( pConverter ); }
205  }
206  }
207  }
208  pConverter->release();
209  }
210  return Status::NO_CONVERTER;
211 }
212 
215  if ( pConverter ) {
216  const CLID& clid = pConverter->objType();
217  removeConverter( clid ).ignore();
218  m_workers.emplace_back( clid, pConverter );
219  return StatusCode::SUCCESS;
220  }
221  return Status::NO_CONVERTER;
222 }
223 
226 
228  [f = CnvTest( clid )]( const WorkerEntry& we ) { return !f( we ); } );
229  if ( i == std::end( m_workers ) ) return Status::NO_CONVERTER;
230  std::for_each( i, std::end( m_workers ), []( WorkerEntry& w ) { w.converter()->finalize().ignore(); } );
231  m_workers.erase( i, std::end( m_workers ) );
232  return StatusCode::SUCCESS;
233 }
234 
237 
240  // Release all workers.
241  for ( auto& i : m_workers ) {
242  if ( i.converter()->finalize().isFailure() ) { error() << "finalizing worker" << endmsg; }
243  }
244  m_workers.clear();
245  // release interfaces
246  m_addressCreator = nullptr;
247  m_dataSvc = nullptr;
248  m_cnvSvc = nullptr;
249  return Service::finalize();
250 }
251 
253 IConverter* ConversionSvc::createConverter( long typ, const CLID& clid, const ICnvFactory* /*fac*/ ) {
254  IConverter* pConverter = Converter::Factory::create( ConverterID( typ, clid ), serviceLocator().get() ).release();
255  if ( !pConverter ) {
256  typ = ( typ < 0xFF ) ? typ : typ & 0xFFFFFF00;
257  pConverter = Converter::Factory::create( ConverterID( typ, clid ), serviceLocator().get() ).release();
258  }
259  return pConverter;
260 }
261 
263 StatusCode ConversionSvc::configureConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
264  if ( !pConverter ) return Status::NO_CONVERTER;
265  pConverter->setConversionSvc( this ).ignore();
266  pConverter->setAddressCreator( m_addressCreator ).ignore();
267  pConverter->setDataProvider( m_dataSvc ).ignore();
268  return StatusCode::SUCCESS;
269 }
270 
272 StatusCode ConversionSvc::initializeConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
273  return pConverter ? pConverter->initialize() : Status::NO_CONVERTER;
274 }
275 
277 StatusCode ConversionSvc::activateConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
278  return pConverter ? StatusCode::SUCCESS : StatusCode( Status::NO_CONVERTER );
279 }
280 
282 const CLID& ConversionSvc::objType() const { return CLID_NULL; }
283 
285 long ConversionSvc::repSvcType() const { return m_type; }
286 
288 StatusCode ConversionSvc::connectOutput( const std::string& outputFile, const std::string& /* openMode */ ) {
289  return connectOutput( outputFile );
290 }
291 
294 
297 
299 StatusCode ConversionSvc::createAddress( long /* svc_type */, const CLID& /* clid */,
300  const std::string* /* par */, const unsigned long* /* ip */,
301  IOpaqueAddress*& refpAddress ) {
302  refpAddress = nullptr;
303  return StatusCode::FAILURE;
304 }
305 
308  refAddress.clear();
309  return StatusCode::FAILURE;
310 }
311 
313 StatusCode ConversionSvc::createAddress( long /* svc_type */, const CLID& /* clid */,
314  const std::string& /* refAddress */, IOpaqueAddress*& refpAddress ) {
315  refpAddress = nullptr;
316  return StatusCode::FAILURE;
317 }
318 
321  : base_class( name, svc ), m_cnvSvc( this ) {
322  m_type = type;
323  m_dataSvc = nullptr;
324  setAddressCreator( this ).ignore();
325 }
UPDATE_REP_REFS
@ UPDATE_REP_REFS
Definition: PersistencySvc.cpp:57
std::for_each
T for_each(T... args)
ConversionSvc::addConverter
StatusCode addConverter(const CLID &clid) override
Add converter object to conversion service.
Definition: ConversionSvc.cpp:189
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
IAddressCreator
Definition: IAddressCreator.h:38
std::string
STL class.
ConversionSvc::m_addressCreator
SmartIF< IAddressCreator > m_addressCreator
Pointer to the address creation service interface.
Definition: ConversionSvc.h:214
ConversionSvc::objType
const CLID & objType() const override
Implementation of IConverter: dummy call.
Definition: ConversionSvc.cpp:282
ConversionSvc::createAddress
StatusCode createAddress(long svc_type, const CLID &clid, const std::string *par, const unsigned long *ip, IOpaqueAddress *&refpAddress) override
Create a Generic address using explicit arguments to identify a single object.
Definition: ConversionSvc.cpp:299
IConverter::fillRepRefs
virtual StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the converted object.
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:355
IConverter::updateRepRefs
virtual StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the references of an already converted object.
ConversionSvc::configureConverter
virtual StatusCode configureConverter(long typ, const CLID &clid, IConverter *cnv)
Configure the new converter before initialize is called.
Definition: ConversionSvc.cpp:263
System.h
CnvSvcAction
CnvSvcAction
Definition: PersistencySvc.cpp:49
ConversionSvc::makeCall
StatusCode makeCall(int typ, bool ignore_add, bool ignore_obj, bool update, IOpaqueAddress *&pAddress, DataObject *&pObject)
Definition: ConversionSvc.cpp:38
CREATE_OBJ
@ CREATE_OBJ
Definition: PersistencySvc.cpp:50
IOpaqueAddress
Definition: IOpaqueAddress.h:33
std::find_if
T find_if(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
ConversionSvc::ConversionSvc
ConversionSvc(const std::string &name, ISvcLocator *svc, long type)
Standard Constructor.
Definition: ConversionSvc.cpp:320
IConverter::setConversionSvc
virtual StatusCode setConversionSvc(IConversionSvc *pService)=0
Set conversion service the converter is connected to.
IConverter
Definition: IConverter.h:68
ConversionSvc::createObj
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Implementation of IConverter: Create the transient representation of an object.
Definition: ConversionSvc.cpp:99
ConversionSvc::converter
IConverter * converter(const CLID &wanted) override
Retrieve converter from list.
Definition: ConversionSvc.cpp:139
IConverter::createObj
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)=0
Create the transient representation of an object.
ConversionSvc::WorkerEntry
Definition: ConversionSvc.h:56
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
ConversionSvc::activateConverter
virtual StatusCode activateConverter(long typ, const CLID &clid, IConverter *cnv)
Activate the new converter after initialization.
Definition: ConversionSvc.cpp:277
ConversionSvc::WorkerEntry::converter
IConverter * converter()
Definition: ConversionSvc.h:81
ConversionSvc::loadConverter
virtual void loadConverter(DataObject *pObject)
Load converter or dictionary needed by the converter.
Definition: ConversionSvc.cpp:94
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
IDataProviderSvc.h
std::string::clear
T clear(T... args)
FILL_OBJ_REFS
@ FILL_OBJ_REFS
Definition: PersistencySvc.cpp:51
ConversionSvc::fillObjRefs
StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Resolve the references of the created transient object.
Definition: ConversionSvc.cpp:104
Converter.h
IConverter::setDataProvider
virtual StatusCode setDataProvider(IDataProviderSvc *pService)=0
Set Data provider service.
ConversionSvc::updateObjRefs
StatusCode updateObjRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an updated transient object.
Definition: ConversionSvc.cpp:114
ConversionSvc::updateServiceState
virtual StatusCode updateServiceState(IOpaqueAddress *pAddress)
Update state of the service.
Definition: ConversionSvc.cpp:96
ConversionSvc::removeConverter
StatusCode removeConverter(const CLID &clid) override
Remove converter object from conversion service (if present).
Definition: ConversionSvc.cpp:225
ConverterID
Definition: Converter.h:144
ConversionSvc::convertAddress
StatusCode convertAddress(const IOpaqueAddress *pAddress, std::string &refAddress) override
Convert an address to string form.
Definition: ConversionSvc.cpp:307
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:391
ConversionSvc::conversionSvc
SmartIF< IConversionSvc > & conversionSvc() const override
Implementation of IConverter: Get conversion service the converter is connected to.
Definition: ConversionSvc.cpp:186
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
IConverter::createRep
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)=0
Convert the transient object to the requested representation.
ConversionSvc::connectOutput
StatusCode connectOutput(const std::string &outputFile, const std::string &openMode) override
Connect the output file to the service with open mode.
Definition: ConversionSvc.cpp:288
IOpaqueAddress.h
CommonMessaging
Definition: CommonMessaging.h:66
ConversionSvc::setConversionSvc
StatusCode setConversionSvc(IConversionSvc *svc) override
Implementation of IConverter: Set conversion service the converter is connected to.
Definition: ConversionSvc.cpp:183
IConverter::setAddressCreator
virtual StatusCode setAddressCreator(IAddressCreator *creator)=0
Set address creator facility.
CREATE_REP
@ CREATE_REP
Definition: PersistencySvc.cpp:54
IConverter::updateObj
virtual StatusCode updateObj(IOpaqueAddress *pAddress, DataObject *refpObject)=0
Update the transient object from the other representation.
IOpaqueAddress::clID
virtual const CLID & clID() const =0
Retrieve class information from link.
ConversionSvc::updateObj
StatusCode updateObj(IOpaqueAddress *pAddress, DataObject *refpObject) override
Implementation of IConverter: Update the transient object from the other representation.
Definition: ConversionSvc.cpp:109
Write.creator
creator
Definition: Write.py:22
ConversionSvc::m_type
long m_type
Conversion service type.
Definition: ConversionSvc.h:218
SmartIF< IDataProviderSvc >
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
ConversionSvc::updateRep
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the converted representation of a transient object.
Definition: ConversionSvc.cpp:129
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
ConversionSvc::updateRepRefs
StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an already converted object.
Definition: ConversionSvc.cpp:134
IConverter::repSvcType
virtual long repSvcType() const =0
Retrieve the class type of the data store the converter uses.
UPDATE_REP
@ UPDATE_REP
Definition: PersistencySvc.cpp:56
IConverter.h
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:156
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
ConversionSvc::m_cnvSvc
SmartIF< IConversionSvc > m_cnvSvc
Pointer to the IConversionSvc interface of this.
Definition: ConversionSvc.h:216
gaudirun.type
type
Definition: gaudirun.py:154
ConversionSvc::setDataProvider
StatusCode setDataProvider(IDataProviderSvc *pService) override
Implementation of IConverter: Set Data provider service.
Definition: ConversionSvc.cpp:154
UPDATE_OBJ_REFS
@ UPDATE_OBJ_REFS
Definition: PersistencySvc.cpp:53
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConversionSvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: ConversionSvc.cpp:236
DataObject.h
ConversionSvc::createConverter
virtual IConverter * createConverter(long typ, const CLID &clid, const ICnvFactory *fac)
Create new Converter using factory.
Definition: ConversionSvc.cpp:253
IConverter::updateObjRefs
virtual StatusCode updateObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the references of an updated transient object.
std::begin
T begin(T... args)
IConverter::updateRep
virtual StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the converted representation of a transient object.
ConversionSvc::finalize
StatusCode finalize() override
stop the service.
Definition: ConversionSvc.cpp:239
GaudiPython.Pythonizations.update
update
Definition: Pythonizations.py:131
ConversionSvc::fillRepRefs
StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Resolve the references of the converted object.
Definition: ConversionSvc.cpp:124
ConversionSvc::commitOutput
StatusCode commitOutput(const std::string &output, bool do_commit) override
Commit pending output.
Definition: ConversionSvc.cpp:296
DataObject
Definition: DataObject.h:40
UPDATE_OBJ
@ UPDATE_OBJ
Definition: PersistencySvc.cpp:52
ConversionSvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: ConversionSvc.cpp:119
IConverter::initialize
virtual StatusCode initialize()=0
Initialize the converter.
IConverter::fillObjRefs
virtual StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the created transient object.
std::end
T end(T... args)
IDataProviderSvc
Definition: IDataProviderSvc.h:53
IConverter::objType
virtual const CLID & objType() const =0
Retrieve the class type of objects the converter produces.
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ConversionSvc::addressCreator
SmartIF< IAddressCreator > & addressCreator() const override
Retrieve address creator facility.
Definition: ConversionSvc.cpp:180
ConversionSvc::initializeConverter
virtual StatusCode initializeConverter(long typ, const CLID &clid, IConverter *cnv)
Initialize the new converter.
Definition: ConversionSvc.cpp:272
ConversionSvc::setAddressCreator
StatusCode setAddressCreator(IAddressCreator *creator) override
Set address creator facility.
Definition: ConversionSvc.cpp:168
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
ConversionSvc.h
ConversionSvc::m_dataSvc
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to data provider service.
Definition: ConversionSvc.h:212
DataObject::clID
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:66
std::partition
T partition(T... args)
ConversionSvc::repSvcType
long repSvcType() const override
Retrieve the class type of the data store the converter uses.
Definition: ConversionSvc.cpp:285
ConversionSvc::dataProvider
SmartIF< IDataProviderSvc > & dataProvider() const override
Implementation of IConverter: Get Data provider service.
Definition: ConversionSvc.cpp:165
FILL_REP_REFS
@ FILL_REP_REFS
Definition: PersistencySvc.cpp:55
MsgStream.h
ConversionSvc::m_workers
std::vector< WorkerEntry > m_workers
List of conversion workers.
Definition: ConversionSvc.h:220
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
IConversionSvc
Definition: IConversionSvc.h:47