10#ifndef SERIALIZATION_INCLUDE_SERIALIZATION_SERIALIZATION_HPP_
11#define SERIALIZATION_INCLUDE_SERIALIZATION_SERIALIZATION_HPP_
16#include "boost/preprocessor.hpp"
23#define DUNE_DAQ_TYPESTRING(Type, typestring) \
25 inline std::string dunedaq::datatype_to_string<Type>() \
30#define DUNE_DAQ_SERIALIZABLE(Type, typestring) \
31 DUNE_DAQ_TYPESTRING(Type, typestring) \
33 struct dunedaq::serialization::is_serializable<Type> \
35 static constexpr bool value = true; \
56#define DUNE_DAQ_SERIALIZE(Type, ...) \
57 MSGPACK_DEFINE(__VA_ARGS__) \
58 static_assert(true, "")
62#define OPACK(r, data, elem) o.pack(m.elem);
64#define OUNPACK(r, data, elem) m.elem = o.via.array.ptr[i++].as<decltype(m.elem)>();
87#define DUNE_DAQ_SERIALIZE_NON_INTRUSIVE(NS, Type, ...) \
88 DUNE_DAQ_SERIALIZABLE(NS::Type, #Type); \
90 MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) \
94 struct pack<NS::Type> \
96 template<typename Stream> \
97 packer<Stream>& operator()(msgpack::packer<Stream>& o, NS::Type const& m) const \
99 o.pack_array(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)); \
100 BOOST_PP_SEQ_FOR_EACH(OPACK, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
105 struct convert<NS::Type> \
107 msgpack::object const& operator()(msgpack::object const& o, NS::Type& m) const \
109 if (o.type != msgpack::type::ARRAY) \
110 throw msgpack::type_error(); \
111 if (o.via.array.size != BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)) \
112 throw msgpack::type_error(); \
114 BOOST_PP_SEQ_FOR_EACH(OUNPACK, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
127 UnknownSerializationTypeString,
128 "Unknown serialization type " << t,
133 "Unknown serialization type",)
136 UnknownSerializationTypeByte,
137 "Unknown serialization type " << t,
142 "Cannot deserialize message",)
154namespace serialization {
176 throw UnknownSerializationTypeString(
ERS_HERE, s);
204 msgpack::sbuffer buf;
205 msgpack::pack(buf, obj);
206 std::vector<uint8_t> ret(buf.size() + 1);
208 std::copy(buf.data(), buf.data() + buf.size(), ret.begin() + 1);
219template<
class T,
typename CharType =
unsigned char>
239 msgpack::object_handle oh = msgpack::unpack(
240 const_cast<char*
>(
reinterpret_cast<const char*
>(v.data() + 1)),
242 [](msgpack::type::object_type , std::size_t ,
void* ) ->
bool { return true; });
243 msgpack::object obj = oh.get();
245 }
catch (msgpack::type_error& e) {
247 }
catch (msgpack::unpack_error& e) {
252 throw UnknownSerializationTypeByte(
ERS_HERE, (
char)v[0]);
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
SerializationType
Serialization methods that are available.
T deserialize(const std::vector< CharType > &v)
Deserialize vector of bytes v into an instance of class T.
std::vector< uint8_t > serialize(const T &obj, SerializationType stype)
Serialize object obj using serialization method stype.
constexpr uint8_t serialization_type_byte(SerializationType stype)
SerializationType from_string(const std::string s)
Convert string to SerializationType.
Unknown serialization Cannot deserialize std::string datatype_to_string()
UnknownSerializationTypeEnum
Unknown serialization CannotDeserializeMessage