Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
struct_map_impl.hpp
Go to the documentation of this file.
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
9#include "concepts.hpp"
10#include "drop_keys.hpp"
11#include <msgpack.hpp>
12
14// reads structs with msgpack() method from a JSON-like dictionary
15template <msgpack_concepts::HasMsgPack T> struct convert<T> {
16 msgpack::object const& operator()(msgpack::object const& o, T& v) const
17 {
19 "MSGPACK_FIELDS requires default-constructible types (used during unpacking)");
20 v.msgpack([&](auto&... args) {
21 auto static_checker = [&](auto&... value_args) {
22 static_assert(msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
23 "MSGPACK_FIELDS requires a constructor that can take the types listed in MSGPACK_FIELDS. "
24 "Type or arg count mismatch, or member initializer constructor not available.");
25 };
26 // Call static checker to ensure we have a constructor that takes all fields - unless we opt-out.
27 if constexpr (!requires { typename T::MSGPACK_NO_STATIC_CHECK; }) {
28 std::apply(static_checker, drop_keys(std::tie(args...)));
29 }
30 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_unpack(o);
31 });
32 return o;
33 }
34};
35
36// converts structs with msgpack() method from a JSON-like dictionary
37template <msgpack_concepts::HasMsgPack T> struct pack<T> {
38 template <typename Stream> packer<Stream>& operator()(msgpack::packer<Stream>& o, T const& v) const
39 {
41 "MSGPACK_FIELDS requires default-constructible types (used during unpacking)");
42 const_cast<T&>(v).msgpack([&](auto&... args) {
43 auto static_checker = [&](auto&... value_args) {
44 static_assert(msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
45 "T requires a constructor that can take the fields listed in MSGPACK_FIELDS (T will be "
46 "in template parameters in the compiler stack trace)"
47 "Check the MSGPACK_FIELDS macro usage in T for incompleteness or wrong order. "
48 "Alternatively, a matching member initializer constructor might not be available for T "
49 "and should be defined.");
50 };
51 // Call static checker to ensure we have a constructor that takes all fields - unless we opt-out.
52 if constexpr (!requires { typename T::MSGPACK_NO_STATIC_CHECK; }) {
53 std::apply(static_checker, drop_keys(std::tie(args...)));
54 }
55 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_pack(o);
56 });
57 return o;
58 }
59};
60
61} // namespace msgpack::adaptor
constexpr std::array< uint8_t, S > convert(const std::string_view &in)
auto drop_keys(std::tuple< Args... > &&tuple)
Drops every first value pairwise of a flat argument tuple, assuming that they are keys.
Definition drop_keys.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
msgpack::object const & operator()(msgpack::object const &o, T &v) const
packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const