test: meta and plugins

This commit is contained in:
Michele Caini
2019-12-09 14:56:19 +01:00
parent 169dcbcd74
commit 84fb3694f2
7 changed files with 204 additions and 41 deletions

View File

@@ -0,0 +1,50 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "types.h"
TEST(Lib, Meta) {
entt::meta<double>().type("double"_hs);
entt::meta<int>().type("int"_hs);
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve<double>().conv<int>());
userdata ud{};
cr_plugin ctx;
ctx.userdata = &ud;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve<double>().conv<int>());
auto pos = entt::resolve("position"_hs).construct(42., 3.);
auto vel = entt::resolve("velocity"_hs).ctor().invoke();
ASSERT_TRUE(pos && vel);
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
ASSERT_TRUE(pos.type().data("y"_hs).get(*pos).try_cast<int>());
ASSERT_EQ(pos.type().data("x"_hs).get(*pos).cast<int>(), 42);
ASSERT_EQ(pos.type().data("y"_hs).get(*pos).cast<int>(), 3);
ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
ASSERT_TRUE(vel.type().data("dy"_hs).get(*vel).convert<int>());
ASSERT_EQ(vel.type().data("dx"_hs).get(*vel).cast<double>(), 0.);
ASSERT_EQ(vel.type().data("dy"_hs).get(*vel).cast<double>(), 0.);
ASSERT_EQ(ud.any.type(), entt::resolve<int>());
ASSERT_EQ(ud.any.cast<int>(), 42);
// these objects have been initialized from a different context
pos.emplace<void>();
vel.emplace<void>();
ud.any.emplace<void>();
cr_plugin_close(ctx);
}

View File

@@ -0,0 +1,53 @@
#include <cr.h>
#include <entt/core/hashed_string.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "types.h"
position create_position(int x, int y) {
return position{x, y};
}
void set_up() {
entt::meta<double>().import("double"_hs);
entt::meta<int>().import("int"_hs);
entt::meta<double>().conv<int>();
entt::meta<position>()
.type("position"_hs)
.ctor<&create_position>()
.data<&position::x>("x"_hs)
.data<&position::y>("y"_hs);
entt::meta<velocity>()
.type("velocity"_hs)
.ctor<>()
.data<&velocity::dx>("dx"_hs)
.data<&velocity::dy>("dy"_hs);
}
void tear_down() {
entt::meta<position>().reset();
entt::meta<velocity>().reset();
}
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_LOAD:
entt::meta_ctx::bind(static_cast<userdata *>(ctx->userdata)->ctx);
set_up();
break;
case CR_STEP:
static_cast<userdata *>(ctx->userdata)->any = 42;
break;
case CR_UNLOAD:
tear_down();
break;
case CR_CLOSE:
// nothing to do here, this is only a test.
break;
}
return 0;
}

View File

@@ -0,0 +1,21 @@
#ifndef ENTT_PLUGIN_META_TYPES_H
#define ENTT_PLUGIN_META_TYPES_H
#include <entt/meta/meta.hpp>
struct position {
int x{};
int y{};
};
struct velocity {
double dx{};
double dy{};
};
struct userdata {
entt::meta_ctx ctx;
entt::meta_any any;
};
#endif // ENTT_PLUGIN_META_TYPES_H