wip improve
This commit is contained in:
parent
e13edf41bb
commit
d3529f8cd0
4 changed files with 80 additions and 55 deletions
|
@ -63,6 +63,9 @@ nameof::nameof_enum(c) -> "RED"
|
|||
|
||||
constexpr auto cx_name = NAMEOF_ENUM(c);
|
||||
static_assert("RED" == cx_name);
|
||||
|
||||
constexpr auto cx_color = Color::RED;
|
||||
nameof::nameof_enum<cx_color>() -> "RED"
|
||||
```
|
||||
|
||||
* Name of type
|
||||
|
@ -107,6 +110,8 @@ NAMEOF_RAW(std::string) -> "std::string"
|
|||
auto c = Color::RED;
|
||||
NAMEOF_ENUM(c) -> "(Color)0"
|
||||
nameof::nameof_enum(c) -> "(Color)0"
|
||||
|
||||
NAMEOF(Color::RED) -> "RED"
|
||||
```
|
||||
|
||||
* Spaces and Tabs ignored
|
||||
|
|
|
@ -92,6 +92,8 @@ int main() {
|
|||
auto color = Color::RED;
|
||||
std::cout << NAMEOF_ENUM(color) << std::endl; // RED
|
||||
std::cout << nameof::nameof_enum(color) << std::endl; // RED
|
||||
constexpr auto cx_color = Color::RED;
|
||||
std::cout << nameof::nameof_enum<cx_color>() << std::endl; // RED
|
||||
|
||||
// Variable name.
|
||||
std::cout << NAMEOF(structvar) << std::endl; // structvar
|
||||
|
|
|
@ -121,17 +121,45 @@ struct identity final {
|
|||
return name;
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr int nameof_enum_impl_() noexcept {
|
||||
template <typename T>
|
||||
[[nodiscard]] constexpr std::string_view nameof_type_impl() noexcept {
|
||||
#if defined(__clang__)
|
||||
return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [E = ") - sizeof("]") + 1;
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">]") - 1;
|
||||
#elif defined(__GNUC__)
|
||||
return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with E = ") - sizeof("]") + 1;
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">; std::string_view = std::basic_string_view<char>]") - 1;
|
||||
#elif defined(_MSC_VER)
|
||||
return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::nameof_enum_impl_<") - sizeof(">(void) noexcept") + 1;
|
||||
constexpr auto str = __FUNCSIG__;
|
||||
constexpr auto size = sizeof(__FUNCSIG__) - 1;
|
||||
constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_type_impl<struct nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">>(void) noexcept") - 1;
|
||||
#else
|
||||
return 0;
|
||||
constexpr auto str = "nameof_type::unsupported_compiler";
|
||||
constexpr auto size = sizeof("nameof_type::unsupported_compiler") - 1;
|
||||
constexpr auto prefix = 0;
|
||||
constexpr auto suffix = 0;
|
||||
#endif
|
||||
|
||||
std::string_view name{str + prefix, size - prefix - suffix};
|
||||
if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') {
|
||||
name.remove_prefix(sizeof("enum"));
|
||||
}
|
||||
if (name.size() > sizeof("class") && name[0] == 'c' && name[1] == 'l' && name[2] == 'a' && name[3] == 's' && name[4] == 's' && name[5] == ' ') {
|
||||
name.remove_prefix(sizeof("class"));
|
||||
}
|
||||
if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') {
|
||||
name.remove_prefix(sizeof("struct"));
|
||||
}
|
||||
while (name.back() == ' ') {
|
||||
name.remove_suffix(1);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
template <typename E, E V>
|
||||
|
@ -139,21 +167,19 @@ template <typename E, E V>
|
|||
#if defined(__clang__)
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_enum_impl() [E = ") + nameof_enum_impl_<E>() + sizeof("; V = ") - 2;
|
||||
constexpr auto suffix = sizeof("]") - 1;
|
||||
return detail::pretty_name({str + prefix, size - prefix - suffix}, false);
|
||||
return detail::pretty_name({str, size - suffix}, false);
|
||||
#elif defined(__GNUC__)
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_enum_impl_<E>() + sizeof("; V = ");
|
||||
constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_type_impl<identity<E>>().length() + sizeof("; V = ");
|
||||
constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view<char>]") - 1;
|
||||
return {str + prefix, size - prefix - suffix};
|
||||
#elif defined(_MSC_VER)
|
||||
constexpr auto str = __FUNCSIG__;
|
||||
constexpr auto size = sizeof(__FUNCSIG__) - 1;
|
||||
constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_enum_impl<") + nameof_enum_impl_<E>();
|
||||
constexpr auto suffix = sizeof(">(void) noexcept") - 1;
|
||||
return detail::pretty_name({str + prefix, size - prefix - suffix}, false);
|
||||
return detail::pretty_name({str, size - suffix}, false);
|
||||
#else
|
||||
return {"nameof_enum::unsupported_compiler"};
|
||||
#endif
|
||||
|
@ -196,50 +222,6 @@ struct nameof_enum_t<E, NAMEOF_ENUM_MAX_SEARCH_DEPTH> final {
|
|||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] constexpr std::string_view nameof_type_impl_(std::string_view name) noexcept {
|
||||
#if defined(_MSC_VER)
|
||||
if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') {
|
||||
name.remove_prefix(sizeof("enum"));
|
||||
}
|
||||
if (name.size() > sizeof("class") && name[0] == 'c' && name[1] == 'l' && name[2] == 'a' && name[3] == 's' && name[4] == 's' && name[5] == ' ') {
|
||||
name.remove_prefix(sizeof("class"));
|
||||
}
|
||||
if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') {
|
||||
name.remove_prefix(sizeof("struct"));
|
||||
}
|
||||
#endif
|
||||
while (name.back() == ' ') {
|
||||
name.remove_suffix(1);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] constexpr std::string_view nameof_type_impl() noexcept {
|
||||
#if defined(__clang__)
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">]") - 1;
|
||||
return nameof_type_impl_({str + prefix, size - prefix - suffix});
|
||||
#elif defined(__GNUC__)
|
||||
constexpr auto str = __PRETTY_FUNCTION__;
|
||||
constexpr auto size = sizeof(__PRETTY_FUNCTION__) - 1;
|
||||
constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">; std::string_view = std::basic_string_view<char>]") - 1;
|
||||
return nameof_type_impl_({str + prefix, size - prefix - suffix});
|
||||
#elif defined(_MSC_VER)
|
||||
constexpr auto str = __FUNCSIG__;
|
||||
constexpr auto size = sizeof(__FUNCSIG__) - 1;
|
||||
constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_type_impl<struct nameof::detail::identity<") - 1;
|
||||
constexpr auto suffix = sizeof(">>(void) noexcept") - 1;
|
||||
return nameof_type_impl_({str + prefix, size - prefix - suffix});
|
||||
#else
|
||||
return {"nameof_type::unsupported_compiler"};
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename T, typename = std::enable_if_t<!std::is_reference_v<T>>>
|
||||
[[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept {
|
||||
return detail::pretty_name(name, with_suffix);
|
||||
|
@ -251,6 +233,7 @@ template <typename T, typename = std::enable_if_t<!std::is_reference_v<T>>>
|
|||
|
||||
} // namespace detail
|
||||
|
||||
// nameof_enum used to obtain the simple (unqualified) string name of enum variable.
|
||||
template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
|
||||
[[nodiscard]] constexpr std::string_view nameof_enum(T value) noexcept {
|
||||
constexpr bool s = std::is_signed_v<std::underlying_type_t<std::decay_t<T>>>;
|
||||
|
@ -258,6 +241,13 @@ template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>
|
|||
return detail::nameof_enum_t<std::decay_t<T>, min>{}(static_cast<int>(value));
|
||||
}
|
||||
|
||||
// nameof_enum used to obtain the simple (unqualified) string name of static storage enum variable.
|
||||
template <auto V, typename = std::enable_if_t<std::is_enum_v<std::decay_t<decltype(V)>>>>
|
||||
[[nodiscard]] constexpr std::string_view nameof_enum() noexcept {
|
||||
return detail::nameof_enum_impl<decltype(V), V>();
|
||||
}
|
||||
|
||||
// nameof_type used to obtain the string name of type.
|
||||
template <typename T>
|
||||
[[nodiscard]] constexpr std::string_view nameof_type() noexcept {
|
||||
return detail::nameof_type_impl<detail::identity<T>>();
|
||||
|
|
|
@ -265,6 +265,34 @@ TEST_CASE("nameof::nameof_enum(value)") {
|
|||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("nameof::nameof_enum<value>()") {
|
||||
constexpr Color color_ = Color::BLUE;
|
||||
constexpr Color m[3] = {Color::RED, Color::GREEN, Color::BLUE};
|
||||
#if defined(__clang__) || defined(_MSC_VER)
|
||||
REQUIRE(nameof::nameof_enum<Color::RED>() == "RED");
|
||||
REQUIRE(nameof::nameof_enum<color>() == "RED");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<Color::BLUE>() == "BLUE");
|
||||
REQUIRE(nameof::nameof_enum<color_>() == "BLUE");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<m[1]>() == "GREEN");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<Directions::Right>() == "Right");
|
||||
REQUIRE(nameof::nameof_enum<directions>() == "Right");
|
||||
#elif defined(__GNUC__)
|
||||
REQUIRE(nameof::nameof_enum<Color::RED>() == "(Color)-1");
|
||||
REQUIRE(nameof::nameof_enum<color>() == "(Color)-1");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<Color::BLUE>() == "(Color)1");
|
||||
REQUIRE(nameof::nameof_enum<color_>() == "(Color)1");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<m[1]>() == "(Color)0");
|
||||
|
||||
REQUIRE(nameof::nameof_enum<Directions::Right>() == "(Directions)2");
|
||||
REQUIRE(nameof::nameof_enum<directions>() == "(Directions)2");
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("NAMEOF_TYPE") {
|
||||
#if defined(__clang__)
|
||||
REQUIRE(NAMEOF_TYPE(struct_var) == "SomeStruct");
|
||||
|
|
Loading…
Reference in a new issue