Ignition Gazebo

API Reference

4.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 <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 
232  public: std::unordered_set<ComponentTypeId> ComponentTypes(
233  Entity _entity) const;
234 
238  public: template<typename ComponentTypeT>
239  const ComponentTypeT *First() const;
240 
244  public: template<typename ComponentTypeT>
245  ComponentTypeT *First();
246 
258  public: template<typename ...ComponentTypeTs>
259  Entity EntityByComponents(
260  const ComponentTypeTs &..._desiredComponents) const;
261 
274  public: template<typename ...ComponentTypeTs>
275  std::vector<Entity> EntitiesByComponents(
276  const ComponentTypeTs &..._desiredComponents) const;
277 
293  public: template<typename ...ComponentTypeTs>
294  std::vector<Entity> ChildrenByComponents(Entity _parent,
295  const ComponentTypeTs &..._desiredComponents) const;
296 
298  private: template <typename T>
299  struct identity; // NOLINT
300 
313  public: template<typename ...ComponentTypeTs>
314  void EachNoCache(typename identity<std::function<
315  bool(const Entity &_entity,
316  const ComponentTypeTs *...)>>::type _f) const;
317 
330  public: template<typename ...ComponentTypeTs>
331  void EachNoCache(typename identity<std::function<
332  bool(const Entity &_entity,
333  ComponentTypeTs *...)>>::type _f);
334 
347  public: template<typename ...ComponentTypeTs>
348  void Each(typename identity<std::function<
349  bool(const Entity &_entity,
350  const ComponentTypeTs *...)>>::type _f) const;
351 
364  public: template<typename ...ComponentTypeTs>
365  void Each(typename identity<std::function<
366  bool(const Entity &_entity,
367  ComponentTypeTs *...)>>::type _f);
368 
373  public: template <class Function, class... ComponentTypeTs>
374  static void ForEach(Function _f, const ComponentTypeTs &... _components);
375 
387  public: template <typename... ComponentTypeTs>
388  void EachNew(typename identity<std::function<
389  bool(const Entity &_entity,
390  ComponentTypeTs *...)>>::type _f);
391 
403  public: template <typename... ComponentTypeTs>
404  void EachNew(typename identity<std::function<
405  bool(const Entity &_entity,
406  const ComponentTypeTs *...)>>::type _f) const;
407 
418  public: template<typename ...ComponentTypeTs>
419  void EachRemoved(typename identity<std::function<
420  bool(const Entity &_entity,
421  const ComponentTypeTs *...)>>::type _f) const;
422 
426  public: const EntityGraph &Entities() const;
427 
433  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
434 
443  public: msgs::SerializedState State(
444  const std::unordered_set<Entity> &_entities = {},
445  const std::unordered_set<ComponentTypeId> &_types = {}) const;
446 
461  public: msgs::SerializedState ChangedState() const;
462 
465  public: bool HasNewEntities() const;
466 
469  public: bool HasEntitiesMarkedForRemoval() const;
470 
474  public: bool HasOneTimeComponentChanges() const;
475 
484  public: void SetState(const msgs::SerializedState &_stateMsg);
485 
496  public: void State(
497  msgs::SerializedStateMap &_state,
498  const std::unordered_set<Entity> &_entities = {},
499  const std::unordered_set<ComponentTypeId> &_types = {},
500  bool _full = false) const;
501 
517  public: void ChangedState(msgs::SerializedStateMap &_state) const;
518 
527  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
528 
533  public: void SetChanged(
534  const Entity _entity, const ComponentTypeId _type,
536 
541  public: gazebo::ComponentState ComponentState(const Entity _entity,
542  const ComponentTypeId _typeId) const;
543 
548  public: void SetEntityCreateOffset(uint64_t _offset);
549 
553  protected: void ClearNewlyCreatedEntities();
554 
557  protected: void ClearRemovedComponents();
558 
562  protected: void ProcessRemoveEntityRequests();
563 
565  protected: void SetAllComponentsUnchanged();
566 
573  private: bool IsNewEntity(const Entity _entity) const;
574 
578  private: bool IsMarkedForRemoval(const Entity _entity) const;
579 
583  private: bool RemoveEntity(const Entity _entity);
584 
588  private: components::BaseComponent *First(
589  const ComponentTypeId _componentTypeId);
590 
597  private: ComponentKey CreateComponentImplementation(
598  const Entity _entity,
599  const ComponentTypeId _componentTypeId,
600  const components::BaseComponent *_data);
601 
607  private: const components::BaseComponent *ComponentImplementation(
608  const Entity _entity,
609  const ComponentTypeId _type) const;
610 
616  private: components::BaseComponent *ComponentImplementation(
617  const Entity _entity,
618  const ComponentTypeId _type);
619 
624  private: const components::BaseComponent *ComponentImplementation(
625  const ComponentKey &_key) const;
626 
631  private: components::BaseComponent *ComponentImplementation(
632  const ComponentKey &_key);
633 
639  private: template<typename FirstComponent,
640  typename ...RemainingComponents,
641  typename std::enable_if<
642  sizeof...(RemainingComponents) == 0, int>::type = 0>
643  void AddComponentsToView(detail::View &_view,
644  const Entity _entity) const;
645 
651  private: template<typename FirstComponent,
652  typename ...RemainingComponents,
653  typename std::enable_if<
654  sizeof...(RemainingComponents) != 0, int>::type = 0>
655  void AddComponentsToView(detail::View &_view,
656  const Entity _entity) const;
657 
662  private: template<typename ...ComponentTypeTs>
663  detail::View &FindView() const;
664 
671  private: bool FindView(const std::set<ComponentTypeId> &_types,
672  std::map<detail::ComponentTypeKey,
673  detail::View>::iterator &_iter) const; // NOLINT
674 
681  AddView(const std::set<ComponentTypeId> &_types,
682  detail::View &&_view) const;
683 
686  private: void UpdateViews(const Entity _entity);
687 
691  private: ComponentId EntityComponentIdFromType(
692  const Entity _entity, const ComponentTypeId _type) const;
693 
699  private: void AddEntityToMessage(msgs::SerializedState &_msg,
700  Entity _entity,
701  const std::unordered_set<ComponentTypeId> &_types = {}) const;
702 
705 
715  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
716  Entity _entity,
717  const std::unordered_set<ComponentTypeId> &_types = {},
718  bool _full = false) const;
719 
720  // Make runners friends so that they can manage entity creation and
721  // removal. This should be safe since runners are internal
722  // to Gazebo.
723  friend class GuiRunner;
724  friend class SimulationRunner;
725 
726  // Make network managers friends so they have control over component
727  // states. Like the runners, the managers are internal.
728  friend class NetworkManagerPrimary;
729  friend class NetworkManagerSecondary;
730 
731  // Make View a friend so that it can access components.
732  // This should be safe since View is internal to Gazebo.
733  friend class detail::View;
734  };
735  }
736  }
737 }
738 
739 #include "ignition/gazebo/detail/EntityComponentManager.hh"
740 
741 #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...