view: default constructible views

This commit is contained in:
Michele Caini
2020-11-09 18:39:28 +01:00
parent f8ec4027ee
commit cd98ecff88
2 changed files with 40 additions and 2 deletions

View File

@@ -284,6 +284,11 @@ public:
/*! @brief Reverse iterator type. */
using reverse_iterator = view_iterator<typename basic_sparse_set<entity_type>::reverse_iterator>;
/*! @brief Default constructor to use to create empty, invalid views. */
basic_view() ENTT_NOEXCEPT
: view{}
{}
/**
* @brief Constructs a multi-type view from a set of storage classes.
* @param component The storage for the types to iterate.
@@ -395,6 +400,14 @@ public:
return (it != end() && *it == entt) ? it : end();
}
/**
* @brief Checks if a view is properly initialized.
* @return True if the view is properly initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
return std::get<0>(pools) != nullptr;
}
/**
* @brief Checks if a view contains an entity.
* @param entt A valid entity identifier.
@@ -520,7 +533,7 @@ public:
private:
const std::tuple<storage_type<Component> *...> pools;
const std::tuple<const storage_type<Exclude> *...> filter;
mutable const basic_sparse_set<entity_type>* view;
mutable const basic_sparse_set<entity_type> *view;
};
@@ -651,6 +664,11 @@ public:
/*! @brief Reversed iterator type. */
using reverse_iterator = typename basic_sparse_set<Entity>::reverse_iterator;
/*! @brief Default constructor to use to create empty, invalid views. */
basic_view() ENTT_NOEXCEPT
: pool{}
{}
/**
* @brief Constructs a single-type view from a storage class.
* @param ref The storage for the type to iterate.
@@ -791,6 +809,14 @@ public:
return begin()[pos];
}
/**
* @brief Checks if a view is properly initialized.
* @return True if the view is properly initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
return pool != nullptr;
}
/**
* @brief Checks if a view contains an entity.
* @param entt A valid entity identifier.
@@ -894,7 +920,7 @@ public:
}
private:
storage_type *pool;
storage_type * const pool;
};

View File

@@ -55,6 +55,12 @@ TEST(SingleComponentView, Functionalities) {
ASSERT_EQ(view.begin(), view.end());
ASSERT_EQ(view.rbegin(), view.rend());
ASSERT_TRUE(view.empty());
decltype(view) invalid{};
ASSERT_TRUE(view);
ASSERT_TRUE(cview);
ASSERT_FALSE(invalid);
}
TEST(SingleComponentView, ElementAccess) {
@@ -349,6 +355,12 @@ TEST(MultiComponentView, Functionalities) {
ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
ASSERT_EQ(cview.get<const char>(entity), '2');
}
decltype(view) invalid{};
ASSERT_TRUE(view);
ASSERT_TRUE(cview);
ASSERT_FALSE(invalid);
}
TEST(MultiComponentView, Iterator) {