Ignition Gazebo

API Reference

5.0.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 
98  public: void RequestRemoveEntities();
99 
103  public: bool HasEntity(const Entity _entity) const;
104 
111  public: Entity ParentEntity(const Entity _entity) const;
112 
121  public: bool SetParentEntity(const Entity _child, const Entity _parent);
122 
126  public: bool HasComponentType(const ComponentTypeId _typeId) const;
127 
132  public: bool EntityHasComponent(const Entity _entity,
133  const ComponentKey &_key) const;
134 
140  public: bool EntityHasComponentType(const Entity _entity,
141  const ComponentTypeId &_typeId) const;
142 
147  public: bool EntityMatches(Entity _entity,
148  const std::set<ComponentTypeId> &_types) const;
149 
155  public: bool RemoveComponent(
156  const Entity _entity, const ComponentKey &_key);
157 
163  public: bool RemoveComponent(
164  const Entity _entity, const ComponentTypeId &_typeId);
165 
171  public: template<typename ComponentTypeT>
172  bool RemoveComponent(Entity _entity);
173 
176  public: void RebuildViews();
177 
184  public: template<typename ComponentTypeT>
185  ComponentKey CreateComponent(const Entity _entity,
186  const ComponentTypeT &_data);
187 
193  public: template<typename ComponentTypeT>
194  const ComponentTypeT *Component(const Entity _entity) const;
195 
201  public: template<typename ComponentTypeT>
202  ComponentTypeT *Component(const Entity _entity);
203 
208  public: template<typename ComponentTypeT>
209  const ComponentTypeT *Component(const ComponentKey &_key) const;
210 
215  public: template<typename ComponentTypeT>
216  ComponentTypeT *Component(const ComponentKey &_key);
217 
226  public: template<typename ComponentTypeT>
227  ComponentTypeT *ComponentDefault(Entity _entity,
228  const typename ComponentTypeT::Type &_default =
229  typename ComponentTypeT::Type());
230 
239  public: template<typename ComponentTypeT>
240  std::optional<typename ComponentTypeT::Type> ComponentData(
241  const Entity _entity) const;
242 
253  public: template<typename ComponentTypeT>
254  bool SetComponentData(const Entity _entity,
255  const typename ComponentTypeT::Type &_data);
256 
260  public: std::unordered_set<ComponentTypeId> ComponentTypes(
261  Entity _entity) const;
262 
266  public: template<typename ComponentTypeT>
267  const ComponentTypeT *First() const;
268 
272  public: template<typename ComponentTypeT>
273  ComponentTypeT *First();
274 
286  public: template<typename ...ComponentTypeTs>
287  Entity EntityByComponents(
288  const ComponentTypeTs &..._desiredComponents) const;
289 
302  public: template<typename ...ComponentTypeTs>
303  std::vector<Entity> EntitiesByComponents(
304  const ComponentTypeTs &..._desiredComponents) const;
305 
321  public: template<typename ...ComponentTypeTs>
322  std::vector<Entity> ChildrenByComponents(Entity _parent,
323  const ComponentTypeTs &..._desiredComponents) const;
324 
326  private: template <typename T>
327  struct identity; // NOLINT
328 
341  public: template<typename ...ComponentTypeTs>
342  void EachNoCache(typename identity<std::function<
343  bool(const Entity &_entity,
344  const ComponentTypeTs *...)>>::type _f) const;
345 
358  public: template<typename ...ComponentTypeTs>
359  void EachNoCache(typename identity<std::function<
360  bool(const Entity &_entity,
361  ComponentTypeTs *...)>>::type _f);
362 
375  public: template<typename ...ComponentTypeTs>
376  void Each(typename identity<std::function<
377  bool(const Entity &_entity,
378  const ComponentTypeTs *...)>>::type _f) const;
379 
392  public: template<typename ...ComponentTypeTs>
393  void Each(typename identity<std::function<
394  bool(const Entity &_entity,
395  ComponentTypeTs *...)>>::type _f);
396 
401  public: template <class Function, class... ComponentTypeTs>
402  static void ForEach(Function _f, const ComponentTypeTs &... _components);
403 
418  public: template <typename... ComponentTypeTs>
419  void EachNew(typename identity<std::function<
420  bool(const Entity &_entity,
421  ComponentTypeTs *...)>>::type _f);
422 
435  public: template <typename... ComponentTypeTs>
436  void EachNew(typename identity<std::function<
437  bool(const Entity &_entity,
438  const ComponentTypeTs *...)>>::type _f) const;
439 
450  public: template<typename ...ComponentTypeTs>
451  void EachRemoved(typename identity<std::function<
452  bool(const Entity &_entity,
453  const ComponentTypeTs *...)>>::type _f) const;
454 
458  public: const EntityGraph &Entities() const;
459 
465  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
466 
475  public: msgs::SerializedState State(
476  const std::unordered_set<Entity> &_entities = {},
477  const std::unordered_set<ComponentTypeId> &_types = {}) const;
478 
491  public: msgs::SerializedState ChangedState() const;
492 
495  public: bool HasNewEntities() const;
496 
499  public: bool HasEntitiesMarkedForRemoval() const;
500 
504  public: bool HasOneTimeComponentChanges() const;
505 
510  ComponentTypesWithPeriodicChanges() const;
511 
520  public: void SetState(const msgs::SerializedState &_stateMsg);
521 
532  public: void State(
533  msgs::SerializedStateMap &_state,
534  const std::unordered_set<Entity> &_entities = {},
535  const std::unordered_set<ComponentTypeId> &_types = {},
536  bool _full = false) const;
537 
551  public: void ChangedState(msgs::SerializedStateMap &_state) const;
552 
561  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
562 
567  public: void SetChanged(
568  const Entity _entity, const ComponentTypeId _type,
570 
575  public: gazebo::ComponentState ComponentState(const Entity _entity,
576  const ComponentTypeId _typeId) const;
577 
582  public: void SetEntityCreateOffset(uint64_t _offset);
583 
587  protected: void ClearNewlyCreatedEntities();
588 
591  protected: void ClearRemovedComponents();
592 
596  protected: void ProcessRemoveEntityRequests();
597 
599  protected: void SetAllComponentsUnchanged();
600 
607  private: bool IsNewEntity(const Entity _entity) const;
608 
612  private: bool IsMarkedForRemoval(const Entity _entity) const;
613 
617  private: bool RemoveEntity(const Entity _entity);
618 
622  private: components::BaseComponent *First(
623  const ComponentTypeId _componentTypeId);
624 
631  private: ComponentKey CreateComponentImplementation(
632  const Entity _entity,
633  const ComponentTypeId _componentTypeId,
634  const components::BaseComponent *_data);
635 
641  private: const components::BaseComponent *ComponentImplementation(
642  const Entity _entity,
643  const ComponentTypeId _type) const;
644 
650  private: components::BaseComponent *ComponentImplementation(
651  const Entity _entity,
652  const ComponentTypeId _type);
653 
658  private: const components::BaseComponent *ComponentImplementation(
659  const ComponentKey &_key) const;
660 
665  private: components::BaseComponent *ComponentImplementation(
666  const ComponentKey &_key);
667 
673  private: template<typename FirstComponent,
674  typename ...RemainingComponents,
675  typename std::enable_if<
676  sizeof...(RemainingComponents) == 0, int>::type = 0>
677  void AddComponentsToView(detail::View &_view,
678  const Entity _entity) const;
679 
685  private: template<typename FirstComponent,
686  typename ...RemainingComponents,
687  typename std::enable_if<
688  sizeof...(RemainingComponents) != 0, int>::type = 0>
689  void AddComponentsToView(detail::View &_view,
690  const Entity _entity) const;
691 
696  private: template<typename ...ComponentTypeTs>
697  detail::View &FindView() const;
698 
705  private: bool FindView(const std::set<ComponentTypeId> &_types,
706  std::map<detail::ComponentTypeKey,
707  detail::View>::iterator &_iter) const; // NOLINT
708 
715  AddView(const std::set<ComponentTypeId> &_types,
716  detail::View &&_view) const;
717 
720  private: void UpdateViews(const Entity _entity);
721 
725  private: ComponentId EntityComponentIdFromType(
726  const Entity _entity, const ComponentTypeId _type) const;
727 
733  private: void AddEntityToMessage(msgs::SerializedState &_msg,
734  Entity _entity,
735  const std::unordered_set<ComponentTypeId> &_types = {}) const;
736 
739 
749  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
750  Entity _entity,
751  const std::unordered_set<ComponentTypeId> &_types = {},
752  bool _full = false) const;
753 
754  // Make runners friends so that they can manage entity creation and
755  // removal. This should be safe since runners are internal
756  // to Gazebo.
757  friend class GuiRunner;
758  friend class SimulationRunner;
759 
760  // Make network managers friends so they have control over component
761  // states. Like the runners, the managers are internal.
762  friend class NetworkManagerPrimary;
763  friend class NetworkManagerSecondary;
764 
765  // Make View a friend so that it can access components.
766  // This should be safe since View is internal to Gazebo.
767  friend class detail::View;
768  };
769  }
770  }
771 }
772 
773 #include "ignition/gazebo/detail/EntityComponentManager.hh"
774 
775 #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: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...