Forráskód Böngészése

utility: as_view (resolve #157)

Michele Caini 7 éve
szülő
commit
781f283c89
3 módosított fájl, 94 hozzáadás és 1 törlés
  1. 3 1
      TODO
  2. 75 0
      src/entt/entity/helper.hpp
  3. 16 0
      test/entt/entity/helper.cpp

+ 3 - 1
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

+ 75 - 0
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<bool Const, typename Entity>
+struct as_view final {
+    /*! @brief Type of registry to convert. */
+    using registry_type = std::conditional_t<Const, const entt::registry<Entity>, entt::registry<Entity>>;
+
+    /**
+     * @brief Constructs a converter for a given registry.
+     * @param reg A valid reference to a registry.
+     */
+    as_view(registry_type &reg) 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<typename... Component>
+    inline operator entt::view<Entity, Component...>() const {
+        return reg.template view<Component...>();
+    }
+
+    /**
+     * @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<typename... Component>
+    inline operator entt::persistent_view<Entity, Component...>() const {
+        return reg.template persistent_view<Component...>();
+    }
+
+    /**
+     * @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<typename Component>
+    inline operator entt::raw_view<Entity, Component>() const {
+        return reg.template raw_view<Component>();
+    }
+
+private:
+    registry_type &reg;
+};
+
+
+/**
+ * @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<typename Entity>
+as_view(registry<Entity> &) ENTT_NOEXCEPT -> as_view<false, Entity>;
+
+
+/**
+ * @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<typename Entity>
+as_view(const registry<Entity> &) ENTT_NOEXCEPT -> as_view<true, Entity>;
+
+
 /**
  * @brief Dependency function prototype.
  *

+ 16 - 0
test/entt/entity/helper.cpp

@@ -3,6 +3,22 @@
 #include <entt/entity/helper.hpp>
 #include <entt/entity/registry.hpp>
 
+TEST(Helper, AsView) {
+    using entity_type = typename entt::registry<>::entity_type;
+
+    entt::registry<> registry;
+    const entt::registry<> cregistry;
+
+    ([](entt::view<entity_type, int, char>) {})(entt::as_view{registry});
+    ([](entt::view<entity_type, const double>) {})(entt::as_view{cregistry});
+
+    ([](entt::persistent_view<entity_type, int, char>) {})(entt::as_view{registry});
+    ([](entt::persistent_view<entity_type, const double, const float>) {})(entt::as_view{cregistry});
+
+    ([](entt::raw_view<entity_type, int>) {})(entt::as_view{registry});
+    ([](entt::raw_view<entity_type, const double>) {})(entt::as_view{cregistry});
+}
+
 TEST(Helper, Dependency) {
     entt::registry<> registry;
     const auto entity = registry.create();