Skip to content

Commit

Permalink
added tests for View and BaseView classes
Browse files Browse the repository at this point in the history
Signed-off-by: Ashton Larkin <[email protected]>
  • Loading branch information
adlarkin committed Jul 9, 2021
1 parent 40101a8 commit ee3d3d0
Show file tree
Hide file tree
Showing 9 changed files with 1,239 additions and 10 deletions.
7 changes: 5 additions & 2 deletions include/ignition/gazebo/detail/BaseView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ class IGNITION_GAZEBO_VISIBLE BaseView
/// added to an entity. It is assumed that the entity is already associated
/// with the view, and that the added component type is required by the view.
/// \param[in] _entity The entity
/// \param[in] _newEntity Whether to add the entity to the list of new
/// entities. The new here is to indicate whether the entity is new to the
/// entity component manager.
/// \param[in] _typeId The type of component that was added to _entity
/// \return true if the notification related to the component addition of type
/// _typeId occurred for _entity, false otherwise
/// \sa HasCachedComponentData, RequiresComponent
public: virtual bool NotifyComponentAddition(const Entity _entity,
const ComponentTypeId _typeId) = 0;
bool _newEntity, const ComponentTypeId _typeId) = 0;

/// \brief Update the internal data in the view because a component has been
/// removed to an entity. It is assumed that the entity is already associated
Expand All @@ -132,7 +135,7 @@ class IGNITION_GAZEBO_VISIBLE BaseView
/// \param[in] _entity The entity to add.
/// \return True if the entity was added to the list, false if the entity
/// was not associated with the view.
public: bool AddEntityToRemoved(const Entity _entity);
public: bool MarkEntityToRemove(const Entity _entity);

/// \brief Update the entities in the view to no longer appear as newly
/// created. This method should be called whenever a new simulation step is
Expand Down
2 changes: 1 addition & 1 deletion include/ignition/gazebo/detail/EntityComponentManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ detail::View<ComponentTypeTs...> *EntityComponentManager::FindView() const
const_cast<EntityComponentManager*>(this)->Component<ComponentTypeTs>(
entity)...);
if (this->IsMarkedForRemoval(entity))
view.AddEntityToRemoved(entity);
view.MarkEntityToRemove(entity);
}

baseViewPtr = this->AddView(viewKey,
Expand Down
31 changes: 28 additions & 3 deletions include/ignition/gazebo/detail/View.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <unordered_set>
#include <utility>

#include <ignition/common/Console.hh>

#include "ignition/gazebo/Entity.hh"
#include "ignition/gazebo/config.hh"
#include "ignition/gazebo/detail/BaseView.hh"
Expand Down Expand Up @@ -55,8 +57,25 @@ class IGNITION_GAZEBO_VISIBLE View : public BaseView
/// \brief Documentation inherited
public: bool HasCachedComponentData(const Entity _entity) const override
{
return this->validData.find(_entity) != this->validData.end() ||
auto cachedComps =
this->validData.find(_entity) != this->validData.end() ||
this->invalidData.find(_entity) != this->invalidData.end();
auto cachedConstComps =
this->validConstData.find(_entity) != this->validConstData.end() ||
this->invalidConstData.find(_entity) != this->invalidConstData.end();

if (cachedComps && !cachedConstComps)
{
ignwarn << "Non-const component data is cached for entity " << _entity
<< ", but const component data is not cached." << std::endl;
}
else if (cachedConstComps && !cachedComps)
{
ignwarn << "Const component data is cached for entity " << _entity
<< ", but non-const component data is not cached." << std::endl;
}

return cachedComps && cachedConstComps;
}

/// \brief Documentation inherited
Expand Down Expand Up @@ -135,7 +154,7 @@ class IGNITION_GAZEBO_VISIBLE View : public BaseView
}

/// \brief Documentation inherited
public: bool NotifyComponentAddition(const Entity _entity,
public: bool NotifyComponentAddition(const Entity _entity, bool _newEntity,
const ComponentTypeId _typeId) override
{
// make sure that _typeId is a type required by the view and that _entity is
Expand All @@ -148,7 +167,10 @@ class IGNITION_GAZEBO_VISIBLE View : public BaseView
// list
auto missingCompsIter = this->missingCompTracker.find(_entity);
if (missingCompsIter == this->missingCompTracker.end())
return false;
{
// the component is already added, so nothing else needs to be done
return true;
}
missingCompsIter->second.erase(_typeId);

// if the entity now has all components that meet the requirements of the
Expand All @@ -160,6 +182,8 @@ class IGNITION_GAZEBO_VISIBLE View : public BaseView
auto constCompNh = this->invalidConstData.extract(_entity);
this->validConstData.insert(std::move(constCompNh));
this->entities.insert(_entity);
if (_newEntity)
this->newEntities.insert(_entity);
this->missingCompTracker.erase(_entity);
}

Expand Down Expand Up @@ -189,6 +213,7 @@ class IGNITION_GAZEBO_VISIBLE View : public BaseView
auto constCompNh = this->validConstData.extract(constCompIt);
this->invalidConstData.insert(std::move(constCompNh));
this->entities.erase(_entity);
this->newEntities.erase(_entity);
}

this->missingCompTracker[_entity].insert(_typeId);
Expand Down
2 changes: 1 addition & 1 deletion src/BaseView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool BaseView::RequiresComponent(const ComponentTypeId _typeId) const
}

//////////////////////////////////////////////////
bool BaseView::AddEntityToRemoved(const Entity _entity)
bool BaseView::MarkEntityToRemove(const Entity _entity)
{
if (this->HasCachedComponentData(_entity) ||
this->IsEntityMarkedForAddition(_entity))
Expand Down
Loading

0 comments on commit ee3d3d0

Please sign in to comment.