Browse Source

allow for null entity members (#174)

Michele Caini 7 years ago
parent
commit
4b1d3a7b6f
3 changed files with 20 additions and 14 deletions
  1. 3 3
      TODO
  2. 10 10
      src/entt/entity/snapshot.hpp
  3. 7 1
      test/entt/entity/snapshot.cpp

+ 3 - 3
TODO

@@ -20,8 +20,8 @@
 * travis + windows is now available, try it
 * events on replace, so that one can track updated components? indagate impact
 * tags revenge: if it's possible, reintroduce them but without a link to entities (see #169 for more details)
+* optimize reset (and some others?), direct call on pool in case of no listeners
+* provide create with a pack of default constructible components to assign
 
 Ready to go:
-* (even more) optimized standard views are possible!
-* add on-the-fly sort functionality
-* introduce induce-respect policy
+* policy based views

+ 10 - 10
src/entt/entity/snapshot.hpp

@@ -11,6 +11,7 @@
 #include <unordered_map>
 #include "../config/config.h"
 #include "entt_traits.hpp"
+#include "entity.hpp"
 
 
 namespace entt {
@@ -567,19 +568,18 @@ public:
 
     /**
      * @brief Returns the identifier to which an entity refers.
-     *
-     * @warning
-     * Attempting to use an entity that isn't managed by the loader results in
-     * undefined behavior.<br/>
-     * An assertion will abort the execution at runtime in debug mode if the
-     * loader doesn't knows about the entity.
-     *
      * @param entity An entity identifier.
-     * @return The identifier to which `entity` refers in the target registry.
+     * @return The local identifier if any, the null entity otherwise.
      */
     entity_type map(entity_type entity) const ENTT_NOEXCEPT {
-        assert(has(entity));
-        return remloc.find(entity)->second.first;
+        const auto it = remloc.find(entity);
+        entity_type other = null;
+
+        if(it != remloc.cend()) {
+            other = it->second.first;
+        }
+
+        return other;
     }
 
 private:

+ 7 - 1
test/entt/entity/snapshot.cpp

@@ -3,6 +3,7 @@
 #include <vector>
 #include <gtest/gtest.h>
 #include <entt/entity/registry.hpp>
+#include <entt/entity/entity.hpp>
 
 template<typename Storage>
 struct output_archive {
@@ -427,7 +428,7 @@ TEST(Snapshot, Continuous) {
     ASSERT_EQ(dst.size<a_component>(), a_component_cnt);
 }
 
-TEST(Snapshot, ContinuousMoreOnShrink) {
+TEST(Snapshot, MoreOnShrink) {
     using entity_type = entt::registry<>::entity_type;
 
     entt::registry<> src;
@@ -480,15 +481,20 @@ TEST(Snapshot, SyncDataMembers) {
     auto parent = src.create();
     auto child = src.create();
 
+    src.assign<what_a_component>(parent, entt::null);
     src.assign<what_a_component>(child, parent).quux.push_back(child);
+
     src.snapshot().entities(output).component<what_a_component>(output);
     loader.entities(input).component<what_a_component>(input, &what_a_component::bar, &what_a_component::quux);
 
     ASSERT_FALSE(dst.valid(parent));
     ASSERT_FALSE(dst.valid(child));
 
+    ASSERT_TRUE(dst.has<what_a_component>(loader.map(parent)));
     ASSERT_TRUE(dst.has<what_a_component>(loader.map(child)));
 
+    ASSERT_EQ(dst.get<what_a_component>(loader.map(parent)).bar, static_cast<entity_type>(entt::null));
+
     const auto &component = dst.get<what_a_component>(loader.map(child));
 
     ASSERT_EQ(component.bar, loader.map(parent));