Skip to content

Commit

Permalink
resource: swap
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Sep 9, 2024
1 parent 7ad5eb8 commit eabe7be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 9 additions & 0 deletions src/entt/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ class resource {
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.
*
Expand Down
25 changes: 25 additions & 0 deletions test/entt/resource/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit eabe7be

Please sign in to comment.