Source

mana-core-gaudikernel / src / Lib / ConversionSvc.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// $Id: ConversionSvc.cpp,v 1.22 2007/09/24 08:58:21 hmd Exp $
#define  GAUDIKERNEL_CONVERSIONSVC_CPP

#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/SvcFactory.h"
#include "GaudiKernel/CnvFactory.h"
#include "GaudiKernel/DataObject.h"
#include "GaudiKernel/System.h"
#include "GaudiKernel/IConverter.h"
#include "GaudiKernel/IDataProviderSvc.h"
#include "GaudiKernel/IOpaqueAddress.h"
#include "GaudiKernel/ConversionSvc.h"

using ROOT::Reflex::PluginService;

namespace {
  template<class T>
  std::string name_of_iface(const T& t)
  {
    SmartIF<INamedInterface> si(t);
    return si->name();
  }
}

enum CnvSvcAction   {
  CREATE_OBJ,
  FILL_OBJ_REFS,
  UPDATE_OBJ,
  UPDATE_OBJ_REFS,
  CREATE_REP,
  FILL_REP_REFS,
  UPDATE_REP,
  UPDATE_REP_REFS
};

StatusCode ConversionSvc::makeCall(int typ,
                                   bool ignore_add,
                                   bool ignore_obj,
                                   bool update,
                                   IOpaqueAddress*& pAddress,
                                   DataObject*& pObject)      {
  if ( 0 != pAddress || ignore_add )    {
    if ( 0 != pObject  || ignore_obj )    {
      const CLID&  obj_class =
        (0 != pObject && !ignore_obj) ? pObject->clID()
        : (0 != pAddress && !ignore_add)
        ? pAddress->clID()
        : CLID_NULL;
      IConverter*  cnv  = converter(obj_class);
      if ( !cnv && pObject ) {
        //Give it a try to autoload the class (dictionary) for which the converter is needed
        loadConverter( pObject);
        cnv  = converter(obj_class);
      }

      StatusCode status(StatusCode::FAILURE,true);
      if ( 0 != cnv )   {
        switch(typ)   {
        case CREATE_OBJ:
          pObject = 0;
          status = cnv->createObj(pAddress, pObject);
          break;
        case FILL_OBJ_REFS:
          status = cnv->fillObjRefs(pAddress, pObject);
          break;
        case UPDATE_OBJ:
          status = cnv->updateObj(pAddress, pObject);
          break;
        case UPDATE_OBJ_REFS:
          status = cnv->updateObjRefs(pAddress, pObject);
          break;
        case CREATE_REP:
          pAddress = 0;
          status = cnv->createRep(pObject, pAddress);
          break;
        case FILL_REP_REFS:
          status = cnv->fillRepRefs(pAddress, pObject);
          break;
        case UPDATE_REP:
          status = cnv->updateRep(pAddress, pObject);
          break;
        case UPDATE_REP_REFS:
          status = cnv->updateRepRefs(pAddress, pObject);
          break;
        default:
          status = StatusCode::FAILURE;
          break;
        }
        if ( status.isSuccess() && update )   {
          status = updateServiceState(pAddress);
        }
        return status;
      }
      status.ignore();
      MsgStream log(msgSvc(), name());
      log << MSG::INFO << "No converter for object ";
      if ( pObject != 0 )   {
        log << System::typeinfoName(typeid(*pObject));
      }
      log << "  CLID= " << obj_class << endmsg;
      return NO_CONVERTER;
    }
    return INVALID_OBJECT;
  }
  return INVALID_ADDRESS;
}

void ConversionSvc::loadConverter(DataObject*) {

}

StatusCode ConversionSvc::updateServiceState(IOpaqueAddress* /*pAddress */)   {
  return StatusCode::SUCCESS;
}

/// Create the transient representation of an object.
StatusCode ConversionSvc::createObj(IOpaqueAddress* pAddress, DataObject*& refpObj)   {
  return makeCall(CREATE_OBJ, false, true, false, pAddress, refpObj);
}

/// Resolve the references of the created transient object.
StatusCode ConversionSvc::fillObjRefs(IOpaqueAddress* pAddress, DataObject* pObj)    {
  return makeCall(FILL_OBJ_REFS, false, true, true, pAddress, pObj);
}

/// Update the transient object from the other representation.
StatusCode ConversionSvc::updateObj(IOpaqueAddress* pAddress, DataObject* pObj)   {
  return makeCall(UPDATE_OBJ, false, true, false, pAddress, pObj);
}

/// Update the references of an updated transient object.
StatusCode ConversionSvc::updateObjRefs(IOpaqueAddress* pAddress, DataObject* pObj)  {
  return makeCall(UPDATE_OBJ_REFS, false, true, true, pAddress, pObj);
}

/// Convert the transient object to the requested representation.
StatusCode ConversionSvc::createRep(DataObject* pObj, IOpaqueAddress*& refpAddress)  {
  return makeCall(CREATE_REP, true, false, false, refpAddress, pObj);
}

/// Resolve the references of the converted object.
StatusCode ConversionSvc::fillRepRefs(IOpaqueAddress* pAddress, DataObject* pObj)  {
  return makeCall(FILL_REP_REFS, true, false, false, pAddress, pObj);
}

/// Update the converted representation of a transient object.
StatusCode ConversionSvc::updateRep(IOpaqueAddress* pAddress, DataObject* pObj)  {
  return makeCall(UPDATE_REP, true, false, false, pAddress, pObj);
}

/// Update the references of an already converted object.
StatusCode ConversionSvc::updateRepRefs(IOpaqueAddress* pAddress, DataObject* pObj)    {
  return makeCall(UPDATE_REP_REFS, true, false, false, pAddress, pObj);
}

/// Retrieve converter from list
IConverter* ConversionSvc::converter(const CLID& clid)     {
  IConverter* cnv = 0;
  Workers::iterator i = std::find_if(m_workers->begin(),m_workers->end(),CnvTest(clid));
  if ( i != m_workers->end() )      {
    cnv = (*i).converter();
  }
  if ( 0 == cnv )     {
    StatusCode status = addConverter(clid);
    if ( status.isSuccess() )   {
      i = std::find_if(m_workers->begin(),m_workers->end(),CnvTest(clid));
      if ( i != m_workers->end() )      {
        cnv = (*i).converter();
      }
    }
  }
  return cnv;
}

/// Define transient data store
StatusCode ConversionSvc::setDataProvider(IDataProviderSvc* pDataSvc)    {
  if ( !pDataSvc ) return StatusCode::SUCCESS; //Atlas does not use DataSvc
  if ( m_dataSvc ) m_dataSvc->release();
  m_dataSvc = pDataSvc;
  m_dataSvc->addRef();
  Workers::iterator stop  = m_workers->end();
  Workers::iterator start = m_workers->begin();
  for(Workers::iterator i=start; i != stop; i++ )    {
    IConverter* cnv = (*i).converter();
    if ( 0 != cnv )   {
      if (cnv->setDataProvider(m_dataSvc).isFailure()) {
        MsgStream log(msgSvc(), name());
        log << MSG::ERROR << "setting Data Provider" << endmsg;
      }
    }
  }
  return StatusCode::SUCCESS;
}

/// Access the transient store
SmartIF<IDataProviderSvc>& ConversionSvc::dataProvider()  const   {
  return m_dataSvc;
}

/// Set address creator facility
StatusCode ConversionSvc::setAddressCreator(IAddressCreator* creator)   {
  m_addressCreator = creator;
  Workers::iterator stop  = m_workers->end();
  Workers::iterator start = m_workers->begin();
  for(Workers::iterator i=start; i != stop; i++ )    {
    IConverter* cnv = (*i).converter();
    if ( 0 != cnv )   {
      if (cnv->setAddressCreator(m_addressCreator).isFailure()) {
	MsgStream log(msgSvc(), name());
	log << MSG::ERROR << "setting Address Creator"  << endmsg;
      }
    }
  }
  return StatusCode::SUCCESS;
}

/// Access the transient store
SmartIF<IAddressCreator>& ConversionSvc::addressCreator()  const   {
  return m_addressCreator;
}

/// Set conversion service the converter is connected to
StatusCode ConversionSvc::setConversionSvc(IConversionSvc* /* svc */)   {
  return StatusCode::FAILURE;
}

/// Get conversion service the converter is connected to
SmartIF<IConversionSvc>& ConversionSvc::conversionSvc()    const   {
  return m_cnvSvc;
}

/// Add converter object to conversion service.
StatusCode ConversionSvc::addConverter(const CLID& clid)  {
  // First look for the more specific converter
  long typ = repSvcType();
  IConverter* pConverter = createConverter(typ, clid, 0);
  if ( 0 != pConverter )    {
    StatusCode status = configureConverter( typ, clid, pConverter );
    if ( status.isSuccess() )   {
      status = initializeConverter( typ, clid, pConverter );
      if ( status.isSuccess() )   {
        status = activateConverter( typ, clid, pConverter );
        if ( status.isSuccess() )   {
          long conv_typ  = pConverter->repSvcType();
          const CLID&   conv_clid = pConverter->objType();
          typ      = (typ<0xFF) ? typ : typ&0xFFFFFF00;
          conv_typ = (conv_typ<0xFF) ? conv_typ : conv_typ&0xFFFFFF00;
          if ( conv_typ == typ && conv_clid == clid )   {
            return addConverter(pConverter);
          }
        }
      }
    }
    pConverter->release();
  }
  return NO_CONVERTER;
}

/// Add converter object to conversion service.
StatusCode ConversionSvc::addConverter(IConverter* pConverter)    {
  if ( 0 != pConverter )    {
    const CLID& clid = pConverter->objType();
    removeConverter(clid).ignore();
    m_workers->push_back(WorkerEntry(clid, pConverter));
    pConverter->addRef();
    return StatusCode::SUCCESS;
  }
  return NO_CONVERTER;
}

/// Remove converter object from conversion service (if present).
StatusCode ConversionSvc::removeConverter(const CLID& clid)  {
  CnvTest test(clid);
  Workers::iterator stop  = m_workers->end();
  Workers::iterator start = m_workers->begin();
  for(Workers::iterator i=start; i != stop; i++ )    {
    if ( test( *i ) )   {
      (*i).converter()->finalize().ignore();
      (*i).converter()->release();
    }
  }
  Workers::iterator j = std::remove_if(start, stop, test);
  if ( j != stop )  {
    m_workers->erase(j, stop);
    return StatusCode::SUCCESS;
  }
  return NO_CONVERTER;
}

/// Initialize the service.
StatusCode ConversionSvc::initialize()     {
  StatusCode status = Service::initialize();
  return status;
}

/// stop the service.
StatusCode ConversionSvc::finalize()      {
  // Release all workers.
  MsgStream log(msgSvc(), name());
  for ( Workers::iterator i = m_workers->begin(); i != m_workers->end(); i++ )    {
    if ( (*i).converter()->finalize().isFailure() ) {
      log << MSG::ERROR << "finalizing worker" << endmsg;
    }
    (*i).converter()->release();
  }
  m_workers->erase(m_workers->begin(), m_workers->end() );
  // release interfaces
#ifdef ATLAS_GAUDI_V21
  if (m_addressCreator) {
    m_addressCreator->addRef();
  }
  if (m_dataSvc) {
    m_dataSvc->addRef();
  }
  if (m_cnvSvc) {
    m_cnvSvc->addRef();
  }
#endif
  m_addressCreator = 0;
  m_dataSvc = 0;
  m_cnvSvc = 0;
  return Service::finalize();
}


/// Create new Converter using factory
IConverter* ConversionSvc::createConverter(long typ,
                                           const CLID& clid,
                                           const ICnvFactory* /*fac*/)   {
  IConverter* pConverter;
  pConverter = PluginService::CreateWithId<IConverter*>(ConverterID(typ,clid),serviceLocator().get());
  if ( 0 == pConverter )  {
    typ = (typ<0xFF) ? typ : typ&0xFFFFFF00;
    pConverter = PluginService::CreateWithId<IConverter*>(ConverterID(typ,clid),serviceLocator().get());
  }
  return pConverter;
}

/// Configure the freshly created converter
StatusCode ConversionSvc::configureConverter(long /* typ */,
                                              const CLID& /* clid */,
                                              IConverter* pConverter)    {
  if ( 0 != pConverter )    {
    pConverter->setConversionSvc(this).ignore();
    pConverter->setAddressCreator(m_addressCreator).ignore();
    pConverter->setDataProvider(m_dataSvc).ignore();
    return StatusCode::SUCCESS;
  }
  return NO_CONVERTER;
}

/// Initialize new converter
StatusCode ConversionSvc::initializeConverter(long /* typ */,
                                              const CLID& /* clid */,
                                              IConverter* pConverter)    {
  if ( pConverter )    {
    return pConverter->initialize();
  }
  return NO_CONVERTER;
}

/// Activate the freshly created converter
StatusCode ConversionSvc::activateConverter(long /* typ */,
                                            const CLID& /* clid */,
                                            IConverter* pConverter)    {
  if ( 0 != pConverter )    {
    return StatusCode::SUCCESS;
  }
  return NO_CONVERTER;
}

/// Retrieve the class type of objects the converter produces.
const CLID& ConversionSvc::objType() const    {
  return CLID_NULL;
}

/// Retrieve the class type of the data store the converter uses.
long ConversionSvc::repSvcType() const {
  return m_type;
}

/// Connect the output file to the service with open mode.
StatusCode ConversionSvc::connectOutput(const std::string&    outputFile,
                                        const std::string& /* openMode */) {
  return connectOutput(outputFile);
}

/// Connect the output file to the service.
StatusCode ConversionSvc::connectOutput(const std::string& /* outputFile */) {
  return StatusCode::SUCCESS;
}

/// Commit pending output.
StatusCode ConversionSvc::commitOutput(const std::string& , bool ) {
  return StatusCode::SUCCESS;
}

/// Create a Generic address using explicit arguments to identify a single object.
StatusCode ConversionSvc::createAddress(long                 /* svc_type */,
                                        const CLID&          /* clid     */,
                                        const std::string*   /* par      */,
                                        const unsigned long* /* ip       */,
                                        IOpaqueAddress*& refpAddress)    {
  refpAddress = 0;
  return StatusCode::FAILURE;
}

/// Convert an address to string form
StatusCode ConversionSvc::convertAddress( const IOpaqueAddress* /* pAddress */,
                                          std::string& refAddress)
{
  refAddress = "";
  return StatusCode::FAILURE;
}

/// Convert an address in string form to object form
StatusCode ConversionSvc::createAddress( long /* svc_type */,
                                        const CLID& /* clid */,
                                        const std::string& /* refAddress */,
                                        IOpaqueAddress*& refpAddress)
{
  refpAddress = 0;
  return StatusCode::FAILURE;
}

/// Standard Constructor
ConversionSvc::ConversionSvc(const std::string& name, ISvcLocator* svc, long type)
 : base_class(name, svc)
{
  m_type            = type;
  m_dataSvc         = 0;
  m_cnvSvc          = this;
  m_workers = new Workers();
  setAddressCreator(this).ignore();
}

/// Standard Destructor
ConversionSvc::~ConversionSvc()   {

#ifdef ATLAS_GAUDI_V21
  if (this->refCount()==0) {
    this->addRef();
  }
  if (m_cnvSvc) {
    SmartIF<INamedInterface> incnv(m_cnvSvc);
    if (incnv->name() == this->name()) {
      m_cnvSvc->addRef();
    }
    m_cnvSvc->addRef();
    m_cnvSvc = 0;
  }
  if (m_addressCreator) {
    SmartIF<INamedInterface> inadd(m_addressCreator);
    if (inadd->name() == this->name()) {
      m_addressCreator->addRef();
    }
    m_addressCreator->addRef();
    m_addressCreator = 0;
  }
  if (m_dataSvc) {
    SmartIF<INamedInterface> inda(m_dataSvc);
    if (inda->name() == this->name()) {
      m_dataSvc->addRef();
    }

    m_dataSvc->addRef();
    m_dataSvc = 0;
  }
#endif

  // Release all workers.
  for ( Workers::iterator i = m_workers->begin(); i != m_workers->end(); i++ )    {
    (*i).converter()->release();
  }
  m_workers->erase(m_workers->begin(), m_workers->end() );
  delete m_workers; m_workers = 0;

}