test
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 namespace cond {
28 
29  // default payload factory
30  template <typename T> T* createPayload( const std::string& payloadTypeName ){
31  std::string userTypeName = demangledName( typeid(T) );
32  if( userTypeName != payloadTypeName )
33  throwException(std::string("Type mismatch, user type: \""+userTypeName+"\", target type: \"")+payloadTypeName+"\"",
34  "createPayload" );
35  return new T;
36  }
37 
38  class StreamerInfo {
39  public:
40  static constexpr char const* TECH_LABEL = "technology";
41  static constexpr char const* TECH_VERSION_LABEL = "tech_version";
42  static constexpr char const* CMSSW_VERSION_LABEL = "CMSSW_version";
43  static constexpr char const* ARCH_LABEL = "architecture";
44  //
45  static constexpr char const* TECHNOLOGY = "boost/serialization" ;
46  static std::string techVersion();
47  static std::string jsonString();
48  };
49 
52 
53  // call for the serialization.
54  template <typename T> std::pair<Binary,Binary> serialize( const T& payload ){
55  std::pair<Binary,Binary> ret;
56  std::string streamerInfo( StreamerInfo::jsonString() );
57  try{
58  // save data to buffers
59  std::ostringstream dataBuffer;
60  CondOutputArchive oa( dataBuffer );
61  oa << payload;
62  //TODO: avoid (2!!) copies
63  ret.first.copy( dataBuffer.str() );
64  ret.second.copy( streamerInfo );
65  } catch ( const std::exception& e ){
66  std::string em( e.what() );
67  throwException("Serialization failed: "+em+". Serialization info:"+streamerInfo,"serialize");
68  }
69  return ret;
70  }
71 
72  // generates an instance of T from the binary serialized data.
73  template <typename T> boost::shared_ptr<T> default_deserialize( const std::string& payloadType,
74  const Binary& payloadData,
75  const Binary& streamerInfoData ){
76  boost::shared_ptr<T> payload;
77  std::stringbuf sstreamerInfoBuf;
78  sstreamerInfoBuf.pubsetbuf( static_cast<char*>(const_cast<void*>(streamerInfoData.data())), streamerInfoData.size() );
79  std::string streamerInfo = sstreamerInfoBuf.str();
80  try{
81  std::stringbuf sdataBuf;
82  sdataBuf.pubsetbuf( static_cast<char*>(const_cast<void*>(payloadData.data())), payloadData.size() );
83  std::istream dataBuffer( &sdataBuf );
84  CondInputArchive ia( dataBuffer );
85  payload.reset( createPayload<T>(payloadType) );
86  ia >> (*payload);
87  } catch ( const std::exception& e ){
88  std::string errorMsg("De-serialization failed: ");
89  std::string em( e.what() );
90  if( em == "unsupported version" ) {
91  errorMsg += "the current boost version ("+StreamerInfo::techVersion()+
92  ") is unable to read the payload. Data might have been serialized with an incompatible version.";
93  } else if( em == "input stream error" ) {
94  errorMsg +="data size does not fit with the current class layout. The Class "+payloadType+" might have been changed with respect to the layout used in the upload.";
95  } else {
96  errorMsg += em;
97  }
98  if( !streamerInfo.empty() ) errorMsg += " Payload serialization info: "+streamerInfo;
99  throwException( errorMsg, "default_deserialize" );
100  }
101  return payload;
102  }
103 
104  // default specialization
105  template <typename T> boost::shared_ptr<T> deserialize( const std::string& payloadType,
106  const Binary& payloadData,
107  const Binary& streamerInfoData ){
108  return default_deserialize<T>( payloadType, payloadData, streamerInfoData );
109  }
110 
111 }
112 
113 #define DESERIALIZE_BASE_CASE( BASETYPENAME ) \
114  if( payloadType == #BASETYPENAME ){ \
115  return default_deserialize<BASETYPENAME>( payloadType, payloadData, streamerInfoData ); \
116  }
117 
118 #define DESERIALIZE_POLIMORPHIC_CASE( BASETYPENAME, DERIVEDTYPENAME ) \
119  if( payloadType == #DERIVEDTYPENAME ){ \
120  return boost::dynamic_pointer_cast<BASETYPENAME>( default_deserialize<DERIVEDTYPENAME>( payloadType, payloadData, streamerInfoData ) ); \
121  }
122 
123 #endif
eos::portable_oarchive OutputArchive
Definition: Archive.h:17
tuple ret
prodAgent to be discontinued
static char const * CMSSW_VERSION_LABEL
Definition: Serialization.h:42
static std::string techVersion()
Definition: Serialization.cc:6
std::pair< Binary, Binary > serialize(const T &payload)
Definition: Serialization.h:54
static char const * TECH_LABEL
Definition: Serialization.h:40
cond::serialization::InputArchive CondInputArchive
Definition: Serialization.h:50
eos::portable_iarchive InputArchive
Definition: Archive.h:16
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
#define constexpr
size_t size() const
Definition: Binary.cc:54
std::string demangledName(const std::type_info &typeInfo)
Definition: ClassUtils.cc:159
boost::shared_ptr< T > deserialize(const std::string &payloadType, const Binary &payloadData, const Binary &streamerInfoData)
T * createPayload(const std::string &payloadTypeName)
Definition: Serialization.h:30
static char const * TECHNOLOGY
Definition: Serialization.h:45
static char const * TECH_VERSION_LABEL
Definition: Serialization.h:41
const void * data() const
Definition: Binary.cc:45
static std::string jsonString()
cond::serialization::OutputArchive CondOutputArchive
Definition: Serialization.h:51
boost::shared_ptr< T > default_deserialize(const std::string &payloadType, const Binary &payloadData, const Binary &streamerInfoData)
Definition: Serialization.h:73
static char const * ARCH_LABEL
Definition: Serialization.h:43
long double T