3838#include < span>
3939#include < boost/throw_exception.hpp>
4040#include < cereal/types/vector.hpp>
41+ #include < msgpack/msgpack.hpp>
4142#if defined(USE_FLATBUFFERS)
42- #include " flatbuffers/flatbuffers.h"
43+ #include < flatbuffers/flatbuffers.h>
4344#endif
4445
4546#define SERIALIZE_TO_STREAM_ARCHIVE (osa, o ) osa(CEREAL_NVP(o))
@@ -79,6 +80,16 @@ struct CORE_LIBRARY_DLL_SHARED_API protobuf_oarchive
7980{
8081};
8182
83+ /* ! \brief In archive placeholder struct for serializing MessagePack. */
84+ struct CORE_LIBRARY_DLL_SHARED_API msgpack_iarchive
85+ {
86+ };
87+
88+ /* ! \brief Out archive placeholder struct for serializing MessagePack. */
89+ struct CORE_LIBRARY_DLL_SHARED_API msgpack_oarchive
90+ {
91+ };
92+
8293/* ! \brief The archives namespace. */
8394namespace archives
8495{
@@ -94,6 +105,8 @@ using out_json_t = cereal::JSONOutputArchive;
94105using out_raw_t = raw_oarchive;
95106/* ! \brief Typedef to output using Google protocol buffers. */
96107using out_protobuf_t = protobuf_oarchive;
108+ /* ! \brief Typedef to output using MessagePack. */
109+ using out_msgpack_t = msgpack_oarchive;
97110/* ! \brief Typedef to input portable binary archive. */
98111using in_port_bin_t = cereal::PortableBinaryInputArchive;
99112/* ! \brief Typedef to input binary archive. */
@@ -106,6 +119,8 @@ using in_json_t = cereal::JSONInputArchive;
106119using in_raw_t = raw_iarchive;
107120/* ! \brief Typedef to input using Google protocol buffers. */
108121using in_protobuf_t = protobuf_iarchive;
122+ /* ! \brief Typedef to input using MessagePack. */
123+ using in_msgpack_t = msgpack_iarchive;
109124
110125} // namespace archives
111126
@@ -252,6 +267,41 @@ template <typename T> struct ToCharVectorImpl<T, archives::out_protobuf_t>
252267 }
253268};
254269
270+ /* ! \brief Serialization to char vector implementation, specialization for Messagepack bufs. */
271+ template <typename T>
272+ struct ToCharVectorImpl <T, archives::out_msgpack_t >
273+ {
274+ /* !
275+ * \brief Function operator
276+ * \param[in] object - Object to serialize
277+ * \return Char vector containing serialized object
278+ *
279+ * This overload creates new memory.
280+ */
281+ char_vector_t operator ()(const T& object) const
282+ {
283+ msgpack::sbuffer buffer;
284+ msgpack::pack (buffer, object);
285+
286+ return char_vector_t (buffer.data (), buffer.data () + buffer.size ());
287+ }
288+
289+ /* !
290+ * \brief Function operator
291+ * \param[in] object - Object to serialize
292+ * \param[out] result - Char vector containing serialized object
293+ *
294+ * This overload uses the memory passed in and resizes if necessary.
295+ */
296+ void operator ()(const T& object, char_vector_t & result) const
297+ {
298+ msgpack::sbuffer buffer;
299+ msgpack::pack (buffer, object);
300+
301+ result.assign (buffer.data (), buffer.data () + buffer.size ());
302+ }
303+ };
304+
255305/* !
256306 * \brief Deserialization to object implementation.
257307 *
@@ -330,6 +380,31 @@ template <typename T> struct ToObjectImpl<T, archives::in_protobuf_t>
330380 }
331381};
332382
383+ /* ! \brief Deserialization to object implementation, specialization for Google protocol buffers. */
384+ template <typename T>
385+ struct ToObjectImpl <T, archives::in_msgpack_t >
386+ {
387+ /* !
388+ * \brief Function operator
389+ * \param[in] charSpan - Char span containing serialized object
390+ * \return Deserialized object
391+ */
392+ T operator ()(char_cspan_buf_t charSpan) const
393+ {
394+ if (charSpan.empty ())
395+ {
396+ BOOST_THROW_EXCEPTION (std::runtime_error (" Cannot deserialize MessagePack object from empty buffer." ));
397+ }
398+
399+ msgpack::object_handle oh = msgpack::unpack (charSpan.data (), charSpan.size ());
400+
401+ T object;
402+ oh.get ().convert (object);
403+
404+ return object;
405+ }
406+ };
407+
333408} // namespace impl
334409
335410/* !
@@ -416,7 +491,7 @@ char_vector_t ToCharVectorFlatBuf(const T& object, PackFunc packFunc)
416491
417492/* !
418493* \brief Function to serialize object via flatbuffers
419- * \param[in] builder - A falttbuffer builder already initialised with the message fields.
494+ * \param[in] builder - A flatbuffer builder already initialised with the message fields.
420495* \return Char vector containing serialized object
421496*
422497* This overload creates new memory but has best performance regarding creating a message view object
0 commit comments