Ignition Gazebo

API Reference

6.6.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 <optional>
26 #include <set>
27 #include <string>
28 #include <typeinfo>
29 #include <type_traits>
30 #include <unordered_set>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
38 #include "ignition/gazebo/Export.hh"
39 #include "ignition/gazebo/Types.hh"
40 
42 #include "ignition/gazebo/detail/View.hh"
43 
44 namespace ignition
45 {
46  namespace gazebo
47  {
48  // Inline bracket to help doxygen filtering.
49  inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
50  // Forward declarations.
51  class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate;
52 
58 
62  class IGNITION_GAZEBO_VISIBLE EntityComponentManager
67  {
69  public: EntityComponentManager();
70 
72  public: ~EntityComponentManager();
73 
76  public: Entity CreateEntity();
77 
110  public: Entity Clone(Entity _entity, Entity _parent,
111  const std::string &_name, bool _allowRename);
112 
115  public: size_t EntityCount() const;
116 
128  public: void RequestRemoveEntity(const Entity _entity,
129  bool _recursive = true);
130 
146  public: void PinEntity(const Entity _entity, bool _recursive = true);
147 
154  public: void UnpinEntity(const Entity _entity, bool _recursive = true);
155 
158  public: void UnpinAllEntities();
159 
163  public: void RequestRemoveEntities();
164 
168  public: bool HasEntity(const Entity _entity) const;
169 
176  public: Entity ParentEntity(const Entity _entity) const;
177 
188  public: bool SetParentEntity(const Entity _child, const Entity _parent);
189 
193  public: bool HasComponentType(const ComponentTypeId _typeId) const;
194 
199  public: bool IGN_DEPRECATED(6) EntityHasComponent(const Entity _entity,
200  const ComponentKey &_key) const;
201 
207  public: bool EntityHasComponentType(const Entity _entity,
208  const ComponentTypeId &_typeId) const;
209 
214  public: bool EntityMatches(Entity _entity,
215  const std::set<ComponentTypeId> &_types) const;
216 
222  public: bool IGN_DEPRECATED(6) RemoveComponent(
223  const Entity _entity, const ComponentKey &_key);
224 
230  public: bool RemoveComponent(
231  const Entity _entity, const ComponentTypeId &_typeId);
232 
238  public: template<typename ComponentTypeT>
239  bool RemoveComponent(Entity _entity);
240 
243  public: void RebuildViews();
244 
253  public: template<typename ComponentTypeT>
254  ComponentTypeT *CreateComponent(
255  const Entity _entity,
256  const ComponentTypeT &_data);
257 
263  public: template<typename ComponentTypeT>
264  const ComponentTypeT *Component(const Entity _entity) const;
265 
271  public: template<typename ComponentTypeT>
272  ComponentTypeT *Component(const Entity _entity);
273 
278  public: template<typename ComponentTypeT>
279  const ComponentTypeT IGN_DEPRECATED(6) * Component(
280  const ComponentKey &_key) const;
281 
286  public: template<typename ComponentTypeT>
287  ComponentTypeT IGN_DEPRECATED(6) * Component(
288  const ComponentKey &_key);
289 
298  public: template<typename ComponentTypeT>
299  ComponentTypeT *ComponentDefault(Entity _entity,
300  const typename ComponentTypeT::Type &_default =
301  typename ComponentTypeT::Type());
302 
311  public: template<typename ComponentTypeT>
312  std::optional<typename ComponentTypeT::Type> ComponentData(
313  const Entity _entity) const;
314 
325  public: template<typename ComponentTypeT>
326  bool SetComponentData(const Entity _entity,
327  const typename ComponentTypeT::Type &_data);
328 
332  public: std::unordered_set<ComponentTypeId> ComponentTypes(
333  Entity _entity) const;
334 
338  public: template<typename ComponentTypeT>
339  const ComponentTypeT IGN_DEPRECATED(6) * First() const;
340 
344  public: template<typename ComponentTypeT>
345  ComponentTypeT IGN_DEPRECATED(6) * First();
346 
358  public: template<typename ...ComponentTypeTs>
359  Entity EntityByComponents(
360  const ComponentTypeTs &..._desiredComponents) const;
361 
374  public: template<typename ...ComponentTypeTs>
375  std::vector<Entity> EntitiesByComponents(
376  const ComponentTypeTs &..._desiredComponents) const;
377 
393  public: template<typename ...ComponentTypeTs>
394  std::vector<Entity> ChildrenByComponents(Entity _parent,
395  const ComponentTypeTs &..._desiredComponents) const;
396 
398  private: template <typename T>
399  struct identity; // NOLINT
400 
413  private: Entity CloneImpl(Entity _entity, Entity _parent,
414  const std::string &_name, bool _allowRename);
415 
428  public: template<typename ...ComponentTypeTs>
429  void EachNoCache(typename identity<std::function<
430  bool(const Entity &_entity,
431  const ComponentTypeTs *...)>>::type _f) const;
432 
445  public: template<typename ...ComponentTypeTs>
446  void EachNoCache(typename identity<std::function<
447  bool(const Entity &_entity,
448  ComponentTypeTs *...)>>::type _f);
449 
462  public: template<typename ...ComponentTypeTs>
463  void Each(typename identity<std::function<
464  bool(const Entity &_entity,
465  const ComponentTypeTs *...)>>::type _f) const;
466 
479  public: template<typename ...ComponentTypeTs>
480  void Each(typename identity<std::function<
481  bool(const Entity &_entity,
482  ComponentTypeTs *...)>>::type _f);
483 
488  public: template <class Function, class... ComponentTypeTs>
489  static void ForEach(Function _f, const ComponentTypeTs &... _components);
490 
505  public: template <typename... ComponentTypeTs>
506  void EachNew(typename identity<std::function<
507  bool(const Entity &_entity,
508  ComponentTypeTs *...)>>::type _f);
509 
522  public: template <typename... ComponentTypeTs>
523  void EachNew(typename identity<std::function<
524  bool(const Entity &_entity,
525  const ComponentTypeTs *...)>>::type _f) const;
526 
537  public: template<typename ...ComponentTypeTs>
538  void EachRemoved(typename identity<std::function<
539  bool(const Entity &_entity,
540  const ComponentTypeTs *...)>>::type _f) const;
541 
545  public: const EntityGraph &Entities() const;
546 
552  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
553 
562  public: msgs::SerializedState State(
563  const std::unordered_set<Entity> &_entities = {},
564  const std::unordered_set<ComponentTypeId> &_types = {}) const;
565 
578  public: msgs::SerializedState ChangedState() const;
579 
582  public: bool HasNewEntities() const;
583 
586  public: bool HasEntitiesMarkedForRemoval() const;
587 
591  public: bool HasOneTimeComponentChanges() const;
592 
597  ComponentTypesWithPeriodicChanges() const;
598 
607  public: void SetState(const msgs::SerializedState &_stateMsg);
608 
620  public: void State(
621  msgs::SerializedStateMap &_state,
622  const std::unordered_set<Entity> &_entities = {},
623  const std::unordered_set<ComponentTypeId> &_types = {},
624  bool _full = false) const;
625 
639  public: void ChangedState(msgs::SerializedStateMap &_state) const;
640 
649  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
650 
655  public: void SetChanged(
656  const Entity _entity, const ComponentTypeId _type,
658 
663  public: gazebo::ComponentState ComponentState(const Entity _entity,
664  const ComponentTypeId _typeId) const;
665 
670  public: void SetEntityCreateOffset(uint64_t _offset);
671 
674  public: bool HasRemovedComponents() const;
675 
679  protected: void ClearNewlyCreatedEntities();
680 
683  protected: void ClearRemovedComponents();
684 
688  protected: void ProcessRemoveEntityRequests();
689 
691  protected: void SetAllComponentsUnchanged();
692 
699  private: bool IsNewEntity(const Entity _entity) const;
700 
704  private: bool IsMarkedForRemoval(const Entity _entity) const;
705 
713  private: bool CreateComponentImplementation(
714  const Entity _entity,
715  const ComponentTypeId _componentTypeId,
716  const components::BaseComponent *_data);
717 
723  private: const components::BaseComponent *ComponentImplementation(
724  const Entity _entity,
725  const ComponentTypeId _type) const;
726 
732  private: components::BaseComponent *ComponentImplementation(
733  const Entity _entity,
734  const ComponentTypeId _type);
735 
740  private: template<typename ...ComponentTypeTs>
741  detail::View<ComponentTypeTs...> *FindView() const;
742 
751  const std::vector<ComponentTypeId> &_types) const;
752 
758  private: detail::BaseView *AddView(
759  const detail::ComponentTypeKey &_types,
761 
767  private: void AddEntityToMessage(msgs::SerializedState &_msg,
768  Entity _entity,
769  const std::unordered_set<ComponentTypeId> &_types = {}) const;
770 
773 
783  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
784  Entity _entity,
785  const std::unordered_set<ComponentTypeId> &_types = {},
786  bool _full = false) const;
787 
796  private: void LockAddingEntitiesToViews(bool _lock);
797 
802  private: bool LockAddingEntitiesToViews() const;
803 
804  // Make runners friends so that they can manage entity creation and
805  // removal. This should be safe since runners are internal
806  // to Gazebo.
807  friend class GuiRunner;
808  friend class SimulationRunner;
809 
810  // Make network managers friends so they have control over component
811  // states. Like the runners, the managers are internal.
812  friend class NetworkManagerPrimary;
813  friend class NetworkManagerSecondary;
814  };
815  }
816  }
817 }
818 
819 #include "ignition/gazebo/detail/EntityComponentManager.hh"
820 
821 #endif
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:66
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: include/ignition/gazebo/Types.hh:90
class IGNITION_GAZEBO_HIDDEN EntityComponentManagerPrivate
Definition: EntityComponentManager.hh:51
STL class.
STL class.
#define IGN_DEPRECATED(version)
ComponentState
Possible states for a component.
Definition: include/ignition/gazebo/Types.hh:65
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...