Ignition Gazebo

API Reference

3.7.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  std::optional<typename ComponentTypeT::Type> ComponentData(
227  const Entity _entity) const;
228 
239  public: template<typename ComponentTypeT>
240  bool SetComponentData(const Entity _entity,
241  const typename ComponentTypeT::Type &_data);
242 
246  public: std::unordered_set<ComponentTypeId> ComponentTypes(
247  Entity _entity) const;
248 
252  public: template<typename ComponentTypeT>
253  const ComponentTypeT *First() const;
254 
258  public: template<typename ComponentTypeT>
259  ComponentTypeT *First();
260 
272  public: template<typename ...ComponentTypeTs>
273  Entity EntityByComponents(
274  const ComponentTypeTs &..._desiredComponents) const;
275 
288  public: template<typename ...ComponentTypeTs>
289  std::vector<Entity> EntitiesByComponents(
290  const ComponentTypeTs &..._desiredComponents) const;
291 
307  public: template<typename ...ComponentTypeTs>
308  std::vector<Entity> ChildrenByComponents(Entity _parent,
309  const ComponentTypeTs &..._desiredComponents) const;
310 
312  private: template <typename T>
313  struct identity; // NOLINT
314 
327  public: template<typename ...ComponentTypeTs>
328  void EachNoCache(typename identity<std::function<
329  bool(const Entity &_entity,
330  const ComponentTypeTs *...)>>::type _f) const;
331 
344  public: template<typename ...ComponentTypeTs>
345  void EachNoCache(typename identity<std::function<
346  bool(const Entity &_entity,
347  ComponentTypeTs *...)>>::type _f);
348 
361  public: template<typename ...ComponentTypeTs>
362  void Each(typename identity<std::function<
363  bool(const Entity &_entity,
364  const ComponentTypeTs *...)>>::type _f) const;
365 
378  public: template<typename ...ComponentTypeTs>
379  void Each(typename identity<std::function<
380  bool(const Entity &_entity,
381  ComponentTypeTs *...)>>::type _f);
382 
387  public: template <class Function, class... ComponentTypeTs>
388  static void ForEach(Function _f, const ComponentTypeTs &... _components);
389 
401  public: template <typename... ComponentTypeTs>
402  void EachNew(typename identity<std::function<
403  bool(const Entity &_entity,
404  ComponentTypeTs *...)>>::type _f);
405 
417  public: template <typename... ComponentTypeTs>
418  void EachNew(typename identity<std::function<
419  bool(const Entity &_entity,
420  const ComponentTypeTs *...)>>::type _f) const;
421 
432  public: template<typename ...ComponentTypeTs>
433  void EachRemoved(typename identity<std::function<
434  bool(const Entity &_entity,
435  const ComponentTypeTs *...)>>::type _f) const;
436 
440  public: const EntityGraph &Entities() const;
441 
447  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
448 
457  public: msgs::SerializedState State(
458  const std::unordered_set<Entity> &_entities = {},
459  const std::unordered_set<ComponentTypeId> &_types = {}) const;
460 
475  public: msgs::SerializedState ChangedState() const;
476 
479  public: bool HasNewEntities() const;
480 
483  public: bool HasEntitiesMarkedForRemoval() const;
484 
488  public: bool HasOneTimeComponentChanges() const;
489 
494  ComponentTypesWithPeriodicChanges() const;
495 
504  public: void SetState(const msgs::SerializedState &_stateMsg);
505 
516  public: void State(
517  msgs::SerializedStateMap &_state,
518  const std::unordered_set<Entity> &_entities = {},
519  const std::unordered_set<ComponentTypeId> &_types = {},
520  bool _full = false) const;
521 
537  public: void ChangedState(msgs::SerializedStateMap &_state) const;
538 
547  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
548 
553  public: void SetChanged(
554  const Entity _entity, const ComponentTypeId _type,
556 
561  public: gazebo::ComponentState ComponentState(const Entity _entity,
562  const ComponentTypeId _typeId) const;
563 
568  public: void SetEntityCreateOffset(uint64_t _offset);
569 
573  protected: void ClearNewlyCreatedEntities();
574 
578  protected: void ProcessRemoveEntityRequests();
579 
581  protected: void SetAllComponentsUnchanged();
582 
589  private: bool IsNewEntity(const Entity _entity) const;
590 
594  private: bool IsMarkedForRemoval(const Entity _entity) const;
595 
599  private: bool RemoveEntity(const Entity _entity);
600 
604  private: components::BaseComponent *First(
605  const ComponentTypeId _componentTypeId);
606 
613  private: ComponentKey CreateComponentImplementation(
614  const Entity _entity,
615  const ComponentTypeId _componentTypeId,
616  const components::BaseComponent *_data);
617 
623  private: const components::BaseComponent *ComponentImplementation(
624  const Entity _entity,
625  const ComponentTypeId _type) const;
626 
632  private: components::BaseComponent *ComponentImplementation(
633  const Entity _entity,
634  const ComponentTypeId _type);
635 
640  private: const components::BaseComponent *ComponentImplementation(
641  const ComponentKey &_key) const;
642 
647  private: components::BaseComponent *ComponentImplementation(
648  const ComponentKey &_key);
649 
655  private: template<typename FirstComponent,
656  typename ...RemainingComponents,
657  typename std::enable_if<
658  sizeof...(RemainingComponents) == 0, int>::type = 0>
659  void AddComponentsToView(detail::View &_view,
660  const Entity _entity) const;
661 
667  private: template<typename FirstComponent,
668  typename ...RemainingComponents,
669  typename std::enable_if<
670  sizeof...(RemainingComponents) != 0, int>::type = 0>
671  void AddComponentsToView(detail::View &_view,
672  const Entity _entity) const;
673 
678  private: template<typename ...ComponentTypeTs>
679  detail::View &FindView() const;
680 
687  private: bool FindView(const std::set<ComponentTypeId> &_types,
688  std::map<detail::ComponentTypeKey,
689  detail::View>::iterator &_iter) const; // NOLINT
690 
697  AddView(const std::set<ComponentTypeId> &_types,
698  detail::View &&_view) const;
699 
702  private: void UpdateViews(const Entity _entity);
703 
707  private: ComponentId EntityComponentIdFromType(
708  const Entity _entity, const ComponentTypeId _type) const;
709 
715  private: void AddEntityToMessage(msgs::SerializedState &_msg,
716  Entity _entity,
717  const std::unordered_set<ComponentTypeId> &_types = {}) const;
718 
721 
731  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
732  Entity _entity,
733  const std::unordered_set<ComponentTypeId> &_types = {},
734  bool _full = false) const;
735 
736  // Make runners friends so that they can manage entity creation and
737  // removal. This should be safe since runners are internal
738  // to Gazebo.
739  friend class GuiRunner;
740  friend class SimulationRunner;
741 
742  // Make network managers friends so they have control over component
743  // states. Like the runners, the managers are internal.
744  friend class NetworkManagerPrimary;
745  friend class NetworkManagerSecondary;
746 
747  // Make View a friend so that it can access components.
748  // This should be safe since View is internal to Gazebo.
749  friend class detail::View;
750  };
751  }
752  }
753 }
754 
755 #include "ignition/gazebo/detail/EntityComponentManager.hh"
756 
757 #endif
Responsible for running GUI systems as new states are received from the backend.
Definition: GuiRunner.hh:40
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:87
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:82
ComponentState
Possible states for a component.
Definition: Types.hh:64
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...