Skip to content

Commit

Permalink
add return type for DBG
Browse files Browse the repository at this point in the history
  • Loading branch information
jyf111 committed Nov 11, 2023
1 parent dbb6d66 commit e52ce5b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
40 changes: 34 additions & 6 deletions examples/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,42 @@ struct data {
};
dir dr;
};
struct Bad {
int x, y;
};
DBG_REGISTER(Bad, x, y)
struct MyStruct {
struct gps {
double latitude;
double longitude;
};
gps location;

struct image {
uint16_t width;
uint16_t height;
std::string url;

struct format {
enum class type { bayer_10bit, yuyv_422 };
type type;
};
format format;
};
image thumbnail;
};

MyStruct s{ { 41.13, -73.70 }, { 480, 340, "https://foo/bar/baz.jpg", { MyStruct::image::format::type::yuyv_422 } } };
int main() {
int a = 2;
short b = 3;
long long x = 7;
int &ref = a;
long &&rref = 4l;
data d{ 1, 2, 3, "hello", { 6, 0, 8 } };
DBG(a);
DBG(b);
DBG(x);
DBG(ref);
DBG(rref);
DBG(a, b, x);
DBG(ref, rref);
DBG(d);
std::deque<int> dq{ 1, 2 };
DBG(dq);
DBG((std::vector<int>{ 1, 2 }));
Expand All @@ -48,7 +72,6 @@ int main() {
DBG(ttt);
DBG(true);
DBG(dbg::type<char[2][3][4]>());
// DBG();
DBG("This is a message");
DBG(std::string("This is a string"));
const char *msg = "MMMM";
Expand All @@ -58,5 +81,10 @@ int main() {
int y = DBG(x) + 2;
const int32_t A = 2;
const int32_t B = DBG(3 * A) + 1;
DBG(B);
DBG(s);
Bad bad = { 2, 5 };
DBG(bad);
DBG();
return 0;
}
31 changes: 23 additions & 8 deletions include/dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,17 @@ void print(std::ostream &os, const base<T, N> &value) {
}
} // namespace printer

template <typename T, typename... U>
struct last {
using type = typename last<U...>::type;
};
template <typename T>
struct last<T> {
using type = T;
};
template <typename... T>
using last_t = typename last<T...>::type;

class Debugger {
public:
Debugger(const char *file, int line, const char *func) : os_(config::get_stream()) {
Expand All @@ -816,14 +827,14 @@ class Debugger {

void print() { os_ << (printer::location_print(location_) + '\n'); }
template <typename... T>
void print(const std::initializer_list<std::string> &exprs, const std::initializer_list<std::string> &type_names,
T &&...values) {
print_impl(exprs.begin(), type_names.begin(), std::forward<T>(values)...);
auto print(const std::initializer_list<std::string> &exprs, const std::initializer_list<std::string> &type_names,
T &&...values) -> last_t<T...> {
return print_impl(exprs.begin(), type_names.begin(), std::forward<T>(values)...);
}

private:
template <typename T>
void print_impl(const std::string *expr_iter, const std::string *type_name_iter, T &&value) {
T &&print_impl(const std::string *expr_iter, const std::string *type_name_iter, T &&value) {
std::stringstream ss;
ss << printer::location_print(location_) << ' ';
ss << printer::expression_print(*expr_iter) << " = ";
Expand All @@ -832,20 +843,24 @@ class Debugger {
ss << printer::value_print(ss2.str());
ss << " (" << printer::type_print(*type_name_iter) << ")\n";
os_ << ss.str();
return std::forward<T>(value);
}
template <size_t N>
void print_impl(const std::string *, const std::string *, const char (&value)[N]) {
auto print_impl(const std::string *, const std::string *, const char (&value)[N]) -> decltype(value) {
os_ << (printer::location_print(location_) + ' ' + printer::message_print(value) + '\n');
return value;
}
template <typename T>
void print_impl(const std::string *, const std::string *type_name_iter, type<T> &&value) {
type<T> &&print_impl(const std::string *, const std::string *type_name_iter, type<T> &&value) {
os_ << (printer::location_print(location_) + ' ' +
printer::type_print(*type_name_iter + " [sizeof " + std::to_string(sizeof(T)) + "]") + '\n');
return std::forward<type<T>>(value);
}
template <typename T, typename... U>
void print_impl(const std::string *expr_iter, const std::string *type_name_iter, T &&value, U &&...rest) {
auto print_impl(const std::string *expr_iter, const std::string *type_name_iter, T &&value, U &&...rest)
-> last_t<U...> {
print_impl(expr_iter, type_name_iter, std::forward<T>(value));
print_impl(expr_iter + 1, type_name_iter + 1, std::forward<U>(rest)...);
return print_impl(expr_iter + 1, type_name_iter + 1, std::forward<U>(rest)...);
}

std::ostream &os_;
Expand Down

0 comments on commit e52ce5b

Please sign in to comment.