Gazebo Gazebo

API Reference

6.16.0
gz/sim/EntityComponentManager.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
18 #define GZ_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
19 
20 #include <gz/msgs/serialized.pb.h>
21 #include <gz/msgs/serialized_map.pb.h>
22 
23 #include <map>
24 #include <memory>
25 #include <optional>
26 #include <set>
27 #include <string>
28 #include <typeinfo>
29 #include <type_traits>
30 #include <unordered_set>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
35 #include <gz/common/Console.hh>
36 #include <gz/math/graph/Graph.hh>
37 #include "gz/sim/Entity.hh"
38 #include "gz/sim/Export.hh"
39 #include "gz/sim/Types.hh"
40 
42 #include "gz/sim/detail/View.hh"
43 
44 namespace ignition
45 {
46  namespace gazebo
47  {
48  // Inline bracket to help doxygen filtering.
49  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
50  // Forward declarations.
51  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
52 
58 
62  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
67  {
69  public: EntityComponentManager();
70 
72  public: ~EntityComponentManager();
73 
76  public: Entity CreateEntity();
77 
110  public: Entity Clone(Entity _entity, Entity _parent,
111  const std::string &_name, bool _allowRename);
112 
115  public: size_t EntityCount() const;
116 
128  public: void RequestRemoveEntity(const Entity _entity,
129  bool _recursive = true);
130 
146  public: void PinEntity(const Entity _entity, bool _recursive = true);
147 
154  public: void UnpinEntity(const Entity _entity, bool _recursive = true);
155 
158  public: void UnpinAllEntities();
159 
163  public: void RequestRemoveEntities();
164 
168  public: bool HasEntity(const Entity _entity) const;
169 
176  public: Entity ParentEntity(const Entity _entity) const;
177 
188  public: bool SetParentEntity(const Entity _child, const Entity _parent);
189 
193  public: bool HasComponentType(const ComponentTypeId _typeId) const;
194 
199  public: bool IGN_DEPRECATED(6) EntityHasComponent(const Entity _entity,
200  const ComponentKey &_key) const;
201 
207  public: bool EntityHasComponentType(const Entity _entity,
208  const ComponentTypeId &_typeId) const;
209 
214  public: bool EntityMatches(Entity _entity,
215  const std::set<ComponentTypeId> &_types) const;
216 
222  public: bool IGN_DEPRECATED(6) RemoveComponent(
223  const Entity _entity, const ComponentKey &_key);
224 
230  public: bool RemoveComponent(
231  const Entity _entity, const ComponentTypeId &_typeId);
232 
238  public: template<typename ComponentTypeT>
239  bool RemoveComponent(Entity _entity);
240 
243  public: void RebuildViews();
244 
253  public: template<typename ComponentTypeT>
254  ComponentTypeT *CreateComponent(
255  const Entity _entity,
256  const ComponentTypeT &_data);
257 
263  public: template<typename ComponentTypeT>
264  const ComponentTypeT *Component(const Entity _entity) const;
265 
271  public: template<typename ComponentTypeT>
272  ComponentTypeT *Component(const Entity _entity);
273 
278  public: template<typename ComponentTypeT>
279  const ComponentTypeT IGN_DEPRECATED(6) * Component(
280  const ComponentKey &_key) const;
281 
286  public: template<typename ComponentTypeT>
287  ComponentTypeT IGN_DEPRECATED(6) * Component(
288  const ComponentKey &_key);
289 
298  public: template<typename ComponentTypeT>
299  ComponentTypeT *ComponentDefault(Entity _entity,
300  const typename ComponentTypeT::Type &_default =
301  typename ComponentTypeT::Type());
302 
311  public: template<typename ComponentTypeT>
312  std::optional<typename ComponentTypeT::Type> ComponentData(
313  const Entity _entity) const;
314 
325  public: template<typename ComponentTypeT>
326  bool SetComponentData(const Entity _entity,
327  const typename ComponentTypeT::Type &_data);
328 
332  public: std::unordered_set<ComponentTypeId> ComponentTypes(
333  Entity _entity) const;
334 
338  public: template<typename ComponentTypeT>
339  const ComponentTypeT IGN_DEPRECATED(6) * First() const;
340 
344  public: template<typename ComponentTypeT>
345  ComponentTypeT IGN_DEPRECATED(6) * First();
346 
358  public: template<typename ...ComponentTypeTs>
359  Entity EntityByComponents(
360  const ComponentTypeTs &..._desiredComponents) const;
361 
374  public: template<typename ...ComponentTypeTs>
375  std::vector<Entity> EntitiesByComponents(
376  const ComponentTypeTs &..._desiredComponents) const;
377 
393  public: template<typename ...ComponentTypeTs>
394  std::vector<Entity> ChildrenByComponents(Entity _parent,
395  const ComponentTypeTs &..._desiredComponents) const;
396 
398  private: template <typename T>
399  struct identity; // NOLINT
400 
413  private: Entity CloneImpl(Entity _entity, Entity _parent,
414  const std::string &_name, bool _allowRename);
415 
428  public: template<typename ...ComponentTypeTs>
429  void EachNoCache(typename identity<std::function<
430  bool(const Entity &_entity,
431  const ComponentTypeTs *...)>>::type _f) const;
432 
445  public: template<typename ...ComponentTypeTs>
446  void EachNoCache(typename identity<std::function<
447  bool(const Entity &_entity,
448  ComponentTypeTs *...)>>::type _f);
449 
462  public: template<typename ...ComponentTypeTs>
463  void Each(typename identity<std::function<
464  bool(const Entity &_entity,
465  const ComponentTypeTs *...)>>::type _f) const;
466 
479  public: template<typename ...ComponentTypeTs>
480  void Each(typename identity<std::function<
481  bool(const Entity &_entity,
482  ComponentTypeTs *...)>>::type _f);
483 
488  public: template <class Function, class... ComponentTypeTs>
489  static void ForEach(Function _f, const ComponentTypeTs &... _components);
490 
505  public: template <typename... ComponentTypeTs>
506  void EachNew(typename identity<std::function<
507  bool(const Entity &_entity,
508  ComponentTypeTs *...)>>::type _f);
509 
522  public: template <typename... ComponentTypeTs>
523  void EachNew(typename identity<std::function<
524  bool(const Entity &_entity,
525  const ComponentTypeTs *...)>>::type _f) const;
526 
537  public: template<typename ...ComponentTypeTs>
538  void EachRemoved(typename identity<std::function<
539  bool(const Entity &_entity,
540  const ComponentTypeTs *...)>>::type _f) const;
541 
545  public: const EntityGraph &Entities() const;
546 
552  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
553 
562  public: msgs::SerializedState State(
563  const std::unordered_set<Entity> &_entities = {},
564  const std::unordered_set<ComponentTypeId> &_types = {}) const;
565 
578  public: msgs::SerializedState ChangedState() const;
579 
582  public: bool HasNewEntities() const;
583 
586  public: bool HasEntitiesMarkedForRemoval() const;
587 
591  public: bool HasOneTimeComponentChanges() const;
592 
596  public: bool HasPeriodicComponentChanges() const;
597 
602  ComponentTypesWithPeriodicChanges() const;
603 
612  public: void UpdatePeriodicChangeCache(std::unordered_map<ComponentTypeId,
614 
623  public: void SetState(const msgs::SerializedState &_stateMsg);
624 
636  public: void State(
637  msgs::SerializedStateMap &_state,
638  const std::unordered_set<Entity> &_entities = {},
639  const std::unordered_set<ComponentTypeId> &_types = {},
640  bool _full = false) const;
641 
650  public: void PeriodicStateFromCache(
651  msgs::SerializedStateMap &_state,
653  std::unordered_set<Entity>> &_cache) const;
654 
668  public: void ChangedState(msgs::SerializedStateMap &_state) const;
669 
678  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
679 
684  public: void SetChanged(
685  const Entity _entity, const ComponentTypeId _type,
687 
692  public: gz::sim::ComponentState ComponentState(const Entity _entity,
693  const ComponentTypeId _typeId) const;
694 
699  public: void SetEntityCreateOffset(uint64_t _offset);
700 
703  public: bool HasRemovedComponents() const;
704 
708  protected: void ClearNewlyCreatedEntities();
709 
712  protected: void ClearRemovedComponents();
713 
717  protected: void ProcessRemoveEntityRequests();
718 
720  protected: void SetAllComponentsUnchanged();
721 
728  private: bool IsNewEntity(const Entity _entity) const;
729 
733  private: bool IsMarkedForRemoval(const Entity _entity) const;
734 
742  private: bool CreateComponentImplementation(
743  const Entity _entity,
744  const ComponentTypeId _componentTypeId,
745  const components::BaseComponent *_data);
746 
752  private: const components::BaseComponent *ComponentImplementation(
753  const Entity _entity,
754  const ComponentTypeId _type) const;
755 
761  private: components::BaseComponent *ComponentImplementation(
762  const Entity _entity,
763  const ComponentTypeId _type);
764 
769  private: template<typename ...ComponentTypeTs>
770  detail::View *FindView() const;
771 
780  const std::vector<ComponentTypeId> &_types) const;
781 
787  private: detail::BaseView *AddView(
788  const detail::ComponentTypeKey &_types,
790 
796  private: void AddEntityToMessage(msgs::SerializedState &_msg,
797  Entity _entity,
798  const std::unordered_set<ComponentTypeId> &_types = {}) const;
799 
802 
812  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
813  Entity _entity,
814  const std::unordered_set<ComponentTypeId> &_types = {},
815  bool _full = false) const;
816 
825  private: void LockAddingEntitiesToViews(bool _lock);
826 
831  private: bool LockAddingEntitiesToViews() const;
832 
833  // Make runners friends so that they can manage entity creation and
834  // removal. This should be safe since runners are internal
835  // to Gazebo.
836  friend class GuiRunner;
837  friend class SimulationRunner;
838 
839  // Make SystemManager friend so it has access to removals
840  friend class SystemManager;
841 
842  // Make network managers friends so they have control over component
843  // states. Like the runners, the managers are internal.
844  friend class NetworkManagerPrimary;
845  friend class NetworkManagerSecondary;
846  };
847  }
848  }
849 }
850 
851 #include "gz/sim/detail/EntityComponentManager.hh"
852 
853 #endif
void Each(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
Get all entities which contain given component types, as well as the components. Note that an entity ...
bool EntityMatches(Entity _entity, const std::set< ComponentTypeId > &_types) const
Get whether an entity has all the given component types.
std::unordered_set< Entity > Descendants(Entity _entity) const
Get all entities which are descendants of a given entity, including the entity itself.
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light....
Definition: gz/sim/Entity.hh:58
#define IGN_DEPRECATED(version)
bool RemoveComponent(const Entity _entity, const ComponentKey &_key)
Remove a component from an entity based on a key.
This library is part of the Gazebo project.
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities....
Definition: gz/sim/EntityComponentManager.hh:66
bool HasOneTimeComponentChanges() const
Get whether there are one-time component changes. These changes do not happen frequently and should b...
void PinEntity(const Entity _entity, bool _recursive=true)
Prevent an entity and optionally its children from being removed.
void EachRemoved(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
Get all entities which contain given component types and are about to be removed, as well as the comp...
Entity EntityByComponents(const ComponentTypeTs &..._desiredComponents) const
Get an entity which matches the value of all the given components. For example, the following will re...
friend class SimulationRunner
Definition: gz/sim/EntityComponentManager.hh:837
Component< Entity, class ParentEntityTag > ParentEntity
This component holds an entity's parent entity.
Definition: gz/sim/components/ParentEntity.hh:41
const ComponentTypeT * First() const
The first component instance of the specified type. This function is now deprecated,...
std::unordered_set< ComponentTypeId > ComponentTypesWithPeriodicChanges() const
Get the components types that are marked as periodic changes.
std::vector< Entity > ChildrenByComponents(Entity _parent, const ComponentTypeTs &..._desiredComponents) const
Get all entities which match the value of all the given components and are immediate children of a gi...
bool HasRemovedComponents() const
Return true if there are components marked for removal.
size_t EntityCount() const
Get the number of entities on the server.
void SetState(const msgs::SerializedState &_stateMsg)
Set the absolute state of the ECM from a serialized message. Entities / components that are in the ne...
void ClearNewlyCreatedEntities()
Clear the list of newly added entities so that a call to EachAdded after this will have no entities t...
friend class GuiRunner
Definition: gz/sim/EntityComponentManager.hh:836
Entity ParentEntity(const Entity _entity) const
Get the first parent of the given entity.
void UpdatePeriodicChangeCache(std::unordered_map< ComponentTypeId, std::unordered_set< Entity >> &) const
Get a cache of components with periodic changes.
void UnpinAllEntities()
Allow all previously pinned entities to be removed.
bool EntityHasComponentType(const Entity _entity, const ComponentTypeId &_typeId) const
Check whether an entity has a specific component type.
Entity CreateEntity()
Creates a new Entity.
ComponentTypeT * CreateComponent(const Entity _entity, const ComponentTypeT &_data)
Create a component of a particular type. This will copy the _data parameter.
bool EntityHasComponent(const Entity _entity, const ComponentKey &_key) const
Check whether an entity has a specific component.
std::optional< typename ComponentTypeT::Type > ComponentData(const Entity _entity) const
Get the data from a component.
bool SetComponentData(const Entity _entity, const typename ComponentTypeT::Type &_data)
Set the data from a component.
msgs::SerializedState ChangedState() const
Get a message with the serialized state of all entities and components that are changing in the curre...
std::unordered_set< ComponentTypeId > ComponentTypes(Entity _entity) const
Get the type IDs of all components attached to an entity.
bool SetParentEntity(const Entity _child, const Entity _parent)
Set the parent of an entity.
ComponentState
Possible states for a component.
Definition: include/gz/sim/Types.hh:65
Entity Clone(Entity _entity, Entity _parent, const std::string &_name, bool _allowRename)
Clone an entity and its components. If the entity has any child entities, they will also be cloned....
bool HasPeriodicComponentChanges() const
Get whether there are periodic component changes. These changes may happen frequently and are process...
std::vector< Entity > EntitiesByComponents(const ComponentTypeTs &..._desiredComponents) const
Get all entities which match the value of all the given components. For example, the following will r...
const ComponentTypeT * Component(const Entity _entity) const
Get a component assigned to an entity based on a component type.
void PeriodicStateFromCache(msgs::SerializedStateMap &_state, const std::unordered_map< ComponentTypeId, std::unordered_set< Entity >> &_cache) const
Populate a message with relevant changes to the state given a periodic change cache.
Base class for all components.
Definition: gz/sim/components/Component.hh:230
friend class SystemManager
Definition: gz/sim/EntityComponentManager.hh:840
void SetEntityCreateOffset(uint64_t _offset)
All future entities will have an id that starts at _offset. This can be used to avoid entity id colli...
bool HasEntity(const Entity _entity) const
Get whether an Entity exists.
void RequestRemoveEntities()
Request to remove all entities. This will insert the request into a queue. The queue is processed tow...
@ OneTimeChange
Component value has suffered a one-time change. This indicates to systems that this change must be pr...
void RequestRemoveEntity(const Entity _entity, bool _recursive=true)
Request an entity deletion. This will insert the request into a queue. The queue is processed toward ...
friend class NetworkManagerSecondary
Definition: gz/sim/EntityComponentManager.hh:845
bool HasComponentType(const ComponentTypeId _typeId) const
Get whether a component type has ever been created.
void SetAllComponentsUnchanged()
Mark all components as not changed.
void SetChanged(const Entity _entity, const ComponentTypeId _type, gz::sim::ComponentState _c=ComponentState::OneTimeChange)
Set the changed state of a component.
const EntityGraph & Entities() const
Get a graph with all the entities. Entities are vertices and edges point from parent to children.
void ClearRemovedComponents()
Clear the list of removed components so that a call to RemoveComponent doesn't make the list grow ind...
static void ForEach(Function _f, const ComponentTypeTs &... _components)
Call a function for each parameter in a pack.
msgs::SerializedState State(const std::unordered_set< Entity > &_entities={}, const std::unordered_set< ComponentTypeId > &_types={}) const
Get a message with the serialized state of the given entities and components.
class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate
Definition: gz/sim/EntityComponentManager.hh:51
bool HasNewEntities() const
Get whether there are new entities.
void UnpinEntity(const Entity _entity, bool _recursive=true)
Allow an entity, and optionally its children, previously marked as pinned to be removed.
friend class NetworkManagerPrimary
Definition: gz/sim/EntityComponentManager.hh:844
ComponentTypeT * ComponentDefault(Entity _entity, const typename ComponentTypeT::Type &_default=typename ComponentTypeT::Type())
Get a mutable component assigned to an entity based on a component type. If the component doesn't exi...
gz::sim::ComponentState ComponentState(const Entity _entity, const ComponentTypeId _typeId) const
Get a component's state.
bool HasEntitiesMarkedForRemoval() const
Get whether there are any entities marked to be removed.
void ProcessRemoveEntityRequests()
Process all entity remove requests. This will remove entities and their components....
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: include/gz/sim/Types.hh:90
void EachNew(typename identity< std::function< bool(const Entity &_entity, ComponentTypeTs *...)>>::type _f)
Get all newly created entities which contain given component types, as well as the components....
STL class.
STL class.
void RebuildViews()
Rebuild all the views. This could be an expensive operation.
void EachNoCache(typename identity< std::function< bool(const Entity &_entity, const ComponentTypeTs *...)>>::type _f) const
A version of Each() that doesn't use a cache. The cached version, Each(), is preferred....