Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ConversionSvc.cpp
Go to the documentation of this file.
1 #define GAUDIKERNEL_CONVERSIONSVC_CPP
2 
10 #include "GaudiKernel/System.h"
11 
12 namespace {
13 
14  auto CnvTest = []( CLID clid ) { return [clid]( const auto& i ) { return i.clID() == clid; }; };
15 
16  enum CnvSvcAction {
17  CREATE_OBJ,
19  UPDATE_OBJ,
21  CREATE_REP,
23  UPDATE_REP,
25  };
26 } // namespace
27 
28 StatusCode ConversionSvc::makeCall( int typ, bool ignore_add, bool ignore_obj, bool update, IOpaqueAddress*& pAddress,
29  DataObject*& pObject ) {
30  if ( !pAddress && !ignore_add ) return Status::INVALID_ADDRESS;
31  if ( !pObject && !ignore_obj ) return Status::INVALID_OBJECT;
32  const CLID& obj_class =
33  ( pObject && !ignore_obj ) ? pObject->clID() : ( pAddress && !ignore_add ) ? pAddress->clID() : CLID_NULL;
34  IConverter* cnv = converter( obj_class );
35  if ( !cnv && pObject ) {
36  // Give it a try to autoload the class (dictionary) for which the converter is needed
37  loadConverter( pObject );
38  cnv = converter( obj_class );
39  }
40 
41  StatusCode status( StatusCode::FAILURE, true );
42  if ( cnv ) {
43  switch ( typ ) {
44  case CREATE_OBJ:
45  pObject = nullptr;
46  status = cnv->createObj( pAddress, pObject );
47  break;
48  case FILL_OBJ_REFS:
49  status = cnv->fillObjRefs( pAddress, pObject );
50  break;
51  case UPDATE_OBJ:
52  status = cnv->updateObj( pAddress, pObject );
53  break;
54  case UPDATE_OBJ_REFS:
55  status = cnv->updateObjRefs( pAddress, pObject );
56  break;
57  case CREATE_REP:
58  pAddress = nullptr;
59  status = cnv->createRep( pObject, pAddress );
60  break;
61  case FILL_REP_REFS:
62  status = cnv->fillRepRefs( pAddress, pObject );
63  break;
64  case UPDATE_REP:
65  status = cnv->updateRep( pAddress, pObject );
66  break;
67  case UPDATE_REP_REFS:
68  status = cnv->updateRepRefs( pAddress, pObject );
69  break;
70  default:
71  status = StatusCode::FAILURE;
72  break;
73  }
74  if ( status.isSuccess() && update ) { status = updateServiceState( pAddress ); }
75  return status;
76  }
77  status.ignore();
78  info() << "No converter for object ";
79  if ( pObject ) { msgStream() << System::typeinfoName( typeid( *pObject ) ); }
80  msgStream() << " CLID= " << obj_class << endmsg;
81  return Status::NO_CONVERTER;
82 }
83 
85 
87 
90  return makeCall( CREATE_OBJ, false, true, false, pAddress, refpObj );
91 }
92 
95  return makeCall( FILL_OBJ_REFS, false, true, true, pAddress, pObj );
96 }
97 
100  return makeCall( UPDATE_OBJ, false, true, false, pAddress, pObj );
101 }
102 
105  return makeCall( UPDATE_OBJ_REFS, false, true, true, pAddress, pObj );
106 }
107 
110  return makeCall( CREATE_REP, true, false, false, refpAddress, pObj );
111 }
112 
115  return makeCall( FILL_REP_REFS, true, false, false, pAddress, pObj );
116 }
117 
120  return makeCall( UPDATE_REP, true, false, false, pAddress, pObj );
121 }
122 
125  return makeCall( UPDATE_REP_REFS, true, false, false, pAddress, pObj );
126 }
127 
130  IConverter* cnv = nullptr;
131  auto i = std::find_if( m_workers.begin(), m_workers.end(), CnvTest( clid ) );
132  if ( i != m_workers.end() ) cnv = i->converter();
133  if ( !cnv ) {
134  StatusCode status = addConverter( clid );
135  if ( status.isSuccess() ) {
136  i = std::find_if( m_workers.begin(), m_workers.end(), CnvTest( clid ) );
137  if ( i != m_workers.end() ) cnv = i->converter();
138  }
139  }
140  return cnv;
141 }
142 
145  if ( !pDataSvc ) return StatusCode::SUCCESS; // Atlas does not use DataSvc
146  m_dataSvc = pDataSvc;
147  for ( auto& i : m_workers ) {
148  IConverter* cnv = i.converter();
149  if ( cnv && cnv->setDataProvider( m_dataSvc ).isFailure() ) { error() << "setting Data Provider" << endmsg; }
150  }
151  return StatusCode::SUCCESS;
152 }
153 
156 
159  m_addressCreator = creator;
160  for ( auto& i : m_workers ) {
161  auto* cnv = i.converter();
162  if ( cnv ) {
163  if ( cnv->setAddressCreator( m_addressCreator ).isFailure() ) { error() << "setting Address Creator" << endmsg; }
164  }
165  }
166  return StatusCode::SUCCESS;
167 }
168 
171 
174 
177 
180  // First look for the more specific converter
181  long typ = repSvcType();
182  IConverter* pConverter = createConverter( typ, clid, nullptr );
183  if ( pConverter ) {
184  StatusCode status = configureConverter( typ, clid, pConverter );
185  if ( status.isSuccess() ) {
186  status = initializeConverter( typ, clid, pConverter );
187  if ( status.isSuccess() ) {
188  status = activateConverter( typ, clid, pConverter );
189  if ( status.isSuccess() ) {
190  long conv_typ = pConverter->repSvcType();
191  const CLID& conv_clid = pConverter->objType();
192  typ = ( typ < 0xFF ) ? typ : typ & 0xFFFFFF00;
193  conv_typ = ( conv_typ < 0xFF ) ? conv_typ : conv_typ & 0xFFFFFF00;
194  if ( conv_typ == typ && conv_clid == clid ) { return addConverter( pConverter ); }
195  }
196  }
197  }
198  pConverter->release();
199  }
200  return Status::NO_CONVERTER;
201 }
202 
205  if ( pConverter ) {
206  const CLID& clid = pConverter->objType();
207  removeConverter( clid ).ignore();
208  m_workers.emplace_back( clid, pConverter );
209  return StatusCode::SUCCESS;
210  }
211  return Status::NO_CONVERTER;
212 }
213 
216 
218  [f = CnvTest( clid )]( const WorkerEntry& we ) { return !f( we ); } );
219  if ( i == std::end( m_workers ) ) return Status::NO_CONVERTER;
220  std::for_each( i, std::end( m_workers ), []( WorkerEntry& w ) { w.converter()->finalize().ignore(); } );
221  m_workers.erase( i, std::end( m_workers ) );
222  return StatusCode::SUCCESS;
223 }
224 
227 
230  // Release all workers.
231  for ( auto& i : m_workers ) {
232  if ( i.converter()->finalize().isFailure() ) { error() << "finalizing worker" << endmsg; }
233  }
234  m_workers.clear();
235  // release interfaces
236  m_addressCreator = nullptr;
237  m_dataSvc = nullptr;
238  m_cnvSvc = nullptr;
239  return Service::finalize();
240 }
241 
243 IConverter* ConversionSvc::createConverter( long typ, const CLID& clid, const ICnvFactory* /*fac*/ ) {
244  IConverter* pConverter = Converter::Factory::create( ConverterID( typ, clid ), serviceLocator().get() ).release();
245  if ( !pConverter ) {
246  typ = ( typ < 0xFF ) ? typ : typ & 0xFFFFFF00;
247  pConverter = Converter::Factory::create( ConverterID( typ, clid ), serviceLocator().get() ).release();
248  }
249  return pConverter;
250 }
251 
253 StatusCode ConversionSvc::configureConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
254  if ( !pConverter ) return Status::NO_CONVERTER;
255  pConverter->setConversionSvc( this ).ignore();
256  pConverter->setAddressCreator( m_addressCreator ).ignore();
257  pConverter->setDataProvider( m_dataSvc ).ignore();
258  return StatusCode::SUCCESS;
259 }
260 
262 StatusCode ConversionSvc::initializeConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
263  return pConverter ? pConverter->initialize() : Status::NO_CONVERTER;
264 }
265 
267 StatusCode ConversionSvc::activateConverter( long /* typ */, const CLID& /* clid */, IConverter* pConverter ) {
268  return pConverter ? StatusCode::SUCCESS : StatusCode( Status::NO_CONVERTER );
269 }
270 
272 const CLID& ConversionSvc::objType() const { return CLID_NULL; }
273 
275 long ConversionSvc::repSvcType() const { return m_type; }
276 
278 StatusCode ConversionSvc::connectOutput( const std::string& outputFile, const std::string& /* openMode */ ) {
279  return connectOutput( outputFile );
280 }
281 
284 
287 
289 StatusCode ConversionSvc::createAddress( long /* svc_type */, const CLID& /* clid */,
290  const std::string* /* par */, const unsigned long* /* ip */,
291  IOpaqueAddress*& refpAddress ) {
292  refpAddress = nullptr;
293  return StatusCode::FAILURE;
294 }
295 
298  refAddress.clear();
299  return StatusCode::FAILURE;
300 }
301 
303 StatusCode ConversionSvc::createAddress( long /* svc_type */, const CLID& /* clid */,
304  const std::string& /* refAddress */, IOpaqueAddress*& refpAddress ) {
305  refpAddress = nullptr;
306  return StatusCode::FAILURE;
307 }
308 
311  : base_class( name, svc ), m_cnvSvc( this ) {
312  m_type = type;
313  m_dataSvc = nullptr;
314  setAddressCreator( this ).ignore();
315 }
IConverter * converter()
Definition: ConversionSvc.h:71
StatusCode initialize() override
Definition: Service.cpp:60
SmartIF< IAddressCreator > & addressCreator() const override
Retrieve address creator facility.
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:25
The data converters are responsible to translate data from one representation into another...
Definition: IConverter.h:58
virtual const CLID & clID() const =0
Retrieve class information from link.
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)=0
Create the transient representation of an object.
StatusCode finalize() override
Definition: Service.cpp:164
StatusCode updateObjRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an updated transient object.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
virtual const CLID & objType() const =0
Retrieve the class type of objects the converter produces.
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:309
ConversionSvc(const std::string &name, ISvcLocator *svc, long type)
Standard Constructor.
bool isSuccess() const
Definition: StatusCode.h:267
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:46
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject) override
Implementation of IConverter: Update the references of an already converted object.
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.
Data provider interface definition.
virtual StatusCode updateRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Update the references of an already converted object.
bool isFailure() const
Definition: StatusCode.h:130
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.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
IConverter * converter(const CLID &wanted) override
Retrieve converter from list.
virtual const CLID & clID() const
Retrieve reference to class definition structure.
Definition: DataObject.cpp:56
StatusCode removeConverter(const CLID &clid) override
Remove converter object from conversion service (if present).
StatusCode setAddressCreator(IAddressCreator *creator) override
Set address creator facility.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
void clear(STATE_TYPE _i=std::ios_base::failbit)
Definition: MsgStream.h:171
SmartIF< IDataProviderSvc > & dataProvider() const override
Implementation of IConverter: Get Data provider service.
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.
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:8
T clear(T...args)
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.
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.
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 long repSvcType() const =0
Retrieve the class type of the data store the converter uses.
T begin(T...args)
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
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.
MsgStream & msgStream() const
Return an uninitialized MsgStream.
long repSvcType() const override
Retrieve the class type of the data store the converter uses.
StatusCode commitOutput(const std::string &output, bool do_commit) override
Commit pending output.
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual IConverter * createConverter(long typ, const CLID &clid, const ICnvFactory *fac)
Create new Converter using factory.
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:30
StatusCode finalize() override
stop the service.
virtual StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the converted object.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
virtual StatusCode activateConverter(long typ, const CLID &clid, IConverter *cnv)
Activate the new converter after initialization.
const CLID & objType() const override
Implementation of IConverter: dummy call.
StatusCode initialize() override
Initialize the service.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
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.
SmartIF< IConversionSvc > & conversionSvc() const override
Implementation of IConverter: Get conversion service the converter is connected to.