meta: export also the meta_node class template (close #464)

This commit is contained in:
Michele Caini
2020-04-17 23:15:04 +02:00
parent efb125b2d5
commit ec4b264868
5 changed files with 53 additions and 52 deletions

View File

@@ -392,11 +392,11 @@ public:
auto alias(const id_type value) ENTT_NOEXCEPT {
auto * const node = internal::meta_info<Type>::resolve();
ENTT_ASSERT(!exists(value, *internal::meta_info<>::global));
ENTT_ASSERT(!exists(node, *internal::meta_info<>::global));
ENTT_ASSERT(!exists(value, *internal::meta_context::global));
ENTT_ASSERT(!exists(node, *internal::meta_context::global));
node->alias = value;
node->next = *internal::meta_info<>::global;
*internal::meta_info<>::global = node;
node->next = *internal::meta_context::global;
*internal::meta_context::global = node;
return meta_factory<Type, Type>{&node->prop};
}
@@ -779,7 +779,7 @@ public:
auto reset() ENTT_NOEXCEPT {
auto * const node = internal::meta_info<Type>::resolve();
internal::meta_info<>::detach(node);
internal::meta_context::detach(node);
const auto unregister_all = y_combinator{
[](auto &&self, auto **curr, auto... member) {
@@ -846,7 +846,7 @@ inline meta_type resolve() ENTT_NOEXCEPT {
inline meta_type resolve(const id_type alias) ENTT_NOEXCEPT {
return internal::find_if([alias](const auto *curr) {
return curr->alias == alias;
}, *internal::meta_info<>::global);
}, *internal::meta_context::global);
}
@@ -858,7 +858,7 @@ inline meta_type resolve(const id_type alias) ENTT_NOEXCEPT {
template<typename Op>
inline std::enable_if_t<std::is_invocable_v<Op, meta_type>, void>
resolve(Op op) {
internal::visit<meta_type>(op, *internal::meta_info<>::global);
internal::visit<meta_type>(op, *internal::meta_context::global);
}

View File

@@ -191,44 +191,11 @@ bool compare(const void *lhs, const void *rhs) {
}
template<typename... Type>
struct meta_node {
static_assert(std::is_same_v<Type..., std::remove_cv_t<std::remove_reference_t<Type>>...>);
inline static meta_type_node * resolve() ENTT_NOEXCEPT {
static meta_type_node node{
type_info<Type...>::id(),
{},
nullptr,
nullptr,
std::is_void_v<Type...>,
std::is_integral_v<Type...>,
std::is_floating_point_v<Type...>,
std::is_array_v<Type...>,
std::is_enum_v<Type...>,
std::is_union_v<Type...>,
std::is_class_v<Type...>,
std::is_pointer_v<Type...>,
std::is_pointer_v<Type...> && std::is_function_v<std::remove_pointer_t<Type>...>,
std::is_member_object_pointer_v<Type...>,
std::is_member_function_pointer_v<Type...>,
std::extent_v<Type...>,
&compare<Type...>, // workaround for an issue with VS2017
&meta_node<std::remove_const_t<std::remove_pointer_t<Type>>...>::resolve,
&meta_node<std::remove_const_t<std::remove_extent_t<Type>>...>::resolve
};
return &node;
}
};
template<>
struct meta_node<> {
struct ENTT_API meta_context {
inline static meta_type_node *local = nullptr;
inline static meta_type_node **global = &local;
inline static void detach(const meta_type_node *node) ENTT_NOEXCEPT {
static void detach(const meta_type_node *node) ENTT_NOEXCEPT {
auto **it = global;
while(*it && *it != node) {
@@ -242,6 +209,38 @@ struct meta_node<> {
};
template<typename Type>
struct ENTT_API meta_node {
static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>);
static meta_type_node * resolve() ENTT_NOEXCEPT {
static meta_type_node node{
type_info<Type>::id(),
{},
nullptr,
nullptr,
std::is_void_v<Type>,
std::is_integral_v<Type>,
std::is_floating_point_v<Type>,
std::is_array_v<Type>,
std::is_enum_v<Type>,
std::is_union_v<Type>,
std::is_class_v<Type>,
std::is_pointer_v<Type>,
std::is_pointer_v<Type> && std::is_function_v<std::remove_pointer_t<Type>>,
std::is_member_object_pointer_v<Type>,
std::is_member_function_pointer_v<Type>,
std::extent_v<Type>,
&compare<Type>, // workaround for an issue with VS2017
&meta_node<std::remove_const_t<std::remove_pointer_t<Type>>>::resolve,
&meta_node<std::remove_const_t<std::remove_extent_t<Type>>>::resolve
};
return &node;
}
};
template<typename... Type>
struct meta_info: meta_node<std::remove_cv_t<std::remove_reference_t<Type>>...> {};
@@ -262,11 +261,11 @@ struct meta_ctx {
* @param other A valid context to which to bind.
*/
static void bind(meta_ctx other) ENTT_NOEXCEPT {
internal::meta_info<>::global = other.ctx;
internal::meta_context::global = other.ctx;
}
private:
internal::meta_type_node **ctx{&internal::meta_info<>::local};
internal::meta_type_node **ctx{&internal::meta_context::local};
};
@@ -1487,7 +1486,7 @@ public:
/*! @brief Removes a meta object from the list of searchable types. */
void detach() ENTT_NOEXCEPT {
internal::meta_info<>::detach(node);
internal::meta_context::detach(node);
}
private:

View File

@@ -1875,7 +1875,7 @@ TEST_F(Meta, PropertiesAndCornerCases) {
}
TEST_F(Meta, Reset) {
ASSERT_NE(*entt::internal::meta_info<>::global, nullptr);
ASSERT_NE(*entt::internal::meta_context::global, nullptr);
entt::meta<char>().reset();
entt::meta<concrete_type>().reset();
@@ -1905,7 +1905,7 @@ TEST_F(Meta, Reset) {
ASSERT_FALSE(entt::resolve("another_abstract_type"_hs));
ASSERT_FALSE(entt::resolve("concrete"_hs));
ASSERT_EQ(*entt::internal::meta_info<>::global, nullptr);
ASSERT_EQ(*entt::internal::meta_context::global, nullptr);
Meta::SetUpAfterUnregistration();
entt::meta_any any{42.};

View File

@@ -8,9 +8,7 @@ position create_position(int x, int y) {
return position{x, y};
}
ENTT_API void set_up(entt::meta_ctx ctx) {
entt::meta_ctx::bind(ctx);
ENTT_API void set_up() {
entt::meta<position>()
.alias("position"_hs)
.ctor<&create_position>()

View File

@@ -4,19 +4,23 @@
#include <entt/meta/meta.hpp>
#include "types.h"
ENTT_API void set_up(entt::meta_ctx);
ENTT_API void set_up();
ENTT_API void tear_down();
ENTT_API entt::meta_any wrap_int(int);
TEST(Lib, Meta) {
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
set_up(entt::meta_ctx{});
set_up();
entt::meta<double>().conv<int>();
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve("velocity"_hs));
ASSERT_EQ(entt::resolve<position>(), entt::resolve("position"_hs));
ASSERT_EQ(entt::resolve<velocity>(), entt::resolve("velocity"_hs));
auto pos = entt::resolve("position"_hs).construct(42., 3.);
auto vel = entt::resolve("velocity"_hs).ctor().invoke();