Browse Source

helper: removed Const template parameter, used the same model of the handle class (const/non-const entity)

Michele Caini 5 years ago
parent
commit
b93a31dd5b
2 changed files with 16 additions and 14 deletions
  1. 14 12
      src/entt/entity/helper.hpp
  2. 2 2
      test/entt/entity/helper.cpp

+ 14 - 12
src/entt/entity/helper.hpp

@@ -15,13 +15,14 @@ namespace entt {
 
 /**
  * @brief Converts a registry to a view.
- * @tparam Const Constness of the accepted registry.
  * @tparam Entity A valid entity type (see entt_traits for more details).
  */
-template<bool Const, typename Entity>
+template<typename Entity>
 struct as_view {
+    /*! @brief Underlying entity identifier. */
+    using entity_type = std::remove_const_t<Entity>;
     /*! @brief Type of registry to convert. */
-    using registry_type = std::conditional_t<Const, const basic_registry<Entity>, basic_registry<Entity>>;
+    using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
 
     /**
      * @brief Constructs a converter for a given registry.
@@ -36,7 +37,7 @@ struct as_view {
      * @return A newly created view.
      */
     template<typename Exclude, typename... Component>
-    operator basic_view<Entity, Exclude, Component...>() const {
+    operator basic_view<entity_type, Exclude, Component...>() const {
         return reg.template view<Component...>(Exclude{});
     }
 
@@ -54,23 +55,24 @@ private:
  * @tparam Entity A valid entity type (see entt_traits for more details).
  */
 template<typename Entity>
-as_view(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<false, Entity>;
+as_view(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<Entity>;
 
 
 /*! @copydoc as_view */
 template<typename Entity>
-as_view(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<true, Entity>;
+as_view(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<const Entity>;
 
 
 /**
  * @brief Converts a registry to a group.
- * @tparam Const Constness of the accepted registry.
  * @tparam Entity A valid entity type (see entt_traits for more details).
  */
-template<bool Const, typename Entity>
+template<typename Entity>
 struct as_group {
+    /*! @brief Underlying entity identifier. */
+    using entity_type = std::remove_const_t<Entity>;
     /*! @brief Type of registry to convert. */
-    using registry_type = std::conditional_t<Const, const basic_registry<Entity>, basic_registry<Entity>>;
+    using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
 
     /**
      * @brief Constructs a converter for a given registry.
@@ -86,7 +88,7 @@ struct as_group {
      * @return A newly created group.
      */
     template<typename Exclude, typename Get, typename... Owned>
-    operator basic_group<Entity, Exclude, Get, Owned...>() const {
+    operator basic_group<entity_type, Exclude, Get, Owned...>() const {
         return reg.template group<Owned...>(Get{}, Exclude{});
     }
 
@@ -104,12 +106,12 @@ private:
  * @tparam Entity A valid entity type (see entt_traits for more details).
  */
 template<typename Entity>
-as_group(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<false, Entity>;
+as_group(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<Entity>;
 
 
 /*! @copydoc as_group */
 template<typename Entity>
-as_group(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<true, Entity>;
+as_group(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<const Entity>;
 
 
 

+ 2 - 2
test/entt/entity/helper.cpp

@@ -17,7 +17,7 @@ TEST(Helper, AsView) {
     ([](entt::view<entt::exclude_t<>, int>) {})(entt::as_view{registry});
     ([](entt::view<entt::exclude_t<int>, char, double>) {})(entt::as_view{registry});
     ([](entt::view<entt::exclude_t<int>, const char, double>) {})(entt::as_view{registry});
-    ([](entt::view<entt::exclude_t<int>, const char, const double>) {})(entt::as_view{registry});
+    ([](entt::view<entt::exclude_t<int>, const char, const double>) {})(entt::as_view{cregistry});
 }
 
 TEST(Helper, AsGroup) {
@@ -26,7 +26,7 @@ TEST(Helper, AsGroup) {
 
     ([](entt::group<entt::exclude_t<int>, entt::get_t<char>, double>) {})(entt::as_group{registry});
     ([](entt::group<entt::exclude_t<int>, entt::get_t<const char>, double>) {})(entt::as_group{registry});
-    ([](entt::group<entt::exclude_t<int>, entt::get_t<const char>, const double>) {})(entt::as_group{registry});
+    ([](entt::group<entt::exclude_t<int>, entt::get_t<const char>, const double>) {})(entt::as_group{cregistry});
 }
 
 TEST(Helper, Invoke) {