Skip to content

Commit

Permalink
Update ComponentKey, EntityComponentStorage
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <[email protected]>
  • Loading branch information
chapulina committed Jul 10, 2021
1 parent 45a982a commit e9eeef0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 47 deletions.
6 changes: 4 additions & 2 deletions include/ignition/gazebo/Types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <functional>
#include <utility>

#include "ignition/gazebo/Entity.hh"

namespace ignition
{
namespace gazebo
Expand Down Expand Up @@ -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<ComponentTypeId, ComponentId>;
using ComponentKey = std::pair<ComponentTypeId, Entity>;

/// \brief typedef for query callbacks
using EntityQueryCallback = std::function<void (const UpdateInfo,
Expand Down
6 changes: 3 additions & 3 deletions include/ignition/gazebo/components/Component.hh
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace serializers

namespace detail
{
class EntityStorage;
class EntityComponentStorage;
}

namespace components
Expand Down Expand Up @@ -296,10 +296,10 @@ namespace components
/// users interact with components.
private: bool removed{false};

// Make EntityStorage and EntityComponentManager a friend of the
// Make EntityComponentStorage and EntityComponentManager a friend of the
// BaseComponent class so that they have access to a component's "removed"
// flag.
friend class ignition::gazebo::detail::EntityStorage;
friend class ignition::gazebo::detail::EntityComponentStorage;
friend class ignition::gazebo::EntityComponentManager;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace gazebo
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace detail
{
/// \brief Enum class that lets external users of EntityStorage know what
/// \brief Enum class that lets external users of EntityComponentStorage know what
/// type of action took place when a component was added to an entity.
enum class ComponentAdditionResult
{
Expand All @@ -59,7 +59,7 @@ enum class ComponentAdditionResult
///
/// \note The symbols are visible so that unit tests can be written for this
/// class.
class IGNITION_GAZEBO_VISIBLE EntityStorage
class IGNITION_GAZEBO_VISIBLE EntityComponentStorage
{
/// \brief Clear all of the entity and component data that has been tracked.
/// This "resets" the storage to its initial, empty state.
Expand Down
12 changes: 6 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ set (sources
BaseView.cc
Conversions.cc
EntityComponentManager.cc
EntityStorage.cc
EntityComponentStorage.cc
LevelManager.cc
Link.cc
Model.cc
Expand All @@ -66,24 +66,24 @@ set (gtest_sources
${gtest_sources}
Barrier_TEST.cc
BaseView_TEST.cc
Component_TEST.cc
ComponentFactory_TEST.cc
EntityStorage_TEST.cc
Component_TEST.cc
Conversions_TEST.cc
EntityComponentManager_TEST.cc
EntityComponentStorage_TEST.cc
EventManager_TEST.cc
ign_TEST.cc
Link_TEST.cc
Model_TEST.cc
SdfEntityCreator_TEST.cc
SdfGenerator_TEST.cc
Server_TEST.cc
ServerConfig_TEST.cc
Server_TEST.cc
SimulationRunner_TEST.cc
System_TEST.cc
SystemLoader_TEST.cc
System_TEST.cc
Util_TEST.cc
World_TEST.cc
ign_TEST.cc
network/NetworkConfig_TEST.cc
network/PeerTracker_TEST.cc
network/NetworkManager_TEST.cc
Expand Down
4 changes: 2 additions & 2 deletions src/EntityComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <ignition/math/graph/GraphAlgorithms.hh>
#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;
Expand Down Expand Up @@ -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<ComponentTypeId> createdCompTypes;
Expand Down
25 changes: 13 additions & 12 deletions src/EntityStorage.cc → src/EntityComponentStorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*
*/
#include "ignition/gazebo/detail/EntityStorage.hh"
#include "ignition/gazebo/detail/EntityComponentStorage.hh"

#include <cstdint>
#include <memory>
Expand All @@ -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<std::unique_ptr<components::BaseComponent>>()});
Expand All @@ -51,15 +51,15 @@ 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);
return removedComponents && removedTypeIdxMap;
}

//////////////////////////////////////////////////
ComponentAdditionResult EntityStorage::AddComponent(
ComponentAdditionResult EntityComponentStorage::AddComponent(
const Entity _entity,
std::unique_ptr<components::BaseComponent> _component)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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
{
Expand Down Expand Up @@ -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<components::BaseComponent *>(
static_cast<const EntityStorage &>(*this).Component(_entity, _typeId));
static_cast<const EntityComponentStorage &>(*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);
Expand All @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions src/EntityStorage_TEST.cc → src/EntityComponentStorage_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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/EntityStorage.hh"
#include "ignition/gazebo/detail/EntityComponentStorage.hh"

using namespace ignition;
using namespace gazebo;
Expand All @@ -42,7 +42,7 @@ const Entity entity3 = 3;
const Entity entity4 = 4;

/////////////////////////////////////////////////
class EntityStorageTest : public ::testing::Test
class EntityComponentStorageTest : public ::testing::Test
{
// Documentation inherited
protected: void SetUp() override
Expand All @@ -65,7 +65,7 @@ class EntityStorageTest : public ::testing::Test
return this->storage.AddComponent(_entity, std::move(compPtr));
}

/// \brief Helper function that uses EntityStorage::ValidComponent to get
/// \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
Expand All @@ -87,15 +87,15 @@ class EntityStorageTest : public ::testing::Test
return static_cast<const ComponentTypeT *>(baseComp);
}

public: detail::EntityStorage storage;
public: detail::EntityComponentStorage storage;

public: const Entity e1{1};

public: const Entity e2{2};
};

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, 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.
Expand All @@ -109,7 +109,7 @@ TEST_F(EntityStorageTest, AddEntity)
}

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, RemoveEntity)
TEST_F(EntityComponentStorageTest, RemoveEntity)
{
// Try to remove entities that aren't in the storage
EXPECT_FALSE(this->storage.RemoveEntity(3));
Expand All @@ -125,7 +125,7 @@ TEST_F(EntityStorageTest, RemoveEntity)
}

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, AddComponent)
TEST_F(EntityComponentStorageTest, AddComponent)
{
// Add components to entities in the storage
EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION,
Expand Down Expand Up @@ -166,8 +166,8 @@ TEST_F(EntityStorageTest, AddComponent)
&linVelComp2));

// We can't check if the modification actually took place since this requires
// functionality beyond the EntityStorage API (see the comments in the
// EntityStorage::AddComponent method definition for more details), but we
// 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<components::Pose>(this->e1));
ASSERT_NE(nullptr, this->Component<components::LinearVelocity>(this->e2));
Expand Down Expand Up @@ -195,7 +195,7 @@ TEST_F(EntityStorageTest, AddComponent)
}

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, RemoveComponent)
TEST_F(EntityComponentStorageTest, RemoveComponent)
{
// Add components to entities
EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION,
Expand Down Expand Up @@ -231,7 +231,7 @@ TEST_F(EntityStorageTest, RemoveComponent)
}

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, ValidComponent)
TEST_F(EntityComponentStorageTest, ValidComponent)
{
// Attach a component to an entity
EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION,
Expand Down Expand Up @@ -260,9 +260,9 @@ TEST_F(EntityStorageTest, ValidComponent)

/////////////////////////////////////////////////
// Similar to the ValidComponent test, but this test covers the const version of
// the EntityStorage::ValidComponent method (the ValidComponent test covered
// the non-const version of the EntityStorage::ValidComponent)
TEST_F(EntityStorageTest, 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,
Expand All @@ -285,7 +285,7 @@ TEST_F(EntityStorageTest, ValidComponentConst)
}

/////////////////////////////////////////////////
TEST_F(EntityStorageTest, Reset)
TEST_F(EntityComponentStorageTest, Reset)
{
// Add components to entities in the storage
EXPECT_EQ(detail::ComponentAdditionResult::NEW_ADDITION,
Expand Down
10 changes: 5 additions & 5 deletions test/integration/view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "ignition/gazebo/Entity.hh"
#include "ignition/gazebo/Types.hh"
#include "ignition/gazebo/detail/View.hh"
#include "ignition/gazebo/detail/EntityStorage.hh"
#include "ignition/gazebo/detail/EntityComponentStorage.hh"
#include "ignition/gazebo/components/Component.hh"
#include "ignition/gazebo/components/Factory.hh"
#include "ignition/gazebo/components/Model.hh"
Expand Down Expand Up @@ -62,7 +62,7 @@ class ViewTest : public::testing::Test
}
};

/// \brief Helper class that wraps EntityStorage to provide a simplified
/// \brief Helper class that wraps EntityComponentStorage to provide a simplified
/// API for working with entities and their components. This class also stores
/// a set of user-defined views and handles updating views as needed whenever
/// an entity's components are modified.
Expand Down Expand Up @@ -187,7 +187,7 @@ class StorageViewWrapper
EXPECT_EQ(entityComp->Data(), _component.Data());
}

/// \brief Helper function that uses EntityStorage::ValidComponent to get
/// \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
Expand Down Expand Up @@ -230,8 +230,8 @@ class StorageViewWrapper
return this->storage.AddComponent(_entity, std::move(compPtr));
}

/// \brief The actual detail::EntityStorage instance
private: detail::EntityStorage storage;
/// \brief The actual detail::EntityComponentStorage instance
private: detail::EntityComponentStorage storage;

/// \brief All of the views that will be updated as needed based on the
/// changes being made to this->storage
Expand Down

0 comments on commit e9eeef0

Please sign in to comment.