Skip to content

Commit

Permalink
simplify type_id()
Browse files Browse the repository at this point in the history
use `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 8abf966 commit b67e0fb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 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
}
84 changes: 59 additions & 25 deletions v8pp/utility.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef V8PP_UTILITY_HPP_INCLUDED
#define V8PP_UTILITY_HPP_INCLUDED

#include <algorithm>
#include <functional>
#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -74,12 +75,43 @@ class basic_string_view
return ptr ? ptr - data_ : npos;
}

size_t find(basic_string_view str) const
{
for (const_iterator it = begin(); it != end(); ++it)
{
const size_t rest = end() - it;
if (str.size() > rest) break;
if (Traits::compare(it, str.data(), str.size()) == 0)
{
return it - begin();
}
}
return npos;
}

size_t rfind(basic_string_view str) const
{
if (size_ >= str.size_)
{
const auto p = std::find_end(begin(), end(), str.begin(), str.end());
if (p != end())
{
return p - begin();
}

}
return npos;
}

basic_string_view substr(size_t pos = 0, size_t count = npos) const
{
if (pos > size_) throw std::out_of_range("pos > size");
return basic_string_view(data_ + pos, std::min(count, size_ - pos));
}

void remove_prefix(size_t n) { data_ += n; size_ -=n;}
void remove_suffix(size_t n) { size_ -= n; }

operator std::basic_string<Char, Traits>() const
{
return std::basic_string<Char, Traits>(data_, size_);
Expand Down Expand Up @@ -489,8 +521,8 @@ class type_info
template<typename T>
friend type_info type_id();

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

Expand All @@ -503,34 +535,36 @@ template<typename T>
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)"
string_view name = __FUNCSIG__;
const std::initializer_list<string_view> all_prefixes{ "type_id<", "struct ", "class " };
const std::initializer_list<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 "]"
string_view name = __PRETTY_FUNCTION__;
const std::initializer_list<string_view> all_prefixes{ "T = " };
const std::initializer_list<string_view> any_suffixes{ ";", "]" };
#else
#error "Unknown compiler"
#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 b67e0fb

Please sign in to comment.