Просмотр исходного кода

core: define entt::id_type alias for ENTT_ID_TYPE (close #416)

Michele Caini 6 лет назад
Родитель
Сommit
ef7c572018

+ 3 - 2
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<typename...>
 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<typename... Type>

+ 18 - 0
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

+ 5 - 4
src/entt/core/hashed_string.hpp

@@ -5,6 +5,7 @@
 #include <cstddef>
 #include <cstdint>
 #include "../config/config.h"
+#include "fwd.hpp"
 
 
 namespace entt {
@@ -61,7 +62,7 @@ struct fnv1a_traits<std::uint64_t> {
  */
 template<typename Char>
 class basic_hashed_string {
-    using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
+    using traits_type = internal::fnv1a_traits<id_type>;
 
     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;
     }

+ 4 - 3
src/entt/core/ident.hpp

@@ -7,6 +7,7 @@
 #include <utility>
 #include <type_traits>
 #include "../config/config.h"
+#include "fwd.hpp"
 
 
 namespace entt {
@@ -44,14 +45,14 @@ class identifier {
     using tuple_type = std::tuple<std::decay_t<Types>...>;
 
     template<typename Type, std::size_t... Indexes>
-    static constexpr ENTT_ID_TYPE get(std::index_sequence<Indexes...>) {
+    static constexpr id_type get(std::index_sequence<Indexes...>) {
         static_assert(std::disjunction_v<std::is_same<Type, Types>...>);
-        return (0 + ... + (std::is_same_v<Type, std::tuple_element_t<Indexes, tuple_type>> ? ENTT_ID_TYPE(Indexes) : ENTT_ID_TYPE{}));
+        return (0 + ... + (std::is_same_v<Type, std::tuple_element_t<Indexes, tuple_type>> ? 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<typename Type>

+ 3 - 2
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<ENTT_ID_TYPE>
+template<id_type>
 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<ENTT_ID_TYPE Value>
+template<id_type Value>
 inline monostate<Value> monostate_v = {};
 
 

+ 7 - 6
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

+ 5 - 4
src/entt/core/type_traits.hpp

@@ -6,7 +6,8 @@
 #include <utility>
 #include <type_traits>
 #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<Member>::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<ENTT_ID_TYPE Value>
-using tag = std::integral_constant<ENTT_ID_TYPE, Value>;
+template<id_type Value>
+using tag = std::integral_constant<id_type, Value>;
 
 
 }

+ 3 - 2
src/entt/entity/entity.hpp

@@ -6,6 +6,7 @@
 #include <type_traits>
 #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);
 
 
 /**

+ 3 - 3
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<typename>
 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<entity>;

+ 21 - 20
src/entt/entity/registry.hpp

@@ -2,28 +2,29 @@
 #define ENTT_ENTITY_REGISTRY_HPP
 
 
-#include <tuple>
-#include <vector>
-#include <memory>
-#include <utility>
+#include <algorithm>
 #include <cstddef>
 #include <iterator>
-#include <algorithm>
+#include <memory>
+#include <tuple>
 #include <type_traits>
+#include <utility>
+#include <vector>
 #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<sparse_set<Entity>> pool{};
         void(* remove)(sparse_set<Entity> &, basic_registry &, const Entity){};
     };
@@ -160,13 +161,13 @@ class basic_registry {
     struct group_data {
         std::size_t size;
         std::unique_ptr<void, void(*)(void *)> 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<void, void(*)(void *)> value;
     };
 
@@ -1294,9 +1295,9 @@ public:
             group_data candidate = {
                 size,
                 { new handler_type{}, [](void *instance) { delete static_cast<handler_type *>(instance); } },
-                []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Owned>>::id()) || ...); },
-                []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Get>>::id()) || ...); },
-                []([[maybe_unused]] const ENTT_ID_TYPE ctype) ENTT_NOEXCEPT { return ((ctype == type_info<Exclude>::id()) || ...); },
+                []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Owned>>::id()) || ...); },
+                []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Get>>::id()) || ...); },
+                []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<Exclude>::id()) || ...); },
             };
 
             handler = static_cast<handler_type *>(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.

+ 1 - 0
src/entt/fwd.hpp

@@ -1,3 +1,4 @@
+#include "core/fwd.hpp"
 #include "entity/fwd.hpp"
 #include "resource/fwd.hpp"
 #include "signal/fwd.hpp"

+ 10 - 9
src/entt/meta/factory.hpp

@@ -2,17 +2,18 @@
 #define ENTT_META_FACTORY_HPP
 
 
-#include <tuple>
 #include <array>
 #include <cstddef>
-#include <utility>
 #include <functional>
+#include <tuple>
 #include <type_traits>
+#include <utility>
 #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<Type> {
     }
 
     template<typename Node>
-    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<Type>::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, typename Policy = as_is_t>
-    auto data(const ENTT_ID_TYPE alias) ENTT_NOEXCEPT {
+    auto data(const id_type alias) ENTT_NOEXCEPT {
         auto * const type = internal::meta_info<Type>::resolve();
         internal::meta_data_node *curr = nullptr;
 
@@ -697,7 +698,7 @@ public:
      * @return An extended meta factory for the parent type.
      */
     template<auto Setter, auto Getter, typename Policy = as_is_t>
-    auto data(const ENTT_ID_TYPE alias) ENTT_NOEXCEPT {
+    auto data(const id_type alias) ENTT_NOEXCEPT {
         using underlying_type = std::invoke_result_t<decltype(Getter), Type &>;
         static_assert(std::is_invocable_v<decltype(Setter), Type &, underlying_type>);
         auto * const type = internal::meta_info<Type>::resolve();
@@ -737,7 +738,7 @@ public:
      * @return An extended meta factory for the parent type.
      */
     template<auto Candidate, typename Policy = as_is_t>
-    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<decltype(Candidate)>;
         auto * const type = internal::meta_info<Type>::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);

+ 13 - 12
src/entt/meta/meta.hpp

@@ -3,10 +3,11 @@
 
 
 #include <cstddef>
-#include <utility>
 #include <functional>
 #include <type_traits>
+#include <utility>
 #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);

+ 2 - 3
src/entt/resource/cache.hpp

@@ -3,10 +3,11 @@
 
 
 #include <memory>
-#include <utility>
 #include <type_traits>
 #include <unordered_map>
+#include <utility>
 #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;

+ 6 - 5
src/entt/signal/dispatcher.hpp

@@ -2,12 +2,13 @@
 #define ENTT_SIGNAL_DISPATCHER_HPP
 
 
-#include <vector>
-#include <memory>
 #include <cstddef>
-#include <utility>
+#include <memory>
 #include <type_traits>
+#include <utility>
+#include <vector>
 #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<typename Event>
@@ -68,7 +69,7 @@ class dispatcher {
             events.emplace_back(std::forward<Args>(args)...);
         }
 
-        ENTT_ID_TYPE type_id() const ENTT_NOEXCEPT override {
+        id_type type_id() const ENTT_NOEXCEPT override {
             return type_info<Event>::id();
         }
 

+ 8 - 7
src/entt/signal/emitter.hpp

@@ -2,15 +2,16 @@
 #define ENTT_SIGNAL_EMITTER_HPP
 
 
-#include <type_traits>
-#include <functional>
 #include <algorithm>
-#include <utility>
+#include <functional>
+#include <iterator>
+#include <list>
 #include <memory>
+#include <type_traits>
+#include <utility>
 #include <vector>
-#include <list>
-#include <iterator>
 #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<typename Event>
@@ -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<Event>::id();
         }
 

+ 1 - 1
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<resource>::id_type id) {
+    cache.each([&cache](entt::id_type id) {
         cache.discard(id);
     });