Squashed 'external/entt/entt/' content from commit fef92113
git-subtree-dir: external/entt/entt git-subtree-split: fef921132cae7588213d0f9bcd2fb9c8ffd8b7fc
This commit is contained in:
8
test/lib/dispatcher/lib.cpp
Normal file
8
test/lib/dispatcher/lib.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/signal/dispatcher.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void trigger(entt::dispatcher &dispatcher) {
|
||||
dispatcher.trigger<event>();
|
||||
dispatcher.trigger(message{42});
|
||||
}
|
28
test/lib/dispatcher/main.cpp
Normal file
28
test/lib/dispatcher/main.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/core/utility.hpp>
|
||||
#include <entt/signal/dispatcher.hpp>
|
||||
#include <entt/signal/sigh.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void trigger(entt::dispatcher &);
|
||||
|
||||
struct listener {
|
||||
void on(message msg) {
|
||||
value = msg.payload;
|
||||
}
|
||||
|
||||
int value{};
|
||||
};
|
||||
|
||||
TEST(Lib, Dispatcher) {
|
||||
entt::dispatcher dispatcher;
|
||||
listener listener;
|
||||
|
||||
ASSERT_EQ(listener.value, 0);
|
||||
|
||||
dispatcher.sink<message>().connect<entt::overload<void(message)>(&listener::on)>(listener);
|
||||
trigger(dispatcher);
|
||||
|
||||
ASSERT_EQ(listener.value, 42);
|
||||
}
|
12
test/lib/dispatcher/types.h
Normal file
12
test/lib/dispatcher/types.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef ENTT_LIB_DISPATCHER_TYPES_H
|
||||
#define ENTT_LIB_DISPATCHER_TYPES_H
|
||||
|
||||
#include <entt/core/attribute.h>
|
||||
|
||||
struct ENTT_API message {
|
||||
int payload;
|
||||
};
|
||||
|
||||
struct ENTT_API event {};
|
||||
|
||||
#endif
|
35
test/lib/dispatcher_plugin/main.cpp
Normal file
35
test/lib/dispatcher_plugin/main.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include <entt/signal/dispatcher.hpp>
|
||||
#include <entt/signal/sigh.hpp>
|
||||
#include "types.h"
|
||||
|
||||
struct listener {
|
||||
void on(message msg) {
|
||||
value = msg.payload;
|
||||
}
|
||||
|
||||
int value{};
|
||||
};
|
||||
|
||||
TEST(Lib, Dispatcher) {
|
||||
entt::dispatcher dispatcher;
|
||||
listener listener;
|
||||
|
||||
ASSERT_EQ(listener.value, 0);
|
||||
|
||||
dispatcher.sink<message>().connect<&listener::on>(listener);
|
||||
|
||||
cr_plugin ctx;
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
|
||||
ctx.userdata = &dispatcher;
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
ASSERT_EQ(listener.value, 42);
|
||||
|
||||
dispatcher = {};
|
||||
cr_plugin_close(ctx);
|
||||
}
|
19
test/lib/dispatcher_plugin/plugin.cpp
Normal file
19
test/lib/dispatcher_plugin/plugin.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <cr.h>
|
||||
#include <entt/signal/dispatcher.hpp>
|
||||
#include "types.h"
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_STEP:
|
||||
static_cast<entt::dispatcher *>(ctx->userdata)->trigger<event>();
|
||||
static_cast<entt::dispatcher *>(ctx->userdata)->trigger(message{42});
|
||||
break;
|
||||
case CR_CLOSE:
|
||||
case CR_LOAD:
|
||||
case CR_UNLOAD:
|
||||
// nothing to do here, this is only a test.
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
10
test/lib/dispatcher_plugin/types.h
Normal file
10
test/lib/dispatcher_plugin/types.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H
|
||||
#define ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H
|
||||
|
||||
struct message {
|
||||
int payload;
|
||||
};
|
||||
|
||||
struct event {};
|
||||
|
||||
#endif
|
8
test/lib/emitter/lib.cpp
Normal file
8
test/lib/emitter/lib.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <entt/core/attribute.h>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void emit(test_emitter &emitter) {
|
||||
emitter.publish(event{});
|
||||
emitter.publish(message{42});
|
||||
emitter.publish(message{3});
|
||||
}
|
21
test/lib/emitter/main.cpp
Normal file
21
test/lib/emitter/main.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/attribute.h>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void emit(test_emitter &);
|
||||
|
||||
TEST(Lib, Emitter) {
|
||||
test_emitter emitter;
|
||||
int value{};
|
||||
|
||||
ASSERT_EQ(value, 0);
|
||||
|
||||
emitter.on<message>([&](message msg, test_emitter &owner) {
|
||||
value = msg.payload;
|
||||
owner.erase<message>();
|
||||
});
|
||||
|
||||
emit(emitter);
|
||||
|
||||
ASSERT_EQ(value, 42);
|
||||
}
|
16
test/lib/emitter/types.h
Normal file
16
test/lib/emitter/types.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef ENTT_LIB_EMITTER_TYPES_H
|
||||
#define ENTT_LIB_EMITTER_TYPES_H
|
||||
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/signal/emitter.hpp>
|
||||
|
||||
struct ENTT_API test_emitter
|
||||
: entt::emitter<test_emitter> {};
|
||||
|
||||
struct ENTT_API message {
|
||||
int payload;
|
||||
};
|
||||
|
||||
struct ENTT_API event {};
|
||||
|
||||
#endif
|
28
test/lib/emitter_plugin/main.cpp
Normal file
28
test/lib/emitter_plugin/main.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include "types.h"
|
||||
|
||||
TEST(Lib, Emitter) {
|
||||
test_emitter emitter;
|
||||
int value{};
|
||||
|
||||
ASSERT_EQ(value, 0);
|
||||
|
||||
emitter.on<message>([&](message msg, test_emitter &owner) {
|
||||
value = msg.payload;
|
||||
owner.erase<message>();
|
||||
});
|
||||
|
||||
cr_plugin ctx;
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
|
||||
ctx.userdata = &emitter;
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
ASSERT_EQ(value, 42);
|
||||
|
||||
emitter = {};
|
||||
cr_plugin_close(ctx);
|
||||
}
|
19
test/lib/emitter_plugin/plugin.cpp
Normal file
19
test/lib/emitter_plugin/plugin.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <cr.h>
|
||||
#include "types.h"
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_STEP:
|
||||
static_cast<test_emitter *>(ctx->userdata)->publish(event{});
|
||||
static_cast<test_emitter *>(ctx->userdata)->publish(message{42});
|
||||
static_cast<test_emitter *>(ctx->userdata)->publish(message{3});
|
||||
break;
|
||||
case CR_CLOSE:
|
||||
case CR_LOAD:
|
||||
case CR_UNLOAD:
|
||||
// nothing to do here, this is only a test.
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
15
test/lib/emitter_plugin/types.h
Normal file
15
test/lib/emitter_plugin/types.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef ENTT_LIB_EMITTER_PLUGIN_TYPES_H
|
||||
#define ENTT_LIB_EMITTER_PLUGIN_TYPES_H
|
||||
|
||||
#include <entt/signal/emitter.hpp>
|
||||
|
||||
struct test_emitter
|
||||
: entt::emitter<test_emitter> {};
|
||||
|
||||
struct message {
|
||||
int payload;
|
||||
};
|
||||
|
||||
struct event {};
|
||||
|
||||
#endif
|
11
test/lib/locator/lib.cpp
Normal file
11
test/lib/locator/lib.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void set_up(const entt::locator<service>::node_type &handle) {
|
||||
entt::locator<service>::reset(handle);
|
||||
}
|
||||
|
||||
ENTT_API void use_service(int value) {
|
||||
entt::locator<service>::value().value = value;
|
||||
}
|
24
test/lib/locator/main.cpp
Normal file
24
test/lib/locator/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void set_up(const entt::locator<service>::node_type &);
|
||||
ENTT_API void use_service(int);
|
||||
|
||||
TEST(Lib, Locator) {
|
||||
entt::locator<service>::emplace().value = 42;
|
||||
|
||||
ASSERT_EQ(entt::locator<service>::value().value, 42);
|
||||
|
||||
set_up(entt::locator<service>::handle());
|
||||
use_service(3);
|
||||
|
||||
ASSERT_EQ(entt::locator<service>::value().value, 3);
|
||||
|
||||
// service updates do not propagate across boundaries
|
||||
entt::locator<service>::emplace().value = 42;
|
||||
use_service(3);
|
||||
|
||||
ASSERT_EQ(entt::locator<service>::value().value, 42);
|
||||
}
|
8
test/lib/locator/types.h
Normal file
8
test/lib/locator/types.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef ENTT_LIB_LOCATOR_TYPES_H
|
||||
#define ENTT_LIB_LOCATOR_TYPES_H
|
||||
|
||||
struct service {
|
||||
int value;
|
||||
};
|
||||
|
||||
#endif
|
32
test/lib/locator_plugin/main.cpp
Normal file
32
test/lib/locator_plugin/main.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include "types.h"
|
||||
|
||||
TEST(Lib, Locator) {
|
||||
entt::locator<service>::emplace().value = 42;
|
||||
|
||||
ASSERT_EQ(entt::locator<service>::value().value, 42);
|
||||
|
||||
userdata ud{};
|
||||
ud.handle = entt::locator<service>::handle();
|
||||
ud.value = 3;
|
||||
|
||||
cr_plugin ctx;
|
||||
ctx.userdata = &ud;
|
||||
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
ASSERT_EQ(entt::locator<service>::value().value, ud.value);
|
||||
|
||||
// service updates do not propagate across boundaries
|
||||
entt::locator<service>::emplace().value = 42;
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
ASSERT_NE(entt::locator<service>::value().value, ud.value);
|
||||
|
||||
cr_plugin_close(ctx);
|
||||
}
|
19
test/lib/locator_plugin/plugin.cpp
Normal file
19
test/lib/locator_plugin/plugin.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <cr.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include "types.h"
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_LOAD:
|
||||
entt::locator<service>::reset(static_cast<userdata *>(ctx->userdata)->handle);
|
||||
break;
|
||||
case CR_STEP:
|
||||
entt::locator<service>::value().value = static_cast<userdata *>(ctx->userdata)->value;
|
||||
break;
|
||||
case CR_UNLOAD:
|
||||
case CR_CLOSE:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
16
test/lib/locator_plugin/types.h
Normal file
16
test/lib/locator_plugin/types.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef ENTT_LIB_LOCATOR_PLUGIN_TYPES_H
|
||||
#define ENTT_LIB_LOCATOR_PLUGIN_TYPES_H
|
||||
|
||||
#include <entt/locator/locator.hpp>
|
||||
|
||||
struct service {
|
||||
int value;
|
||||
};
|
||||
|
||||
struct userdata {
|
||||
using node_type = typename entt::locator<service>::node_type;
|
||||
node_type handle;
|
||||
int value;
|
||||
};
|
||||
|
||||
#endif
|
38
test/lib/meta/lib.cpp
Normal file
38
test/lib/meta/lib.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include <entt/core/attribute.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};
|
||||
}
|
||||
|
||||
ENTT_API void share(entt::locator<entt::meta_ctx>::node_type handle) {
|
||||
entt::locator<entt::meta_ctx>::reset(handle);
|
||||
}
|
||||
|
||||
ENTT_API void set_up() {
|
||||
using namespace entt::literals;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
ENTT_API void tear_down() {
|
||||
entt::meta_reset<position>();
|
||||
entt::meta_reset<velocity>();
|
||||
}
|
||||
|
||||
ENTT_API entt::meta_any wrap_int(int value) {
|
||||
return value;
|
||||
}
|
55
test/lib/meta/main.cpp
Normal file
55
test/lib/meta/main.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/meta/factory.hpp>
|
||||
#include <entt/meta/meta.hpp>
|
||||
#include <entt/meta/resolve.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void share(entt::locator<entt::meta_ctx>::node_type);
|
||||
ENTT_API void set_up();
|
||||
ENTT_API void tear_down();
|
||||
ENTT_API entt::meta_any wrap_int(int);
|
||||
|
||||
TEST(Lib, Meta) {
|
||||
using namespace entt::literals;
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
ASSERT_FALSE(entt::resolve("velocity"_hs));
|
||||
|
||||
share(entt::locator<entt::meta_ctx>::handle());
|
||||
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).construct();
|
||||
|
||||
ASSERT_TRUE(pos && vel);
|
||||
|
||||
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
|
||||
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
|
||||
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).allow_cast<double>());
|
||||
ASSERT_EQ(vel.type().data("dx"_hs).get(vel).cast<double>(), 0.);
|
||||
ASSERT_EQ(vel.type().data("dy"_hs).get(vel).cast<double>(), 0.);
|
||||
|
||||
pos.reset();
|
||||
vel.reset();
|
||||
|
||||
ASSERT_EQ(wrap_int(42).type(), entt::resolve<int>());
|
||||
ASSERT_EQ(wrap_int(42).cast<int>(), 42);
|
||||
|
||||
tear_down();
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
ASSERT_FALSE(entt::resolve("velocity"_hs));
|
||||
}
|
14
test/lib/meta/types.h
Normal file
14
test/lib/meta/types.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef ENTT_LIB_META_TYPES_H
|
||||
#define ENTT_LIB_META_TYPES_H
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct velocity {
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
#endif
|
58
test/lib/meta_plugin/main.cpp
Normal file
58
test/lib/meta_plugin/main.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include <entt/meta/factory.hpp>
|
||||
#include <entt/meta/meta.hpp>
|
||||
#include <entt/meta/resolve.hpp>
|
||||
#include "types.h"
|
||||
|
||||
TEST(Lib, Meta) {
|
||||
using namespace entt::literals;
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
|
||||
userdata ud{};
|
||||
ud.ctx = entt::locator<entt::meta_ctx>::handle();
|
||||
|
||||
cr_plugin ctx;
|
||||
ctx.userdata = &ud;
|
||||
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
entt::meta<double>().conv<int>();
|
||||
|
||||
ASSERT_TRUE(entt::resolve("position"_hs));
|
||||
ASSERT_TRUE(entt::resolve("velocity"_hs));
|
||||
|
||||
auto pos = entt::resolve("position"_hs).construct(42., 3.);
|
||||
auto vel = entt::resolve("velocity"_hs).construct();
|
||||
|
||||
ASSERT_TRUE(pos && vel);
|
||||
|
||||
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
|
||||
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
|
||||
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).allow_cast<double>());
|
||||
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);
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
ASSERT_FALSE(entt::resolve("velocity"_hs));
|
||||
}
|
50
test/lib/meta_plugin/plugin.cpp
Normal file
50
test/lib/meta_plugin/plugin.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <cr.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include <entt/meta/context.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() {
|
||||
using namespace entt::literals;
|
||||
|
||||
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_reset<position>();
|
||||
entt::meta_reset<velocity>();
|
||||
}
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_LOAD:
|
||||
entt::locator<entt::meta_ctx>::reset(static_cast<userdata *>(ctx->userdata)->ctx);
|
||||
set_up();
|
||||
break;
|
||||
case CR_STEP:
|
||||
static_cast<userdata *>(ctx->userdata)->any = 42;
|
||||
break;
|
||||
case CR_UNLOAD:
|
||||
case CR_CLOSE:
|
||||
tear_down();
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
22
test/lib/meta_plugin/types.h
Normal file
22
test/lib/meta_plugin/types.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ENTT_LIB_META_PLUGIN_TYPES_H
|
||||
#define ENTT_LIB_META_PLUGIN_TYPES_H
|
||||
|
||||
#include <entt/locator/locator.hpp>
|
||||
#include <entt/meta/meta.hpp>
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct velocity {
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
struct userdata {
|
||||
entt::locator<entt::meta_ctx>::node_type ctx;
|
||||
entt::meta_any any;
|
||||
};
|
||||
|
||||
#endif
|
57
test/lib/meta_plugin_std/main.cpp
Normal file
57
test/lib/meta_plugin_std/main.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/meta/factory.hpp>
|
||||
#include <entt/meta/meta.hpp>
|
||||
#include <entt/meta/resolve.hpp>
|
||||
#include "types.h"
|
||||
|
||||
TEST(Lib, Meta) {
|
||||
using namespace entt::literals;
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
|
||||
userdata ud{};
|
||||
ud.ctx = entt::locator<entt::meta_ctx>::handle();
|
||||
|
||||
cr_plugin ctx;
|
||||
ctx.userdata = &ud;
|
||||
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
entt::meta<double>().conv<int>();
|
||||
|
||||
ASSERT_TRUE(entt::resolve("position"_hs));
|
||||
ASSERT_TRUE(entt::resolve("velocity"_hs));
|
||||
|
||||
auto pos = entt::resolve("position"_hs).construct(42., 3.);
|
||||
auto vel = entt::resolve("velocity"_hs).construct();
|
||||
|
||||
ASSERT_TRUE(pos && vel);
|
||||
|
||||
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
|
||||
ASSERT_NE(pos.type().data("y"_hs).get(pos).try_cast<int>(), nullptr);
|
||||
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).allow_cast<double>());
|
||||
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);
|
||||
|
||||
ASSERT_FALSE(entt::resolve("position"_hs));
|
||||
ASSERT_FALSE(entt::resolve("velocity"_hs));
|
||||
}
|
49
test/lib/meta_plugin_std/plugin.cpp
Normal file
49
test/lib/meta_plugin_std/plugin.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
#include <cr.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/meta/context.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() {
|
||||
using namespace entt::literals;
|
||||
|
||||
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_reset<position>();
|
||||
entt::meta_reset<velocity>();
|
||||
}
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_LOAD:
|
||||
entt::locator<entt::meta_ctx>::reset(static_cast<userdata *>(ctx->userdata)->ctx);
|
||||
set_up();
|
||||
break;
|
||||
case CR_STEP:
|
||||
static_cast<userdata *>(ctx->userdata)->any = 42;
|
||||
break;
|
||||
case CR_UNLOAD:
|
||||
case CR_CLOSE:
|
||||
tear_down();
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
42
test/lib/meta_plugin_std/types.h
Normal file
42
test/lib/meta_plugin_std/types.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef ENTT_LIB_META_PLUGIN_TYPES_STD_H
|
||||
#define ENTT_LIB_META_PLUGIN_TYPES_STD_H
|
||||
|
||||
#include <type_traits>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/core/type_info.hpp>
|
||||
#include <entt/meta/meta.hpp>
|
||||
|
||||
template<typename>
|
||||
struct custom_type_hash;
|
||||
|
||||
#define ASSIGN_TYPE_ID(clazz) \
|
||||
template<> \
|
||||
struct entt::type_hash<clazz> { \
|
||||
static constexpr entt::id_type value() noexcept { \
|
||||
return entt::basic_hashed_string<std::remove_const_t<std::remove_pointer_t<std::decay_t<decltype(#clazz)>>>>{#clazz}; \
|
||||
} \
|
||||
}
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct velocity {
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
struct userdata {
|
||||
entt::locator<entt::meta_ctx>::node_type ctx;
|
||||
entt::meta_any any;
|
||||
};
|
||||
|
||||
ASSIGN_TYPE_ID(void);
|
||||
ASSIGN_TYPE_ID(std::size_t);
|
||||
ASSIGN_TYPE_ID(position);
|
||||
ASSIGN_TYPE_ID(velocity);
|
||||
ASSIGN_TYPE_ID(double);
|
||||
ASSIGN_TYPE_ID(int);
|
||||
|
||||
#endif
|
19
test/lib/registry/lib.cpp
Normal file
19
test/lib/registry/lib.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void update_position(entt::registry ®istry) {
|
||||
registry.view<position, velocity>().each([](auto &pos, auto &vel) {
|
||||
pos.x += static_cast<int>(16 * vel.dx);
|
||||
pos.y += static_cast<int>(16 * vel.dy);
|
||||
});
|
||||
}
|
||||
|
||||
ENTT_API void emplace_velocity(entt::registry ®istry) {
|
||||
// forces the creation of the pool for the velocity component
|
||||
static_cast<void>(registry.storage<velocity>());
|
||||
|
||||
for(auto entity: registry.view<position>()) {
|
||||
registry.emplace<velocity>(entity, 1., 1.);
|
||||
}
|
||||
}
|
27
test/lib/registry/main.cpp
Normal file
27
test/lib/registry/main.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/attribute.h>
|
||||
#include <entt/entity/entity.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include "types.h"
|
||||
|
||||
ENTT_API void update_position(entt::registry &);
|
||||
ENTT_API void emplace_velocity(entt::registry &);
|
||||
|
||||
TEST(Lib, Registry) {
|
||||
entt::registry registry;
|
||||
|
||||
for(auto i = 0; i < 3; ++i) {
|
||||
const auto entity = registry.create();
|
||||
registry.emplace<position>(entity, i, i);
|
||||
}
|
||||
|
||||
emplace_velocity(registry);
|
||||
update_position(registry);
|
||||
|
||||
ASSERT_EQ(registry.storage<position>().size(), registry.storage<velocity>().size());
|
||||
|
||||
registry.view<position>().each([](auto entity, auto &position) {
|
||||
ASSERT_EQ(position.x, static_cast<int>(entt::to_integral(entity) + 16));
|
||||
ASSERT_EQ(position.y, static_cast<int>(entt::to_integral(entity) + 16));
|
||||
});
|
||||
}
|
16
test/lib/registry/types.h
Normal file
16
test/lib/registry/types.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef ENTT_LIB_REGISTRY_TYPES_H
|
||||
#define ENTT_LIB_REGISTRY_TYPES_H
|
||||
|
||||
#include <entt/core/attribute.h>
|
||||
|
||||
struct ENTT_API position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct ENTT_API velocity {
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
#endif
|
31
test/lib/registry_plugin/main.cpp
Normal file
31
test/lib/registry_plugin/main.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#define CR_HOST
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cr.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include "types.h"
|
||||
|
||||
TEST(Lib, Registry) {
|
||||
entt::registry registry;
|
||||
|
||||
for(auto i = 0; i < 3; ++i) {
|
||||
registry.emplace<position>(registry.create(), i, i);
|
||||
}
|
||||
|
||||
cr_plugin ctx;
|
||||
cr_plugin_load(ctx, PLUGIN);
|
||||
|
||||
ctx.userdata = ®istry;
|
||||
cr_plugin_update(ctx);
|
||||
|
||||
ASSERT_EQ(registry.storage<position>().size(), registry.storage<velocity>().size());
|
||||
ASSERT_EQ(registry.storage<position>().size(), registry.size());
|
||||
|
||||
registry.view<position>().each([](auto entity, auto &position) {
|
||||
ASSERT_EQ(position.x, static_cast<int>(entt::to_integral(entity) + 16u));
|
||||
ASSERT_EQ(position.y, static_cast<int>(entt::to_integral(entity) + 16u));
|
||||
});
|
||||
|
||||
registry = {};
|
||||
cr_plugin_close(ctx);
|
||||
}
|
30
test/lib/registry_plugin/plugin.cpp
Normal file
30
test/lib/registry_plugin/plugin.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <cr.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include "types.h"
|
||||
|
||||
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
|
||||
switch(operation) {
|
||||
case CR_STEP: {
|
||||
// forces things to break
|
||||
auto ®istry = *static_cast<entt::registry *>(ctx->userdata);
|
||||
|
||||
// forces the creation of the pool for the velocity component
|
||||
static_cast<void>(registry.storage<velocity>());
|
||||
|
||||
const auto view = registry.view<position>();
|
||||
registry.insert(view.begin(), view.end(), velocity{1., 1.});
|
||||
|
||||
registry.view<position, velocity>().each([](position &pos, velocity &vel) {
|
||||
pos.x += static_cast<int>(16 * vel.dx);
|
||||
pos.y += static_cast<int>(16 * vel.dy);
|
||||
});
|
||||
} break;
|
||||
case CR_CLOSE:
|
||||
case CR_LOAD:
|
||||
case CR_UNLOAD:
|
||||
// nothing to do here, this is only a test.
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
14
test/lib/registry_plugin/types.h
Normal file
14
test/lib/registry_plugin/types.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef ENTT_LIB_REGISTRY_PLUGIN_TYPES_H
|
||||
#define ENTT_LIB_REGISTRY_PLUGIN_TYPES_H
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct velocity {
|
||||
double dx;
|
||||
double dy;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user