Skip to content

Commit

Permalink
[reflection] fix type_string that lose const (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
miyanyan authored Jan 9, 2025
1 parent da88211 commit e424dca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
15 changes: 4 additions & 11 deletions include/ylt/reflection/template_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ constexpr std::string_view get_raw_name() {
template <typename T>
inline constexpr std::string_view type_string() {
constexpr std::string_view sample = get_raw_name<int>();
constexpr size_t pos = sample.find("int");
constexpr size_t prefix_length = sample.find("int");
constexpr size_t suffix_length = sample.size() - prefix_length - 3;

constexpr std::string_view str = get_raw_name<T>();
constexpr auto next1 = str.rfind(sample[pos + 3]);
#if defined(_MSC_VER)
constexpr std::size_t npos = str.find_first_of(" ", pos);
if constexpr (npos != std::string_view::npos)
return str.substr(npos + 1, next1 - npos - 1);
else
return str.substr(pos, next1 - pos);
#else
return str.substr(pos, next1 - pos);
#endif
return str.substr(prefix_length, str.size() - prefix_length - suffix_length);
}

template <auto T>
Expand Down
57 changes: 57 additions & 0 deletions src/reflection/tests/test_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ylt/reflection/member_value.hpp"
#include "ylt/reflection/private_visitor.hpp"
#include "ylt/reflection/template_string.hpp"
#include "ylt/reflection/template_switch.hpp"
#include "ylt/reflection/user_reflect_macro.hpp"

Expand Down Expand Up @@ -465,6 +466,62 @@ TEST_CASE("test visit private") {
auto name = bank.*(std::get<1>(tp)); // ok
}

namespace test_type_string {
struct struct_test {};
class class_test {};
union union_test {};
} // namespace test_type_string

TEST_CASE("test type_string") {
CHECK(type_string<int>() == "int");
CHECK(type_string<const int>() == "const int");
CHECK(type_string<volatile int>() == "volatile int");

#if defined(__clang__)
CHECK(type_string<int&>() == "int &");
CHECK(type_string<int&&>() == "int &&");
CHECK(type_string<const int&>() == "const int &");
CHECK(type_string<const int&&>() == "const int &&");
CHECK(type_string<volatile int&>() == "volatile int &");
CHECK(type_string<volatile int&&>() == "volatile int &&");
#else
CHECK(type_string<int&>() == "int&");
CHECK(type_string<int&&>() == "int&&");
CHECK(type_string<const int&>() == "const int&");
CHECK(type_string<const int&&>() == "const int&&");
CHECK(type_string<volatile int&>() == "volatile int&");
CHECK(type_string<volatile int&&>() == "volatile int&&");
#endif

#if defined(_MSC_VER) && !defined(__clang__)
CHECK(type_string<test_type_string::struct_test>() ==
"struct test_type_string::struct_test");
CHECK(type_string<const test_type_string::struct_test>() ==
"const struct test_type_string::struct_test");
CHECK(type_string<test_type_string::class_test>() ==
"class test_type_string::class_test");
CHECK(type_string<const test_type_string::class_test>() ==
"const class test_type_string::class_test");
CHECK(type_string<test_type_string::union_test>() ==
"union test_type_string::union_test");
CHECK(type_string<const test_type_string::union_test>() ==
"const union test_type_string::union_test");
#else
CHECK(type_string<test_type_string::struct_test>() ==
"test_type_string::struct_test");
CHECK(type_string<const test_type_string::struct_test>() ==
"const test_type_string::struct_test");
CHECK(type_string<test_type_string::class_test>() ==
"test_type_string::class_test");
CHECK(type_string<const test_type_string::class_test>() ==
"const test_type_string::class_test");
CHECK(type_string<test_type_string::union_test>() ==
"test_type_string::union_test");
CHECK(type_string<const test_type_string::union_test>() ==
"const test_type_string::union_test");
#endif
}

DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
int main(int argc, char** argv) { return doctest::Context(argc, argv).run(); }
DOCTEST_MSVC_SUPPRESS_WARNING_POP

0 comments on commit e424dca

Please sign in to comment.