Ignition Gazebo

API Reference

2.10.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 
20 #include <ignition/msgs/serialized.pb.h>
21 #include <ignition/msgs/serialized_map.pb.h>
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: template<typename ComponentTypeT>
221  const ComponentTypeT *First() const;
222 
226  public: template<typename ComponentTypeT>
227  ComponentTypeT *First();
228 
240  public: template<typename ...ComponentTypeTs>
241  Entity EntityByComponents(
242  const ComponentTypeTs &..._desiredComponents) const;
243 
256  public: template<typename ...ComponentTypeTs>
257  std::vector<Entity> EntitiesByComponents(
258  const ComponentTypeTs &..._desiredComponents) const;
259 
275  public: template<typename ...ComponentTypeTs>
276  std::vector<Entity> ChildrenByComponents(Entity _parent,
277  const ComponentTypeTs &..._desiredComponents) const;
278 
280  private: template <typename T>
281  struct identity; // NOLINT
282 
295  public: template<typename ...ComponentTypeTs>
296  void EachNoCache(typename identity<std::function<
297  bool(const Entity &_entity,
298  const ComponentTypeTs *...)>>::type _f) const;
299 
312  public: template<typename ...ComponentTypeTs>
313  void EachNoCache(typename identity<std::function<
314  bool(const Entity &_entity,
315  ComponentTypeTs *...)>>::type _f);
316 
329  public: template<typename ...ComponentTypeTs>
330  void Each(typename identity<std::function<
331  bool(const Entity &_entity,
332  const ComponentTypeTs *...)>>::type _f) const;
333 
346  public: template<typename ...ComponentTypeTs>
347  void Each(typename identity<std::function<
348  bool(const Entity &_entity,
349  ComponentTypeTs *...)>>::type _f);
350 
355  public: template <class Function, class... ComponentTypeTs>
356  static void ForEach(Function _f, const ComponentTypeTs &... _components);
357 
369  public: template <typename... ComponentTypeTs>
370  void EachNew(typename identity<std::function<
371  bool(const Entity &_entity,
372  ComponentTypeTs *...)>>::type _f);
373 
385  public: template <typename... ComponentTypeTs>
386  void EachNew(typename identity<std::function<
387  bool(const Entity &_entity,
388  const ComponentTypeTs *...)>>::type _f) const;
389 
400  public: template<typename ...ComponentTypeTs>
401  void EachRemoved(typename identity<std::function<
402  bool(const Entity &_entity,
403  const ComponentTypeTs *...)>>::type _f) const;
404 
408  public: const EntityGraph &Entities() const;
409 
415  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
416 
425  public: msgs::SerializedState State(
426  const std::unordered_set<Entity> &_entities = {},
427  const std::unordered_set<ComponentTypeId> &_types = {}) const;
428 
443  public: msgs::SerializedState ChangedState() const;
444 
447  public: bool HasNewEntities() const;
448 
451  public: bool HasEntitiesMarkedForRemoval() const;
452 
456  public: bool HasOneTimeComponentChanges() const;
457 
466  public: void SetState(const msgs::SerializedState &_stateMsg);
467 
472  public: gazebo::ComponentState ComponentState(const Entity _entity,
473  const ComponentTypeId _typeId) const;
474 
478  protected: void ClearNewlyCreatedEntities();
479 
483  protected: void ProcessRemoveEntityRequests();
484 
491  private: bool IsNewEntity(const Entity _entity) const;
492 
496  private: bool IsMarkedForRemoval(const Entity _entity) const;
497 
501  private: bool RemoveEntity(const Entity _entity);
502 
506  private: components::BaseComponent *First(
507  const ComponentTypeId _componentTypeId);
508 
515  private: ComponentKey CreateComponentImplementation(
516  const Entity _entity,
517  const ComponentTypeId _componentTypeId,
518  const components::BaseComponent *_data);
519 
525  private: const components::BaseComponent *ComponentImplementation(
526  const Entity _entity,
527  const ComponentTypeId _type) const;
528 
534  private: components::BaseComponent *ComponentImplementation(
535  const Entity _entity,
536  const ComponentTypeId _type);
537 
542  private: const components::BaseComponent *ComponentImplementation(
543  const ComponentKey &_key) const;
544 
549  private: components::BaseComponent *ComponentImplementation(
550  const ComponentKey &_key);
551 
557  private: template<typename FirstComponent,
558  typename ...RemainingComponents,
559  typename std::enable_if<
560  sizeof...(RemainingComponents) == 0, int>::type = 0>
561  void AddComponentsToView(detail::View &_view,
562  const Entity _entity) const;
563 
569  private: template<typename FirstComponent,
570  typename ...RemainingComponents,
571  typename std::enable_if<
572  sizeof...(RemainingComponents) != 0, int>::type = 0>
573  void AddComponentsToView(detail::View &_view,
574  const Entity _entity) const;
575 
580  private: template<typename ...ComponentTypeTs>
581  detail::View &FindView() const;
582 
589  private: bool FindView(const std::set<ComponentTypeId> &_types,
590  std::map<detail::ComponentTypeKey,
591  detail::View>::iterator &_iter) const;
592 
599  AddView(const std::set<ComponentTypeId> &_types,
600  detail::View &&_view) const;
601 
604  private: void UpdateViews(const Entity _entity);
605 
609  private: ComponentId EntityComponentIdFromType(
610  const Entity _entity, const ComponentTypeId _type) const;
611 
617  private: void AddEntityToMessage(msgs::SerializedState &_msg,
618  Entity _entity,
619  const std::unordered_set<ComponentTypeId> &_types = {}) const;
620 
623 
624  // Make runners friends so that they can manage entity creation and
625  // removal. This should be safe since runners are internal
626  // to Gazebo.
627  friend class GuiRunner;
628  friend class SimulationRunner;
629 
630  // Make network managers friends so they have control over component
631  // states. Like the runners, the managers are internal.
632  friend class NetworkManagerPrimary;
633  friend class NetworkManagerSecondary;
634 
635  // Make View a friend so that it can access components.
636  // This should be safe since View is internal to Gazebo.
637  friend class detail::View;
638 
639  // The following function are here for ABI compatibility.
640 
641 
652  public: void State(
653  msgs::SerializedStateMap &_state,
654  const std::unordered_set<Entity> &_entities = {},
655  const std::unordered_set<ComponentTypeId> &_types = {},
656  bool _full = false) const;
657 
673  public: void ChangedState(msgs::SerializedStateMap &_state) const;
674 
683  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
684 
689  public: void SetChanged(
690  const Entity _entity, const ComponentTypeId _type,
692 
694  protected: void SetAllComponentsUnchanged();
695 
705  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
706  Entity _entity,
707  const std::unordered_set<ComponentTypeId> &_types = {},
708  bool _full = false) const;
709  };
710  }
711  }
712 }
713 
714 #include "ignition/gazebo/detail/EntityComponentManager.hh"
715 
716 #endif
Definition: GuiRunner.hh:37
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.
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...