diff --git a/TODO b/TODO index 101390dc4..536f4abd0 100644 --- a/TODO +++ b/TODO @@ -20,4 +20,6 @@ * destroy overload that accepts a couple of iterators (see create) * allow for built-in parallel each if possible * add on-the-fly sort functionality (is it possible?) -* add support for composable filters +* write/show how to work with missing components using and-strategies +* write/show how to create an archetype based model on top of EnTT +* mention hunter and conan in the readme file, section packaging tools diff --git a/src/entt/entity/helper.hpp b/src/entt/entity/helper.hpp index 7a8519867..9c466273b 100644 --- a/src/entt/entity/helper.hpp +++ b/src/entt/entity/helper.hpp @@ -11,6 +11,81 @@ namespace entt { +/** + * @brief Converts a registry to a view. + * @tparam Const Constness of the accepted registry. + * @tparam Entity A valid entity type (see entt_traits for more details). + */ +template +struct as_view final { + /*! @brief Type of registry to convert. */ + using registry_type = std::conditional_t, entt::registry>; + + /** + * @brief Constructs a converter for a given registry. + * @param reg A valid reference to a registry. + */ + as_view(registry_type ®) ENTT_NOEXCEPT: reg{reg} {} + + /** + * @brief Conversion function from a registry to a view. + * @tparam Component Type of components used to construct the view. + * @return A newly created standard view. + */ + template + inline operator entt::view() const { + return reg.template view(); + } + + /** + * @brief Conversion function from a registry to a persistent view. + * @tparam Component Types of components used to construct the view. + * @return A newly created persistent view. + */ + template + inline operator entt::persistent_view() const { + return reg.template persistent_view(); + } + + /** + * @brief Conversion function from a registry to a raw view. + * @tparam Component Type of component used to construct the view. + * @return A newly created raw view. + */ + template + inline operator entt::raw_view() const { + return reg.template raw_view(); + } + +private: + registry_type ® +}; + + +/** + * @brief Deduction guideline. + * + * It allows to deduce the constness of a registry directly from the instance + * provided to the constructor. + * + * @tparam Entity A valid entity type (see entt_traits for more details). + */ +template +as_view(registry &) ENTT_NOEXCEPT -> as_view; + + +/** + * @brief Deduction guideline. + * + * It allows to deduce the constness of a registry directly from the instance + * provided to the constructor. + * + * @tparam Entity A valid entity type (see entt_traits for more details). + */ +template +as_view(const registry &) ENTT_NOEXCEPT -> as_view; + + /** * @brief Dependency function prototype. * diff --git a/test/entt/entity/helper.cpp b/test/entt/entity/helper.cpp index c51ab0841..d9311d6c3 100644 --- a/test/entt/entity/helper.cpp +++ b/test/entt/entity/helper.cpp @@ -3,6 +3,22 @@ #include #include +TEST(Helper, AsView) { + using entity_type = typename entt::registry<>::entity_type; + + entt::registry<> registry; + const entt::registry<> cregistry; + + ([](entt::view) {})(entt::as_view{registry}); + ([](entt::view) {})(entt::as_view{cregistry}); + + ([](entt::persistent_view) {})(entt::as_view{registry}); + ([](entt::persistent_view) {})(entt::as_view{cregistry}); + + ([](entt::raw_view) {})(entt::as_view{registry}); + ([](entt::raw_view) {})(entt::as_view{cregistry}); +} + TEST(Helper, Dependency) { entt::registry<> registry; const auto entity = registry.create();