CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Serialization.h
Go to the documentation of this file.
1 #ifndef CondCore_CondDB_Serialization_h
2 #define CondCore_CondDB_Serialization_h
3 //
4 // Package: CondDB
5 //
9 //
10 // Author: Giacomo Govi
11 // Created: October 2013
12 //
13 //
14 
18 //
19 #include <sstream>
20 #include <iostream>
21 //
22 // temporarely
23 #include <boost/shared_ptr.hpp>
24 
26 
27 class RootStreamBuffer;
28 
29 namespace cond {
30 
31  // default payload factory
32  template <typename T> T* createPayload( const std::string& payloadTypeName ){
33  std::string userTypeName = demangledName( typeid(T) );
34  if( userTypeName != payloadTypeName )
35  throwException(std::string("Type mismatch, user type: \""+userTypeName+"\", target type: \"")+payloadTypeName+"\"",
36  "createPayload" );
37  return new T;
38  }
39 
40  // Archives for the streaming based on ROOT.
41 
42  // output
44  public:
45  RootOutputArchive( std::ostream& dataDest, std::ostream& streamerInfoDest );
46 
47  template <typename T>
49  private:
50  // type and ptr of the object to stream
51  void write( const std::type_info& sourceType, const void* sourceInstance);
52  private:
53  // here is where the write function will write on...
54  std::ostream& m_dataBuffer;
55  std::ostream& m_streamerInfoBuffer;
56  };
57 
58  template <typename T> inline RootOutputArchive& RootOutputArchive::operator<<( const T& instance ){
59  write( typeid(T), &instance );
60  return *this;
61  }
62 
63  // input
65  public:
66  RootInputArchive( std::istream& binaryData, std::istream& binaryStreamerInfo );
67 
68  virtual ~RootInputArchive();
69 
70  template <typename T>
72  private:
73  // type and ptr of the object to restore
74  void read( const std::type_info& destinationType, void* destinationInstance);
75  private:
76  // copy of the input stream. is referenced by the TBufferFile.
80  };
81 
82  template <typename T> inline RootInputArchive& RootInputArchive::operator>>( T& instance ){
83  read( typeid(T), &instance );
84  return *this;
85  }
86 
89 
90  // call for the serialization. Setting packingOnly = TRUE the data will stay in the original memory layout
91  // ( no serialization in this case ). This option is used by the ORA backend - will be dropped after the changeover
92  template <typename T> std::pair<Binary,Binary> serialize( const T& payload, bool packingOnly = false ){
93  std::pair<Binary,Binary> ret;
94  if( !packingOnly ){
95  // save data to buffers
96  std::ostringstream dataBuffer;
97  std::ostringstream streamerInfoBuffer;
98  CondOutputArchive oa( dataBuffer );
99  oa << payload;
100  //TODO: avoid (2!!) copies
101  ret.first.copy( dataBuffer.str() );
102  ret.second.copy( streamerInfoBuffer.str() );
103  } else {
104  // ORA objects case: nothing to serialize, the object is kept in memory in the original layout - the bare pointer is exchanged
105  ret.first = Binary( payload );
106  }
107  return ret;
108  }
109 
110  // generates an instance of T from the binary serialized data. With unpackingOnly = true the memory is already storing the object in the final
111  // format. Only a cast is required in this case - Used by the ORA backed, will be dropped in the future.
112  template <typename T> boost::shared_ptr<T> default_deserialize( const std::string& payloadType,
113  const Binary& payloadData,
114  const Binary& streamerInfoData,
115  bool unpackingOnly ){
116  boost::shared_ptr<T> payload;
117  if( !unpackingOnly ){
118  std::stringbuf sdataBuf;
119  sdataBuf.pubsetbuf( static_cast<char*>(const_cast<void*>(payloadData.data())), payloadData.size() );
120  std::stringbuf sstreamerInfoBuf;
121  sstreamerInfoBuf.pubsetbuf( static_cast<char*>(const_cast<void*>(streamerInfoData.data())), streamerInfoData.size() );
122 
123  std::istream dataBuffer( &sdataBuf );
124  std::istream streamerInfoBuffer( &sstreamerInfoBuf );
125  CondInputArchive ia( dataBuffer );
126  payload.reset( createPayload<T>(payloadType) );
127  ia >> (*payload);
128  } else {
129  // ORA objects case: nothing to de-serialize, the object is already in memory in the final layout, ready to be casted
130  payload = boost::static_pointer_cast<T>(payloadData.oraObject().makeShared());
131  }
132  return payload;
133  }
134 
135  // default specialization
136  template <typename T> boost::shared_ptr<T> deserialize( const std::string& payloadType,
137  const Binary& payloadData,
138  const Binary& streamerInfoData,
139  bool unpackingOnly = false){
140  return default_deserialize<T>( payloadType, payloadData, streamerInfoData, unpackingOnly );
141  }
142 
143 }
144 
145 #define DESERIALIZE_BASE_CASE( BASETYPENAME ) \
146  if( payloadType == #BASETYPENAME ){ \
147  return default_deserialize<BASETYPENAME>( payloadType, payloadData, streamerInfoData, unpackingOnly ); \
148  }
149 
150 #define DESERIALIZE_POLIMORPHIC_CASE( BASETYPENAME, DERIVEDTYPENAME ) \
151  if( payloadType == #DERIVEDTYPENAME ){ \
152  return boost::dynamic_pointer_cast<BASETYPENAME>( default_deserialize<DERIVEDTYPENAME>( payloadType, payloadData, streamerInfoData, unpackingOnly ) ); \
153  }
154 
155 #endif
eos::portable_oarchive OutputArchive
Definition: Archive.h:13
boost::shared_ptr< void > makeShared() const
Definition: Object.cc:73
static PFTauRenderPlugin instance
cond::serialization::InputArchive CondInputArchive
Definition: Serialization.h:87
eos::portable_iarchive InputArchive
Definition: Archive.h:12
std::ostream & m_streamerInfoBuffer
Definition: Serialization.h:55
void read(const std::type_info &destinationType, void *destinationInstance)
size_t size() const
Definition: Binary.cc:57
RootOutputArchive(std::ostream &dataDest, std::ostream &streamerInfoDest)
std::string m_dataBuffer
Definition: Serialization.h:77
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:82
void write(const std::type_info &sourceType, const void *sourceInstance)
T * createPayload(const std::string &payloadTypeName)
Definition: Serialization.h:32
ora::Object oraObject() const
Definition: Binary.cc:62
std::string m_streamerInfoBuffer
Definition: Serialization.h:78
const void * data() const
Definition: Binary.cc:48
RootOutputArchive & operator<<(const T &instance)
Definition: Serialization.h:58
cond::serialization::OutputArchive CondOutputArchive
Definition: Serialization.h:88
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
RootInputArchive & operator>>(T &instance)
Definition: Serialization.h:82
boost::shared_ptr< T > deserialize(const std::string &payloadType, const Binary &payloadData, const Binary &streamerInfoData, bool unpackingOnly=false)
RootStreamBuffer * m_streamer
Definition: Serialization.h:79
std::pair< Binary, Binary > serialize(const T &payload, bool packingOnly=false)
Definition: Serialization.h:92
std::ostream & m_dataBuffer
Definition: Serialization.h:54
boost::shared_ptr< T > default_deserialize(const std::string &payloadType, const Binary &payloadData, const Binary &streamerInfoData, bool unpackingOnly)
long double T
RootInputArchive(std::istream &binaryData, std::istream &binaryStreamerInfo)