Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an easy way to print vectors in debug output. #8072

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,61 @@ class debug {
static int debug_level();
};

/** Allow easily printing the contents of containers, or std::vector-like containers,
* in debug output. Used like so:
* std::vector<Type> arg_types;
* debug(4) << "arg_types: " << ConPrint(arg_types) << "\n";
* Which results in output like "arg_types: { uint8x8, uint8x8 }" on one line.
* "ConPrint" Stands for "Container Print." */
template<typename T>
struct ConPrint {
zvookin marked this conversation as resolved.
Show resolved Hide resolved
const T &container;
ConPrint(const T &container)
: container(container) {
}
};

template<typename StreamT, typename T>
inline StreamT &operator<<(StreamT &stream, const ConPrint<T> &wrapper) {
stream << "{ ";
const char *sep = "";
for (const auto &e : wrapper.container) {
stream << sep << e;
sep = ", ";
}
stream << " }";
return stream;
}

/** Allow easily printing the contents of containers, or std::vector-like containers,
* in debug output. Used like so:
* std::vector<Type> arg_types;
* debug(4) << "arg_types: " << ConPrint(arg_types) << "\n";
* Which results in output like:
* arg_types:
* {
* uint8x8,
* uint8x8,
* }
* Indentation uses a tab character. "ConPrintLn" Stands for "Container Print Line." */
template<typename T>
struct ConPrintLn {
const T &container;
ConPrintLn(const T &container)
: container(container) {
}
};

template<typename StreamT, typename T>
inline StreamT &operator<<(StreamT &stream, const ConPrintLn<T> &wrapper) {
stream << "\n{\n";
for (const auto &e : wrapper.container) {
stream << "\t" << e << ",\n";
}
stream << "}\n";
return stream;
}

} // namespace Internal
} // namespace Halide

Expand Down
Loading