Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
dump_vars.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// DUMP_VARS() is a convenience macro for writing objects to text logs. It
15// prints all of its arguments as key-value pairs.
16//
17// Example:
18// int foo = 42;
19// vector<int> bar = {1, 2, 3};
20// // Prints: foo = 42, bar.size() = 3
21// LOG(INFO) << DUMP_VARS(foo, bar.size());
22//
23// DUMP_VARS() produces high quality human-readable output for most types:
24// builtin types, strings, anything with operator<<.
25//
26// ====[ Limitations ]====
27//
28// DUMP_VARS() accepts at most 8 arguments.
29//
30// Structured bindings require an extra step to make DUMP_VARS print them. They
31// need to be listed as first argument of DUMP_VARS_WITH_BINDINGS:
32//
33// for (const auto& [x, y, z] : Foo()) {
34// // Would not compile:
35// // LOG(INFO) << DUMP_VARS(x, *y, f(z), other_var);
36// LOG(INFO) << DUMP_VARS_WITH_BINDINGS((x, y, z), x, *y, f(z), other_var);
37// }
38
39#ifndef OR_TOOLS_BASE_DUMP_VARS_H_
40#define OR_TOOLS_BASE_DUMP_VARS_H_
41
42#include <optional>
43#include <ostream>
44#include <sstream>
45#include <string>
46#include <type_traits>
47#include <utility>
48#include <vector>
49
50#include "absl/container/inlined_vector.h"
51
52/* need extra level to force extra eval */
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__)
65
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__))
69
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__))
76
77#define DUMP_VARS(...) DUMP_VARS_WITH_BINDINGS((), __VA_ARGS__)
78
79/* need extra level to force extra eval */
80#define DUMP_STRINGIZE(a) #a,
81#define DUMP_STRINGIFY(...) DUMP_FOR_EACH(DUMP_STRINGIZE, __VA_ARGS__)
82
83// Returns the arguments.
84#define DUMP_IDENTITY(...) __VA_ARGS__
85// Removes parenthesis. Requires argument enclosed in parenthesis.
86#define DUMP_RM_PARENS(...) DUMP_IDENTITY __VA_ARGS__
87
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))
91
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& \
100 names) { \
101 ::operations_research::base::internal_dump_vars::print_fields{ \
102 .os = os, \
103 .field_sep = field_sep, \
104 .kv_sep = kv_sep, \
105 .names = names, \
106 }(__VA_ARGS__); \
107 })
108
110namespace internal_dump_vars {
111
112// needed by routing
113template <typename T>
114std::ostream& operator<<(std::ostream& os,
115 const ::absl::InlinedVector<T, 8>& vec) {
116 for (T it : vec) {
117 os << ::std::to_string(it) << ',';
118 }
119 return os;
120}
121
122// needed by algorithms tests
123template <typename T>
124std::ostream& operator<<(std::ostream& os, const ::std::vector<T>& vec) {
125 for (T it : vec) {
126 os << ::std::to_string(it) << ',';
127 }
128 return os;
129}
130
131template <typename T>
132std::ostream& operator<<(std::ostream& os, const ::std::optional<T>& opt) {
133 if (opt.has_value())
134 os << ::std::to_string(opt.value());
135 else
136 os << "(none)";
137 return os;
138}
139
140using DumpNames = ::std::vector<::std::string>;
141
143 void operator()() {}
144
145 template <class T>
146 void operator()(const T& t) {
147 os << names[n++] << kv_sep << t;
148 }
149
150 template <class T1, class T2, class... Ts>
151 void operator()(const T1& t1, const T2& t2, const Ts&... ts) {
152 os << names[n++] << kv_sep << t1 << field_sep;
153 (*this)(t2, ts...);
154 }
155
156 std::ostream& os;
157 const ::std::string& field_sep;
158 const ::std::string& kv_sep;
160 ::std::size_t n = 0;
161};
162
163template <class F>
164class Dump {
165 public:
166 explicit Dump(const ::std::string&& field_sep, const ::std::string&& kv_sep,
167 DumpNames&& names, F f)
168 : field_sep_(::std::move(field_sep)),
169 kv_sep_(::std::move(kv_sep)),
170 names_(::std::move(names)),
171 f_(::std::move(f)) {}
172
173 ::std::string str() const {
174 ::std::ostringstream oss;
175 oss << *this;
176 return oss.str();
177 }
178
179 template <class... N>
180 Dump<F> as(N&&... names) const {
181 return Dump<F>(::std::string{field_sep_}, ::std::string{kv_sep_},
182 DumpNames{names...}, f_);
183 }
184
185 Dump& sep(::std::string&& field_sep) {
186 field_sep_ = ::std::move(field_sep);
187 return *this;
188 }
189
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);
193 return *this;
194 }
195
196 friend ::std::ostream& operator<<(::std::ostream& os, const Dump& dump) {
197 dump.print_fields_(os);
198 return os;
199 }
200
201 private:
202 void print_fields_(::std::ostream& os) const {
203 f_(os, field_sep_, kv_sep_, names_);
204 }
205
206 ::std::string field_sep_;
207 ::std::string kv_sep_;
208 DumpNames names_;
209 F f_;
210};
211
212template <class F>
214 return Dump<F>(
215 /*field_sep=*/", ",
216 /*kv_sep=*/" = ", ::std::move(names), ::std::move(f));
217}
218
219} // namespace internal_dump_vars
220} // namespace operations_research::base
221
222#endif // OR_TOOLS_BASE_DUMP_VARS_H_
friend::std::ostream & operator<<(::std::ostream &os, const Dump &dump)
Definition dump_vars.h:196
Dump & sep(::std::string &&field_sep, ::std::string &&kv_sep)
Definition dump_vars.h:190
Dump & sep(::std::string &&field_sep)
Definition dump_vars.h:185
Dump(const ::std::string &&field_sep, const ::std::string &&kv_sep, DumpNames &&names, F f)
Definition dump_vars.h:166
Dump< F > make_dump_vars(DumpNames &&names, F f)
Definition dump_vars.h:213
std::ostream & operator<<(std::ostream &os, const ::absl::InlinedVector< T, 8 > &vec)
needed by routing
Definition dump_vars.h:114
::std::vector<::std::string > DumpNames
Definition dump_vars.h:140
STL namespace.
void operator()(const T1 &t1, const T2 &t2, const Ts &... ts)
Definition dump_vars.h:151