From 12cded6ec5146804d9b8181ed4899b740156ef90 Mon Sep 17 00:00:00 2001 From: jyf111 Date: Sat, 11 Nov 2023 17:14:54 +0800 Subject: [PATCH] use std::concept to simplify the code --- examples/demo.cpp | 22 +- include/dbg.h | 559 +++++++++++++++++-------------------------- tests/unit_test.cpp | 30 ++- utils/gen_flatten.py | 7 +- 4 files changed, 258 insertions(+), 360 deletions(-) diff --git a/examples/demo.cpp b/examples/demo.cpp index 2052a0b..14c607b 100644 --- a/examples/demo.cpp +++ b/examples/demo.cpp @@ -1,6 +1,3 @@ -#include -#include - #include "dbg.h" struct data { int a, b, c; @@ -13,8 +10,8 @@ struct data { }; struct Bad { int x, y; + std::initializer_list 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 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()); + DBG(std::type_identity()); 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, std::greater> pq; + pq.push(2); + pq.push(1); + DBG(pq); + dbg::last_t gggg = 1.; return 0; } diff --git a/include/dbg.h b/include/dbg.h index 6e941cb..ab53b1f 100644 --- a/include/dbg.h +++ b/include/dbg.h @@ -1,6 +1,8 @@ #pragma once +#include #include +#include #include #include #include @@ -19,6 +21,7 @@ #include #include #include +#include #include #include @@ -66,7 +69,7 @@ inline std::ostream &get_stream() { else return std::cerr; } -inline std::size_t &container_length() { return getter().CONTAINER_LENGTH; } +inline size_t &container_length() { return getter().CONTAINER_LENGTH; } inline std::string &location_color() { return getter().LOCATION_COLOR; } inline std::string &expression_color() { return getter().EXPRESSION_COLOR; } inline std::string &value_color() { return getter().VALUE_COLOR; } @@ -104,34 +107,71 @@ DBG_COLOR_PRINT(type); #undef DBG_COLOR_PRINT } // namespace printer +template +struct Base { + Base(T val) : val_(val) {} + T val_; +}; template -struct type {}; - -template -struct base { - static_assert(std::is_integral::value, "Only integral types are supported!"); - - base(T val) : val_(val) {} +using Bin = Base; +template +using Oct = Base; +template +using Hex = Base; - T val_; +template +concept is_aggregate = std::is_aggregate_v; +template +concept is_enum = std::is_enum_v; +template +concept is_union = std::is_union_v; +template +concept has_begin = requires(T t) { + std::begin(t); +}; +template +concept has_end = requires(T t) { + std::end(t); }; template -using bin = base; +concept has_size = requires(T t) { + std::size(t); +}; template -using oct = base; +concept has_ostream_operator = requires(std::ostream &os, T t) { + os << t; +}; template -using hex = base; +concept is_container = has_begin && has_end && has_size && !std::same_as, std::string>; +// Ignore explicitly specified template arguments template -std::enable_if_t && !std::is_union_v, std::string> type_name(type) { +requires(!std::is_enum_v && !std::is_union_v) std::string type_name(std::type_identity) { std::string_view pretty_name(std::source_location::current().function_name()); const auto L = pretty_name.find("T = ") + 4; const auto R = pretty_name.find_last_of(';'); return std::string(pretty_name.substr(L, R - L)); } +template +std::string type_name(std::type_identity) { + std::string_view pretty_name(std::source_location::current().function_name()); + const auto L = pretty_name.find("Enum = ") + 7; + const auto R = pretty_name.find_last_of(';'); + return "enum " + std::string(pretty_name.substr(L, R - L)) + " : " + + type_name(std::type_identity>{}); +} +template +std::string type_name(std::type_identity) { + std::string_view pretty_name(std::source_location::current().function_name()); + const auto L = pretty_name.find("Union = ") + 8; + const auto R = pretty_name.find_last_of(';'); + return "union " + std::string(pretty_name.substr(L, R - L)); +} -#define DBG_PRIMITIVE_TYPE_NAME(std_type, bit_type, bits) \ - inline std::string type_name(type) { return sizeof(std_type) * CHAR_BIT == bits ? #bit_type : #std_type; } +#define DBG_PRIMITIVE_TYPE_NAME(std_type, bit_type, bits) \ + inline std::string type_name(std::type_identity) { \ + return sizeof(std_type) * CHAR_BIT == bits ? #bit_type : #std_type; \ + } DBG_PRIMITIVE_TYPE_NAME(signed char, int8_t, 8); DBG_PRIMITIVE_TYPE_NAME(unsigned char, uint8_t, 8); DBG_PRIMITIVE_TYPE_NAME(short, int16_t, 16); @@ -144,11 +184,10 @@ DBG_PRIMITIVE_TYPE_NAME(long long, int64_t, 64); DBG_PRIMITIVE_TYPE_NAME(unsigned long long, uint64_t, 64); #undef DBG_PRIMITIVE_TYPE_NAME -inline std::string type_name(type) { return "std::any"; } -inline std::string type_name(type) { return "std::string"; } -inline std::string type_name(type) { return "std::string_view"; } +inline std::string type_name(std::type_identity) { return "std::any"; } +inline std::string type_name(std::type_identity) { return "std::string"; } +inline std::string type_name(std::type_identity) { return "std::string_view"; } -// TODO 完美转发有什么问题 template std::string get_type_name() { if (std::is_volatile_v) { @@ -174,30 +213,12 @@ std::string get_type_name() { if (std::is_rvalue_reference_v) { return get_type_name>() + " &&"; } - return type_name(type{}); // thanks for ADL -} - -template -std::enable_if_t, std::string> type_name(type) { - std::string_view pretty_name(std::source_location::current().function_name()); - const auto L = pretty_name.find("Enum = ") + 7; - const auto R = pretty_name.find_last_of(';'); - std::string name(pretty_name.substr(L, R - L)); - return "enum " + name + " : " + get_type_name>(); -} - -template -std::enable_if_t, std::string> type_name(type) { - std::string_view pretty_name(std::source_location::current().function_name()); - const auto L = pretty_name.find("Union = ") + 8; - const auto R = pretty_name.find_last_of(';'); - std::string name(pretty_name.substr(L, R - L)); - return "union " + name; + return type_name(std::type_identity{}); } #define DBG_TEMPLATE_TYPE_NAME_1(std_type, temp, get_temp_type_name) \ template \ - inline std::string type_name(type>) { \ + inline std::string type_name(std::type_identity>) { \ return #std_type "<" + get_temp_type_name + ">"; \ } DBG_TEMPLATE_TYPE_NAME_1(std::vector, typename T, get_type_name()); @@ -219,7 +240,7 @@ DBG_TEMPLATE_TYPE_NAME_1(std::bitset, size_t T, std::to_string(T)); #define DBG_TEMPLATE_TYPE_NAME_2(std_type, temp1, get_temp1_type_name, temp2, get_temp2_type_name) \ template \ - inline std::string type_name(type>) { \ + inline std::string type_name(std::type_identity>) { \ return #std_type "<" + get_temp1_type_name + ", " + get_temp2_type_name + ">"; \ } DBG_TEMPLATE_TYPE_NAME_2(std::array, typename T1, get_type_name(), size_t T2, std::to_string(T2)); @@ -240,270 +261,160 @@ std::string type_list_to_string() { return result; } template -std::string type_name(type>) { +std::string type_name(std::type_identity>) { return "std::tuple<" + type_list_to_string() + ">"; } template -std::string type_name(type>) { +std::string type_name(std::type_identity>) { return "std::variant<" + type_list_to_string() + ">"; } template -inline std::string type_name(type>) { +std::string type_name(std::type_identity>) { return get_type_name(); } -template -std::string type_name(type>) { +template +std::string type_name(std::type_identity>) { return get_type_name(); } -namespace helper { -template class Op, class... Args> -struct detector { - using value_t = std::false_type; - using type = Default; -}; - -template class Op, class... Args> -struct detector>, Op, Args...> { - using value_t = std::true_type; - using type = Op; -}; -} // namespace helper - -struct nonesuch {}; - -template