Ignition Gazebo

API Reference

3.2.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 
220  public: std::unordered_set<ComponentTypeId> ComponentTypes(
221  Entity _entity) const;
222 
226  public: template<typename ComponentTypeT>
227  const ComponentTypeT *First() const;
228 
232  public: template<typename ComponentTypeT>
233  ComponentTypeT *First();
234 
246  public: template<typename ...ComponentTypeTs>
247  Entity EntityByComponents(
248  const ComponentTypeTs &..._desiredComponents) const;
249 
262  public: template<typename ...ComponentTypeTs>
263  std::vector<Entity> EntitiesByComponents(
264  const ComponentTypeTs &..._desiredComponents) const;
265 
281  public: template<typename ...ComponentTypeTs>
282  std::vector<Entity> ChildrenByComponents(Entity _parent,
283  const ComponentTypeTs &..._desiredComponents) const;
284 
286  private: template <typename T>
287  struct identity; // NOLINT
288 
301  public: template<typename ...ComponentTypeTs>
302  void EachNoCache(typename identity<std::function<
303  bool(const Entity &_entity,
304  const ComponentTypeTs *...)>>::type _f) const;
305 
318  public: template<typename ...ComponentTypeTs>
319  void EachNoCache(typename identity<std::function<
320  bool(const Entity &_entity,
321  ComponentTypeTs *...)>>::type _f);
322 
335  public: template<typename ...ComponentTypeTs>
336  void Each(typename identity<std::function<
337  bool(const Entity &_entity,
338  const ComponentTypeTs *...)>>::type _f) const;
339 
352  public: template<typename ...ComponentTypeTs>
353  void Each(typename identity<std::function<
354  bool(const Entity &_entity,
355  ComponentTypeTs *...)>>::type _f);
356 
361  public: template <class Function, class... ComponentTypeTs>
362  static void ForEach(Function _f, const ComponentTypeTs &... _components);
363 
375  public: template <typename... ComponentTypeTs>
376  void EachNew(typename identity<std::function<
377  bool(const Entity &_entity,
378  ComponentTypeTs *...)>>::type _f);
379 
391  public: template <typename... ComponentTypeTs>
392  void EachNew(typename identity<std::function<
393  bool(const Entity &_entity,
394  const ComponentTypeTs *...)>>::type _f) const;
395 
406  public: template<typename ...ComponentTypeTs>
407  void EachRemoved(typename identity<std::function<
408  bool(const Entity &_entity,
409  const ComponentTypeTs *...)>>::type _f) const;
410 
414  public: const EntityGraph &Entities() const;
415 
421  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
422 
431  public: msgs::SerializedState State(
432  const std::unordered_set<Entity> &_entities = {},
433  const std::unordered_set<ComponentTypeId> &_types = {}) const;
434 
449  public: msgs::SerializedState ChangedState() const;
450 
453  public: bool HasNewEntities() const;
454 
457  public: bool HasEntitiesMarkedForRemoval() const;
458 
462  public: bool HasOneTimeComponentChanges() const;
463 
472  public: void SetState(const msgs::SerializedState &_stateMsg);
473 
484  public: void State(
485  msgs::SerializedStateMap &_state,
486  const std::unordered_set<Entity> &_entities = {},
487  const std::unordered_set<ComponentTypeId> &_types = {},
488  bool _full = false) const;
489 
505  public: void ChangedState(msgs::SerializedStateMap &_state) const;
506 
515  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
516 
521  public: void SetChanged(
522  const Entity _entity, const ComponentTypeId _type,
524 
529  public: gazebo::ComponentState ComponentState(const Entity _entity,
530  const ComponentTypeId _typeId) const;
531 
535  protected: void ClearNewlyCreatedEntities();
536 
540  protected: void ProcessRemoveEntityRequests();
541 
543  protected: void SetAllComponentsUnchanged();
544 
551  private: bool IsNewEntity(const Entity _entity) const;
552 
556  private: bool IsMarkedForRemoval(const Entity _entity) const;
557 
561  private: bool RemoveEntity(const Entity _entity);
562 
566  private: components::BaseComponent *First(
567  const ComponentTypeId _componentTypeId);
568 
575  private: ComponentKey CreateComponentImplementation(
576  const Entity _entity,
577  const ComponentTypeId _componentTypeId,
578  const components::BaseComponent *_data);
579 
585  private: const components::BaseComponent *ComponentImplementation(
586  const Entity _entity,
587  const ComponentTypeId _type) const;
588 
594  private: components::BaseComponent *ComponentImplementation(
595  const Entity _entity,
596  const ComponentTypeId _type);
597 
602  private: const components::BaseComponent *ComponentImplementation(
603  const ComponentKey &_key) const;
604 
609  private: components::BaseComponent *ComponentImplementation(
610  const ComponentKey &_key);
611 
617  private: template<typename FirstComponent,
618  typename ...RemainingComponents,
619  typename std::enable_if<
620  sizeof...(RemainingComponents) == 0, int>::type = 0>
621  void AddComponentsToView(detail::View &_view,
622  const Entity _entity) const;
623 
629  private: template<typename FirstComponent,
630  typename ...RemainingComponents,
631  typename std::enable_if<
632  sizeof...(RemainingComponents) != 0, int>::type = 0>
633  void AddComponentsToView(detail::View &_view,
634  const Entity _entity) const;
635 
640  private: template<typename ...ComponentTypeTs>
641  detail::View &FindView() const;
642 
649  private: bool FindView(const std::set<ComponentTypeId> &_types,
650  std::map<detail::ComponentTypeKey,
651  detail::View>::iterator &_iter) const; // NOLINT
652 
659  AddView(const std::set<ComponentTypeId> &_types,
660  detail::View &&_view) const;
661 
664  private: void UpdateViews(const Entity _entity);
665 
669  private: ComponentId EntityComponentIdFromType(
670  const Entity _entity, const ComponentTypeId _type) const;
671 
677  private: void AddEntityToMessage(msgs::SerializedState &_msg,
678  Entity _entity,
679  const std::unordered_set<ComponentTypeId> &_types = {}) const;
680 
683 
693  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
694  Entity _entity,
695  const std::unordered_set<ComponentTypeId> &_types = {},
696  bool _full = false) const;
697 
698  // Make runners friends so that they can manage entity creation and
699  // removal. This should be safe since runners are internal
700  // to Gazebo.
701  friend class GuiRunner;
702  friend class SimulationRunner;
703 
704  // Make network managers friends so they have control over component
705  // states. Like the runners, the managers are internal.
706  friend class NetworkManagerPrimary;
707  friend class NetworkManagerSecondary;
708 
709  // Make View a friend so that it can access components.
710  // This should be safe since View is internal to Gazebo.
711  friend class detail::View;
712  };
713  }
714  }
715 }
716 
717 #include "ignition/gazebo/detail/EntityComponentManager.hh"
718 
719 #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...