Просмотр исходного кода

helper: added to_entity function that returns the entity associated with a given component

Michele Caini 5 лет назад
Родитель
Сommit
1b5295a8fe
3 измененных файлов с 49 добавлено и 1 удалено
  1. 15 0
      docs/md/entity.md
  2. 14 0
      src/entt/entity/helper.hpp
  3. 20 1
      test/entt/entity/helper.cpp

+ 15 - 0
docs/md/entity.md

@@ -19,6 +19,7 @@
   * [Sorting: is it possible?](#sorting-is-it-possible)
   * [Helpers](#helpers)
     * [Null entity](#null-entity)
+    * [To entity](#to-entity)
     * [Dependencies](#dependencies)
     * [Invoke](#invoke)
     * [Handle](#handle)
@@ -603,6 +604,20 @@ initialized entity isn't the same as `entt::null`. Therefore, although
 `entt::entity{}` is in some sense an alias for entity 0, none of them can be
 used to create a null entity.
 
+### To entity
+
+Sometimes it's useful to get the entity from a component instance.<br/>
+This is what the `entt::to_entity` helper does. It accepts a registry and an
+instance of a component and returns the entity associated with the latter:
+
+```cpp
+const auto entity = entt::to_entity(registry, position);
+```
+
+This utility doesn't perform any check on the validity of the component.
+Therefore, trying to take the entity of an invalid element or of an instance
+that isn't associated with the given registry can result in undefined behavior.
+
 ### Dependencies
 
 The `registry` class is designed to be able to create short circuits between its

+ 14 - 0
src/entt/entity/helper.hpp

@@ -129,6 +129,20 @@ void invoke(basic_registry<Entity> &reg, const Entity entt) {
 }
 
 
+/**
+ * @brief Returns the entity associated with a given component.
+ * @tparam Entity A valid entity type (see entt_traits for more details).
+ * @tparam Component Type of component.
+ * @param reg A registry that contains the given entity and its components.
+ * @param component A valid component instance.
+ * @return The entity associated with the given component.
+ */
+template<typename Entity, typename Component>
+Entity to_entity(const basic_registry<Entity> &reg, const Component &component) {
+    return *(reg.template data<Component>() + (&component - reg.template raw<Component>()));
+}
+
+
 }
 
 

+ 20 - 1
test/entt/entity/helper.cpp

@@ -29,7 +29,7 @@ TEST(Helper, AsGroup) {
     ([](entt::group<entt::exclude_t<int>, entt::get_t<const char>, const double>) {})(entt::as_group{registry});
 }
 
-TEST(Invoke, MemberFunction) {
+TEST(Helper, Invoke) {
     entt::registry registry;
     const auto entity = registry.create();
 
@@ -38,3 +38,22 @@ TEST(Invoke, MemberFunction) {
 
     ASSERT_EQ(entity, registry.get<clazz>(entity).entt);
 }
+
+TEST(Helper, ToEntity) {
+    entt::registry registry;
+    const auto entity = registry.create();
+    const auto other = registry.create();
+
+    registry.emplace<int>(entity);
+    registry.emplace<int>(other);
+    registry.emplace<char>(other);
+
+    ASSERT_EQ(entt::to_entity(registry, registry.get<int>(entity)), entity);
+    ASSERT_EQ(entt::to_entity(registry, registry.get<int>(other)), other);
+    ASSERT_EQ(entt::to_entity(registry, registry.get<char>(other)), other);
+
+    registry.destroy(entity);
+
+    ASSERT_EQ(entt::to_entity(registry, registry.get<int>(other)), other);
+    ASSERT_EQ(entt::to_entity(registry, registry.get<char>(other)), other);
+}