diff --git a/iguana/enum_reflection.hpp b/iguana/field_reflection.hpp similarity index 96% rename from iguana/enum_reflection.hpp rename to iguana/field_reflection.hpp index 9b871f8c..dc159b8c 100644 --- a/iguana/enum_reflection.hpp +++ b/iguana/field_reflection.hpp @@ -59,6 +59,12 @@ inline constexpr std::string_view enum_string() { return s1; } +template +inline constexpr std::string_view field_string() { + constexpr std::string_view raw_name = enum_string(); + return raw_name.substr(raw_name.rfind(":") + 1); +} + #if defined(__clang__) && (__clang_major__ >= 17) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wenum-constexpr-conversion" diff --git a/iguana/reflection.hpp b/iguana/reflection.hpp index 6bad499c..474e27fa 100644 --- a/iguana/reflection.hpp +++ b/iguana/reflection.hpp @@ -21,7 +21,7 @@ #include "detail/string_stream.hpp" #include "detail/traits.hpp" -#include "enum_reflection.hpp" +#include "field_reflection.hpp" #include "frozen/string.h" #include "frozen/unordered_map.h" diff --git a/iguana/util.hpp b/iguana/util.hpp index 2d89e74d..1f08ed52 100644 --- a/iguana/util.hpp +++ b/iguana/util.hpp @@ -14,8 +14,8 @@ #include "define.h" #include "detail/charconv.h" #include "detail/utf.hpp" -#include "enum_reflection.hpp" #include "error_code.h" +#include "field_reflection.hpp" #include "reflection.hpp" namespace iguana { diff --git a/test/test_util.cpp b/test/test_util.cpp index 4ceba28d..fc874b9a 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -1,20 +1,20 @@ -#include +#include #include #define DOCTEST_CONFIG_IMPLEMENT #include "doctest.h" -#if defined(__clang__) || defined(_MSC_VER) || \ +#if defined(__clang__) || defined(_MSC_VER) || \ (defined(__GNUC__) && __GNUC__ > 8) class message {}; namespace ns { class message {}; -} // namespace ns +} // namespace ns namespace ns::ns2 { class message {}; -} // namespace ns::ns2 +} // namespace ns::ns2 TEST_CASE("test type string") { static_assert(iguana::type_string() == "message"); @@ -41,12 +41,12 @@ enum Size { small, large }; namespace ns { enum class Color { red, black }; enum Size { small, large }; -} // namespace ns +} // namespace ns namespace ns::ns2 { enum class Color { red, black }; enum Size { small, large }; -} // namespace ns::ns2 +} // namespace ns::ns2 TEST_CASE("test enum string") { static_assert(iguana::enum_string() == "Color::red"); @@ -83,6 +83,28 @@ TEST_CASE("test enum string") { std::cout << s5 << "\n"; CHECK(s5 == "ns::ns2::large"); } + +struct sub { + int id; +}; + +struct person { + ns::ns2::Color color; + int id; + sub s; + std::string str; +}; + +TEST_CASE("test field string") { + constexpr auto field_name1 = iguana::field_string<&person::color>(); + constexpr auto field_name2 = iguana::field_string<&person::id>(); + constexpr auto field_name3 = iguana::field_string<&person::s>(); + constexpr auto field_name4 = iguana::field_string<&person::str>(); + CHECK(field_name1 == "color"); + CHECK(field_name2 == "id"); + CHECK(field_name3 == "s"); + CHECK(field_name4 == "str"); +} #endif DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)