Ignition Gazebo

API Reference

1.0.2
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 <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <typeinfo>
25 #include <type_traits>
26 #include <utility>
27 #include <vector>
28 #include <ignition/common/Console.hh>
29 #include <ignition/math/graph/Graph.hh>
31 #include "ignition/gazebo/Export.hh"
32 #include "ignition/gazebo/Types.hh"
33 
37 
38 namespace ignition
39 {
40  namespace gazebo
41  {
42  // Inline bracket to help doxygen filtering.
43  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
44  // Forward declarations.
45  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
46 
51  using EntityGraph = math::graph::DirectedGraph<Entity, bool>;
52 
56  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
61  {
63  public: EntityComponentManager();
64 
66  public: ~EntityComponentManager();
67 
70  public: Entity CreateEntity();
71 
74  public: size_t EntityCount() const;
75 
87  public: void RequestRemoveEntity(const Entity _entity,
88  bool _recursive = true);
89 
93  public: void RequestRemoveEntities();
94 
98  public: bool HasEntity(const Entity _entity) const;
99 
106  public: Entity ParentEntity(const Entity _entity) const;
107 
116  public: bool SetParentEntity(const Entity _child, const Entity _parent);
117 
121  public: bool HasComponentType(const ComponentTypeId _typeId) const;
122 
127  public: bool EntityHasComponent(const Entity _entity,
128  const ComponentKey &_key) const;
129 
135  public: bool EntityHasComponentType(const Entity _entity,
136  const ComponentTypeId &_typeId) const;
137 
142  public: bool EntityMatches(Entity _entity,
143  const std::set<ComponentTypeId> &_types) const;
144 
150  public: bool RemoveComponent(
151  const Entity _entity, const ComponentKey &_key);
152 
158  public: bool RemoveComponent(
159  const Entity _entity, const ComponentTypeId &_typeId);
160 
166  public: template<typename ComponentTypeT>
167  bool RemoveComponent(Entity _entity);
168 
171  public: void RebuildViews();
172 
179  public: template<typename ComponentTypeT>
180  ComponentKey CreateComponent(const Entity _entity,
181  const ComponentTypeT &_data);
182 
188  public: template<typename ComponentTypeT>
189  const ComponentTypeT *Component(const Entity _entity) const;
190 
196  public: template<typename ComponentTypeT>
197  ComponentTypeT *Component(const Entity _entity);
198 
203  public: template<typename ComponentTypeT>
204  const ComponentTypeT *Component(const ComponentKey &_key) const;
205 
210  public: template<typename ComponentTypeT>
211  ComponentTypeT *Component(const ComponentKey &_key);
212 
216  public: template<typename ComponentTypeT>
217  const ComponentTypeT *First() const;
218 
222  public: template<typename ComponentTypeT>
223  ComponentTypeT *First();
224 
236  public: template<typename ...ComponentTypeTs>
237  Entity EntityByComponents(
238  const ComponentTypeTs &..._desiredComponents) const;
239 
255  public: template<typename ...ComponentTypeTs>
256  std::vector<Entity> ChildrenByComponents(Entity _parent,
257  const ComponentTypeTs &..._desiredComponents) const;
258 
260  private: template <typename T>
261  struct identity; // NOLINT
262 
275  public: template<typename ...ComponentTypeTs>
276  void EachNoCache(typename identity<std::function<
277  bool(const Entity &_entity,
278  const ComponentTypeTs *...)>>::type _f) const;
279 
292  public: template<typename ...ComponentTypeTs>
293  void EachNoCache(typename identity<std::function<
294  bool(const Entity &_entity,
295  ComponentTypeTs *...)>>::type _f);
296 
309  public: template<typename ...ComponentTypeTs>
310  void Each(typename identity<std::function<
311  bool(const Entity &_entity,
312  const ComponentTypeTs *...)>>::type _f) const;
313 
326  public: template<typename ...ComponentTypeTs>
327  void Each(typename identity<std::function<
328  bool(const Entity &_entity,
329  ComponentTypeTs *...)>>::type _f);
330 
335  public: template <class Function, class... ComponentTypeTs>
336  static void ForEach(Function _f, const ComponentTypeTs &... _components);
337 
349  public: template <typename... ComponentTypeTs>
350  void EachNew(typename identity<std::function<
351  bool(const Entity &_entity,
352  ComponentTypeTs *...)>>::type _f);
353 
365  public: template <typename... ComponentTypeTs>
366  void EachNew(typename identity<std::function<
367  bool(const Entity &_entity,
368  const ComponentTypeTs *...)>>::type _f) const;
369 
380  public: template<typename ...ComponentTypeTs>
381  void EachRemoved(typename identity<std::function<
382  bool(const Entity &_entity,
383  const ComponentTypeTs *...)>>::type _f) const;
384 
388  public: const EntityGraph &Entities() const;
389 
393  protected: void ClearNewlyCreatedEntities();
394 
398  protected: void ProcessRemoveEntityRequests();
399 
406  private: bool IsNewEntity(const Entity _entity) const;
407 
411  private: bool IsMarkedForRemoval(const Entity _entity) const;
412 
416  private: bool RemoveEntity(const Entity _entity);
417 
421  private: components::BaseComponent *First(
422  const ComponentTypeId _componentTypeId);
423 
430  private: ComponentKey CreateComponentImplementation(
431  const Entity _entity,
432  const ComponentTypeId _componentTypeId,
433  const components::BaseComponent *_data);
434 
440  private: const components::BaseComponent *ComponentImplementation(
441  const Entity _entity,
442  const ComponentTypeId _type) const;
443 
449  private: components::BaseComponent *ComponentImplementation(
450  const Entity _entity,
451  const ComponentTypeId _type);
452 
457  private: const components::BaseComponent *ComponentImplementation(
458  const ComponentKey &_key) const;
459 
464  private: components::BaseComponent *ComponentImplementation(
465  const ComponentKey &_key);
466 
472  private: template<typename FirstComponent,
473  typename ...RemainingComponents,
474  typename std::enable_if<
475  sizeof...(RemainingComponents) == 0, int>::type = 0>
476  void AddComponentsToView(detail::View &_view,
477  const Entity _entity) const;
478 
484  private: template<typename FirstComponent,
485  typename ...RemainingComponents,
486  typename std::enable_if<
487  sizeof...(RemainingComponents) != 0, int>::type = 0>
488  void AddComponentsToView(detail::View &_view,
489  const Entity _entity) const;
490 
495  private: template<typename ...ComponentTypeTs>
496  detail::View &FindView() const;
497 
504  private: bool FindView(const std::set<ComponentTypeId> &_types,
505  std::map<detail::ComponentTypeKey,
506  detail::View>::iterator &_iter) const;
507 
513  private: std::map<detail::ComponentTypeKey, detail::View>::iterator
514  AddView(const std::set<ComponentTypeId> &_types,
515  detail::View &&_view) const;
516 
519  private: void UpdateViews(const Entity _entity);
520 
521  private: ComponentId EntityComponentIdFromType(
522  const Entity _entity, const ComponentTypeId _type) const;
523 
525  private: std::unique_ptr<EntityComponentManagerPrivate> dataPtr;
526 
530  friend class SimulationRunner;
531 
533  // This should be safe since View is internal to Gazebo.
534  friend class detail::View;
535  };
536  }
537  }
538 }
539 
541 
542 #endif
Component< Entity, class ParentEntityTag > ParentEntity
This component holds an entity&#39;s parent entity.
Definition: ParentEntity.hh:41
std::pair< ComponentTypeId, ComponentId > ComponentKey
A key that uniquely identifies, at the global scope, a component instance.
Definition: Types.hh:75
The EntityComponentManager constructs, deletes, and returns components and entities. A component can be of any class which inherits from components::BaseComponent.
Definition: EntityComponentManager.hh:60
math::graph::DirectedGraph< Entity, bool > EntityGraph
Type alias for the graph that holds entities. Each vertex is an entity, and the direction points from...
Definition: EntityComponentManager.hh:51
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: Types.hh:71
class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate
Definition: EntityComponentManager.hh:45
int ComponentId
A unique identifier for a component instance. The uniqueness of a ComponentId is scoped to the compon...
Definition: Types.hh:66
This library is part of the Ignition Robotics project.
Definition: Altimeter.hh:25
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:195