Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
streams.hpp
Go to the documentation of this file.
1#pragma once
4#include <iomanip>
5#include <map>
6#include <memory>
7#include <optional>
8#include <ostream>
9#include <vector>
10
11// clang-format off
12// disabling the following style guides:
13// cert-dcl58-cpp , restricts modifying the standard library. We need to do this for portable serialization methods
14// NOLINTBEGIN(cert-dcl58-cpp)
15// clang-format on
16
17namespace serialize {
21template <typename T> void _msgpack_stream_write(std::ostream& os, const std::shared_ptr<T>& field)
22{
23 using namespace serialize;
24 os << *field;
25}
29inline void _msgpack_stream_write(std::ostream& os, const auto& field)
30{
31 using namespace serialize;
32 os << field;
33}
37inline void _msgpack_stream_write_key_value_pairs(std::ostream& os)
38{
39 // base case
40 (void)os; // unused
41}
45inline void _msgpack_stream_write_key_value_pairs(std::ostream& os,
46 const std::string& key,
47 const auto& value,
48 const auto&... rest)
49{
50 os << key << ": ";
52 os << '\n';
53 _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT
54}
60inline void _msgpack_stream_write_key_value_pairs(std::ostream& os,
61 const std::string& key,
63 const auto&... rest)
64{
65 os << key << ":\n";
67 os << '\n';
68 _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT
69}
70} // namespace serialize
71
72namespace std {
79template <msgpack_concepts::HasMsgPack T> std::ostream& operator<<(std::ostream& os, const T& obj)
80{
81 // We must use const_cast as our method is meant to be polymorphic over const, but there's no such concept in C++
82 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
83 const_cast<T&>(obj).msgpack([&](auto&... key_value_pairs) {
84 // apply 'operator<<' to each object field
86 });
87 return os;
88}
89
90inline std::ostream& operator<<(std::ostream& os, std::vector<uint8_t> const& arr)
91{
92 std::ios_base::fmtflags f(os.flags());
93 os << "[" << std::hex << std::setfill('0');
94 for (auto byte : arr) {
96 }
97 os << " ]";
98 os.flags(f);
99 return os;
100}
101
102template <std::integral T, typename A> inline std::ostream& operator<<(std::ostream& os, std::vector<T, A> const& arr)
103{
104 os << "[";
105 for (auto element : arr) {
106 os << ' ' << element;
107 }
108 os << " ]";
109 return os;
110}
111
112template <typename T, typename A>
113 requires(!std::integral<T>)
114inline std::ostream& operator<<(std::ostream& os, std::vector<T, A> const& arr)
115{
116 os << "[\n";
117 for (auto element : arr) {
118 os << ' ' << element << '\n';
119 }
120 os << "]";
121 return os;
122}
123
124template <size_t S> inline std::ostream& operator<<(std::ostream& os, std::array<uint8_t, S> const& arr)
125{
126 std::ios_base::fmtflags f(os.flags());
127 os << "[" << std::hex << std::setfill('0');
128 for (auto byte : arr) {
130 }
131 os << " ]";
132 os.flags(f);
133 return os;
134}
135
136template <typename T, size_t S> inline std::ostream& operator<<(std::ostream& os, std::array<T, S> const& arr)
137{
138 std::ios_base::fmtflags f(os.flags());
139 os << "[" << std::hex << std::setfill('0');
140 for (auto element : arr) {
141 os << ' ' << element;
142 }
143 os << " ]";
144 os.flags(f);
145 return os;
146}
147
148template <typename T, typename U> inline std::ostream& operator<<(std::ostream& os, std::pair<T, U> const& pair)
149{
150 os << "(" << pair.first << ", " << pair.second << ")";
151 return os;
152}
153
154template <typename T> inline std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
155{
156 return opt ? os << *opt : os << "std::nullopt";
157}
158
159template <typename T, typename U> inline std::ostream& operator<<(std::ostream& os, std::map<T, U> const& map)
160{
161 os << "[\n";
162 for (const auto& elem : map) {
163 os << " " << elem.first << ": " << elem.second << "\n";
164 }
165 os << "]";
166 return os;
167}
168} // namespace std
169
170// NOLINTEND(cert-dcl58-cpp)
void _msgpack_stream_write(std::ostream &os, const std::shared_ptr< T > &field)
Helper method for streaming msgpack values, specialized for shared_ptr.
Definition streams.hpp:21
void _msgpack_stream_write_key_value_pairs(std::ostream &os)
Recursive helper method for streaming msgpack key value pairs, base case.
Definition streams.hpp:37
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::ostream & operator<<(std::ostream &os, const T &obj)
Automatically derived stream operator for any object that defines .msgpack() (implicitly defined by M...
Definition streams.hpp:79