From 5bb0efce092725058134701d0420c93bcdc000a9 Mon Sep 17 00:00:00 2001 From: Louise Poubel Date: Fri, 9 Jul 2021 15:14:27 -0700 Subject: [PATCH] Update ComponentKey, EntityComponentStorage Signed-off-by: Louise Poubel --- include/ignition/gazebo/Types.hh | 6 ++-- .../ignition/gazebo/components/Component.hh | 6 ++-- ...tyStorage.hh => EntityComponentStorage.hh} | 4 +-- src/CMakeLists.txt | 12 +++---- src/EntityComponentManager.cc | 4 +-- ...tyStorage.cc => EntityComponentStorage.cc} | 25 ++++++------- ...TEST.cc => EntityComponentStorage_TEST.cc} | 36 ++++++++++--------- test/integration/view.cc | 7 ++-- 8 files changed, 53 insertions(+), 47 deletions(-) rename include/ignition/gazebo/detail/{EntityStorage.hh => EntityComponentStorage.hh} (98%) rename src/{EntityStorage.cc => EntityComponentStorage.cc} (87%) rename src/{ComponentStorage_TEST.cc => EntityComponentStorage_TEST.cc} (92%) diff --git a/include/ignition/gazebo/Types.hh b/include/ignition/gazebo/Types.hh index ee0a75e1065..d30ae00d067 100644 --- a/include/ignition/gazebo/Types.hh +++ b/include/ignition/gazebo/Types.hh @@ -22,6 +22,8 @@ #include #include +#include "ignition/gazebo/Entity.hh" + namespace ignition { namespace gazebo @@ -89,10 +91,10 @@ namespace ignition /// \brief A key that uniquely identifies, at the global scope, a component /// instance - /// \note On version 6, the 2nd element equals the entity ID. + /// \note On version 6, the 2nd element was changed to the entity ID. /// \deprecated Deprecated on version 6, removed on version 7. Use /// ComponentTypeId + Entity instead. - using ComponentKey = std::pair; + using ComponentKey = std::pair; /// \brief typedef for query callbacks using EntityQueryCallback = std::function #include "ignition/gazebo/components/Component.hh" #include "ignition/gazebo/components/Factory.hh" -#include "ignition/gazebo/detail/EntityStorage.hh" +#include "ignition/gazebo/detail/EntityComponentStorage.hh" #include "ignition/gazebo/EntityComponentManager.hh" using namespace ignition; @@ -78,7 +78,7 @@ class ignition::gazebo::EntityComponentManagerPrivate /// \brief A class that stores all components and maps entities to their /// component types - public: detail::EntityStorage entityStorage; + public: detail::EntityComponentStorage entityStorage; /// \brief All component types that have ever been created. public: std::unordered_set createdCompTypes; diff --git a/src/EntityStorage.cc b/src/EntityComponentStorage.cc similarity index 87% rename from src/EntityStorage.cc rename to src/EntityComponentStorage.cc index 51c694eff2b..495441342e1 100644 --- a/src/EntityStorage.cc +++ b/src/EntityComponentStorage.cc @@ -14,7 +14,7 @@ * limitations under the License. * */ -#include "ignition/gazebo/detail/EntityStorage.hh" +#include "ignition/gazebo/detail/EntityComponentStorage.hh" #include #include @@ -30,14 +30,14 @@ using namespace gazebo; using namespace detail; ////////////////////////////////////////////////// -void EntityStorage::Reset() +void EntityComponentStorage::Reset() { this->entityComponents.clear(); this->componentTypeIndex.clear(); } ////////////////////////////////////////////////// -bool EntityStorage::AddEntity(const Entity _entity) +bool EntityComponentStorage::AddEntity(const Entity _entity) { const auto [it, success] = this->entityComponents.insert({_entity, std::vector>()}); @@ -51,7 +51,7 @@ bool EntityStorage::AddEntity(const Entity _entity) } ////////////////////////////////////////////////// -bool EntityStorage::RemoveEntity(const Entity _entity) +bool EntityComponentStorage::RemoveEntity(const Entity _entity) { const auto removedComponents = this->entityComponents.erase(_entity); const auto removedTypeIdxMap = this->componentTypeIndex.erase(_entity); @@ -59,7 +59,7 @@ bool EntityStorage::RemoveEntity(const Entity _entity) } ////////////////////////////////////////////////// -ComponentAdditionResult EntityStorage::AddComponent( +ComponentAdditionResult EntityComponentStorage::AddComponent( const Entity _entity, std::unique_ptr _component) { @@ -101,7 +101,7 @@ ComponentAdditionResult EntityStorage::AddComponent( } ////////////////////////////////////////////////// -bool EntityStorage::RemoveComponent(const Entity _entity, +bool EntityComponentStorage::RemoveComponent(const Entity _entity, const ComponentTypeId _typeId) { auto compPtr = this->Component(_entity, _typeId); @@ -116,7 +116,7 @@ bool EntityStorage::RemoveComponent(const Entity _entity, } ////////////////////////////////////////////////// -const components::BaseComponent *EntityStorage::Component( +const components::BaseComponent *EntityComponentStorage::Component( const Entity _entity, const ComponentTypeId _typeId) const { @@ -144,15 +144,16 @@ const components::BaseComponent *EntityStorage::Component( } ////////////////////////////////////////////////// -components::BaseComponent *EntityStorage::Component(const Entity _entity, - const ComponentTypeId _typeId) +components::BaseComponent *EntityComponentStorage::Component( + const Entity _entity, const ComponentTypeId _typeId) { return const_cast( - static_cast(*this).Component(_entity, _typeId)); + static_cast(*this).Component(_entity, + _typeId)); } ////////////////////////////////////////////////// -const components::BaseComponent *EntityStorage::ValidComponent( +const components::BaseComponent *EntityComponentStorage::ValidComponent( const Entity _entity, const ComponentTypeId _typeId) const { auto compPtr = this->Component(_entity, _typeId); @@ -162,7 +163,7 @@ const components::BaseComponent *EntityStorage::ValidComponent( } ////////////////////////////////////////////////// -components::BaseComponent *EntityStorage::ValidComponent( +components::BaseComponent *EntityComponentStorage::ValidComponent( const Entity _entity, const ComponentTypeId _typeId) { auto compPtr = this->Component(_entity, _typeId); diff --git a/src/ComponentStorage_TEST.cc b/src/EntityComponentStorage_TEST.cc similarity index 92% rename from src/ComponentStorage_TEST.cc rename to src/EntityComponentStorage_TEST.cc index 49f125f0310..e5b339d8207 100644 --- a/src/ComponentStorage_TEST.cc +++ b/src/EntityComponentStorage_TEST.cc @@ -26,7 +26,7 @@ #include "ignition/gazebo/components/Factory.hh" #include "ignition/gazebo/components/LinearVelocity.hh" #include "ignition/gazebo/components/Pose.hh" -#include "ignition/gazebo/detail/ComponentStorage.hh" +#include "ignition/gazebo/detail/EntityComponentStorage.hh" using namespace ignition; using namespace gazebo; @@ -42,7 +42,7 @@ const Entity entity3 = 3; const Entity entity4 = 4; ///////////////////////////////////////////////// -class ComponentStorageTest : public ::testing::Test +class EntityComponentStorageTest : public ::testing::Test { // Documentation inherited protected: void SetUp() override @@ -65,8 +65,8 @@ class ComponentStorageTest : public ::testing::Test return this->storage.AddComponent(_entity, std::move(compPtr)); } - /// \brief Helper function that uses ComponentStorage::ValidComponent to get - /// a component of a particular type that belongs to an entity. + /// \brief Helper function that uses EntityComponentStorage::ValidComponent + /// to get a component of a particular type that belongs to an entity. /// \param[in] _entity The entity /// \return A pointer to the component of the templated type. If no such /// component exists, nullptr is returned @@ -87,7 +87,7 @@ class ComponentStorageTest : public ::testing::Test return static_cast(baseComp); } - public: detail::ComponentStorage storage; + public: detail::EntityComponentStorage storage; public: const Entity e1{1}; @@ -95,7 +95,7 @@ class ComponentStorageTest : public ::testing::Test }; ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, AddEntity) +TEST_F(EntityComponentStorageTest, AddEntity) { // Entities were already added to the storage in the test fixture's SetUp // method. So, try to add entities that are already in the storage. @@ -109,7 +109,7 @@ TEST_F(ComponentStorageTest, AddEntity) } ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, RemoveEntity) +TEST_F(EntityComponentStorageTest, RemoveEntity) { // Try to remove entities that aren't in the storage EXPECT_FALSE(this->storage.RemoveEntity(3)); @@ -125,7 +125,7 @@ TEST_F(ComponentStorageTest, RemoveEntity) } ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, AddComponent) +TEST_F(EntityComponentStorageTest, AddComponent) { // Add components to entities in the storage EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION, @@ -166,9 +166,10 @@ TEST_F(ComponentStorageTest, AddComponent) &linVelComp2)); // We can't check if the modification actually took place since this requires - // functionality beyond the ComponentStorage API (see the comments in the - // ComponentStorage::AddComponent method definition for more details), but we - // can at least check that the components still exist after modification + // functionality beyond the EntityComponentStorage API (see the comments in + // the EntityComponentStorage::AddComponent method definition for more + // details), but we can at least check that the components still exist after + // modification ASSERT_NE(nullptr, this->Component(this->e1)); ASSERT_NE(nullptr, this->Component(this->e2)); @@ -195,7 +196,7 @@ TEST_F(ComponentStorageTest, AddComponent) } ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, RemoveComponent) +TEST_F(EntityComponentStorageTest, RemoveComponent) { // Add components to entities EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION, @@ -231,7 +232,7 @@ TEST_F(ComponentStorageTest, RemoveComponent) } ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, ValidComponent) +TEST_F(EntityComponentStorageTest, ValidComponent) { // Attach a component to an entity EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION, @@ -260,9 +261,10 @@ TEST_F(ComponentStorageTest, ValidComponent) ///////////////////////////////////////////////// // Similar to the ValidComponent test, but this test covers the const version of -// the ComponentStorage::ValidComponent method (the ValidComponent test covered -// the non-const version of the ComponentStorage::ValidComponent) -TEST_F(ComponentStorageTest, ValidComponentConst) +// the EntityComponentStorage::ValidComponent method (the ValidComponent test +// covered +// the non-const version of the EntityComponentStorage::ValidComponent) +TEST_F(EntityComponentStorageTest, ValidComponentConst) { // Attach a component to an entity EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION, @@ -285,7 +287,7 @@ TEST_F(ComponentStorageTest, ValidComponentConst) } ///////////////////////////////////////////////// -TEST_F(ComponentStorageTest, Reset) +TEST_F(EntityComponentStorageTest, Reset) { // Add components to entities in the storage EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION, diff --git a/test/integration/view.cc b/test/integration/view.cc index 7d8fd0af801..4401912ba47 100644 --- a/test/integration/view.cc +++ b/test/integration/view.cc @@ -18,14 +18,15 @@ #include #include "ignition/gazebo/detail/View.hh" -#include "ignition/gazebo/detail/ComponentStorage.hh" +#include "ignition/gazebo/detail/EntityComponentStorage.hh" #include "ignition/gazebo/components/Model.hh" #include "ignition/gazebo/components/Name.hh" #include "ignition/gazebo/components/Sensor.hh" #include "ignition/gazebo/components/Visual.hh" -// TODO(adlarkin) write tests for the view class. Can use ComponentStorage as a -// helper to make sure that updating entities and components in ComponentStorage +// TODO(adlarkin) write tests for the view class. Can use +// EntityComponentStorage as a helper to make sure that updating entities +// and components in EntityComponentStorage // results in the views being updated properly as well // // Can create entities that have a few different sets of components: