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