Skip to content

Commit

Permalink
extract v8pp::detail::type_info into a header file
Browse files Browse the repository at this point in the history
... for better modularization.
  • Loading branch information
pmed committed Feb 17, 2024
1 parent 388ce0e commit dbb00f9
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 67 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_executable(v8pp_test
test_property.cpp
test_ptr_traits.cpp
test_throw_ex.cpp
test_type_info.cpp
test_utility.cpp
)

Expand Down
2 changes: 2 additions & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

void run_tests()
{
void test_type_info();
void test_utility();
void test_context();
void test_convert();
Expand All @@ -29,6 +30,7 @@ void run_tests()

std::pair<char const*, void (*)()> tests[] =
{
{ "test_type_info", test_type_info },
{"test_utility", test_utility},
{"test_context", test_context},
{"test_convert", test_convert},
Expand Down
1 change: 1 addition & 0 deletions test/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "v8pp/context.hpp"
#include "v8pp/convert.hpp"
#include "v8pp/utility.hpp"
#include "v8pp/type_info.hpp"

template<typename Sequence>
std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2]);
Expand Down
1 change: 1 addition & 0 deletions test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<ClCompile Include="test_property.cpp" />
<ClCompile Include="test_ptr_traits.cpp" />
<ClCompile Include="test_throw_ex.cpp" />
<ClCompile Include="test_type_info.cpp" />
<ClCompile Include="test_utility.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions test/test.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ClCompile Include="test_json.cpp" />
<ClCompile Include="test_utility.cpp" />
<ClCompile Include="test_ptr_traits.cpp" />
<ClCompile Include="test_type_info.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="test.hpp" />
Expand Down
10 changes: 0 additions & 10 deletions test/test_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,10 @@ void test_type_traits()
static_assert(v8pp::detail::is_shared_ptr<std::shared_ptr<int>>::value, "int");
}

struct some_struct {};
namespace test { class some_class {}; }

void test_utility()
{
test_type_traits();
test_function_traits();
test_tuple_tail();
test_is_callable();

using v8pp::detail::type_id;

check_eq("type_id", type_id<int>().name(), "int");
check_eq("type_id", type_id<bool>().name(), "bool");
check_eq("type_id", type_id<some_struct>().name(), "some_struct");
check_eq("type_id", type_id<test::some_class>().name(), "test::some_class");
}
1 change: 1 addition & 0 deletions v8pp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(V8PP_HEADERS
property.hpp
ptr_traits.hpp
throw_ex.hpp
type_info.hpp
utility.hpp
version.hpp
)
Expand Down
1 change: 1 addition & 0 deletions v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "v8pp/function.hpp"
#include "v8pp/property.hpp"
#include "v8pp/ptr_traits.hpp"
#include "v8pp/type_info.hpp"

namespace v8pp {

Expand Down
68 changes: 68 additions & 0 deletions v8pp/type_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef V8PP_TYPE_INFO_HPP_INCLUDED
#define V8PP_TYPE_INFO_HPP_INCLUDED

#include <string_view>

namespace v8pp::detail {

/// Type information for custom RTTI
class type_info
{
public:
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>
constexpr friend type_info type_id();

constexpr explicit type_info(std::string_view name)
: name_(name)
{
}

std::string_view name_;
};

/// Get type information for type T
/// The idea is borrowed from https://github.com/Manu343726/ctti
template<typename T>
constexpr type_info type_id()
{
#if defined(_MSC_VER) && !defined(__clang__)
std::string_view name = __FUNCSIG__;
constexpr std::initializer_list<std::string_view> all_prefixes{ "type_id<", "struct ", "class " };
constexpr std::initializer_list<std::string_view> any_suffixes{ ">" };
#elif defined(__clang__) || defined(__GNUC__)
std::string_view name = __PRETTY_FUNCTION__;
constexpr std::initializer_list<std::string_view> all_prefixes{ "T = " };
constexpr 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());
}
}

for (auto&& suffix : any_suffixes)
{
const auto p = name.rfind(suffix);
if (p != name.npos)
{
name.remove_suffix(name.size() - p);
break;
}
}

return type_info(name);
}

} // namespace v8pp::detail

#endif // V8PP_TYPE_INFO_HPP_INCLUDED
57 changes: 0 additions & 57 deletions v8pp/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,63 +332,6 @@ template<typename F>
using is_callable = std::integral_constant<bool,
is_callable_impl<F, std::is_class<F>::value>::value>;

/// Type information for custom RTTI
class type_info
{
public:
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> constexpr friend type_info type_id();

constexpr explicit type_info(std::string_view name)
: name_(name)
{
}

std::string_view name_;
};

/// Get type information for type T
/// The idea is borrowed from https://github.com/Manu343726/ctti
template<typename T>
constexpr type_info type_id()
{
#if defined(_MSC_VER) && !defined(__clang__)
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__)
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());
}
}

for (auto&& suffix : any_suffixes)
{
const auto p = name.rfind(suffix);
if (p != name.npos)
{
name.remove_suffix(name.size() - p);
break;
}
}

return type_info(name);
}

}} // namespace v8pp::detail

#endif // V8PP_UTILITY_HPP_INCLUDED
1 change: 1 addition & 0 deletions v8pp/v8pp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<ClInclude Include="property.hpp" />
<ClInclude Include="ptr_traits.hpp" />
<ClInclude Include="throw_ex.hpp" />
<ClInclude Include="type_info.hpp" />
<ClInclude Include="utility.hpp" />
<ClInclude Include="version.hpp" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions v8pp/v8pp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ClInclude Include="config.hpp" />
<ClInclude Include="module.hpp" />
<ClInclude Include="throw_ex.hpp" />
<ClInclude Include="type_info.hpp" />
<ClInclude Include="class.hpp" />
<ClInclude Include="call_from_v8.hpp" />
<ClInclude Include="call_v8.hpp" />
Expand Down

0 comments on commit dbb00f9

Please sign in to comment.