]> code.bitgloo.com Git - clyne/entityx.git/commitdiff
Added operator< overload for Entity class. Allowing use of Entity obects in std:...
authorBen Kelly <ben@benjii.net>
Thu, 7 Aug 2014 12:47:31 +0000 (22:47 +1000)
committerBen Kelly <ben@benjii.net>
Thu, 7 Aug 2014 12:47:31 +0000 (22:47 +1000)
entityx/Entity.h
entityx/Entity_test.cc [changed mode: 0755->0644]

index 7312e25943698055b19905db9416be0b4484b3df..e30f87e6a5e5a44da0e789e0ff976fd43718c6f4 100644 (file)
@@ -102,6 +102,10 @@ public:
     return !(other == *this);
   }
 
+  bool operator < (const Entity &other) const {
+    return other.id_ < id_;
+  }
+
   /**
    * Is this Entity handle valid?
    *
old mode 100755 (executable)
new mode 100644 (file)
index ee1ccac..9140773
@@ -15,6 +15,8 @@
 #include <string>
 #include <utility>
 #include <vector>
+#include <set>
+#include <map>
 #include "entityx/3rdparty/catch.hpp"
 #include "entityx/Entity.h"
 
@@ -23,6 +25,9 @@ using namespace entityx;
 
 using std::ostream;
 using std::vector;
+using std::set;
+using std::map;
+using std::pair;
 using std::string;
 
 template <typename T>
@@ -491,3 +496,26 @@ TEST_CASE_METHOD(EntityManagerFixture, "TestDeleteEntityWithNoComponents") {
   Entity b = em.create();
   b.destroy();
 }
+
+TEST_CASE_METHOD(EntityManagerFixture, "TestEntityInStdSet") {
+  Entity a = em.create();
+  Entity b = em.create();
+  Entity c = em.create();
+  set<Entity> entitySet;
+  REQUIRE(entitySet.insert(a).second);
+  REQUIRE(entitySet.insert(b).second);
+  REQUIRE(entitySet.insert(c).second);
+}
+
+TEST_CASE_METHOD(EntityManagerFixture, "TestEntityInStdMap") {
+  Entity a = em.create();
+  Entity b = em.create();
+  Entity c = em.create();
+  map<Entity, int> entityMap;
+  REQUIRE(entityMap.insert(pair<Entity, int>(a, 1)).second);
+  REQUIRE(entityMap.insert(pair<Entity, int>(b, 2)).second);
+  REQUIRE(entityMap.insert(pair<Entity, int>(c, 3)).second);
+  REQUIRE(entityMap[a] == 1);
+  REQUIRE(entityMap[b] == 2);
+  REQUIRE(entityMap[c] == 3);
+}