Skip to content

Commit

Permalink
fit for msvc
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jan 15, 2025
1 parent 1f38419 commit 4239a59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 12 additions & 9 deletions iguana/ylt/reflection/template_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ 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 std::string_view str = get_raw_name<T>();
constexpr auto next1 = str.rfind(sample[pos + 3]);
constexpr size_t suffix_length = sample.size() - prefix_length - 3;
constexpr auto name =
str.substr(prefix_length, str.size() - prefix_length - suffix_length);
#if defined(_MSC_VER)
constexpr std::size_t npos = str.find_first_of(" ", pos);
if (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);
constexpr size_t space_pos = name.find(" ");
if constexpr (space_pos != std::string_view::npos) {
constexpr auto prefix = name.substr(0, space_pos);
if constexpr (prefix != "const" && prefix != "volatile") {
return name.substr(space_pos + 1);
}
}
#endif
return name;
}

template <auto T>
Expand Down
6 changes: 3 additions & 3 deletions test/test_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,15 @@ TEST_CASE("test type_string") {
#endif
#if defined(_MSC_VER) && !defined(__clang__)
CHECK(type_string<test_type_string::struct_test>() ==
"struct test_type_string::struct_test");
"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");
"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");
"test_type_string::union_test");
CHECK(type_string<const test_type_string::union_test>() ==
"const union test_type_string::union_test");
#else
Expand Down

0 comments on commit 4239a59

Please sign in to comment.