Browse Source

entity: make entt_traits sfinae-friendly and avoid using std::underlying_type_t to define the entity traits types

Michele Caini 5 years ago
parent
commit
371b541fbc

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

@@ -18,10 +18,20 @@ namespace entt {
  * Primary template isn't defined on purpose. All the specializations give a
  * Primary template isn't defined on purpose. All the specializations give a
  * compile-time error unless the template parameter is an accepted entity type.
  * compile-time error unless the template parameter is an accepted entity type.
  */
  */
-template<typename>
+template<typename, typename = void>
 struct entt_traits;
 struct entt_traits;
 
 
 
 
+/**
+ * @brief Entity traits for enumeration types.
+ * @tparam Type The type to check.
+ */
+template<typename Type>
+struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
+        : entt_traits<std::underlying_type_t<Type>>
+{};
+
+
 /**
 /**
  * @brief Entity traits for a 16 bits entity identifier.
  * @brief Entity traits for a 16 bits entity identifier.
  *
  *
@@ -111,7 +121,7 @@ namespace internal {
 
 
 class null {
 class null {
     template<typename Entity>
     template<typename Entity>
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
 public:
 public:
     template<typename Entity>
     template<typename Entity>

+ 1 - 1
src/entt/entity/registry.hpp

@@ -41,7 +41,7 @@ namespace entt {
  */
  */
 template<typename Entity>
 template<typename Entity>
 class basic_registry {
 class basic_registry {
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
     template<typename Component>
     template<typename Component>
     struct pool_handler final: storage<Entity, Component> {
     struct pool_handler final: storage<Entity, Component> {

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

@@ -32,7 +32,7 @@ class basic_snapshot {
     /*! @brief A registry is allowed to create snapshots. */
     /*! @brief A registry is allowed to create snapshots. */
     friend class basic_registry<Entity>;
     friend class basic_registry<Entity>;
 
 
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
     template<typename Component, typename Archive, typename It>
     template<typename Component, typename Archive, typename It>
     void get(Archive &archive, std::size_t sz, It first, It last) const {
     void get(Archive &archive, std::size_t sz, It first, It last) const {
@@ -164,7 +164,7 @@ class basic_snapshot_loader {
     /*! @brief A registry is allowed to create snapshot loaders. */
     /*! @brief A registry is allowed to create snapshot loaders. */
     friend class basic_registry<Entity>;
     friend class basic_registry<Entity>;
 
 
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
     template<typename Type, typename Archive>
     template<typename Type, typename Archive>
     void assign(Archive &archive) const {
     void assign(Archive &archive) const {
@@ -299,7 +299,7 @@ private:
  */
  */
 template<typename Entity>
 template<typename Entity>
 class basic_continuous_loader {
 class basic_continuous_loader {
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
     void destroy(Entity entt) {
     void destroy(Entity entt) {
         const auto it = remloc.find(entt);
         const auto it = remloc.find(entt);

+ 1 - 1
src/entt/entity/sparse_set.hpp

@@ -44,7 +44,7 @@ class sparse_set {
     static_assert(ENTT_PAGE_SIZE && ((ENTT_PAGE_SIZE & (ENTT_PAGE_SIZE - 1)) == 0), "ENTT_PAGE_SIZE must be a power of two");
     static_assert(ENTT_PAGE_SIZE && ((ENTT_PAGE_SIZE & (ENTT_PAGE_SIZE - 1)) == 0), "ENTT_PAGE_SIZE must be a power of two");
     static constexpr auto entt_per_page = ENTT_PAGE_SIZE / sizeof(Entity);
     static constexpr auto entt_per_page = ENTT_PAGE_SIZE / sizeof(Entity);
 
 
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
     using page_type = std::unique_ptr<Entity[]>;
     using page_type = std::unique_ptr<Entity[]>;
 
 
     class sparse_set_iterator final {
     class sparse_set_iterator final {

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

@@ -49,7 +49,7 @@ class storage: public sparse_set<Entity> {
     static_assert(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>, "The managed type must be at least move constructible and assignable");
     static_assert(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>, "The managed type must be at least move constructible and assignable");
 
 
     using underlying_type = sparse_set<Entity>;
     using underlying_type = sparse_set<Entity>;
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
 
 
     template<bool Const>
     template<bool Const>
     class storage_iterator final {
     class storage_iterator final {
@@ -478,7 +478,7 @@ private:
 /*! @copydoc storage */
 /*! @copydoc storage */
 template<typename Entity, typename Type>
 template<typename Entity, typename Type>
 class storage<Entity, Type, std::enable_if_t<ENTT_IS_EMPTY(Type)>>: public sparse_set<Entity> {
 class storage<Entity, Type, std::enable_if_t<ENTT_IS_EMPTY(Type)>>: public sparse_set<Entity> {
-    using traits_type = entt_traits<std::underlying_type_t<Entity>>;
+    using traits_type = entt_traits<Entity>;
     using underlying_type = sparse_set<Entity>;
     using underlying_type = sparse_set<Entity>;
 
 
 public:
 public: