Skip to content

Commit e424dca

Browse files
authored
[reflection] fix type_string that lose const (#874)
1 parent da88211 commit e424dca

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

include/ylt/reflection/template_string.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,11 @@ constexpr std::string_view get_raw_name() {
3131
template <typename T>
3232
inline constexpr std::string_view type_string() {
3333
constexpr std::string_view sample = get_raw_name<int>();
34-
constexpr size_t pos = sample.find("int");
34+
constexpr size_t prefix_length = sample.find("int");
35+
constexpr size_t suffix_length = sample.size() - prefix_length - 3;
36+
3537
constexpr std::string_view str = get_raw_name<T>();
36-
constexpr auto next1 = str.rfind(sample[pos + 3]);
37-
#if defined(_MSC_VER)
38-
constexpr std::size_t npos = str.find_first_of(" ", pos);
39-
if constexpr (npos != std::string_view::npos)
40-
return str.substr(npos + 1, next1 - npos - 1);
41-
else
42-
return str.substr(pos, next1 - pos);
43-
#else
44-
return str.substr(pos, next1 - pos);
45-
#endif
38+
return str.substr(prefix_length, str.size() - prefix_length - suffix_length);
4639
}
4740

4841
template <auto T>

src/reflection/tests/test_reflection.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ylt/reflection/member_value.hpp"
55
#include "ylt/reflection/private_visitor.hpp"
6+
#include "ylt/reflection/template_string.hpp"
67
#include "ylt/reflection/template_switch.hpp"
78
#include "ylt/reflection/user_reflect_macro.hpp"
89

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

469+
namespace test_type_string {
470+
struct struct_test {};
471+
class class_test {};
472+
union union_test {};
473+
} // namespace test_type_string
474+
475+
TEST_CASE("test type_string") {
476+
CHECK(type_string<int>() == "int");
477+
CHECK(type_string<const int>() == "const int");
478+
CHECK(type_string<volatile int>() == "volatile int");
479+
480+
#if defined(__clang__)
481+
CHECK(type_string<int&>() == "int &");
482+
CHECK(type_string<int&&>() == "int &&");
483+
CHECK(type_string<const int&>() == "const int &");
484+
CHECK(type_string<const int&&>() == "const int &&");
485+
CHECK(type_string<volatile int&>() == "volatile int &");
486+
CHECK(type_string<volatile int&&>() == "volatile int &&");
487+
#else
488+
CHECK(type_string<int&>() == "int&");
489+
CHECK(type_string<int&&>() == "int&&");
490+
CHECK(type_string<const int&>() == "const int&");
491+
CHECK(type_string<const int&&>() == "const int&&");
492+
CHECK(type_string<volatile int&>() == "volatile int&");
493+
CHECK(type_string<volatile int&&>() == "volatile int&&");
494+
#endif
495+
496+
#if defined(_MSC_VER) && !defined(__clang__)
497+
CHECK(type_string<test_type_string::struct_test>() ==
498+
"struct test_type_string::struct_test");
499+
CHECK(type_string<const test_type_string::struct_test>() ==
500+
"const struct test_type_string::struct_test");
501+
CHECK(type_string<test_type_string::class_test>() ==
502+
"class test_type_string::class_test");
503+
CHECK(type_string<const test_type_string::class_test>() ==
504+
"const class test_type_string::class_test");
505+
CHECK(type_string<test_type_string::union_test>() ==
506+
"union test_type_string::union_test");
507+
CHECK(type_string<const test_type_string::union_test>() ==
508+
"const union test_type_string::union_test");
509+
#else
510+
CHECK(type_string<test_type_string::struct_test>() ==
511+
"test_type_string::struct_test");
512+
CHECK(type_string<const test_type_string::struct_test>() ==
513+
"const test_type_string::struct_test");
514+
CHECK(type_string<test_type_string::class_test>() ==
515+
"test_type_string::class_test");
516+
CHECK(type_string<const test_type_string::class_test>() ==
517+
"const test_type_string::class_test");
518+
CHECK(type_string<test_type_string::union_test>() ==
519+
"test_type_string::union_test");
520+
CHECK(type_string<const test_type_string::union_test>() ==
521+
"const test_type_string::union_test");
522+
#endif
523+
}
524+
468525
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
469526
int main(int argc, char** argv) { return doctest::Context(argc, argv).run(); }
470527
DOCTEST_MSVC_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)