diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9d9692e5..ff24ea40 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 ) diff --git a/test/main.cpp b/test/main.cpp index 6e730f40..e5b8162f 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -13,6 +13,7 @@ void run_tests() { + void test_type_info(); void test_utility(); void test_context(); void test_convert(); @@ -29,6 +30,7 @@ void run_tests() std::pair tests[] = { + { "test_type_info", test_type_info }, {"test_utility", test_utility}, {"test_context", test_context}, {"test_convert", test_convert}, diff --git a/test/test.hpp b/test/test.hpp index 84ae1555..d217a67c 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -20,6 +20,7 @@ #include "v8pp/context.hpp" #include "v8pp/convert.hpp" #include "v8pp/utility.hpp" +#include "v8pp/type_info.hpp" template std::ostream& print_sequence(std::ostream& os, Sequence const& sequence, char const brackets[2]); diff --git a/test/test.vcxproj b/test/test.vcxproj index f248adc7..d643e286 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -42,6 +42,7 @@ + diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters index bf69ab2c..18174a90 100644 --- a/test/test.vcxproj.filters +++ b/test/test.vcxproj.filters @@ -15,6 +15,7 @@ + diff --git a/test/test_call_from_v8.cpp b/test/test_call_from_v8.cpp index fe92c8e7..369340ee 100644 --- a/test/test_call_from_v8.cpp +++ b/test/test_call_from_v8.cpp @@ -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" diff --git a/test/test_type_info.cpp b/test/test_type_info.cpp new file mode 100644 index 00000000..66673790 --- /dev/null +++ b/test/test_type_info.cpp @@ -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().name(), "int"); + check_eq("type_id", type_id().name(), "bool"); + check_eq("type_id", type_id().name(), "some_struct"); + check_eq("type_id", type_id().name(), "test::some_class"); + check_eq("type_id", type_id().name(), "test::some_class"); +} diff --git a/test/test_utility.cpp b/test/test_utility.cpp index b9f34ca7..26721639 100644 --- a/test/test_utility.cpp +++ b/test/test_utility.cpp @@ -245,20 +245,10 @@ void test_type_traits() static_assert(v8pp::detail::is_shared_ptr>::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().name(), "int"); - check_eq("type_id", type_id().name(), "bool"); - check_eq("type_id", type_id().name(), "some_struct"); - check_eq("type_id", type_id().name(), "test::some_class"); } diff --git a/v8pp/CMakeLists.txt b/v8pp/CMakeLists.txt index 074627bc..f1dc25b6 100644 --- a/v8pp/CMakeLists.txt +++ b/v8pp/CMakeLists.txt @@ -33,6 +33,7 @@ set(V8PP_HEADERS property.hpp ptr_traits.hpp throw_ex.hpp + type_info.hpp utility.hpp version.hpp ) diff --git a/v8pp/class.hpp b/v8pp/class.hpp index 573cc851..3e111bac 100644 --- a/v8pp/class.hpp +++ b/v8pp/class.hpp @@ -11,6 +11,7 @@ #include "v8pp/function.hpp" #include "v8pp/property.hpp" #include "v8pp/ptr_traits.hpp" +#include "v8pp/type_info.hpp" namespace v8pp { diff --git a/v8pp/type_info.hpp b/v8pp/type_info.hpp new file mode 100644 index 00000000..ec66a4c8 --- /dev/null +++ b/v8pp/type_info.hpp @@ -0,0 +1,68 @@ +#ifndef V8PP_TYPE_INFO_HPP_INCLUDED +#define V8PP_TYPE_INFO_HPP_INCLUDED + +#include + +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 + 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 +constexpr type_info type_id() +{ +#if defined(_MSC_VER) && !defined(__clang__) + std::string_view name = __FUNCSIG__; + const std::initializer_list all_prefixes{ "type_id<", "struct ", "class " }; + const std::initializer_list any_suffixes{ ">" }; +#elif defined(__clang__) || defined(__GNUC__) + std::string_view name = __PRETTY_FUNCTION__; + const std::initializer_list all_prefixes{ "T = " }; + const std::initializer_list 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 diff --git a/v8pp/utility.hpp b/v8pp/utility.hpp index c1d2f4a8..7471d95c 100644 --- a/v8pp/utility.hpp +++ b/v8pp/utility.hpp @@ -332,63 +332,6 @@ template using is_callable = std::integral_constant::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 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 -constexpr type_info type_id() -{ -#if defined(_MSC_VER) && !defined(__clang__) - std::string_view name = __FUNCSIG__; - const std::initializer_list all_prefixes{ "type_id<", "struct ", "class " }; - const std::initializer_list any_suffixes{ ">" }; -#elif defined(__clang__) || defined(__GNUC__) - std::string_view name = __PRETTY_FUNCTION__; - const std::initializer_list all_prefixes{ "T = " }; - const std::initializer_list 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 diff --git a/v8pp/v8pp.vcxproj b/v8pp/v8pp.vcxproj index 4e71fb54..2b597cf9 100644 --- a/v8pp/v8pp.vcxproj +++ b/v8pp/v8pp.vcxproj @@ -50,6 +50,7 @@ + diff --git a/v8pp/v8pp.vcxproj.filters b/v8pp/v8pp.vcxproj.filters index 5e913caf..c981d547 100644 --- a/v8pp/v8pp.vcxproj.filters +++ b/v8pp/v8pp.vcxproj.filters @@ -13,6 +13,7 @@ +