diff --git a/TODO b/TODO index e78d18439..9d5ae10c7 100644 --- a/TODO +++ b/TODO @@ -26,6 +26,6 @@ - ::group improve, reduce code * Mission: get rid of named types - - add tests for type_info fallback and type_info specializations + - add tests for type_info specializations (eg dispatcher_plugin_std) - update doc: dispatcher, emitter, registry, meta, across boundaries - review and suppress warnings, if any diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index d2fc22cd6..7dc44bd06 100644 --- a/src/entt/core/type_info.hpp +++ b/src/entt/core/type_info.hpp @@ -7,35 +7,21 @@ #include "hashed_string.hpp" -namespace entt { - - -/** - * @brief Types identifiers. - * @tparam Type Type for which to generate an identifier. - */ -template -struct ENTT_API type_info { - /** - * @brief Returns the numeric representation of a given type. - * @return The numeric representation of the given type. - */ -#if defined ENTT_PRETTY_FUNCTION - static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT { - return entt::hashed_string{ENTT_PRETTY_FUNCTION}; - } +#ifdef ENTT_PRETTY_FUNCTION +# define ENTT_TYPE_ID_API #else - static ENTT_ID_TYPE id() ENTT_NOEXCEPT; -}; +# define ENTT_TYPE_ID_API ENTT_API +#endif +#ifndef ENTT_PRETTY_FUNCTION /** * @cond TURN_OFF_DOXYGEN * Internal details not to be documented. */ -namespace internal { +namespace entt::internal { struct ENTT_API type_id_generator { @@ -53,12 +39,31 @@ struct ENTT_API type_id_generator { * Internal details not to be documented. * @endcond TURN_OFF_DOXYGEN */ +#endif -template -ENTT_ID_TYPE type_info::id() ENTT_NOEXCEPT { - static const ENTT_ID_TYPE value = internal::type_id_generator::next(); - return value; +namespace entt { + + +/** + * @brief Types identifiers. + * @tparam Type Type for which to generate an identifier. + */ +template +struct ENTT_TYPE_ID_API type_info { + /** + * @brief Returns the numeric representation of a given type. + * @return The numeric representation of the given type. + */ +#ifdef ENTT_PRETTY_FUNCTION + static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT { + return entt::hashed_string{ENTT_PRETTY_FUNCTION}; + } +#else + static ENTT_ID_TYPE id() ENTT_NOEXCEPT { + static const ENTT_ID_TYPE value = internal::type_id_generator::next(); + return value; + } #endif }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7fc9da12a..8aa040540 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -50,8 +50,8 @@ macro(SETUP_LIB_TEST TEST_NAME) add_library(_${TEST_NAME} SHARED lib/${TEST_NAME}/lib.cpp) SETUP_TARGET(_${TEST_NAME}) SETUP_BASIC_TEST(lib_${TEST_NAME} lib/${TEST_NAME}/main.cpp) - target_compile_definitions(_${TEST_NAME} PRIVATE ENTT_API_EXPORT) - target_compile_definitions(lib_${TEST_NAME} PRIVATE ENTT_API_IMPORT) + target_compile_definitions(_${TEST_NAME} PRIVATE ENTT_API_EXPORT ${ARGV1}) + target_compile_definitions(lib_${TEST_NAME} PRIVATE ENTT_API_IMPORT ${ARGV1}) target_link_libraries(lib_${TEST_NAME} PRIVATE _${TEST_NAME}) endmacro() @@ -86,6 +86,11 @@ if(BUILD_LIB) SETUP_LIB_TEST(meta) SETUP_LIB_TEST(registry) + SETUP_LIB_TEST(dispatcher_std ENTT_STANDARD_CPP) + SETUP_LIB_TEST(emitter_std ENTT_STANDARD_CPP) + SETUP_LIB_TEST(meta_std ENTT_STANDARD_CPP) + SETUP_LIB_TEST(registry_std ENTT_STANDARD_CPP) + SETUP_PLUGIN_TEST(dispatcher_plugin) SETUP_PLUGIN_TEST(emitter_plugin) SETUP_PLUGIN_TEST(meta_plugin) diff --git a/test/lib/dispatcher_plugin/types.h b/test/lib/dispatcher_plugin/types.h index b1a3f283d..b0707106b 100644 --- a/test/lib/dispatcher_plugin/types.h +++ b/test/lib/dispatcher_plugin/types.h @@ -1,5 +1,5 @@ -#ifndef ENTT_PLUGIN_DISPATCHER_TYPES_H -#define ENTT_PLUGIN_DISPATCHER_TYPES_H +#ifndef ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H +#define ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H struct message { int payload; diff --git a/test/lib/dispatcher_std/lib.cpp b/test/lib/dispatcher_std/lib.cpp new file mode 100644 index 000000000..3641667fe --- /dev/null +++ b/test/lib/dispatcher_std/lib.cpp @@ -0,0 +1,8 @@ +#include +#include +#include "types.h" + +ENTT_API void trigger(entt::dispatcher &dispatcher) { + dispatcher.trigger(); + dispatcher.trigger(42); +} diff --git a/test/lib/dispatcher_std/main.cpp b/test/lib/dispatcher_std/main.cpp new file mode 100644 index 000000000..73998534b --- /dev/null +++ b/test/lib/dispatcher_std/main.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#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().connect(&listener::on)>(listener); + trigger(dispatcher); + + ASSERT_EQ(listener.value, 42); +} diff --git a/test/lib/dispatcher_std/types.h b/test/lib/dispatcher_std/types.h new file mode 100644 index 000000000..f17c0d6af --- /dev/null +++ b/test/lib/dispatcher_std/types.h @@ -0,0 +1,12 @@ +#ifndef ENTT_LIB_DISPATCHER_STD_TYPES_H +#define ENTT_LIB_DISPATCHER_STD_TYPES_H + +#include + +struct ENTT_API message { + int payload; +}; + +struct ENTT_API event {}; + +#endif diff --git a/test/lib/emitter_plugin/types.h b/test/lib/emitter_plugin/types.h index 81a230c9a..41cef4dab 100644 --- a/test/lib/emitter_plugin/types.h +++ b/test/lib/emitter_plugin/types.h @@ -1,5 +1,5 @@ -#ifndef ENTT_PLUGIN_EMITTER_TYPES_H -#define ENTT_PLUGIN_EMITTER_TYPES_H +#ifndef ENTT_LIB_EMITTER_PLUGIN_TYPES_H +#define ENTT_LIB_EMITTER_PLUGIN_TYPES_H #include diff --git a/test/lib/emitter_std/lib.cpp b/test/lib/emitter_std/lib.cpp new file mode 100644 index 000000000..e9c4594dc --- /dev/null +++ b/test/lib/emitter_std/lib.cpp @@ -0,0 +1,9 @@ +#include +#include +#include "types.h" + +ENTT_API void emit(test_emitter &emitter) { + emitter.publish(); + emitter.publish(42); + emitter.publish(3); +} diff --git a/test/lib/emitter_std/main.cpp b/test/lib/emitter_std/main.cpp new file mode 100644 index 000000000..9c879a0dd --- /dev/null +++ b/test/lib/emitter_std/main.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +#include "types.h" + +ENTT_API void emit(test_emitter &); + +TEST(Lib, Emitter) { + test_emitter emitter; + int value{}; + + ASSERT_EQ(value, 0); + + emitter.once([&](message msg, test_emitter &) { value = msg.payload; }); + emit(emitter); + + ASSERT_EQ(value, 42); +} diff --git a/test/lib/emitter_std/types.h b/test/lib/emitter_std/types.h new file mode 100644 index 000000000..d220924e5 --- /dev/null +++ b/test/lib/emitter_std/types.h @@ -0,0 +1,17 @@ +#ifndef ENTT_LIB_EMITTER_STD_TYPES_H +#define ENTT_LIB_EMITTER_STD_TYPES_H + +#include +#include + +struct ENTT_API test_emitter + : entt::emitter +{}; + +struct ENTT_API message { + int payload; +}; + +struct ENTT_API event {}; + +#endif diff --git a/test/lib/meta_plugin/types.h b/test/lib/meta_plugin/types.h index b611af51c..7816d696a 100644 --- a/test/lib/meta_plugin/types.h +++ b/test/lib/meta_plugin/types.h @@ -1,5 +1,5 @@ -#ifndef ENTT_PLUGIN_META_TYPES_H -#define ENTT_PLUGIN_META_TYPES_H +#ifndef ENTT_LIB_META_PLUGIN_TYPES_H +#define ENTT_LIB_META_PLUGIN_TYPES_H #include diff --git a/test/lib/meta_std/lib.cpp b/test/lib/meta_std/lib.cpp new file mode 100644 index 000000000..af922c136 --- /dev/null +++ b/test/lib/meta_std/lib.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include "types.h" + +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::meta() + .type("position"_hs) + .ctor<&create_position>() + .data<&position::x>("x"_hs) + .data<&position::y>("y"_hs); + + entt::meta() + .type("velocity"_hs) + .ctor<>() + .data<&velocity::dx>("dx"_hs) + .data<&velocity::dy>("dy"_hs); +} + +ENTT_API void tear_down() { + entt::meta().reset(); + entt::meta().reset(); +} + +ENTT_API entt::meta_any wrap_int(int value) { + return value; +} diff --git a/test/lib/meta_std/main.cpp b/test/lib/meta_std/main.cpp new file mode 100644 index 000000000..2b7fb3a00 --- /dev/null +++ b/test/lib/meta_std/main.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include "types.h" + +ENTT_API void set_up(entt::meta_ctx); +ENTT_API void tear_down(); +ENTT_API entt::meta_any wrap_int(int); + +TEST(Lib, Meta) { + ASSERT_FALSE(entt::resolve("position"_hs)); + + set_up(entt::meta_ctx{}); + entt::meta().conv(); + + 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).ctor().invoke(); + + ASSERT_TRUE(pos && vel); + + ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve()); + ASSERT_TRUE(pos.type().data("y"_hs).get(*pos).try_cast()); + ASSERT_EQ(pos.type().data("x"_hs).get(*pos).cast(), 42); + ASSERT_EQ(pos.type().data("y"_hs).get(*pos).cast(), 3); + + ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve()); + ASSERT_TRUE(vel.type().data("dy"_hs).get(*vel).convert()); + ASSERT_EQ(vel.type().data("dx"_hs).get(*vel).cast(), 0.); + ASSERT_EQ(vel.type().data("dy"_hs).get(*vel).cast(), 0.); + + ASSERT_EQ(wrap_int(42).type(), entt::resolve()); + ASSERT_EQ(wrap_int(42).cast(), 42); + + tear_down(); + + ASSERT_FALSE(entt::resolve("position"_hs)); + ASSERT_FALSE(entt::resolve("velocity"_hs)); +} diff --git a/test/lib/meta_std/types.h b/test/lib/meta_std/types.h new file mode 100644 index 000000000..83e6a0489 --- /dev/null +++ b/test/lib/meta_std/types.h @@ -0,0 +1,14 @@ +#ifndef ENTT_LIB_META_STD_TYPES_H +#define ENTT_LIB_META_STD_TYPES_H + +struct position { + int x{}; + int y{}; +}; + +struct velocity { + double dx{}; + double dy{}; +}; + +#endif diff --git a/test/lib/registry_plugin/types.h b/test/lib/registry_plugin/types.h index b7578d209..2e0c14e42 100644 --- a/test/lib/registry_plugin/types.h +++ b/test/lib/registry_plugin/types.h @@ -1,5 +1,5 @@ -#ifndef ENTT_PLUGIN_REGISTRY_TYPES_H -#define ENTT_PLUGIN_REGISTRY_TYPES_H +#ifndef ENTT_LIB_REGISTRY_PLUGIN_TYPES_H +#define ENTT_LIB_REGISTRY_PLUGIN_TYPES_H struct position { int x; diff --git a/test/lib/registry_std/lib.cpp b/test/lib/registry_std/lib.cpp new file mode 100644 index 000000000..77eaaa412 --- /dev/null +++ b/test/lib/registry_std/lib.cpp @@ -0,0 +1,19 @@ +#include +#include +#include "types.h" + +ENTT_API void update_position(entt::registry ®istry) { + registry.view().each([](auto &pos, auto &vel) { + pos.x += 16 * vel.dx; + pos.y += 16 * vel.dy; + }); +} + +ENTT_API void assign_velocity(entt::registry ®istry) { + // forces the creation of the pool for the velocity component + registry.prepare(); + + for(auto entity: registry.view()) { + registry.assign(entity, 1., 1.); + } +} diff --git a/test/lib/registry_std/main.cpp b/test/lib/registry_std/main.cpp new file mode 100644 index 000000000..493a20551 --- /dev/null +++ b/test/lib/registry_std/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include "types.h" + +ENTT_API void update_position(entt::registry &); +ENTT_API void assign_velocity(entt::registry &); + +TEST(Lib, Registry) { + entt::registry registry; + + for(auto i = 0; i < 3; ++i) { + const auto entity = registry.create(); + registry.assign(entity, i, i); + } + + assign_velocity(registry); + update_position(registry); + + ASSERT_EQ(registry.size(), registry.size()); + + registry.view().each([](auto entity, auto &position) { + ASSERT_EQ(position.x, entt::to_integral(entity) + 16); + ASSERT_EQ(position.y, entt::to_integral(entity) + 16); + }); +} diff --git a/test/lib/registry_std/types.h b/test/lib/registry_std/types.h new file mode 100644 index 000000000..159c16fd9 --- /dev/null +++ b/test/lib/registry_std/types.h @@ -0,0 +1,16 @@ +#ifndef ENTT_LIB_REGISTRY_STD_TYPES_H +#define ENTT_LIB_REGISTRY_STD_TYPES_H + +#include + +struct ENTT_API position { + int x; + int y; +}; + +struct ENTT_API velocity { + double dx; + double dy; +}; + +#endif