Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
flavor_macros.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
9// Macros for defining the flavor classes.
10// These are used to derive iterator methods along with the body of a 'flavor' class.
11// DEFINE_FLAVOR_MEMBERS lets you define a flavor entity as a collection of individual members, and derive an iterator.
12// while DEFINE_COMPOUND_GET_ALL lets you combine the iterators of substructures or base
13// classes.
14
19
20#include <array>
21#include <iostream>
22#include <sstream>
23#include <tuple>
24#include <type_traits>
25
26namespace bb::detail {
27
28template <typename T, typename... BaseClass> constexpr std::size_t _sum_base_class_size(const T& arg)
29{
30 return (static_cast<const BaseClass&>(arg).size() + ...);
31}
32template <typename T, typename... BaseClass> auto _concatenate_base_class_get_all(T& arg)
33{
34 return concatenate(static_cast<BaseClass&>(arg).get_all()...);
35}
36template <typename T, typename... BaseClass> auto _concatenate_base_class_get_all_const(const T& arg)
37{
38 return concatenate(static_cast<const BaseClass&>(arg).get_all()...);
39}
40template <typename... BaseClass> auto _static_concatenate_base_class_get_labels()
41{
42 return concatenate(BaseClass::get_labels()...);
43}
44
45} // namespace bb::detail
46
47// Needed to force expansion of __VA_ARGS__ before converting to string.
48#define VARARGS_TO_STRING(...) #__VA_ARGS__
49
50// We use std::remove_reference to support a flavor that has references as members. This is an AVM use case.
51#define DEFINE_REF_VIEW(...) \
52 [[nodiscard]] auto get_all() \
53 { \
54 return RefArray<std::remove_reference_t<DataType>, _members_size>{ __VA_ARGS__ }; \
55 } \
56 [[nodiscard]] auto get_all() const \
57 { \
58 return RefArray<const std::remove_reference_t<DataType>, _members_size>{ __VA_ARGS__ }; \
59 }
60
68#define DEFINE_FLAVOR_MEMBERS(DataType, ...) \
69 __VA_OPT__(DataType __VA_ARGS__;) \
70 static constexpr size_t _members_size = std::tuple_size_v<decltype(std::make_tuple(__VA_ARGS__))>; \
71 DEFINE_REF_VIEW(__VA_ARGS__) \
72 static const std::vector<std::string>& get_labels() \
73 { \
74 static const std::vector<std::string> labels = \
75 bb::detail::split_and_trim(VARARGS_TO_STRING(__VA_ARGS__), ','); \
76 return labels; \
77 } \
78 static constexpr std::size_t size() \
79 { \
80 return _members_size; \
81 }
82
83#define DEFINE_COMPOUND_GET_ALL(...) \
84 [[nodiscard]] auto get_all() \
85 { \
86 return bb::detail::_concatenate_base_class_get_all<decltype(*this), __VA_ARGS__>(*this); \
87 } \
88 [[nodiscard]] auto get_all() const \
89 { \
90 return bb::detail::_concatenate_base_class_get_all_const<decltype(*this), __VA_ARGS__>(*this); \
91 } \
92 constexpr std::size_t size() const \
93 { \
94 return bb::detail::_sum_base_class_size<decltype(*this), __VA_ARGS__>(*this); \
95 } \
96 static const std::vector<std::string>& get_labels() \
97 { \
98 static const auto labels = bb::detail::_static_concatenate_base_class_get_labels<__VA_ARGS__>(); \
99 return labels; \
100 }
auto _static_concatenate_base_class_get_labels()
auto _concatenate_base_class_get_all(T &arg)
constexpr std::size_t _sum_base_class_size(const T &arg)
auto _concatenate_base_class_get_all_const(const T &arg)
RefArray< T,(Ns+...)> constexpr concatenate(const RefArray< T, Ns > &... ref_arrays)
Concatenates multiple RefArray objects into a single RefArray.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13