소스 검색

view: support converting storage views

skypjack 3 달 전
부모
커밋
fc936c3e6f
2개의 변경된 파일34개의 추가작업 그리고 0개의 파일을 삭제
  1. 10 0
      src/entt/entity/view.hpp
  2. 24 0
      test/entt/entity/view.cpp

+ 10 - 0
src/entt/entity/view.hpp

@@ -938,6 +938,16 @@ public:
     basic_view(std::tuple<Get &> value, std::tuple<> = {}) noexcept
         : basic_view{std::get<0>(value)} {}
 
+    /**
+     * @brief Constructs a view from a convertible counterpart.
+     * @tparam OGet Type of storage of the convertible counterpart.
+     * @param other A storage view to convert from.
+     */
+    template<typename OGet, typename = std::enable_if_t<!std::is_same_v<Get, OGet> && std::is_constructible_v<Get, OGet>>>
+    basic_view(const basic_view<get_t<OGet>, exclude_t<>> &other) noexcept
+        : base_type{other} {
+    }
+
     /**
      * @brief Returns the storage for a given element type, if any.
      * @tparam Type Type of element of which to return the storage.

+ 24 - 0
test/entt/entity/view.cpp

@@ -64,6 +64,30 @@ TEST(SingleStorageView, Functionalities) {
     ASSERT_FALSE(invalid);
 }
 
+TEST(SingleStorageView, Conversion) {
+    entt::basic_view<entt::get_t<entt::storage<char>>, entt::exclude_t<>> view{};
+    entt::basic_view<entt::get_t<const entt::storage<char>>, entt::exclude_t<>> cview{view};
+
+    ASSERT_FALSE(view);
+    ASSERT_FALSE(cview);
+
+    entt::storage<char> storage{};
+    view.storage(storage);
+    cview = view;
+
+    ASSERT_TRUE(view);
+    ASSERT_TRUE(cview);
+
+    ASSERT_TRUE(view.empty());
+    ASSERT_TRUE(cview.empty());
+
+    const entt::entity entity{1};
+    storage.emplace(entity, 'c');
+
+    ASSERT_FALSE(view.empty());
+    ASSERT_FALSE(cview.empty());
+}
+
 TEST(SingleStorageView, InvalidView) {
     entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<>> view{};
     auto iterable = view.each();