Michele Caini 1 год назад
Родитель
Сommit
eabe7be7da
3 измененных файлов с 34 добавлено и 1 удалено
  1. 0 1
      TODO
  2. 9 0
      src/entt/resource/resource.hpp
  3. 25 0
      test/entt/resource/resource.cpp

+ 0 - 1
TODO

@@ -45,4 +45,3 @@ TODO:
 * view and view iterator specializations for multi, single and filtered elements
 * organizer support to groups
 * meta range: move id to meta objects and return plain types (?), then remove id from meta base and meta ctor too
-* resource: swap support

+ 9 - 0
src/entt/resource/resource.hpp

@@ -116,6 +116,15 @@ public:
         return *this;
     }
 
+    /**
+     * @brief Exchanges the content with that of a given resource.
+     * @param other Resource to exchange the content with.
+     */
+    void swap(resource &other) {
+        using std::swap;
+        swap(value, other.value);
+    }
+
     /**
      * @brief Returns a reference to the managed resource.
      *

+ 25 - 0
test/entt/resource/resource.cpp

@@ -70,6 +70,31 @@ TEST(Resource, Functionalities) {
     ASSERT_NE(copy, move);
 }
 
+TEST(Resource, Swap) {
+    entt::resource<int> resource{};
+    entt::resource<int> other{};
+
+    ASSERT_FALSE(resource);
+    ASSERT_FALSE(other);
+
+    resource.swap(other);
+
+    ASSERT_FALSE(resource);
+    ASSERT_FALSE(other);
+
+    resource.reset(std::make_shared<int>(1));
+
+    ASSERT_TRUE(resource);
+    ASSERT_EQ(*resource, 1);
+    ASSERT_FALSE(other);
+
+    resource.swap(other);
+
+    ASSERT_FALSE(resource);
+    ASSERT_TRUE(other);
+    ASSERT_EQ(*other, 1);
+}
+
 TEST(Resource, DerivedToBase) {
     const entt::resource<derived> resource{std::make_shared<derived>()};
     entt::resource<base> other{resource};