Parcourir la source

added actor::operator bool() + minor changes

Michele Caini il y a 6 ans
Parent
commit
7a3fa68468
4 fichiers modifiés avec 27 ajouts et 3 suppressions
  1. 1 0
      TODO
  2. 12 2
      src/entt/entity/actor.hpp
  3. 1 1
      src/entt/entity/prototype.hpp
  4. 13 0
      test/entt/entity/actor.cpp

+ 1 - 0
TODO

@@ -22,5 +22,6 @@
   - standard each, use bitmask to speed up the whole thing and avoid accessing the pools to test for the page
   - iterator based each with a couple of iterators passed from outside (use bitmask + has)
 * stable component handle that isn't affected by reallocations
+* default constructor + init/re-init function to actor
 * multi component registry::remove and some others?
 * built-in support for dual (or N-) buffering

+ 12 - 2
src/entt/entity/actor.hpp

@@ -33,13 +33,15 @@ struct basic_actor {
      * @brief Constructs an actor by using the given registry.
      * @param ref An entity-component system properly initialized.
      */
-    basic_actor(registry_type &ref)
+    explicit basic_actor(registry_type &ref)
         : reg{&ref}, entt{ref.create()}
     {}
 
     /*! @brief Default destructor. */
     virtual ~basic_actor() {
-        reg->destroy(entt);
+        if(*this) {
+            reg->destroy(entt);
+        }
     }
 
     /**
@@ -168,6 +170,14 @@ struct basic_actor {
         return entt;
     }
 
+    /**
+     * @brief Checks if an actor refers to a valid entity or not.
+     * @return True if the actor refers to a valid entity, false otherwise.
+     */
+    explicit operator bool() const ENTT_NOEXCEPT {
+        return reg && reg->valid(entt);
+    }
+
 private:
     registry_type *reg;
     Entity entt;

+ 1 - 1
src/entt/entity/prototype.hpp

@@ -68,7 +68,7 @@ public:
      * @brief Constructs a prototype that is bound to a given registry.
      * @param ref A valid reference to a registry.
      */
-    basic_prototype(registry_type &ref)
+    explicit basic_prototype(registry_type &ref)
         : reg{&ref},
           entity{ref.create()}
     {}

+ 13 - 0
test/entt/entity/actor.cpp

@@ -41,6 +41,19 @@ TEST(Actor, Component) {
 }
 
 TEST(Actor, EntityLifetime) {
+    entt::registry registry;
+    entt::actor actor{registry};
+    actor.assign<int>();
+
+    ASSERT_TRUE(actor);
+    ASSERT_TRUE(actor);
+
+    registry.destroy(actor.entity());
+
+    ASSERT_FALSE(actor);
+}
+
+TEST(Actor, ActorLifetime) {
     entt::registry registry;
     auto *actor = new entt::actor{registry};
     actor->assign<int>();