CMS 3D CMS Logo

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