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
13#ifndef IPC_CODEGEN_MSGPACK_STRUCT_MAP_ADAPTOR_DEFINED
14#define IPC_CODEGEN_MSGPACK_STRUCT_MAP_ADAPTOR_DEFINED
15
17// reads structs with msgpack() method from a JSON-like dictionary
18template <msgpack_concepts::HasMsgPack T> struct convert<T> {
19 msgpack::object const& operator()(msgpack::object const& o, T& v) const
20 {
22 "SERIALIZATION_FIELDS requires default-constructible types (used during unpacking)");
23 v.msgpack([&](auto&... args) {
24 auto static_checker = [&](auto&... value_args) {
25 static_assert(msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
26 "SERIALIZATION_FIELDS requires a constructor that can take the types listed in "
27 "SERIALIZATION_FIELDS. "
28 "Type or arg count mismatch, or member initializer constructor not available.");
29 };
30 // Call static checker to ensure we have a constructor that takes all fields - unless we opt-out.
31 if constexpr (!requires { typename T::MSGPACK_NO_STATIC_CHECK; }) {
32 std::apply(static_checker, drop_keys(std::tie(args...)));
33 }
34 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_unpack(o);
35 });
36 return o;
37 }
38};
39
40// converts structs with msgpack() method from a JSON-like dictionary
41template <msgpack_concepts::HasMsgPack T> struct pack<T> {
42 template <typename Stream> packer<Stream>& operator()(msgpack::packer<Stream>& o, T const& v) const
43 {
45 "SERIALIZATION_FIELDS requires default-constructible types (used during unpacking)");
46 const_cast<T&>(v).msgpack([&](auto&... args) {
47 auto static_checker = [&](auto&... value_args) {
48 static_assert(
49 msgpack_concepts::MsgpackConstructible<T, decltype(value_args)...>,
50 "T requires a constructor that can take the fields listed in SERIALIZATION_FIELDS (T will be "
51 "in template parameters in the compiler stack trace)"
52 "Check the SERIALIZATION_FIELDS macro usage in T for incompleteness or wrong order. "
53 "Alternatively, a matching member initializer constructor might not be available for T "
54 "and should be defined.");
55 };
56 // Call static checker to ensure we have a constructor that takes all fields - unless we opt-out.
57 if constexpr (!requires { typename T::MSGPACK_NO_STATIC_CHECK; }) {
58 std::apply(static_checker, drop_keys(std::tie(args...)));
59 }
60 msgpack::type::define_map<decltype(args)...>{ args... }.msgpack_pack(o);
61 });
62 return o;
63 }
64};
65
66} // namespace msgpack::adaptor
67
68#endif
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:18
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