Ignition Gazebo

API Reference

4.6.0
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 IGNITION_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
18 #define IGNITION_GAZEBO_ENTITYCOMPONENTMANAGER_HH_
19 
22 
23 #include <map>
24 #include <memory>
25 #include <set>
26 #include <string>
27 #include <typeinfo>
28 #include <type_traits>
29 #include <unordered_set>
30 #include <utility>
31 #include <vector>
32 
36 #include "ignition/gazebo/Export.hh"
37 #include "ignition/gazebo/Types.hh"
38 
40 #include "ignition/gazebo/detail/View.hh"
41 
42 namespace ignition
43 {
44  namespace gazebo
45  {
46  // Inline bracket to help doxygen filtering.
47  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
48  // Forward declarations.
49  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
50 
56 
60  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
65  {
67  public: EntityComponentManager();
68 
70  public: ~EntityComponentManager();
71 
74  public: Entity CreateEntity();
75 
78  public: size_t EntityCount() const;
79 
91  public: void RequestRemoveEntity(const Entity _entity,
92  bool _recursive = true);
93 
97  public: void RequestRemoveEntities();
98 
102  public: bool HasEntity(const Entity _entity) const;
103 
110  public: Entity ParentEntity(const Entity _entity) const;
111 
120  public: bool SetParentEntity(const Entity _child, const Entity _parent);
121 
125  public: bool HasComponentType(const ComponentTypeId _typeId) const;
126 
131  public: bool EntityHasComponent(const Entity _entity,
132  const ComponentKey &_key) const;
133 
139  public: bool EntityHasComponentType(const Entity _entity,
140  const ComponentTypeId &_typeId) const;
141 
146  public: bool EntityMatches(Entity _entity,
147  const std::set<ComponentTypeId> &_types) const;
148 
154  public: bool RemoveComponent(
155  const Entity _entity, const ComponentKey &_key);
156 
162  public: bool RemoveComponent(
163  const Entity _entity, const ComponentTypeId &_typeId);
164 
170  public: template<typename ComponentTypeT>
171  bool RemoveComponent(Entity _entity);
172 
175  public: void RebuildViews();
176 
183  public: template<typename ComponentTypeT>
184  ComponentKey CreateComponent(const Entity _entity,
185  const ComponentTypeT &_data);
186 
192  public: template<typename ComponentTypeT>
193  const ComponentTypeT *Component(const Entity _entity) const;
194 
200  public: template<typename ComponentTypeT>
201  ComponentTypeT *Component(const Entity _entity);
202 
207  public: template<typename ComponentTypeT>
208  const ComponentTypeT *Component(const ComponentKey &_key) const;
209 
214  public: template<typename ComponentTypeT>
215  ComponentTypeT *Component(const ComponentKey &_key);
216 
225  public: template<typename ComponentTypeT>
226  ComponentTypeT *ComponentDefault(Entity _entity,
227  const typename ComponentTypeT::Type &_default =
228  typename ComponentTypeT::Type());
229 
238  public: template<typename ComponentTypeT>
239  std::optional<typename ComponentTypeT::Type> ComponentData(
240  const Entity _entity) const;
241 
252  public: template<typename ComponentTypeT>
253  bool SetComponentData(const Entity _entity,
254  const typename ComponentTypeT::Type &_data);
255 
259  public: std::unordered_set<ComponentTypeId> ComponentTypes(
260  Entity _entity) const;
261 
265  public: template<typename ComponentTypeT>
266  const ComponentTypeT *First() const;
267 
271  public: template<typename ComponentTypeT>
272  ComponentTypeT *First();
273 
285  public: template<typename ...ComponentTypeTs>
286  Entity EntityByComponents(
287  const ComponentTypeTs &..._desiredComponents) const;
288 
301  public: template<typename ...ComponentTypeTs>
302  std::vector<Entity> EntitiesByComponents(
303  const ComponentTypeTs &..._desiredComponents) const;
304 
320  public: template<typename ...ComponentTypeTs>
321  std::vector<Entity> ChildrenByComponents(Entity _parent,
322  const ComponentTypeTs &..._desiredComponents) const;
323 
325  private: template <typename T>
326  struct identity; // NOLINT
327 
340  public: template<typename ...ComponentTypeTs>
341  void EachNoCache(typename identity<std::function<
342  bool(const Entity &_entity,
343  const ComponentTypeTs *...)>>::type _f) const;
344 
357  public: template<typename ...ComponentTypeTs>
358  void EachNoCache(typename identity<std::function<
359  bool(const Entity &_entity,
360  ComponentTypeTs *...)>>::type _f);
361 
374  public: template<typename ...ComponentTypeTs>
375  void Each(typename identity<std::function<
376  bool(const Entity &_entity,
377  const ComponentTypeTs *...)>>::type _f) const;
378 
391  public: template<typename ...ComponentTypeTs>
392  void Each(typename identity<std::function<
393  bool(const Entity &_entity,
394  ComponentTypeTs *...)>>::type _f);
395 
400  public: template <class Function, class... ComponentTypeTs>
401  static void ForEach(Function _f, const ComponentTypeTs &... _components);
402 
414  public: template <typename... ComponentTypeTs>
415  void EachNew(typename identity<std::function<
416  bool(const Entity &_entity,
417  ComponentTypeTs *...)>>::type _f);
418 
430  public: template <typename... ComponentTypeTs>
431  void EachNew(typename identity<std::function<
432  bool(const Entity &_entity,
433  const ComponentTypeTs *...)>>::type _f) const;
434 
445  public: template<typename ...ComponentTypeTs>
446  void EachRemoved(typename identity<std::function<
447  bool(const Entity &_entity,
448  const ComponentTypeTs *...)>>::type _f) const;
449 
453  public: const EntityGraph &Entities() const;
454 
460  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
461 
470  public: msgs::SerializedState State(
471  const std::unordered_set<Entity> &_entities = {},
472  const std::unordered_set<ComponentTypeId> &_types = {}) const;
473 
488  public: msgs::SerializedState ChangedState() const;
489 
492  public: bool HasNewEntities() const;
493 
496  public: bool HasEntitiesMarkedForRemoval() const;
497 
501  public: bool HasOneTimeComponentChanges() const;
502 
507  ComponentTypesWithPeriodicChanges() const;
508 
517  public: void SetState(const msgs::SerializedState &_stateMsg);
518 
529  public: void State(
530  msgs::SerializedStateMap &_state,
531  const std::unordered_set<Entity> &_entities = {},
532  const std::unordered_set<ComponentTypeId> &_types = {},
533  bool _full = false) const;
534 
550  public: void ChangedState(msgs::SerializedStateMap &_state) const;
551 
560  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
561 
566  public: void SetChanged(
567  const Entity _entity, const ComponentTypeId _type,
569 
574  public: gazebo::ComponentState ComponentState(const Entity _entity,
575  const ComponentTypeId _typeId) const;
576 
581  public: void SetEntityCreateOffset(uint64_t _offset);
582 
586  protected: void ClearNewlyCreatedEntities();
587 
590  protected: void ClearRemovedComponents();
591 
595  protected: void ProcessRemoveEntityRequests();
596 
598  protected: void SetAllComponentsUnchanged();
599 
606  private: bool IsNewEntity(const Entity _entity) const;
607 
611  private: bool IsMarkedForRemoval(const Entity _entity) const;
612 
616  private: bool RemoveEntity(const Entity _entity);
617 
621  private: components::BaseComponent *First(
622  const ComponentTypeId _componentTypeId);
623 
630  private: ComponentKey CreateComponentImplementation(
631  const Entity _entity,
632  const ComponentTypeId _componentTypeId,
633  const components::BaseComponent *_data);
634 
640  private: const components::BaseComponent *ComponentImplementation(
641  const Entity _entity,
642  const ComponentTypeId _type) const;
643 
649  private: components::BaseComponent *ComponentImplementation(
650  const Entity _entity,
651  const ComponentTypeId _type);
652 
657  private: const components::BaseComponent *ComponentImplementation(
658  const ComponentKey &_key) const;
659 
664  private: components::BaseComponent *ComponentImplementation(
665  const ComponentKey &_key);
666 
672  private: template<typename FirstComponent,
673  typename ...RemainingComponents,
674  typename std::enable_if<
675  sizeof...(RemainingComponents) == 0, int>::type = 0>
676  void AddComponentsToView(detail::View &_view,
677  const Entity _entity) const;
678 
684  private: template<typename FirstComponent,
685  typename ...RemainingComponents,
686  typename std::enable_if<
687  sizeof...(RemainingComponents) != 0, int>::type = 0>
688  void AddComponentsToView(detail::View &_view,
689  const Entity _entity) const;
690 
695  private: template<typename ...ComponentTypeTs>
696  detail::View &FindView() const;
697 
704  private: bool FindView(const std::set<ComponentTypeId> &_types,
705  std::map<detail::ComponentTypeKey,
706  detail::View>::iterator &_iter) const; // NOLINT
707 
714  AddView(const std::set<ComponentTypeId> &_types,
715  detail::View &&_view) const;
716 
719  private: void UpdateViews(const Entity _entity);
720 
724  private: ComponentId EntityComponentIdFromType(
725  const Entity _entity, const ComponentTypeId _type) const;
726 
732  private: void AddEntityToMessage(msgs::SerializedState &_msg,
733  Entity _entity,
734  const std::unordered_set<ComponentTypeId> &_types = {}) const;
735 
738 
748  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
749  Entity _entity,
750  const std::unordered_set<ComponentTypeId> &_types = {},
751  bool _full = false) const;
752 
753  // Make runners friends so that they can manage entity creation and
754  // removal. This should be safe since runners are internal
755  // to Gazebo.
756  friend class GuiRunner;
757  friend class SimulationRunner;
758 
759  // Make network managers friends so they have control over component
760  // states. Like the runners, the managers are internal.
761  friend class NetworkManagerPrimary;
762  friend class NetworkManagerSecondary;
763 
764  // Make View a friend so that it can access components.
765  // This should be safe since View is internal to Gazebo.
766  friend class detail::View;
767  };
768  }
769  }
770 }
771 
772 #include "ignition/gazebo/detail/EntityComponentManager.hh"
773 
774 #endif
Responsible for running GUI systems as new states are received from the backend.
Definition: GuiRunner.hh:38
Component< Entity, class ParentEntityTag > ParentEntity
This component holds an entity&#39;s parent entity.
Definition: ParentEntity.hh:41
STL class.
The EntityComponentManager constructs, deletes, and returns components and entities. A component can be of any class which inherits from components::BaseComponent.
Definition: EntityComponentManager.hh:64
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: Types.hh:86
class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate
Definition: EntityComponentManager.hh:49
STL class.
int ComponentId
A unique identifier for a component instance. The uniqueness of a ComponentId is scoped to the compon...
Definition: Types.hh:81
ComponentState
Possible states for a component.
Definition: Types.hh:63
This library is part of the Ignition Robotics project.
uint64_t Entity
An Entity identifies a single object in simulation such as a model, link, or light. At its core, an Entity is just an identifier.
Definition: Entity.hh:59
Base class for all components.
Definition: Component.hh:230
Component value has suffered a one-time change. This indicates to systems that this change must be pr...