From ef7c572018ec37e4c66a9709faf7eb2c4d0a4173 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Wed, 4 Mar 2020 22:25:41 +0100 Subject: [PATCH] core: define entt::id_type alias for ENTT_ID_TYPE (close #416) --- src/entt/core/family.hpp | 5 ++-- src/entt/core/fwd.hpp | 18 +++++++++++++++ src/entt/core/hashed_string.hpp | 9 ++++---- src/entt/core/ident.hpp | 7 +++--- src/entt/core/monostate.hpp | 5 ++-- src/entt/core/type_info.hpp | 13 ++++++----- src/entt/core/type_traits.hpp | 9 ++++---- src/entt/entity/entity.hpp | 5 ++-- src/entt/entity/fwd.hpp | 6 ++--- src/entt/entity/registry.hpp | 41 +++++++++++++++++---------------- src/entt/fwd.hpp | 1 + src/entt/meta/factory.hpp | 19 +++++++-------- src/entt/meta/meta.hpp | 25 ++++++++++---------- src/entt/resource/cache.hpp | 5 ++-- src/entt/signal/dispatcher.hpp | 11 +++++---- src/entt/signal/emitter.hpp | 17 +++++++------- test/entt/resource/resource.cpp | 2 +- 17 files changed, 114 insertions(+), 84 deletions(-) create mode 100644 src/entt/core/fwd.hpp diff --git a/src/entt/core/family.hpp b/src/entt/core/family.hpp index 69a92e5f4..de0a8ce7c 100644 --- a/src/entt/core/family.hpp +++ b/src/entt/core/family.hpp @@ -3,6 +3,7 @@ #include "../config/config.h" +#include "fwd.hpp" namespace entt { @@ -17,11 +18,11 @@ namespace entt { */ template class family { - inline static ENTT_MAYBE_ATOMIC(ENTT_ID_TYPE) identifier{}; + inline static ENTT_MAYBE_ATOMIC(id_type) identifier{}; public: /*! @brief Unsigned integer type. */ - using family_type = ENTT_ID_TYPE; + using family_type = id_type; /*! @brief Statically generated unique identifier for the given type. */ template diff --git a/src/entt/core/fwd.hpp b/src/entt/core/fwd.hpp new file mode 100644 index 000000000..c155979a4 --- /dev/null +++ b/src/entt/core/fwd.hpp @@ -0,0 +1,18 @@ +#ifndef ENTT_CORE_FWD_HPP +#define ENTT_CORE_FWD_HPP + + +#include "../config/config.h" + + +namespace entt { + + +/*! @brief Alias declaration for type identifiers. */ +using id_type = ENTT_ID_TYPE; + + +} + + +#endif diff --git a/src/entt/core/hashed_string.hpp b/src/entt/core/hashed_string.hpp index bb7d5d8a0..7531b0667 100644 --- a/src/entt/core/hashed_string.hpp +++ b/src/entt/core/hashed_string.hpp @@ -5,6 +5,7 @@ #include #include #include "../config/config.h" +#include "fwd.hpp" namespace entt { @@ -61,7 +62,7 @@ struct fnv1a_traits { */ template class basic_hashed_string { - using traits_type = internal::fnv1a_traits; + using traits_type = internal::fnv1a_traits; struct const_wrapper { // non-explicit constructor on purpose @@ -70,7 +71,7 @@ class basic_hashed_string { }; // Fowler–Noll–Vo hash function v. 1a - the good - static constexpr ENTT_ID_TYPE helper(const Char *curr) ENTT_NOEXCEPT { + static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT { auto value = traits_type::offset; while(*curr != 0) { @@ -84,7 +85,7 @@ public: /*! @brief Character type. */ using value_type = Char; /*! @brief Unsigned integer type. */ - using hash_type = ENTT_ID_TYPE; + using hash_type = id_type; /** * @brief Returns directly the numeric representation of a string. @@ -122,7 +123,7 @@ public: * @return The numeric representation of the string. */ static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT { - ENTT_ID_TYPE partial{traits_type::offset}; + id_type partial{traits_type::offset}; while(size--) { partial = (partial^(str++)[0])*traits_type::prime; } return partial; } diff --git a/src/entt/core/ident.hpp b/src/entt/core/ident.hpp index 9fc378161..f146d2ff9 100644 --- a/src/entt/core/ident.hpp +++ b/src/entt/core/ident.hpp @@ -7,6 +7,7 @@ #include #include #include "../config/config.h" +#include "fwd.hpp" namespace entt { @@ -44,14 +45,14 @@ class identifier { using tuple_type = std::tuple...>; template - static constexpr ENTT_ID_TYPE get(std::index_sequence) { + static constexpr id_type get(std::index_sequence) { static_assert(std::disjunction_v...>); - return (0 + ... + (std::is_same_v> ? ENTT_ID_TYPE(Indexes) : ENTT_ID_TYPE{})); + return (0 + ... + (std::is_same_v> ? id_type(Indexes) : id_type{})); } public: /*! @brief Unsigned integer type. */ - using identifier_type = ENTT_ID_TYPE; + using identifier_type = id_type; /*! @brief Statically generated unique identifier for the given type. */ template diff --git a/src/entt/core/monostate.hpp b/src/entt/core/monostate.hpp index 1010d514e..e25dd5313 100644 --- a/src/entt/core/monostate.hpp +++ b/src/entt/core/monostate.hpp @@ -3,6 +3,7 @@ #include "../config/config.h" +#include "fwd.hpp" namespace entt { @@ -19,7 +20,7 @@ namespace entt { * both during an assignment and when they try to read back their data. * Otherwise, they can incur in unexpected results. */ -template +template struct monostate { /** * @brief Assigns a value of a specific type to a given key. @@ -51,7 +52,7 @@ private: * @brief Helper variable template. * @tparam Value Value used to differentiate between different variables. */ -template +template inline monostate monostate_v = {}; diff --git a/src/entt/core/type_info.hpp b/src/entt/core/type_info.hpp index a1ad0f26b..ba225bfc1 100644 --- a/src/entt/core/type_info.hpp +++ b/src/entt/core/type_info.hpp @@ -5,6 +5,7 @@ #include "../config/config.h" #include "../core/attribute.h" #include "hashed_string.hpp" +#include "fwd.hpp" #ifndef ENTT_PRETTY_FUNCTION @@ -28,8 +29,8 @@ namespace internal { struct ENTT_API type_id_generator { - static ENTT_ID_TYPE next() ENTT_NOEXCEPT { - static ENTT_ID_TYPE value{}; + static id_type next() ENTT_NOEXCEPT { + static id_type value{}; return value++; } }; @@ -56,18 +57,18 @@ struct ENTT_TYPE_ID_API type_info { * @return The numeric representation of the given type. */ #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR - static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT { + static constexpr id_type id() ENTT_NOEXCEPT { constexpr auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION_CONSTEXPR); return value; } #elif defined ENTT_PRETTY_FUNCTION - static ENTT_ID_TYPE id() ENTT_NOEXCEPT { + static id_type id() ENTT_NOEXCEPT { static const auto value = entt::hashed_string::value(ENTT_PRETTY_FUNCTION); return value; } #else - static ENTT_ID_TYPE id() ENTT_NOEXCEPT { - static const ENTT_ID_TYPE value = internal::type_id_generator::next(); + static id_type id() ENTT_NOEXCEPT { + static const id_type value = internal::type_id_generator::next(); return value; } #endif diff --git a/src/entt/core/type_traits.hpp b/src/entt/core/type_traits.hpp index 06acf687f..3df3c220a 100644 --- a/src/entt/core/type_traits.hpp +++ b/src/entt/core/type_traits.hpp @@ -6,7 +6,8 @@ #include #include #include "../config/config.h" -#include "../core/hashed_string.hpp" +#include "hashed_string.hpp" +#include "fwd.hpp" namespace entt { @@ -203,10 +204,10 @@ using member_class_t = typename member_class::type; /** * @brief Alias template to ease the creation of named values. - * @tparam Value A constant value at least convertible to `ENTT_ID_TYPE`. + * @tparam Value A constant value at least convertible to `id_type`. */ -template -using tag = std::integral_constant; +template +using tag = std::integral_constant; } diff --git a/src/entt/entity/entity.hpp b/src/entt/entity/entity.hpp index a9ffedd44..564d4717b 100644 --- a/src/entt/entity/entity.hpp +++ b/src/entt/entity/entity.hpp @@ -6,6 +6,7 @@ #include #include "../config/config.h" #include "../core/type_traits.hpp" +#include "../core/fwd.hpp" namespace entt { @@ -159,8 +160,8 @@ constexpr bool operator!=(const Entity entity, null other) ENTT_NOEXCEPT { */ -/*! @brief Alias declaration for the most common use case. */ -ENTT_OPAQUE_TYPE(entity, ENTT_ID_TYPE); +/*! @brief Default entity identifier. */ +ENTT_OPAQUE_TYPE(entity, id_type); /** diff --git a/src/entt/entity/fwd.hpp b/src/entt/entity/fwd.hpp index f38663da1..d4bc4d93d 100644 --- a/src/entt/entity/fwd.hpp +++ b/src/entt/entity/fwd.hpp @@ -2,7 +2,7 @@ #define ENTT_ENTITY_FWD_HPP -#include "../config/config.h" +#include "../core/fwd.hpp" namespace entt { @@ -44,8 +44,8 @@ class basic_snapshot_loader; template class basic_continuous_loader; -/*! @brief Alias declaration for the most common use case. */ -enum class entity: ENTT_ID_TYPE; +/*! @class entity */ +enum class entity: id_type; /*! @brief Alias declaration for the most common use case. */ using registry = basic_registry; diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index bacddf7b4..a1c862bec 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -2,28 +2,29 @@ #define ENTT_ENTITY_REGISTRY_HPP -#include -#include -#include -#include +#include #include #include -#include +#include +#include #include +#include +#include #include "../config/config.h" #include "../core/algorithm.hpp" +#include "../core/fwd.hpp" #include "../core/type_info.hpp" #include "../core/type_traits.hpp" #include "../signal/sigh.hpp" +#include "entity.hpp" +#include "fwd.hpp" +#include "group.hpp" #include "runtime_view.hpp" -#include "sparse_set.hpp" #include "snapshot.hpp" +#include "sparse_set.hpp" #include "storage.hpp" #include "utility.hpp" -#include "entity.hpp" -#include "group.hpp" #include "view.hpp" -#include "fwd.hpp" namespace entt { @@ -109,7 +110,7 @@ class basic_registry { }; struct pool_data { - ENTT_ID_TYPE type_id{}; + id_type type_id{}; std::unique_ptr> pool{}; void(* remove)(sparse_set &, basic_registry &, const Entity){}; }; @@ -160,13 +161,13 @@ class basic_registry { struct group_data { std::size_t size; std::unique_ptr group; - bool (* owned)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; - bool (* get)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; - bool (* exclude)(const ENTT_ID_TYPE) ENTT_NOEXCEPT; + bool (* owned)(const id_type) ENTT_NOEXCEPT; + bool (* get)(const id_type) ENTT_NOEXCEPT; + bool (* exclude)(const id_type) ENTT_NOEXCEPT; }; struct variable_data { - ENTT_ID_TYPE type_id; + id_type type_id; std::unique_ptr value; }; @@ -1294,9 +1295,9 @@ public: group_data candidate = { size, { new handler_type{}, [](void *instance) { delete static_cast(instance); } }, - []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, - []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, - []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); }, + []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info>::id()) || ...); }, + []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info::id()) || ...); }, }; handler = static_cast(candidate.group.get()); @@ -1508,7 +1509,7 @@ public: * The signature of the function should be equivalent to the following: * * @code{.cpp} - * void(const ENTT_ID_TYPE); + * void(const id_type); * @endcode * * Returned identifiers are those of the components owned by the entity. @@ -1538,7 +1539,7 @@ public: * The signature of the function should be equivalent to the following: * * @code{.cpp} - * void(const ENTT_ID_TYPE); + * void(const id_type); * @endcode * * Returned identifiers are those of the components managed by the registry. @@ -1654,7 +1655,7 @@ public: * The signature of the function should be equivalent to the following: * * @code{.cpp} - * void(const ENTT_ID_TYPE); + * void(const id_type); * @endcode * * Returned identifiers are those of the context variables currently set. diff --git a/src/entt/fwd.hpp b/src/entt/fwd.hpp index df1389e13..0f16f11b0 100644 --- a/src/entt/fwd.hpp +++ b/src/entt/fwd.hpp @@ -1,3 +1,4 @@ +#include "core/fwd.hpp" #include "entity/fwd.hpp" #include "resource/fwd.hpp" #include "signal/fwd.hpp" diff --git a/src/entt/meta/factory.hpp b/src/entt/meta/factory.hpp index 958df25f8..4e5618fb0 100644 --- a/src/entt/meta/factory.hpp +++ b/src/entt/meta/factory.hpp @@ -2,17 +2,18 @@ #define ENTT_META_FACTORY_HPP -#include #include #include -#include #include +#include #include +#include #include "../config/config.h" +#include "../core/fwd.hpp" #include "../core/type_traits.hpp" #include "../core/utility.hpp" -#include "policy.hpp" #include "meta.hpp" +#include "policy.hpp" namespace entt { @@ -378,7 +379,7 @@ class meta_factory { } template - bool exists(const ENTT_ID_TYPE alias, const Node *node) ENTT_NOEXCEPT { + bool exists(const id_type alias, const Node *node) ENTT_NOEXCEPT { return node && (node->alias == alias || exists(alias, node->next)); } @@ -388,7 +389,7 @@ public: * @param value Unique identifier. * @return An extended meta factory for the given type. */ - auto alias(const ENTT_ID_TYPE value) ENTT_NOEXCEPT { + auto alias(const id_type value) ENTT_NOEXCEPT { auto * const node = internal::meta_info::resolve(); ENTT_ASSERT(!exists(value, *internal::meta_info<>::global)); @@ -612,7 +613,7 @@ public: * @return An extended meta factory for the parent type. */ template - auto data(const ENTT_ID_TYPE alias) ENTT_NOEXCEPT { + auto data(const id_type alias) ENTT_NOEXCEPT { auto * const type = internal::meta_info::resolve(); internal::meta_data_node *curr = nullptr; @@ -697,7 +698,7 @@ public: * @return An extended meta factory for the parent type. */ template - auto data(const ENTT_ID_TYPE alias) ENTT_NOEXCEPT { + auto data(const id_type alias) ENTT_NOEXCEPT { using underlying_type = std::invoke_result_t; static_assert(std::is_invocable_v); auto * const type = internal::meta_info::resolve(); @@ -737,7 +738,7 @@ public: * @return An extended meta factory for the parent type. */ template - auto func(const ENTT_ID_TYPE alias) ENTT_NOEXCEPT { + auto func(const id_type alias) ENTT_NOEXCEPT { using helper_type = internal::meta_function_helper_t; auto * const type = internal::meta_info::resolve(); @@ -849,7 +850,7 @@ inline meta_type resolve() ENTT_NOEXCEPT { * @param alias Unique identifier. * @return The meta type associated with the given alias, if any. */ -inline meta_type resolve(const ENTT_ID_TYPE alias) 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); diff --git a/src/entt/meta/meta.hpp b/src/entt/meta/meta.hpp index 25def3439..a61d11f4d 100644 --- a/src/entt/meta/meta.hpp +++ b/src/entt/meta/meta.hpp @@ -3,10 +3,11 @@ #include -#include #include #include +#include #include "../config/config.h" +#include "../core/fwd.hpp" #include "../core/type_info.hpp" #include "../core/type_traits.hpp" @@ -71,7 +72,7 @@ struct meta_dtor_node { struct meta_data_node { - ENTT_ID_TYPE alias; + id_type alias; meta_type_node * const parent; meta_data_node * next; meta_prop_node * prop; @@ -85,7 +86,7 @@ struct meta_data_node { struct meta_func_node { using size_type = std::size_t; - ENTT_ID_TYPE alias; + id_type alias; meta_type_node * const parent; meta_func_node * next; meta_prop_node * prop; @@ -100,8 +101,8 @@ struct meta_func_node { struct meta_type_node { using size_type = std::size_t; - const ENTT_ID_TYPE type_id; - ENTT_ID_TYPE alias; + const id_type type_id; + id_type alias; meta_type_node * next; meta_prop_node * prop; const bool is_void; @@ -870,7 +871,7 @@ struct meta_data { {} /*! @copydoc meta_type::alias */ - ENTT_ID_TYPE alias() const ENTT_NOEXCEPT { + id_type alias() const ENTT_NOEXCEPT { return node->alias; } @@ -1010,7 +1011,7 @@ struct meta_func { {} /*! @copydoc meta_type::alias */ - ENTT_ID_TYPE alias() const ENTT_NOEXCEPT { + id_type alias() const ENTT_NOEXCEPT { return node->alias; } @@ -1136,7 +1137,7 @@ public: * @brief Returns the id of the underlying type. * @return The id of the underlying type. */ - ENTT_ID_TYPE id() const ENTT_NOEXCEPT { + id_type id() const ENTT_NOEXCEPT { return node->type_id; } @@ -1144,7 +1145,7 @@ public: * @brief Returns the alias assigned to a given meta object. * @return The alias assigned to the meta object. */ - ENTT_ID_TYPE alias() const ENTT_NOEXCEPT { + id_type alias() const ENTT_NOEXCEPT { return node->alias; } @@ -1290,7 +1291,7 @@ public: * @param alias Unique identifier. * @return The meta base associated with the given alias, if any. */ - meta_base base(const ENTT_ID_TYPE alias) const { + meta_base base(const id_type alias) const { return internal::find_if<&internal::meta_type_node::base>([alias](const auto *curr) { return curr->type()->alias == alias; }, node); @@ -1361,7 +1362,7 @@ public: * @param alias Unique identifier. * @return The meta data associated with the given alias, if any. */ - meta_data data(const ENTT_ID_TYPE alias) const { + meta_data data(const id_type alias) const { return internal::find_if<&internal::meta_type_node::data>([alias](const auto *curr) { return curr->alias == alias; }, node); @@ -1389,7 +1390,7 @@ public: * @param alias Unique identifier. * @return The meta function associated with the given alias, if any. */ - meta_func func(const ENTT_ID_TYPE alias) const { + meta_func func(const id_type alias) const { return internal::find_if<&internal::meta_type_node::func>([alias](const auto *curr) { return curr->alias == alias; }, node); diff --git a/src/entt/resource/cache.hpp b/src/entt/resource/cache.hpp index aba6e6732..4ddf91baf 100644 --- a/src/entt/resource/cache.hpp +++ b/src/entt/resource/cache.hpp @@ -3,10 +3,11 @@ #include -#include #include #include +#include #include "../config/config.h" +#include "../core/fwd.hpp" #include "handle.hpp" #include "loader.hpp" #include "fwd.hpp" @@ -31,8 +32,6 @@ struct cache { using size_type = std::size_t; /*! @brief Type of resources managed by a cache. */ using resource_type = Resource; - /*! @brief Unique identifier type for resources. */ - using id_type = ENTT_ID_TYPE; /*! @brief Default constructor. */ cache() = default; diff --git a/src/entt/signal/dispatcher.hpp b/src/entt/signal/dispatcher.hpp index 37733e9cd..5f4d8273e 100644 --- a/src/entt/signal/dispatcher.hpp +++ b/src/entt/signal/dispatcher.hpp @@ -2,12 +2,13 @@ #define ENTT_SIGNAL_DISPATCHER_HPP -#include -#include #include -#include +#include #include +#include +#include #include "../config/config.h" +#include "../core/fwd.hpp" #include "../core/type_info.hpp" #include "sigh.hpp" @@ -32,7 +33,7 @@ class dispatcher { virtual ~basic_pool() = default; virtual void publish() = 0; virtual void clear() ENTT_NOEXCEPT = 0; - virtual ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT = 0; + virtual id_type type_id() const ENTT_NOEXCEPT = 0; }; template @@ -68,7 +69,7 @@ class dispatcher { events.emplace_back(std::forward(args)...); } - ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override { + id_type type_id() const ENTT_NOEXCEPT override { return type_info::id(); } diff --git a/src/entt/signal/emitter.hpp b/src/entt/signal/emitter.hpp index 412508300..78bd98c26 100644 --- a/src/entt/signal/emitter.hpp +++ b/src/entt/signal/emitter.hpp @@ -2,15 +2,16 @@ #define ENTT_SIGNAL_EMITTER_HPP -#include -#include #include -#include -#include -#include -#include +#include #include +#include +#include +#include +#include +#include #include "../config/config.h" +#include "../core/fwd.hpp" #include "../core/type_info.hpp" @@ -44,7 +45,7 @@ class emitter { virtual ~basic_pool() = default; virtual bool empty() const ENTT_NOEXCEPT = 0; virtual void clear() ENTT_NOEXCEPT = 0; - virtual ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT = 0; + virtual id_type type_id() const ENTT_NOEXCEPT = 0; }; template @@ -113,7 +114,7 @@ class emitter { on_list.remove_if([](auto &&element) { return element.first; }); } - ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override { + id_type type_id() const ENTT_NOEXCEPT override { return type_info::id(); } diff --git a/test/entt/resource/resource.cpp b/test/entt/resource/resource.cpp index 4841a6a0b..bd25f3e18 100644 --- a/test/entt/resource/resource.cpp +++ b/test/entt/resource/resource.cpp @@ -128,7 +128,7 @@ TEST(Resource, Each) { ASSERT_FALSE(cache.empty()); ASSERT_EQ(cache.handle("resource"_hs)->value, 2); - cache.each([&cache](entt::cache::id_type id) { + cache.each([&cache](entt::id_type id) { cache.discard(id); });