The Gaudi Framework  v33r0 (d5ea422b)
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 
51  StatusCode status( StatusCode::FAILURE, true );
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 
169  m_addressCreator = creator;
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 }
IConverter * converter()
Definition: ConversionSvc.h:81
virtual const CLID & objType() const =0
Retrieve the class type of objects the converter produces.
StatusCode initialize() override
Definition: Service.cpp:70
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:287
virtual StatusCode configureConverter(long typ, const CLID &clid, IConverter *cnv)
Configure the new converter before initialize is called.
StatusCode makeCall(int typ, bool ignore_add, bool ignore_obj, bool update, IOpaqueAddress *&pAddress, DataObject *&pObject)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
The data converters are responsible to translate data from one representation into another.
Definition: IConverter.h:68
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)=0
Create the transient representation of an object.
const CLID & objType() const override
Implementation of IConverter: dummy call.
StatusCode finalize() override
Definition: Service.cpp:174
StatusCode updateObjRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an updated transient object.
StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the converted representation of a transient object.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
ConversionSvc(const std::string &name, ISvcLocator *svc, long type)
Standard Constructor.
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
IAddressCreator interface definition.
virtual StatusCode setDataProvider(IDataProviderSvc *pService)=0
Set Data provider service.
Definition: ConversionSvc.h:56
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an already converted object.
long repSvcType() const override
Retrieve the class type of the data store the converter uses.
T end(T... args)
SmartIF< IAddressCreator > m_addressCreator
Pointer to the address creation service interface.
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)=0
Convert the transient object to the requested representation.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Data provider interface definition.
virtual StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the references of an already converted object.
virtual void loadConverter(DataObject *pObject)
Load converter or dictionary needed by the converter.
T partition(T... args)
virtual StatusCode updateObj(IOpaqueAddress *pAddress, DataObject *refpObject)=0
Update the transient object from the other representation.
STL class.
virtual StatusCode setConversionSvc(IConversionSvc *pService)=0
Set conversion service the converter is connected to.
IConverter * converter(const CLID &wanted) override
Retrieve converter from list.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
virtual const CLID & clID() const =0
Retrieve class information from link.
StatusCode removeConverter(const CLID &clid) override
Remove converter object from conversion service (if present).
StatusCode setAddressCreator(IAddressCreator *creator) override
Set address creator facility.
virtual long repSvcType() const =0
Retrieve the class type of the data store the converter uses.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
virtual StatusCode updateObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the references of an updated transient object.
StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Resolve the references of the converted object.
CnvSvcAction
virtual StatusCode updateRep(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the converted representation of a transient object.
SmartIF< IDataProviderSvc > & dataProvider() const override
Implementation of IConverter: Get Data provider service.
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.
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
T clear(T... args)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
bool isSuccess() const
Definition: StatusCode.h:361
virtual StatusCode initialize()=0
Initialize the converter.
StatusCode convertAddress(const IOpaqueAddress *pAddress, std::string &refAddress) override
Convert an address to string form.
virtual StatusCode updateServiceState(IOpaqueAddress *pAddress)
Update state of the service.
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to data provider service.
StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject) override
Implementation of IConverter: Create the transient representation of an object.
SmartIF< IAddressCreator > & addressCreator() const override
Retrieve address creator facility.
virtual StatusCode setAddressCreator(IAddressCreator *creator)=0
Set address creator facility.
StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Resolve the references of the created transient object.
T find_if(T... args)
virtual StatusCode initializeConverter(long typ, const CLID &clid, IConverter *cnv)
Initialize the new converter.
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:164
virtual StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the created transient object.
virtual unsigned long release()=0
Release Interface instance.
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:66
T begin(T... args)
SmartIF< IConversionSvc > m_cnvSvc
Pointer to the IConversionSvc interface of this.
StatusCode updateObj(IOpaqueAddress *pAddress, DataObject *refpObject) override
Implementation of IConverter: Update the transient object from the other representation.
StatusCode setDataProvider(IDataProviderSvc *pService) override
Implementation of IConverter: Set Data provider service.
long m_type
Conversion service type.
StatusCode commitOutput(const std::string &output, bool do_commit) override
Commit pending output.
constexpr static const auto FAILURE
Definition: StatusCode.h:97
virtual IConverter * createConverter(long typ, const CLID &clid, const ICnvFactory *fac)
Create new Converter using factory.
bool isFailure() const
Definition: StatusCode.h:141
Opaque address interface definition.
StatusCode addConverter(const CLID &clid) override
Add converter object to conversion service.
T for_each(T... args)
std::vector< WorkerEntry > m_workers
List of conversion workers.
StatusCode setConversionSvc(IConversionSvc *svc) override
Implementation of IConverter: Set conversion service the converter is connected to.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
StatusCode finalize() override
stop the service.
virtual StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the converted object.
virtual StatusCode activateConverter(long typ, const CLID &clid, IConverter *cnv)
Activate the new converter after initialization.
StatusCode initialize() override
Initialize the service.
SmartIF< IConversionSvc > & conversionSvc() const override
Implementation of IConverter: Get conversion service the converter is connected to.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
StatusCode connectOutput(const std::string &outputFile, const std::string &openMode) override
Connect the output file to the service with open mode.
virtual StatusCode finalize()=0
Terminate the converter.