17#ifndef SERIALIZATION_INCLUDE_SERIALIZATION_SERIALIZATION_HPP_
18#define SERIALIZATION_INCLUDE_SERIALIZATION_SERIALIZATION_HPP_
23#include "boost/preprocessor.hpp"
36#define DUNE_DAQ_TYPESTRING(Type, typestring) \
38 inline std::string dunedaq::datatype_to_string<Type>() \
47#define DUNE_DAQ_SERIALIZABLE(Type, typestring) \
48 DUNE_DAQ_TYPESTRING(Type, typestring) \
50 struct dunedaq::serialization::is_serializable<Type> \
52 static constexpr bool value = true; \
73#define DUNE_DAQ_SERIALIZE(Type, ...) \
74 MSGPACK_DEFINE(__VA_ARGS__) \
75 static_assert(true, "")
79#define OPACK(r, data, elem) o.pack(m.elem);
82#define OUNPACK(r, data, elem) m.elem = o.via.array.ptr[i++].as<decltype(m.elem)>();
105#define DUNE_DAQ_SERIALIZE_NON_INTRUSIVE(NS, Type, ...) \
106 DUNE_DAQ_SERIALIZABLE(NS::Type, #Type); \
107 namespace msgpack { \
108 MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) \
110 namespace adaptor { \
112 struct pack<NS::Type> \
114 template<typename Stream> \
115 packer<Stream>& operator()(msgpack::packer<Stream>& o, NS::Type const& m) const \
117 o.pack_array(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)); \
118 BOOST_PP_SEQ_FOR_EACH(OPACK, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
123 struct convert<NS::Type> \
125 msgpack::object const& operator()(msgpack::object const& o, NS::Type& m) const \
127 if (o.type != msgpack::type::ARRAY) \
128 throw msgpack::type_error(); \
129 if (o.via.array.size != BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)) \
130 throw msgpack::type_error(); \
132 BOOST_PP_SEQ_FOR_EACH(OUNPACK, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
144#define DUNE_DAQ_SERIALIZE_ENUM(Type) MSGPACK_ADD_ENUM(Type)
150ERS_DECLARE_ISSUE(serialization, UnknownSerializationTypeString,
"Unknown serialization type " << t, ((std::string)t))
152ERS_DECLARE_ISSUE(serialization, UnknownSerializationTypeEnum,
"Unknown serialization type", )
156ERS_DECLARE_ISSUE(serialization, CannotDeserializeMessage, "Cannot deserialize message", )
166namespace serialization {
169struct is_serializable : std::false_type
175enum SerializationType
183inline SerializationType
184from_string(
const std::string s)
188 throw UnknownSerializationTypeString(
ERS_HERE, s);
192serialization_type_byte(SerializationType stype)
198 throw UnknownSerializationTypeEnum(
ERS_HERE);
202constexpr SerializationType DEFAULT_SERIALIZATION_TYPE = kMsgPack;
209serialize(
const T& obj, SerializationType stype = DEFAULT_SERIALIZATION_TYPE)
218 msgpack::sbuffer buf;
219 msgpack::pack(buf, obj);
220 std::vector<uint8_t>
ret(buf.size() + 1);
221 ret[0] = serialization_type_byte(stype);
222 std::copy(buf.data(), buf.data() + buf.size(),
ret.begin() + 1);
226 throw UnknownSerializationTypeEnum(
ERS_HERE);
233template<
class T,
typename CharType =
unsigned char>
235deserialize(
const std::vector<CharType>& v)
240 case serialization_type_byte(kMsgPack): {
253 msgpack::object_handle oh = msgpack::unpack(
254 const_cast<char*
>(
reinterpret_cast<const char*
>(
v.data() + 1)),
256 [](msgpack::type::object_type , std::size_t ,
void* ) ->
bool { return true; });
257 msgpack::object
obj = oh.get();
259 }
catch (msgpack::type_error& e) {
260 throw CannotDeserializeMessage(
ERS_HERE, e);
261 }
catch (msgpack::unpack_error& e) {
262 throw CannotDeserializeMessage(
ERS_HERE, e);
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
UnknownSerializationTypeByte