Ignition Gazebo

API Reference

3.3.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 
536  public: void SetEntityCreateOffset(uint64_t _offset);
537 
541  protected: void ClearNewlyCreatedEntities();
542 
546  protected: void ProcessRemoveEntityRequests();
547 
549  protected: void SetAllComponentsUnchanged();
550 
557  private: bool IsNewEntity(const Entity _entity) const;
558 
562  private: bool IsMarkedForRemoval(const Entity _entity) const;
563 
567  private: bool RemoveEntity(const Entity _entity);
568 
572  private: components::BaseComponent *First(
573  const ComponentTypeId _componentTypeId);
574 
581  private: ComponentKey CreateComponentImplementation(
582  const Entity _entity,
583  const ComponentTypeId _componentTypeId,
584  const components::BaseComponent *_data);
585 
591  private: const components::BaseComponent *ComponentImplementation(
592  const Entity _entity,
593  const ComponentTypeId _type) const;
594 
600  private: components::BaseComponent *ComponentImplementation(
601  const Entity _entity,
602  const ComponentTypeId _type);
603 
608  private: const components::BaseComponent *ComponentImplementation(
609  const ComponentKey &_key) const;
610 
615  private: components::BaseComponent *ComponentImplementation(
616  const ComponentKey &_key);
617 
623  private: template<typename FirstComponent,
624  typename ...RemainingComponents,
625  typename std::enable_if<
626  sizeof...(RemainingComponents) == 0, int>::type = 0>
627  void AddComponentsToView(detail::View &_view,
628  const Entity _entity) const;
629 
635  private: template<typename FirstComponent,
636  typename ...RemainingComponents,
637  typename std::enable_if<
638  sizeof...(RemainingComponents) != 0, int>::type = 0>
639  void AddComponentsToView(detail::View &_view,
640  const Entity _entity) const;
641 
646  private: template<typename ...ComponentTypeTs>
647  detail::View &FindView() const;
648 
655  private: bool FindView(const std::set<ComponentTypeId> &_types,
656  std::map<detail::ComponentTypeKey,
657  detail::View>::iterator &_iter) const; // NOLINT
658 
665  AddView(const std::set<ComponentTypeId> &_types,
666  detail::View &&_view) const;
667 
670  private: void UpdateViews(const Entity _entity);
671 
675  private: ComponentId EntityComponentIdFromType(
676  const Entity _entity, const ComponentTypeId _type) const;
677 
683  private: void AddEntityToMessage(msgs::SerializedState &_msg,
684  Entity _entity,
685  const std::unordered_set<ComponentTypeId> &_types = {}) const;
686 
689 
699  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
700  Entity _entity,
701  const std::unordered_set<ComponentTypeId> &_types = {},
702  bool _full = false) const;
703 
704  // Make runners friends so that they can manage entity creation and
705  // removal. This should be safe since runners are internal
706  // to Gazebo.
707  friend class GuiRunner;
708  friend class SimulationRunner;
709 
710  // Make network managers friends so they have control over component
711  // states. Like the runners, the managers are internal.
712  friend class NetworkManagerPrimary;
713  friend class NetworkManagerSecondary;
714 
715  // Make View a friend so that it can access components.
716  // This should be safe since View is internal to Gazebo.
717  friend class detail::View;
718  };
719  }
720  }
721 }
722 
723 #include "ignition/gazebo/detail/EntityComponentManager.hh"
724 
725 #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...