-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract
v8pp::detail::type_info
into a header file
... for better modularization.
- Loading branch information
Showing
14 changed files
with
96 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ set(V8PP_HEADERS | |
property.hpp | ||
ptr_traits.hpp | ||
throw_ex.hpp | ||
type_info.hpp | ||
utility.hpp | ||
version.hpp | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters