LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIOCollectionHandler.cc
Go to the documentation of this file.
2 
3 // -- lcio headers
4 #include "SIO/LCSIO.h"
5 #include "EVENT/LCIO.h"
6 #include "SIO/SIOObjectHandler.h"
7 #include "SIO/SIOLCParameters.h"
9 
10 // -- sio headers
11 #include <sio/io_device.h>
12 #include <sio/version.h>
13 
14 namespace SIO {
15 
17  sio::block( colname, LCSIO::blockVersion() ),
18  _handler( handler ) {
19  /* nop */
20  }
21 
22  //----------------------------------------------------------------------------
23 
25  return _handler->collectionType() ;
26  }
27 
28  //----------------------------------------------------------------------------
29 
31  _collection = col ;
32  }
33 
34  //----------------------------------------------------------------------------
35 
36  void SIOCollectionHandler::read( sio::read_device &device, sio::version_type vers ) {
38  if( nullptr == ioCol ) {
39  SIO_THROW( sio::error_code::invalid_argument, "Expected collection of type LCCollectionIOVec!" ) ;
40  }
41  _handler->initReading( device, _collection, vers ) ;
42  // read out number of objects in the collection
43  int nObj ;
44  SIO_DATA( device , &nObj , 1 ) ;
45  // reserve the space for the pointers to all objects
46  ioCol->resize( nObj ) ;
47  if( _handler->flag() & ( 1 << EVENT::LCCollection::BITSubset ) ) {
48  // read out only pointers
49  for( int i=0 ; i< nObj ; i ++ ) {
50  SIO_PNTR( device , &(*ioCol)[i] ) ;
51  }
52  }
53  else {
54  // std::cout << "Reading a full collection" << std::endl ;
55  // read out all objects in the collection
56  for( int i=0 ; i< nObj ; i ++ ) {
57  (*ioCol)[i] = _handler->create() ;
58  try {
59  _handler->read( device , (*ioCol)[i] , vers ) ;
60  }
61  catch( std::exception &e ) {
62  std::stringstream ss ; ss << "Couldn't read out object of type '" << _handler->collectionType() << "' at index " << i ;
63  SIO_RETHROW( e, sio::error_code::io_failure, ss.str() ) ;
64  }
65  catch( ... ) {
66  std::stringstream ss ; ss << "Couldn't read out object of type '" << _handler->collectionType() << "' at index " << i ;
67  SIO_THROW( sio::error_code::io_failure, ss.str() ) ;
68  }
69  }
70  }
71  }
72 
73  //----------------------------------------------------------------------------
74 
75  void SIOCollectionHandler::write( sio::write_device &device ) {
76  _handler->initWriting( device, _collection ) ;
77  // write number of objects
78  int nObj = _collection->getNumberOfElements() ;
79  SIO_DATA( device, &nObj , 1 ) ;
80  if( _handler->flag() & ( 1 << EVENT::LCCollection::BITSubset ) ) {
81  // write only pointers
82  for( int i=0 ; i< nObj ; i ++ ) {
83  auto ptr = _collection->getElementAt(i) ;
84  SIO_PNTR( device , &ptr ) ;
85  }
86  }
87  else {
88  // write all the objects
89  for( int i=0 ; i< nObj ; i ++ ) {
90  _handler->write( device, _collection->getElementAt(i) ) ;
91  }
92  }
93 
94  }
95 
96 } // namespace
EVENT::LCCollection * _collection
The collection to read/write.
const std::string & type() const
Get the collection type.
std::shared_ptr< SIOObjectHandler > _handler
The object handler for reading/writing.
Adding stuff needed for io (friend declarations, etc.)
void read(sio::read_device &device, sio::version_type vers) override
Collection of constants and helper functions.
Definition: LCSIO.h:16
T resize(T...args)
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
STL class.
T str(T...args)
STL class.
void write(sio::write_device &device) override
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
The generic collection used in LCIO.
Definition: LCCollection.h:29
void setCollection(EVENT::LCCollection *col)
Set the collection to read/write.
static const int BITSubset
Definition: LCCollection.h:70