Ignition Gazebo

API Reference

5.1.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 <optional>
26 #include <set>
27 #include <string>
28 #include <typeinfo>
29 #include <type_traits>
30 #include <unordered_set>
31 #include <utility>
32 #include <vector>
33 
37 #include "ignition/gazebo/Export.hh"
38 #include "ignition/gazebo/Types.hh"
39 
41 #include "ignition/gazebo/detail/View.hh"
42 
43 namespace ignition
44 {
45  namespace gazebo
46  {
47  // Inline bracket to help doxygen filtering.
48  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
49  // Forward declarations.
50  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
51 
57 
61  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
66  {
68  public: EntityComponentManager();
69 
71  public: ~EntityComponentManager();
72 
75  public: Entity CreateEntity();
76 
79  public: size_t EntityCount() const;
80 
92  public: void RequestRemoveEntity(const Entity _entity,
93  bool _recursive = true);
94 
110  public: void PinEntity(const Entity _entity, bool _recursive = true);
111 
118  public: void UnpinEntity(const Entity _entity, bool _recursive = true);
119 
122  public: void UnpinAllEntities();
123 
127  public: void RequestRemoveEntities();
128 
132  public: bool HasEntity(const Entity _entity) const;
133 
140  public: Entity ParentEntity(const Entity _entity) const;
141 
152  public: bool SetParentEntity(const Entity _child, const Entity _parent);
153 
157  public: bool HasComponentType(const ComponentTypeId _typeId) const;
158 
163  public: bool EntityHasComponent(const Entity _entity,
164  const ComponentKey &_key) const;
165 
171  public: bool EntityHasComponentType(const Entity _entity,
172  const ComponentTypeId &_typeId) const;
173 
178  public: bool EntityMatches(Entity _entity,
179  const std::set<ComponentTypeId> &_types) const;
180 
186  public: bool RemoveComponent(
187  const Entity _entity, const ComponentKey &_key);
188 
194  public: bool RemoveComponent(
195  const Entity _entity, const ComponentTypeId &_typeId);
196 
202  public: template<typename ComponentTypeT>
203  bool RemoveComponent(Entity _entity);
204 
207  public: void RebuildViews();
208 
215  public: template<typename ComponentTypeT>
216  ComponentKey CreateComponent(const Entity _entity,
217  const ComponentTypeT &_data);
218 
224  public: template<typename ComponentTypeT>
225  const ComponentTypeT *Component(const Entity _entity) const;
226 
232  public: template<typename ComponentTypeT>
233  ComponentTypeT *Component(const Entity _entity);
234 
239  public: template<typename ComponentTypeT>
240  const ComponentTypeT *Component(const ComponentKey &_key) const;
241 
246  public: template<typename ComponentTypeT>
247  ComponentTypeT *Component(const ComponentKey &_key);
248 
257  public: template<typename ComponentTypeT>
258  ComponentTypeT *ComponentDefault(Entity _entity,
259  const typename ComponentTypeT::Type &_default =
260  typename ComponentTypeT::Type());
261 
270  public: template<typename ComponentTypeT>
271  std::optional<typename ComponentTypeT::Type> ComponentData(
272  const Entity _entity) const;
273 
284  public: template<typename ComponentTypeT>
285  bool SetComponentData(const Entity _entity,
286  const typename ComponentTypeT::Type &_data);
287 
291  public: std::unordered_set<ComponentTypeId> ComponentTypes(
292  Entity _entity) const;
293 
297  public: template<typename ComponentTypeT>
298  const ComponentTypeT *First() const;
299 
303  public: template<typename ComponentTypeT>
304  ComponentTypeT *First();
305 
317  public: template<typename ...ComponentTypeTs>
318  Entity EntityByComponents(
319  const ComponentTypeTs &..._desiredComponents) const;
320 
333  public: template<typename ...ComponentTypeTs>
334  std::vector<Entity> EntitiesByComponents(
335  const ComponentTypeTs &..._desiredComponents) const;
336 
352  public: template<typename ...ComponentTypeTs>
353  std::vector<Entity> ChildrenByComponents(Entity _parent,
354  const ComponentTypeTs &..._desiredComponents) const;
355 
357  private: template <typename T>
358  struct identity; // NOLINT
359 
372  public: template<typename ...ComponentTypeTs>
373  void EachNoCache(typename identity<std::function<
374  bool(const Entity &_entity,
375  const ComponentTypeTs *...)>>::type _f) const;
376 
389  public: template<typename ...ComponentTypeTs>
390  void EachNoCache(typename identity<std::function<
391  bool(const Entity &_entity,
392  ComponentTypeTs *...)>>::type _f);
393 
406  public: template<typename ...ComponentTypeTs>
407  void Each(typename identity<std::function<
408  bool(const Entity &_entity,
409  const ComponentTypeTs *...)>>::type _f) const;
410 
423  public: template<typename ...ComponentTypeTs>
424  void Each(typename identity<std::function<
425  bool(const Entity &_entity,
426  ComponentTypeTs *...)>>::type _f);
427 
432  public: template <class Function, class... ComponentTypeTs>
433  static void ForEach(Function _f, const ComponentTypeTs &... _components);
434 
449  public: template <typename... ComponentTypeTs>
450  void EachNew(typename identity<std::function<
451  bool(const Entity &_entity,
452  ComponentTypeTs *...)>>::type _f);
453 
466  public: template <typename... ComponentTypeTs>
467  void EachNew(typename identity<std::function<
468  bool(const Entity &_entity,
469  const ComponentTypeTs *...)>>::type _f) const;
470 
481  public: template<typename ...ComponentTypeTs>
482  void EachRemoved(typename identity<std::function<
483  bool(const Entity &_entity,
484  const ComponentTypeTs *...)>>::type _f) const;
485 
489  public: const EntityGraph &Entities() const;
490 
496  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
497 
506  public: msgs::SerializedState State(
507  const std::unordered_set<Entity> &_entities = {},
508  const std::unordered_set<ComponentTypeId> &_types = {}) const;
509 
522  public: msgs::SerializedState ChangedState() const;
523 
526  public: bool HasNewEntities() const;
527 
530  public: bool HasEntitiesMarkedForRemoval() const;
531 
535  public: bool HasOneTimeComponentChanges() const;
536 
541  ComponentTypesWithPeriodicChanges() const;
542 
551  public: void SetState(const msgs::SerializedState &_stateMsg);
552 
564  public: void State(
565  msgs::SerializedStateMap &_state,
566  const std::unordered_set<Entity> &_entities = {},
567  const std::unordered_set<ComponentTypeId> &_types = {},
568  bool _full = false) const;
569 
583  public: void ChangedState(msgs::SerializedStateMap &_state) const;
584 
593  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
594 
599  public: void SetChanged(
600  const Entity _entity, const ComponentTypeId _type,
602 
607  public: gazebo::ComponentState ComponentState(const Entity _entity,
608  const ComponentTypeId _typeId) const;
609 
614  public: void SetEntityCreateOffset(uint64_t _offset);
615 
619  protected: void ClearNewlyCreatedEntities();
620 
623  protected: void ClearRemovedComponents();
624 
628  protected: void ProcessRemoveEntityRequests();
629 
631  protected: void SetAllComponentsUnchanged();
632 
639  private: bool IsNewEntity(const Entity _entity) const;
640 
644  private: bool IsMarkedForRemoval(const Entity _entity) const;
645 
649  private: bool RemoveEntity(const Entity _entity);
650 
654  private: components::BaseComponent *First(
655  const ComponentTypeId _componentTypeId);
656 
663  private: ComponentKey CreateComponentImplementation(
664  const Entity _entity,
665  const ComponentTypeId _componentTypeId,
666  const components::BaseComponent *_data);
667 
673  private: const components::BaseComponent *ComponentImplementation(
674  const Entity _entity,
675  const ComponentTypeId _type) const;
676 
682  private: components::BaseComponent *ComponentImplementation(
683  const Entity _entity,
684  const ComponentTypeId _type);
685 
690  private: const components::BaseComponent *ComponentImplementation(
691  const ComponentKey &_key) const;
692 
697  private: components::BaseComponent *ComponentImplementation(
698  const ComponentKey &_key);
699 
705  private: template<typename FirstComponent,
706  typename ...RemainingComponents,
707  typename std::enable_if<
708  sizeof...(RemainingComponents) == 0, int>::type = 0>
709  void AddComponentsToView(detail::View &_view,
710  const Entity _entity) const;
711 
717  private: template<typename FirstComponent,
718  typename ...RemainingComponents,
719  typename std::enable_if<
720  sizeof...(RemainingComponents) != 0, int>::type = 0>
721  void AddComponentsToView(detail::View &_view,
722  const Entity _entity) const;
723 
728  private: template<typename ...ComponentTypeTs>
729  detail::View &FindView() const;
730 
737  private: bool FindView(const std::set<ComponentTypeId> &_types,
738  std::map<detail::ComponentTypeKey,
739  detail::View>::iterator &_iter) const; // NOLINT
740 
747  AddView(const std::set<ComponentTypeId> &_types,
748  detail::View &&_view) const;
749 
752  private: void UpdateViews(const Entity _entity);
753 
757  private: ComponentId EntityComponentIdFromType(
758  const Entity _entity, const ComponentTypeId _type) const;
759 
765  private: void AddEntityToMessage(msgs::SerializedState &_msg,
766  Entity _entity,
767  const std::unordered_set<ComponentTypeId> &_types = {}) const;
768 
771 
781  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
782  Entity _entity,
783  const std::unordered_set<ComponentTypeId> &_types = {},
784  bool _full = false) const;
785 
786  // Make runners friends so that they can manage entity creation and
787  // removal. This should be safe since runners are internal
788  // to Gazebo.
789  friend class GuiRunner;
790  friend class SimulationRunner;
791 
792  // Make network managers friends so they have control over component
793  // states. Like the runners, the managers are internal.
794  friend class NetworkManagerPrimary;
795  friend class NetworkManagerSecondary;
796 
797  // Make View a friend so that it can access components.
798  // This should be safe since View is internal to Gazebo.
799  friend class detail::View;
800  };
801  }
802  }
803 }
804 
805 #include "ignition/gazebo/detail/EntityComponentManager.hh"
806 
807 #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:65
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:50
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...