Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract v8pp::detail::type_info into a header file #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions test/test_call_from_v8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "v8pp/context.hpp"
#include "v8pp/function.hpp"
#include "v8pp/ptr_traits.hpp"
#include "v8pp/throw_ex.hpp"

#include "test.hpp"

Expand Down
17 changes: 17 additions & 0 deletions test/test_type_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "v8pp/type_info.hpp"
#include "test.hpp"

struct some_struct {};
namespace test { class some_class {}; }
namespace { using other_class = test::some_class; }

void test_type_info()
{
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");
check_eq("type_id", type_id<other_class>().name(), "test::some_class");
}
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__;
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_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
Loading