sandbox
Loading...
Searching...
No Matches
type_name.hpp
1#ifndef LIBSBX_UTILITY_TYPE_NAME_HPP_
2#define LIBSBX_UTILITY_TYPE_NAME_HPP_
3
4#include <string_view>
5
6#include <libsbx/utility/target.hpp>
7
8namespace sbx::utility {
9
10namespace detail {
11
12constexpr auto parse_type_name(std::string_view prefix, std::string_view suffix, std::string_view function) -> std::string_view {
13 const auto start = function.find(prefix) + prefix.size();
14 const auto end = function.find(suffix);
15 const auto size = end - start;
16
17 return function.substr(start, size);
18}
19
20} // namespace detail
21
22template<typename Type>
23constexpr auto type_name() -> std::string_view {
24#if defined(__clang__)
25 constexpr auto prefix = std::string_view{"[Type = "};
26 constexpr auto suffix = "]";
27 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
28
29 return detail::parse_type_name(prefix, suffix, function);
30#elif (defined(__GNUC__) || defined(__GNUG__) || defined(__MINGW32__))
31 constexpr auto prefix = std::string_view{"with Type = "};
32 constexpr auto suffix = "; ";
33 constexpr auto function = std::string_view{__PRETTY_FUNCTION__};
34
35 return detail::parse_type_name(prefix, suffix, function);
36#elif defined(__MSC_VER)
37 constexpr auto prefix = std::string_view{"type_name<"};
38 constexpr auto suffix = ">(void)";
39 constexpr auto function = std::string_view{__FUNCSIG__};
40
41 return detail::parse_type_name(prefix, suffix, function);
42#else
43 return typeid(Type).name();
44#endif
45}
46
47} // namespace sbx::utility
48
49#endif // LIBSBX_UTILITY_TYPE_NAME_HPP_