Skip to content

Commit

Permalink
use std::concept to simplify the code
Browse files Browse the repository at this point in the history
jyf111 committed Nov 11, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent e52ce5b commit 12cded6
Showing 4 changed files with 258 additions and 360 deletions.
22 changes: 15 additions & 7 deletions examples/demo.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#include <any>
#include <valarray>

#include "dbg.h"
struct data {
int a, b, c;
@@ -13,8 +10,8 @@ struct data {
};
struct Bad {
int x, y;
std::initializer_list<int> z = { 5, 9, 10 };
};
DBG_REGISTER(Bad, x, y)
struct MyStruct {
struct gps {
double latitude;
@@ -35,7 +32,13 @@ struct MyStruct {
};
image thumbnail;
};

int gg;
struct MyStruct3 {
int a;
float b;
std::string c;
int arr[5];
} s3{ 0, 1., "23" };
MyStruct s{ { 41.13, -73.70 }, { 480, 340, "https://foo/bar/baz.jpg", { MyStruct::image::format::type::yuyv_422 } } };
int main() {
int a = 2;
@@ -56,7 +59,6 @@ int main() {
std::valarray<int> g = { 1, 2 };
DBG(g);
std::any aa = 1;
DBG(aa);
union gb_union {
int x;
short y;
@@ -71,7 +73,7 @@ int main() {
uint8_t ttt = 1;
DBG(ttt);
DBG(true);
DBG(dbg::type<char[2][3][4]>());
DBG(std::type_identity<char[2][3][4]>());
DBG("This is a message");
DBG(std::string("This is a string"));
const char *msg = "MMMM";
@@ -86,5 +88,11 @@ int main() {
Bad bad = { 2, 5 };
DBG(bad);
DBG();
DBG(s3);
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
pq.push(2);
pq.push(1);
DBG(pq);
dbg::last_t<int, double> gggg = 1.;
return 0;
}
559 changes: 223 additions & 336 deletions include/dbg.h

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions tests/unit_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <any>
#include <catch2/catch_test_macros.hpp>

#include "dbg.h"
@@ -260,19 +259,19 @@ TEST_CASE("variadic argument", "[variadic]") {
TEST_CASE("base output", "[base]") {
char value = 110;
short neg = -12;
SECTION("hex") {
DBG(dbg::hex(value));
DBG(dbg::hex(neg));
DBG(dbg::hex(-neg));
SECTION("Hex") {
DBG(dbg::Hex(value));
DBG(dbg::Hex(neg));
DBG(dbg::Hex(-neg));
}
SECTION("bin") {
DBG(dbg::bin(value));
DBG(dbg::bin(neg));
DBG(dbg::bin(-neg));
SECTION("Bin") {
DBG(dbg::Bin(value));
DBG(dbg::Bin(neg));
DBG(dbg::Bin(-neg));
}
SECTION("oct") {
DBG(dbg::oct(value));
DBG(dbg::oct(neg));
SECTION("Oct") {
DBG(dbg::Oct(value));
DBG(dbg::Oct(neg));
}
}

@@ -350,7 +349,7 @@ union gb_union {
TEST_CASE("union") {
gb_union gb;
gb.x = 2;
DBG(dbg::type<decltype(gb)>());
DBG(std::type_identity<decltype(gb)>());
DBG(gb.x);
union data {
int n;
@@ -360,7 +359,7 @@ TEST_CASE("union") {
ud.f = 1.23;
DBG(ud.f);
// dbg(ud); // ! cann't know the value
DBG(dbg::type<decltype(ud)>());
DBG(std::type_identity<decltype(ud)>());
}

enum gb_enum { bad, ok };
@@ -414,7 +413,6 @@ TEST_CASE("aggregate") {
gb_struct gs{ 1, 2, 3, 4 };
DBG(gs);
gb_class_private gcp;
DBG(gcp);
DBG(std::is_aggregate_v<gb_class_private>);
gb_class gc{ 166 };
DBG(gc);
@@ -436,7 +434,7 @@ TEST_CASE("aggregate") {
char ch;
std::string name;
};
DBG(dbg::flatten::unique_counter_impl<complex_data>());
DBG(dbg::flatten::num_aggregate_fields_v<complex_data>);
int tmp = 6;
complex_data cd = { { 1, 2, 3 }, { 6, 6.6 }, &tmp, "complex", 'P', "Alex" };
DBG(cd);
7 changes: 6 additions & 1 deletion utils/gen_flatten.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
HEADER = "template <typename T>\nconstexpr auto flatten_impl(const T& t, std::integral_constant<std::size_t, 0> N1) noexcept {\n"
HEADER = "template <is_aggregate Aggregate>\nconstexpr auto flatten_impl(const Aggregate& t, std::integral_constant<size_t, 0> N1) noexcept {\n"
index = HEADER.find('0')

def gen_head(num):
@@ -31,3 +31,8 @@ def gen(num):
else:
for i in range(int(sys.argv[1]) + 1):
print(gen(i))
print("template <is_aggregate Aggregate, size_t N>")
print("constexpr auto flatten_impl(const Aggregate &t, std::integral_constant<size_t, N> N1) noexcept {")
print(" config::get_stream() << printer::error_print(\"Please rerun utils/gen_flatten.py to generate more binds!\");")
print(" return std::forward_as_tuple();")
print("}")

0 comments on commit 12cded6

Please sign in to comment.