HALDLS
cerealization.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "hate/type_list.h"
4 #include "hate/visibility.h"
5 #include <cstdint>
6 #include <iosfwd>
7 #include <cereal/macros.hpp>
8 
9 #if defined(__GENPYBIND__) or defined(__GENPYBIND_GENERATED__)
10 #include <pybind11/pybind11.h>
11 #endif
12 
13 namespace cereal {
14 
15 class access;
16 class JSONOutputArchive;
17 class JSONInputArchive;
18 class PortableBinaryOutputArchive;
19 class PortableBinaryInputArchive;
20 
21 } // namespace cereal
22 
23 
24 namespace haldls::vx {
25 
26 template <typename T>
27 std::string to_json(T const& t);
28 
29 template <typename T>
30 void from_json(T& t, std::string const& s);
31 
32 template <typename T>
33 std::string to_portablebinary(T const& t);
34 
35 template <typename T>
36 void from_portablebinary(T& t, std::string const& s);
37 
38 #if defined(__GENPYBIND__) or defined(__GENPYBIND_GENERATED__)
39 template <typename... Ts>
40 struct WrapToFromFunctions
41 {
42  static void apply(pybind11::module& m)
43  {
44  // convert binary-encoded std::string to bytes in Python
45  (m.def("to_json", &haldls::vx::to_json<Ts>), ...);
46  (m.def("from_json", &haldls::vx::from_json<Ts>), ...);
47  // clang-format off
48  (m.def("to_portablebinary", [](Ts const& t) {
49  return pybind11::bytes(haldls::vx::to_portablebinary<Ts>(t));
50  }), ...);
51  // clang-format on
52  (m.def("from_portablebinary", &haldls::vx::from_portablebinary<Ts>), ...);
53  }
54 };
55 
56 template <typename... Ts>
57 struct WrapToFromFunctions<hate::type_list<Ts...>>
58 {
59  static void apply(pybind11::module& m)
60  {
61  WrapToFromFunctions<Ts...>::apply(m);
62  }
63 };
64 
65 // Use with something like this:
66 // typedef hate::type_list<my_type1, my_type2> my_types;
67 // GENPYBIND_MANUAL({
68 // wrap_tofrom_functions<my_types>(parent);
69 // })
70 #endif
71 
72 } // namespace haldls::vx
73 
74 // explicitly instantiate our template SYMBOL_VISIBLE functions; includes to_json/from_json
75 #define EXTERN_INSTANTIATE_CEREAL_SERIALIZE(CLASS_NAME) \
76  extern template SYMBOL_VISIBLE void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
77  cereal::JSONOutputArchive&, std::uint32_t const); \
78  extern template SYMBOL_VISIBLE void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
79  cereal::JSONInputArchive&, std::uint32_t const); \
80  extern template SYMBOL_VISIBLE void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
81  cereal::PortableBinaryOutputArchive&, std::uint32_t const); \
82  extern template SYMBOL_VISIBLE void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
83  cereal::PortableBinaryInputArchive&, std::uint32_t const); \
84  extern template SYMBOL_VISIBLE std::string haldls::vx::to_json(CLASS_NAME const&); \
85  extern template SYMBOL_VISIBLE std::string haldls::vx::to_portablebinary(CLASS_NAME const&); \
86  extern template SYMBOL_VISIBLE void haldls::vx::from_json(CLASS_NAME&, std::string const&); \
87  extern template SYMBOL_VISIBLE void haldls::vx::from_portablebinary( \
88  CLASS_NAME&, std::string const&);
89 
90 #define EXPLICIT_INSTANTIATE_CEREAL_SERIALIZE(CLASS_NAME) \
91  template void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
92  cereal::JSONOutputArchive&, std::uint32_t const); \
93  template void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
94  cereal::JSONInputArchive&, std::uint32_t const); \
95  template void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
96  cereal::PortableBinaryOutputArchive&, std::uint32_t const); \
97  template void CLASS_NAME ::CEREAL_SERIALIZE_FUNCTION_NAME( \
98  cereal::PortableBinaryInputArchive&, std::uint32_t const); \
99  template std::string haldls::vx::to_json(CLASS_NAME const&); \
100  template std::string haldls::vx::to_portablebinary(CLASS_NAME const&); \
101  template void haldls::vx::from_json(CLASS_NAME&, std::string const&); \
102  template void haldls::vx::from_portablebinary(CLASS_NAME&, std::string const&);
103 
104 #define EXTERN_INSTANTIATE_CEREAL_SERIALIZE_FREE(CLASS_NAME) \
105  extern template SYMBOL_VISIBLE void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
106  cereal::JSONOutputArchive&, CLASS_NAME&); \
107  extern template SYMBOL_VISIBLE void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
108  cereal::JSONInputArchive&, CLASS_NAME&); \
109  extern template SYMBOL_VISIBLE void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
110  cereal::PortableBinaryOutputArchive&, CLASS_NAME&); \
111  extern template SYMBOL_VISIBLE void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
112  cereal::PortableBinaryInputArchive&, CLASS_NAME&); \
113  extern template SYMBOL_VISIBLE std::string haldls::vx::to_json(CLASS_NAME const&); \
114  extern template SYMBOL_VISIBLE std::string haldls::vx::to_portablebinary(CLASS_NAME const&); \
115  extern template SYMBOL_VISIBLE void haldls::vx::from_json(CLASS_NAME&, std::string const&); \
116  extern template SYMBOL_VISIBLE void haldls::vx::from_portablebinary( \
117  CLASS_NAME&, std::string const&);
118 
119 #define EXPLICIT_INSTANTIATE_CEREAL_SERIALIZE_FREE(CLASS_NAME) \
120  template void cereal::CEREAL_SERIALIZE_FUNCTION_NAME(cereal::JSONOutputArchive&, CLASS_NAME&); \
121  template void cereal::CEREAL_SERIALIZE_FUNCTION_NAME(cereal::JSONInputArchive&, CLASS_NAME&); \
122  template void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
123  cereal::PortableBinaryOutputArchive&, CLASS_NAME&); \
124  template void cereal::CEREAL_SERIALIZE_FUNCTION_NAME( \
125  cereal::PortableBinaryInputArchive&, CLASS_NAME&); \
126  template std::string haldls::vx::to_json(CLASS_NAME const&); \
127  template std::string haldls::vx::to_portablebinary(CLASS_NAME const&); \
128  template void haldls::vx::from_json(CLASS_NAME&, std::string const&); \
129  template void haldls::vx::from_portablebinary(CLASS_NAME&, std::string const&);
std::string to_portablebinary(T const &t)
void from_portablebinary(T &t, std::string const &s)
void from_json(T &t, std::string const &s)
std::string to_json(T const &t)