barretenberg
Loading...
Searching...
No Matches
struct_map_impl.hpp
1#pragma once
2// Note: heavy header due to serialization logic, don't include if msgpack.hpp will do
3#include <cassert>
4#include <iomanip>
5#include <iostream>
6#include <sstream>
7#include <string>
8#define MSGPACK_NO_BOOST
9#define MSGPACK_USE_STD_VARIANT_ADAPTOR
10#include "concepts.hpp"
11#include "drop_keys.hpp"
12#include <msgpack.hpp>
13
14namespace msgpack::adaptor {
15// reads structs with msgpack() method from a JSON-like dictionary
16template <msgpack_concepts::HasMsgPack T> struct convert<T> {
17 msgpack::object const& operator()(msgpack::object const& o, T& v) const
18 {
19 static_assert(std::is_default_constructible_v<T>,
20 "MSGPACK_FIELDS requires default-constructible types (used during unpacking)");
21 v.msgpack([&](auto&... args) {
22 auto static_checker = [&](auto&... value_args) {
23 static_assert(msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
24 "MSGPACK_FIELDS requires a constructor that can take the types listed in MSGPACK_FIELDS. "
25 "Type or arg count mismatch, or member initializer constructor not available.");
26 };
27 std::apply(static_checker, drop_keys(std::tie(args...)));
28 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_unpack(o);
29 });
30 return o;
31 }
32};
33
34// converts structs with msgpack() method from a JSON-like dictionary
35template <msgpack_concepts::HasMsgPack T> struct pack<T> {
36 template <typename Stream> packer<Stream>& operator()(msgpack::packer<Stream>& o, T const& v) const
37 {
38 static_assert(std::is_default_constructible_v<T>,
39 "MSGPACK_FIELDS requires default-constructible types (used during unpacking)");
40 const_cast<T&>(v).msgpack([&](auto&... args) {
41 auto static_checker = [&](auto&... value_args) {
42 static_assert(msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
43 "T requires a constructor that can take the fields listed in MSGPACK_FIELDS (T will be "
44 "in template parameters in the compiler stack trace)"
45 "Check the MSGPACK_FIELDS macro usage in T for incompleteness or wrong order."
46 "Alternatively, a matching member initializer constructor might not be available for T "
47 "and should be defined.");
48 };
49 std::apply(static_checker, drop_keys(std::tie(args...)));
50 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_pack(o);
51 });
52 return o;
53 }
54};
55
56} // namespace msgpack::adaptor
Definition: concepts.hpp:16