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

registry: also support empty queries to get and try_get (with tests)

Michele Caini 3 лет назад
Родитель
Сommit
08adb29d91
2 измененных файлов с 14 добавлено и 2 удалено
  1. 10 2
      src/entt/entity/registry.hpp
  2. 4 0
      test/entt/entity/registry.cpp

+ 10 - 2
src/entt/entity/registry.hpp

@@ -1053,13 +1053,21 @@ public:
      */
     template<typename... Type>
     [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
-        return view<Type...>().template get<const Type...>(entt);
+        if constexpr(sizeof...(Type) == 1u) {
+            return (assure<std::remove_const_t<Type>>().get(entt), ...);
+        } else {
+            return std::forward_as_tuple(get<Type>(entt)...);
+        }
     }
 
     /*! @copydoc get */
     template<typename... Type>
     [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) {
-        return view<Type...>().template get<Type...>(entt);
+        if constexpr(sizeof...(Type) == 1u) {
+            return (const_cast<Type &>(std::as_const(*this).template get<Type>(entt)), ...);
+        } else {
+            return std::forward_as_tuple(get<Type>(entt)...);
+        }
     }
 
     /**

+ 4 - 0
test/entt/entity/registry.cpp

@@ -1894,9 +1894,11 @@ TEST(Registry, Constness) {
     static_assert((std::is_same_v<decltype(registry.emplace<int>({})), int &>));
     static_assert((std::is_same_v<decltype(registry.emplace<empty_type>({})), void>));
 
+    static_assert((std::is_same_v<decltype(registry.get<>({})), std::tuple<>>));
     static_assert((std::is_same_v<decltype(registry.get<int>({})), int &>));
     static_assert((std::is_same_v<decltype(registry.get<int, const char>({})), std::tuple<int &, const char &>>));
 
+    static_assert((std::is_same_v<decltype(registry.try_get<>({})), std::tuple<>>));
     static_assert((std::is_same_v<decltype(registry.try_get<int>({})), int *>));
     static_assert((std::is_same_v<decltype(registry.try_get<int, const char>({})), std::tuple<int *, const char *>>));
 
@@ -1906,9 +1908,11 @@ TEST(Registry, Constness) {
     static_assert((std::is_same_v<decltype(registry.ctx().find<int>()), int *>));
     static_assert((std::is_same_v<decltype(registry.ctx().find<const char>()), const char *>));
 
+    static_assert((std::is_same_v<decltype(std::as_const(registry).get<>({})), std::tuple<>>));
     static_assert((std::is_same_v<decltype(std::as_const(registry).get<int>({})), const int &>));
     static_assert((std::is_same_v<decltype(std::as_const(registry).get<int, const char>({})), std::tuple<const int &, const char &>>));
 
+    static_assert((std::is_same_v<decltype(std::as_const(registry).try_get<>({})), std::tuple<>>));
     static_assert((std::is_same_v<decltype(std::as_const(registry).try_get<int>({})), const int *>));
     static_assert((std::is_same_v<decltype(std::as_const(registry).try_get<int, const char>({})), std::tuple<const int *, const char *>>));