39#ifndef OR_TOOLS_BASE_DUMP_VARS_H_
40#define OR_TOOLS_BASE_DUMP_VARS_H_
50#include "absl/container/inlined_vector.h"
53#define DUMP_FOR_EACH_N0(F)
54#define DUMP_FOR_EACH_N1(F, a) F(a)
55#define DUMP_FOR_EACH_N2(F, a, ...) F(a) DUMP_FOR_EACH_N1(F, __VA_ARGS__)
56#define DUMP_FOR_EACH_N3(F, a, ...) F(a) DUMP_FOR_EACH_N2(F, __VA_ARGS__)
57#define DUMP_FOR_EACH_N4(F, a, ...) F(a) DUMP_FOR_EACH_N3(F, __VA_ARGS__)
58#define DUMP_FOR_EACH_N5(F, a, ...) F(a) DUMP_FOR_EACH_N4(F, __VA_ARGS__)
59#define DUMP_FOR_EACH_N6(F, a, ...) F(a) DUMP_FOR_EACH_N5(F, __VA_ARGS__)
60#define DUMP_FOR_EACH_N7(F, a, ...) F(a) DUMP_FOR_EACH_N6(F, __VA_ARGS__)
61#define DUMP_FOR_EACH_N8(F, a, ...) F(a) DUMP_FOR_EACH_N7(F, __VA_ARGS__)
62#define DUMP_FOR_EACH_N9(F, a, ...) F(a) DUMP_FOR_EACH_N8(F, __VA_ARGS__)
63#define DUMP_FOR_EACH_N10(F, a, ...) F(a) DUMP_FOR_EACH_N9(F, __VA_ARGS__)
64#define DUMP_FOR_EACH_N11(F, a, ...) F(a) DUMP_FOR_EACH_N10(F, __VA_ARGS__)
66#define DUMP_CONCATENATE(x, y) x##y
67#define DUMP_FOR_EACH_(N, F, ...) \
68 DUMP_CONCATENATE(DUMP_FOR_EACH_N, N)(F __VA_OPT__(, __VA_ARGS__))
70#define DUMP_NARG(...) DUMP_NARG_(__VA_OPT__(__VA_ARGS__, ) DUMP_RSEQ_N())
71#define DUMP_NARG_(...) DUMP_ARG_N(__VA_ARGS__)
72#define DUMP_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, N, ...) N
73#define DUMP_RSEQ_N() 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
74#define DUMP_FOR_EACH(F, ...) \
75 DUMP_FOR_EACH_(DUMP_NARG(__VA_ARGS__), F __VA_OPT__(, __VA_ARGS__))
77#define DUMP_VARS(...) DUMP_VARS_WITH_BINDINGS((), __VA_ARGS__)
80#define DUMP_STRINGIZE(a) #a,
81#define DUMP_STRINGIFY(...) DUMP_FOR_EACH(DUMP_STRINGIZE, __VA_ARGS__)
84#define DUMP_IDENTITY(...) __VA_ARGS__
86#define DUMP_RM_PARENS(...) DUMP_IDENTITY __VA_ARGS__
88#define DUMP_GEN_ONE_BINDING(a) , &a = a
89#define DUMP_GEN_BINDING(binding) \
90 &DUMP_FOR_EACH(DUMP_GEN_ONE_BINDING, DUMP_RM_PARENS(binding))
92#define DUMP_VARS_WITH_BINDINGS(binding, ...) \
93 ::operations_research::base::internal_dump_vars::make_dump_vars<>( \
94 ::operations_research::base::internal_dump_vars::DumpNames{ \
95 DUMP_STRINGIFY(__VA_ARGS__)}, \
96 [DUMP_GEN_BINDING(binding)]( \
97 ::std::ostream& os, const ::std::string& field_sep, \
98 const ::std::string& kv_sep, \
99 const ::operations_research::base::internal_dump_vars::DumpNames& \
101 ::operations_research::base::internal_dump_vars::print_fields{ \
103 .field_sep = field_sep, \
110namespace internal_dump_vars {
115 const ::absl::InlinedVector<T, 8>& vec) {
117 os << ::std::to_string(it) <<
',';
124std::ostream&
operator<<(std::ostream& os, const ::std::vector<T>& vec) {
126 os << ::std::to_string(it) <<
',';
132std::ostream&
operator<<(std::ostream& os, const ::std::optional<T>& opt) {
134 os << ::std::to_string(opt.value());
150 template <
class T1,
class T2,
class... Ts>
151 void operator()(
const T1& t1,
const T2& t2,
const Ts&... ts) {
166 explicit Dump(const ::std::string&& field_sep, const ::std::string&& kv_sep,
168 : field_sep_(::
std::move(field_sep)),
169 kv_sep_(::
std::move(kv_sep)),
170 names_(::
std::move(names)),
171 f_(::
std::move(f)) {}
173 ::std::string
str()
const {
174 ::std::ostringstream oss;
179 template <
class... N>
181 return Dump<F>(::std::string{field_sep_}, ::std::string{kv_sep_},
186 field_sep_ = ::std::move(field_sep);
190 Dump&
sep(::std::string&& field_sep, ::std::string&& kv_sep) {
191 field_sep_ = ::std::move(field_sep);
192 kv_sep_ = ::std::move(kv_sep);
197 dump.print_fields_(os);
202 void print_fields_(::std::ostream& os)
const {
203 f_(os, field_sep_, kv_sep_, names_);
206 ::std::string field_sep_;
207 ::std::string kv_sep_;
216 " = ", ::std::move(names), ::std::move(f));
::std::string str() const
Dump< F > as(N &&... names) const
friend::std::ostream & operator<<(::std::ostream &os, const Dump &dump)
Dump & sep(::std::string &&field_sep, ::std::string &&kv_sep)
Dump & sep(::std::string &&field_sep)
Dump(const ::std::string &&field_sep, const ::std::string &&kv_sep, DumpNames &&names, F f)
Dump< F > make_dump_vars(DumpNames &&names, F f)
std::ostream & operator<<(std::ostream &os, const ::absl::InlinedVector< T, 8 > &vec)
needed by routing
::std::vector<::std::string > DumpNames
const ::std::string & kv_sep
const ::std::string & field_sep
void operator()(const T1 &t1, const T2 &t2, const Ts &... ts)
void operator()(const T &t)