CMS 3D CMS Logo

SerializationHelper.h
Go to the documentation of this file.
1 #ifndef CondFormats_SerializationHelper_SerializationHelper_h
2 #define CondFormats_SerializationHelper_SerializationHelper_h
3 // -*- C++ -*-
4 //
5 // Package: CondFormats/SerializationHelper
6 // Class : SerializationHelper
7 //
16 //
17 // Original Author: Christopher Jones
18 // Created: Wed, 31 May 2023 14:55:13 GMT
19 //
20 
21 // system include files
22 #include <string_view>
23 
24 // user include files
28 
29 // forward declarations
30 
31 namespace cond::serialization {
32  struct NoInitializer {};
33 
34  template <typename T>
35  struct ClassName;
36 
37  template <typename T>
38  std::unique_ptr<T> makeClass() {
39  return std::make_unique<T>();
40  }
41 
42  template <typename T>
43  struct BaseClassInfo {
44  constexpr static bool kAbstract = false;
46  };
47 
48  template <typename T, bool IsAbstract, typename... INHERITING>
50  constexpr static bool kAbstract = IsAbstract;
51  using inheriting_classes_t = edm::mpl::Vector<INHERITING...>;
52  };
53 
54  template <typename T, typename INIT = NoInitializer>
56  public:
57  SerializationHelper() = default;
58 
59  SerializationHelper(const SerializationHelper&) = delete; // stop default
60  const SerializationHelper& operator=(const SerializationHelper&) = delete; // stop default
61 
62  // ---------- const member functions ---------------------
63 
64  unique_void_ptr deserialize(std::streambuf& iBuff, const std::string_view iClassName) const final {
65  if constexpr (not BaseClassInfo<T>::kAbstract) {
66  using BaseClassAndInheriting =
68  return deserialize_impl<BaseClassAndInheriting>(iBuff, iClassName);
69  } else {
70  return deserialize_impl<typename BaseClassInfo<T>::inheriting_classes_t>(iBuff, iClassName);
71  }
72  }
73 
74  std::string_view serialize(std::streambuf& oBuff, void const* iObj) const final {
75  auto iTypedObjectPtr = static_cast<T const*>(iObj);
76  if constexpr (BaseClassInfo<T>::kAbstract) {
77  return serialize_impl<typename BaseClassInfo<T>::inheriting_classes_t>(oBuff, iTypedObjectPtr);
78  } else {
79  using BaseClassAndInheriting =
81  return serialize_impl<BaseClassAndInheriting>(oBuff, iTypedObjectPtr);
82  }
83  }
84 
85  const std::type_info& type() const final { return typeid(T); }
86 
87  private:
88  template <typename TYPELIST>
89  static unique_void_ptr deserialize_impl(std::streambuf& iBuff, const std::string_view iClassName) {
90  if constexpr (edm::mpl::Pop<TYPELIST>::empty) {
91  return {};
92  } else {
93  using CheckType = typename edm::mpl::Pop<TYPELIST>::Item;
94  if (iClassName == ClassName<CheckType>::kName) {
95  std::unique_ptr<CheckType> tmp = makeClass<CheckType>();
96 
97  InputArchive ia(iBuff);
98  ia >> (*tmp);
99  if constexpr (not std::is_same_v<INIT, NoInitializer>) {
100  INIT init;
101  init(*tmp);
102  }
103  return unique_void_ptr(tmp.release(), [](const void* iPtr) { delete static_cast<const T*>(iPtr); });
104 
105  } else {
106  return deserialize_impl<typename edm::mpl::Pop<TYPELIST>::Remaining>(iBuff, iClassName);
107  }
108  }
109  }
110  template <typename TYPELIST>
111  static std::string_view serialize_impl(std::streambuf& oBuff, T const* iObj) {
112  if constexpr (edm::mpl::Pop<TYPELIST>::empty) {
113  return {};
114  } else {
115  if (typeid(*iObj) == typeid(typename edm::mpl::Pop<TYPELIST>::Item)) {
116  auto iTypedObjectPtr = dynamic_cast<typename edm::mpl::Pop<TYPELIST>::Item const*>(iObj);
117  OutputArchive oa(oBuff);
118  oa << *iTypedObjectPtr;
119 
121 
122  } else {
123  return serialize_impl<typename edm::mpl::Pop<TYPELIST>::Remaining>(oBuff, iObj);
124  }
125  }
126  }
127  };
128 } // namespace cond::serialization
129 #endif
eos::portable_oarchive OutputArchive
Definition: Archive.h:18
const std::type_info & type() const final
const SerializationHelper & operator=(const SerializationHelper &)=delete
int init
Definition: HydjetWrapper.h:66
eos::portable_iarchive InputArchive
Definition: Archive.h:17
std::unique_ptr< T > makeClass()
std::string_view serialize(std::streambuf &oBuff, void const *iObj) const final
unique_void_ptr deserialize(std::streambuf &iBuff, const std::string_view iClassName) const final
static std::string_view serialize_impl(std::streambuf &oBuff, T const *iObj)
static unique_void_ptr deserialize_impl(std::streambuf &iBuff, const std::string_view iClassName)
tmp
align.sh
Definition: createJobs.py:716
long double T