implemented Reflection::type_name()
This commit is contained in:
63
Core/include/Core/Reflection.hpp
Normal file
63
Core/include/Core/Reflection.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
// This next line assumes C++17; otherwise, replace it with
|
||||
// your own string view implementation
|
||||
#include <string_view>
|
||||
#if __cplusplus >= 202002L
|
||||
# include <source_location>
|
||||
#endif
|
||||
|
||||
namespace Reflection
|
||||
{
|
||||
template <typename T> constexpr std::string_view type_name();
|
||||
|
||||
template <>
|
||||
constexpr std::string_view type_name<void>()
|
||||
{ return "void"; }
|
||||
|
||||
namespace detail {
|
||||
|
||||
using type_name_prober = void;
|
||||
|
||||
template <typename T>
|
||||
constexpr std::string_view wrapped_type_name()
|
||||
{
|
||||
#if __cplusplus >= 202002L
|
||||
return std::source_location::current().function_name();
|
||||
#else
|
||||
# if defined(__clang__) || defined(__GNUC__)
|
||||
return __PRETTY_FUNCTION__;
|
||||
# elif defined(_MSC_VER)
|
||||
return __FUNCSIG__;
|
||||
# else
|
||||
# error "Unsupported compiler"
|
||||
# endif
|
||||
#endif // __cplusplus >= 202002L
|
||||
}
|
||||
|
||||
constexpr std::size_t wrapped_type_name_prefix_length()
|
||||
{
|
||||
return wrapped_type_name<type_name_prober>()
|
||||
.find(type_name<type_name_prober>());
|
||||
}
|
||||
|
||||
constexpr std::size_t wrapped_type_name_suffix_length()
|
||||
{
|
||||
return wrapped_type_name<type_name_prober>().length()
|
||||
- wrapped_type_name_prefix_length()
|
||||
- type_name<type_name_prober>().length();
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
constexpr std::string_view type_name()
|
||||
{
|
||||
constexpr auto wrapped_name = detail::wrapped_type_name<T>();
|
||||
constexpr auto prefix_length = detail::wrapped_type_name_prefix_length();
|
||||
constexpr auto suffix_length = detail::wrapped_type_name_suffix_length();
|
||||
constexpr auto type_name_length =
|
||||
wrapped_name.length() - prefix_length - suffix_length;
|
||||
return wrapped_name.substr(prefix_length, type_name_length);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user