From 4af7d975c1f830cf905025df4ffd0701d129688a Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 21 Dec 2019 23:32:50 +0100 Subject: [PATCH] type_id -> type info + allow also fully runtime ids --- src/entt/core/type_info.hpp | 14 ++----- src/entt/entity/registry.hpp | 42 ++++++++++---------- src/entt/meta/meta.hpp | 2 +- src/entt/signal/dispatcher.hpp | 12 +++--- src/entt/signal/emitter.hpp | 12 +++--- test/benchmark/benchmark.cpp | 65 ++++++++++++++++++++----------- test/entt/core/type_info.cpp | 6 +-- test/entt/entity/runtime_view.cpp | 15 +++---- test/mod/mod.cpp | 8 ++-- 9 files changed, 95 insertions(+), 81 deletions(-) diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index 656c09dc0..49df2b22e 100644 --- a/src/entt/core/type_info.hpp +++ b/src/entt/core/type_info.hpp @@ -14,13 +14,13 @@ namespace entt { * @tparam Type Type for which to generate an identifier. */ template -struct type_id { +struct type_info { #if defined _MSC_VER /** * @brief Returns the numeric representation of a given type. * @return The numeric representation of the given type. */ - static constexpr ENTT_ID_TYPE value() ENTT_NOEXCEPT { + static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT { return entt::hashed_string{__FUNCSIG__}; } #elif defined __GNUC__ @@ -28,21 +28,13 @@ struct type_id { * @brief Returns the numeric representation of a given type. * @return The numeric representation of the given type. */ - static constexpr ENTT_ID_TYPE value() ENTT_NOEXCEPT { + static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT { return entt::hashed_string{__PRETTY_FUNCTION__}; } #endif }; -/** - * @brief Helper variable template. - * @tparam Type Type for which to generate an identifier. - */ -template -static constexpr auto type_id_v = type_id::value(); - - } diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 280bdaebf..c057b45c3 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -177,7 +177,7 @@ class basic_registry { {} ENTT_ID_TYPE id() const ENTT_NOEXCEPT override { - return type_id_v; + return type_info::id(); } }; @@ -213,15 +213,15 @@ class basic_registry { static_assert(std::is_same_v>); static std::size_t index{pools.size()}; - if(!(index < pools.size()) || pools[index].type_id != type_id_v) { + if(!(index < pools.size()) || pools[index].type_id != type_info::id()) { index = std::find_if(pools.cbegin(), pools.cend(), [](auto &&cpool) { - return cpool.type_id == type_id_v; + return cpool.type_id == type_info::id(); }) - pools.cbegin(); if(index == pools.size()) { auto &&pdata = pools.emplace_back(); - pdata.type_id = type_id_v; + pdata.type_id = type_info::id(); pdata.pool = std::make_unique>(std::forward(args)...); pdata.remove = [](sparse_set &cpool, basic_registry &owner, const Entity entt) { @@ -273,7 +273,7 @@ public: */ template void prepare(Args &&... args) { - ENTT_ASSERT(std::none_of(pools.cbegin(), pools.cend(), [](auto &&pdata) { return pdata.type_id == type_id_v; })); + ENTT_ASSERT(std::none_of(pools.cbegin(), pools.cend(), [](auto &&pdata) { return pdata.type_id == type_info::id(); })); assure(std::forward(args)...); } @@ -284,7 +284,7 @@ public: template void discard() { pools.erase(std::remove_if(pools.begin(), pools.end(), [](auto &&pdata) { - return ((pdata.type_id == type_id_v) || ...); + return ((pdata.type_id == type_info::id()) || ...); }), pools.end()); } @@ -1276,9 +1276,9 @@ public: if(auto it = std::find_if(groups.cbegin(), groups.cend(), [&extent](const auto &gdata) { return std::equal(std::begin(extent), std::end(extent), std::begin(gdata.extent)) - && (gdata.owned(type_id_v>) && ...) - && (gdata.get(type_id_v>) && ...) - && (gdata.exclude(type_id_v) && ...); + && (gdata.owned(type_info>::id()) && ...) + && (gdata.get(type_info>::id()) && ...) + && (gdata.exclude(type_info::id()) && ...); }); it != groups.cend()) { handler = static_cast(it->group.get()); @@ -1291,9 +1291,9 @@ public: group_data gdata{ { sizeof...(Owned), sizeof...(Get), sizeof...(Exclude) }, decltype(group_data::group){new handler_type{cpools}, [](void *gptr) { delete static_cast(gptr); }}, - [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_id_v>) || ...); }, - [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_id_v>) || ...); }, - [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_id_v) || ...); } + [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + [](const auto ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); } }; if constexpr(sizeof...(Owned) == 0) { @@ -1301,21 +1301,21 @@ public: } else { ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [&extent](const auto &curr) { const std::size_t diff[3]{ - (0u + ... + curr.owned(type_id_v>)), - (0u + ... + curr.get(type_id_v>)), - (0u + ... + curr.exclude(type_id_v)) + (0u + ... + curr.owned(type_info>::id())), + (0u + ... + curr.get(type_info>::id())), + (0u + ... + curr.exclude(type_info::id())) }; return !diff[0] || ((std::equal(std::begin(diff), std::end(diff), extent) || std::equal(std::begin(diff), std::end(diff), curr.extent))); })); const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [&size](const auto &curr) { - const std::size_t diff = (0u + ... + curr.owned(type_id_v>)); + const std::size_t diff = (0u + ... + curr.owned(type_info>::id())); return !diff || (size > (curr.extent[0] + curr.extent[1] + curr.extent[2])); }); const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &curr) { - return (0u + ... + curr.owned(type_id_v>)); + return (0u + ... + curr.owned(type_info>::id())); }); maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get()); @@ -1487,7 +1487,7 @@ public: if constexpr(sizeof...(Component) == 0) { for(size_type pos{}; pos < pools.size(); ++pos) { - if(const auto &pdata = pools[pos]; pdata.assure && ((pdata.type_id != type_id_v) && ...)) { + if(const auto &pdata = pools[pos]; pdata.assure && ((pdata.type_id != type_info::id()) && ...)) { pdata.assure(other, *pdata.pool); } } @@ -1540,7 +1540,7 @@ public: void stomp(const entity_type dst, const basic_registry &other, const entity_type src, exclude_t = {}) { if constexpr(sizeof...(Component) == 0) { for(size_type pos{}; pos < other.pools.size(); ++pos) { - if(const auto &pdata = other.pools[pos]; pdata.stomp && ((pdata.type_id != type_id_v) && ...) && pdata.pool->has(src)) { + if(const auto &pdata = other.pools[pos]; pdata.stomp && ((pdata.type_id != type_info::id()) && ...) && pdata.pool->has(src)) { pdata.stomp(*this, dst, *pdata.pool, src); } } @@ -1648,7 +1648,7 @@ public: template void unset() { vars.erase(std::remove_if(vars.begin(), vars.end(), [](auto &&handler) { - return handler->id() == type_id_v; + return handler->id() == type_info::id(); }), vars.end()); } @@ -1678,7 +1678,7 @@ public: template const Type * try_ctx() const { auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&handler) { - return handler->id() == type_id_v; + return handler->id() == type_info::id(); }); return it == vars.cend() ? nullptr : &static_cast &>(*it->get()).value; diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 2cd7ae159..c97e56f67 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -196,7 +196,7 @@ struct meta_node { inline static meta_type_node * resolve() ENTT_NOEXCEPT { static meta_type_node node{ - type_id_v, + type_info::id(), {}, nullptr, nullptr, diff --git a/src/entt/signal/dispatcher.hpp b/src/entt/signal/dispatcher.hpp index 8193785d2..46078d672 100644 --- a/src/entt/signal/dispatcher.hpp +++ b/src/entt/signal/dispatcher.hpp @@ -34,7 +34,7 @@ class dispatcher { virtual ~basic_pool() = default; virtual void publish() = 0; virtual void clear() ENTT_NOEXCEPT = 0; - virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0; + virtual ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT = 0; }; template @@ -70,8 +70,8 @@ class dispatcher { events.emplace_back(std::forward(args)...); } - ENTT_ID_TYPE id() const ENTT_NOEXCEPT override { - return type_id_v; + ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override { + return type_info::id(); } private: @@ -84,9 +84,9 @@ class dispatcher { static_assert(std::is_same_v>); static std::size_t index{pools.size()}; - if(!(index < pools.size()) || pools[index]->id() != type_id_v) { + if(!(index < pools.size()) || pools[index]->type_id() != type_info::id()) { index = std::find_if(pools.cbegin(), pools.cend(), [](auto &&cpool) { - return cpool->id() == type_id_v; + return cpool->type_id() == type_info::id(); }) - pools.cbegin(); if(index == pools.size()) { @@ -105,7 +105,7 @@ public: template void discard() { pools.erase(std::remove_if(pools.begin(), pools.end(), [](auto &&cpool) { - return ((cpool->id() == type_id_v) || ...); + return ((cpool->type_id() == type_info::id()) || ...); }), pools.end()); } diff --git a/src/entt/signal/emitter.hpp b/src/entt/signal/emitter.hpp index 45bbd0cff..b7f1cb396 100644 --- a/src/entt/signal/emitter.hpp +++ b/src/entt/signal/emitter.hpp @@ -44,7 +44,7 @@ class emitter { virtual ~basic_pool() = default; virtual bool empty() const ENTT_NOEXCEPT = 0; virtual void clear() ENTT_NOEXCEPT = 0; - virtual ENTT_ID_TYPE id() const ENTT_NOEXCEPT = 0; + virtual ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT = 0; }; template @@ -108,8 +108,8 @@ class emitter { on_list.remove_if([](auto &&element) { return element.first; }); } - ENTT_ID_TYPE id() const ENTT_NOEXCEPT override { - return type_id_v; + ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override { + return type_info::id(); } private: @@ -123,9 +123,9 @@ class emitter { static_assert(std::is_same_v>); static std::size_t index{pools.size()}; - if(!(index < pools.size()) || pools[index]->id() != type_id_v) { + if(!(index < pools.size()) || pools[index]->type_id() != type_info::id()) { index = std::find_if(pools.cbegin(), pools.cend(), [](auto &&cpool) { - return cpool->id() == type_id_v; + return cpool->type_id() == type_info::id(); }) - pools.cbegin(); if(index == pools.size()) { @@ -193,7 +193,7 @@ public: template void discard() { pools.erase(std::remove_if(pools.begin(), pools.end(), [](auto &&cpool) { - return ((cpool->id() == type_id_v) || ...); + return ((cpool->type_id() == type_info::id()) || ...); }), pools.end()); } diff --git a/test/benchmark/benchmark.cpp b/test/benchmark/benchmark.cpp index d6d12ba08..a4c74d523 100644 --- a/test/benchmark/benchmark.cpp +++ b/test/benchmark/benchmark.cpp @@ -168,7 +168,7 @@ TEST(Benchmark, IterateSingleComponentRuntime1M) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id() }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -333,7 +333,10 @@ TEST(Benchmark, IterateTwoComponentsRuntime1M) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -361,7 +364,10 @@ TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -389,7 +395,10 @@ TEST(Benchmark, IterateTwoComponentsRuntime1MOne) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -562,7 +571,11 @@ TEST(Benchmark, IterateThreeComponentsRuntime1M) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v, entt::type_id_v> }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -592,7 +605,11 @@ TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v, entt::type_id_v> }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -622,7 +639,11 @@ TEST(Benchmark, IterateThreeComponentsRuntime1MOne) { } auto test = [®istry](auto func) { - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v, entt::type_id_v> }; + ENTT_ID_TYPE types[] = { + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id() + }; timer timer; registry.runtime_view(std::begin(types), std::end(types)).each(func); @@ -837,11 +858,11 @@ TEST(Benchmark, IterateFiveComponentsRuntime1M) { auto test = [®istry](auto func) { ENTT_ID_TYPE types[] = { - entt::type_id_v, - entt::type_id_v, - entt::type_id_v>, - entt::type_id_v>, - entt::type_id_v> + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id(), + entt::type_info>::id(), + entt::type_info>::id() }; timer timer; @@ -877,11 +898,11 @@ TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) { auto test = [®istry](auto func) { ENTT_ID_TYPE types[] = { - entt::type_id_v, - entt::type_id_v, - entt::type_id_v>, - entt::type_id_v>, - entt::type_id_v> + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id(), + entt::type_info>::id(), + entt::type_info>::id() }; timer timer; @@ -917,11 +938,11 @@ TEST(Benchmark, IterateFiveComponentsRuntime1MOne) { auto test = [®istry](auto func) { ENTT_ID_TYPE types[] = { - entt::type_id_v, - entt::type_id_v, - entt::type_id_v>, - entt::type_id_v>, - entt::type_id_v> + entt::type_info::id(), + entt::type_info::id(), + entt::type_info>::id(), + entt::type_info>::id(), + entt::type_info>::id() }; timer timer; diff --git a/test/entt/core/type_info.cpp b/test/entt/core/type_info.cpp index a0f91293c..091b626fe 100644 --- a/test/entt/core/type_info.cpp +++ b/test/entt/core/type_info.cpp @@ -3,7 +3,7 @@ #include TEST(TypeId, Functionalities) { - ASSERT_NE(entt::type_id_v, entt::type_id_v); - ASSERT_NE(entt::type_id_v, entt::type_id_v); - ASSERT_EQ(entt::type_id_v, entt::type_id_v); + ASSERT_NE(entt::type_info::id(), entt::type_info::id()); + ASSERT_NE(entt::type_info::id(), entt::type_info::id()); + ASSERT_EQ(entt::type_info::id(), entt::type_info::id()); } diff --git a/test/entt/entity/runtime_view.cpp b/test/entt/entity/runtime_view.cpp index e6b207cf0..2386fe801 100644 --- a/test/entt/entity/runtime_view.cpp +++ b/test/entt/entity/runtime_view.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -11,7 +12,7 @@ TEST(RuntimeView, Functionalities) { registry.reserve(0); registry.reserve(0); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); ASSERT_TRUE(view.empty()); @@ -54,7 +55,7 @@ TEST(RuntimeView, Iterator) { registry.assign(entity); registry.assign(entity); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); using iterator_type = typename decltype(view)::iterator_type; @@ -90,7 +91,7 @@ TEST(RuntimeView, Contains) { registry.destroy(e0); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); ASSERT_FALSE(view.contains(e0)); @@ -109,7 +110,7 @@ TEST(RuntimeView, Empty) { registry.assign(e1); registry.assign(e1); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); view.each([](auto) { FAIL(); }); @@ -129,7 +130,7 @@ TEST(RuntimeView, Each) { registry.assign(e1); registry.assign(e1); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); std::size_t cnt = 0; @@ -151,7 +152,7 @@ TEST(RuntimeView, EachWithHoles) { registry.assign(e0, 0); registry.assign(e2, 2); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); view.each([e0](auto entity) { @@ -165,7 +166,7 @@ TEST(RuntimeView, MissingPool) { const auto e0 = registry.create(); registry.assign(e0); - ENTT_ID_TYPE types[] = { entt::type_id_v, entt::type_id_v }; + ENTT_ID_TYPE types[] = { entt::type_info::id(), entt::type_info::id() }; auto view = registry.runtime_view(std::begin(types), std::end(types)); ASSERT_TRUE(view.empty()); diff --git a/test/mod/mod.cpp b/test/mod/mod.cpp index c4e13cc6b..92eeefd22 100644 --- a/test/mod/mod.cpp +++ b/test/mod/mod.cpp @@ -134,7 +134,7 @@ class duktape_registry { template void reg() { - ((func[entt::type_id_v] = { + ((func[entt::type_info::id()] = { &::set, &::unset, &::has, @@ -163,7 +163,7 @@ class duktape_registry { const auto it = func.find(type); return (it == func.cend()) - ? (func[entt::type_id_v].*Op)(ctx, registry) + ? (func[entt::type_info::id()].*Op)(ctx, registry) : (it->second.*Op)(ctx, registry); } @@ -217,7 +217,7 @@ public: if(dreg.func.find(type) == dreg.func.cend()) { if(runtime.empty()) { - components.push_back(entt::type_id_v); + components.push_back(entt::type_info::id()); } runtime.push_back(type); @@ -268,7 +268,7 @@ const duk_function_list_entry js_duktape_registry_methods[] = { void export_types(duk_context *context) { auto export_type = [idx = duk_push_object(context)](auto *ctx, auto type, const auto *name) { duk_push_string(ctx, name); - duk_push_uint(ctx, entt::type_id_v); + duk_push_uint(ctx, entt::type_info::id()); duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_CLEAR_WRITABLE); };