1 #ifndef OOKII_TYPE_INFO_H_
4 #define OOKII_TYPE_INFO_H_
33 using name_ptr = std::unique_ptr<char, malloc_deleter<char>>;
37 inline static std::string_view remove_prefix(std::string_view type_name, std::string_view prefix)
39 if (type_name.starts_with(prefix))
40 return type_name.substr(prefix.length());
45 inline std::string_view make_short(std::string_view type_name)
49 auto index = type_name.find_last_of(
':', type_name.find_first_of(
'<'));
50 if (index == std::string_view::npos)
52 type_name = remove_prefix(type_name,
"class ");
53 type_name = remove_prefix(type_name,
"struct ");
57 type_name = type_name.substr(index + 1);
63 template<
typename CharType,
typename Traits,
typename Alloc>
64 inline auto get_type_name(
const std::type_info &type,
bool short_name)
66 std::string_view type_name = type.name();
69 name_ptr demangled_name{abi::__cxa_demangle(type_name.data(),
nullptr,
nullptr, &status)};
71 type_name = demangled_name.get();
74 type_name = make_short(type_name);
89 template<
typename CharType =
char,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
92 return details::get_type_name<CharType, Traits, Alloc>(type,
false);
104 template<
typename CharType =
char,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
107 return details::get_type_name<CharType, Traits, Alloc>(type,
true);
118 template<
typename T,
typename CharType =
char,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
121 return get_type_name<CharType, Traits, Alloc>(
typeid(T));
133 template<
typename T,
typename CharType =
char,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
136 return get_short_type_name<CharType, Traits, Alloc>(
typeid(T));
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
auto get_type_name(const std::type_info &type)
Gets the name of a type.
Definition: type_info.h:90
auto get_short_type_name(const std::type_info &type)
Gets the name of a type excluding the namespace name.
Definition: type_info.h:105
Provides helper types and functions for working with strings.
static std::basic_string< CharType, Traits, Alloc > from_bytes(std::string_view value, const std::locale &loc={})
Converts a string to the specified character type.
Definition: string_helper.h:171