Skip to content

Commit

Permalink
simplify type_id(), make it constexpr
Browse files Browse the repository at this point in the history
use `std::string_view` methods to remove prefixes and suffixes from the `__PRETTY_FUNCTION__`
and remove class/struct from the type name on Windows
  • Loading branch information
pmed committed Jan 2, 2022
1 parent 2f67770 commit 52f6406
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
5 changes: 0 additions & 5 deletions test/test_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,6 @@ void test_utility()

check_eq("type_id", type_id<int>().name(), "int");
check_eq("type_id", type_id<bool>().name(), "bool");
#if defined(_MSC_VER) && !defined(__clang__)
check_eq("type_id", type_id<some_struct>().name(), "struct some_struct");
check_eq("type_id", type_id<test::some_class>().name(), "class test::some_class");
#else
check_eq("type_id", type_id<some_struct>().name(), "some_struct");
check_eq("type_id", type_id<test::some_class>().name(), "test::some_class");
#endif
}
62 changes: 31 additions & 31 deletions v8pp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,15 @@ using is_callable = std::integral_constant<bool,
class type_info
{
public:
std::string_view name() const { return name_; }

bool operator==(type_info const& other) const { return name_ == other.name_; }
bool operator!=(type_info const& other) const { return name_ != other.name_; }
constexpr std::string_view name() const { return name_; }
constexpr bool operator==(type_info const& other) const { return name_ == other.name_; }
constexpr bool operator!=(type_info const& other) const { return name_ != other.name_; }

private:
template<typename T>
friend type_info type_id();
template<typename T> constexpr friend type_info type_id();

type_info(char const* name, size_t size)
: name_(name, size)
constexpr explicit type_info(std::string_view name)
: name_(name)
{
}

Expand All @@ -356,37 +354,39 @@ class type_info
/// Get type information for type T
/// The idea is borrowed from https://github.com/Manu343726/ctti
template<typename T>
type_info type_id()
constexpr type_info type_id()
{
#if defined(_MSC_VER) && !defined(__clang__)
#define V8PP_PRETTY_FUNCTION __FUNCSIG__
#define V8PP_PRETTY_FUNCTION_PREFIX "class v8pp::detail::type_info __cdecl v8pp::detail::type_id<"
#define V8PP_PRETTY_FUNCTION_SUFFIX ">(void)"
std::string_view name = __FUNCSIG__;
const std::initializer_list<std::string_view> all_prefixes{ "type_id<", "struct ", "class " };
const std::initializer_list<std::string_view> any_suffixes{ ">" };
#elif defined(__clang__) || defined(__GNUC__)
#define V8PP_PRETTY_FUNCTION __PRETTY_FUNCTION__
#if !defined(__clang__)
#define V8PP_PRETTY_FUNCTION_PREFIX "v8pp::detail::type_info v8pp::detail::type_id() [with T = "
#else
#define V8PP_PRETTY_FUNCTION_PREFIX "v8pp::detail::type_info v8pp::detail::type_id() [T = "
#endif
#define V8PP_PRETTY_FUNCTION_SUFFIX "]"
std::string_view name = __PRETTY_FUNCTION__;
const std::initializer_list<std::string_view> all_prefixes{ "T = " };
const std::initializer_list<std::string_view> any_suffixes{ ";", "]" };
#else
#error "Unknown compiler"
#endif
for (auto&& prefix : all_prefixes)
{
const auto p = name.find(prefix);
if (p != name.npos)
{
name.remove_prefix(p + prefix.size());
}
}

#define V8PP_PRETTY_FUNCTION_LEN (sizeof(V8PP_PRETTY_FUNCTION) - 1)
#define V8PP_PRETTY_FUNCTION_PREFIX_LEN (sizeof(V8PP_PRETTY_FUNCTION_PREFIX) - 1)
#define V8PP_PRETTY_FUNCTION_SUFFIX_LEN (sizeof(V8PP_PRETTY_FUNCTION_SUFFIX) - 1)

return type_info(V8PP_PRETTY_FUNCTION + V8PP_PRETTY_FUNCTION_PREFIX_LEN,
V8PP_PRETTY_FUNCTION_LEN - V8PP_PRETTY_FUNCTION_PREFIX_LEN - V8PP_PRETTY_FUNCTION_SUFFIX_LEN);
for (auto&& suffix : any_suffixes)
{
const auto p = name.rfind(suffix);
if (p != name.npos)
{
name.remove_suffix(name.size() - p);
break;
}
}

#undef V8PP_PRETTY_FUNCTION
#undef V8PP_PRETTY_FUNCTION_PREFIX
#undef V8PP_PRETTY_FUNCTION_SUFFFIX
#undef V8PP_PRETTY_FUNCTION_LEN
#undef V8PP_PRETTY_FUNCTION_PREFIX_LEN
#undef V8PP_PRETTY_FUNCTION_SUFFFIX_LEN
return type_info(name);
}

}} // namespace v8pp::detail
Expand Down

0 comments on commit 52f6406

Please sign in to comment.