Ignition Gazebo

API Reference

7.0.0~pre1
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 
107  public: Entity Clone(Entity _entity, Entity _parent,
108  const std::string &_name, bool _allowRename);
109 
112  public: size_t EntityCount() const;
113 
125  public: void RequestRemoveEntity(const Entity _entity,
126  bool _recursive = true);
127 
143  public: void PinEntity(const Entity _entity, bool _recursive = true);
144 
151  public: void UnpinEntity(const Entity _entity, bool _recursive = true);
152 
155  public: void UnpinAllEntities();
156 
160  public: void RequestRemoveEntities();
161 
165  public: bool HasEntity(const Entity _entity) const;
166 
173  public: Entity ParentEntity(const Entity _entity) const;
174 
185  public: bool SetParentEntity(const Entity _child, const Entity _parent);
186 
190  public: bool HasComponentType(const ComponentTypeId _typeId) const;
191 
196  public: bool IGN_DEPRECATED(6) EntityHasComponent(const Entity _entity,
197  const ComponentKey &_key) const;
198 
204  public: bool EntityHasComponentType(const Entity _entity,
205  const ComponentTypeId &_typeId) const;
206 
211  public: bool EntityMatches(Entity _entity,
212  const std::set<ComponentTypeId> &_types) const;
213 
219  public: bool IGN_DEPRECATED(6) RemoveComponent(
220  const Entity _entity, const ComponentKey &_key);
221 
227  public: bool RemoveComponent(
228  const Entity _entity, const ComponentTypeId &_typeId);
229 
235  public: template<typename ComponentTypeT>
236  bool RemoveComponent(Entity _entity);
237 
240  public: void RebuildViews();
241 
250  public: template<typename ComponentTypeT>
251  ComponentTypeT *CreateComponent(
252  const Entity _entity,
253  const ComponentTypeT &_data);
254 
260  public: template<typename ComponentTypeT>
261  const ComponentTypeT *Component(const Entity _entity) const;
262 
268  public: template<typename ComponentTypeT>
269  ComponentTypeT *Component(const Entity _entity);
270 
275  public: template<typename ComponentTypeT>
276  const ComponentTypeT IGN_DEPRECATED(6) * Component(
277  const ComponentKey &_key) const;
278 
283  public: template<typename ComponentTypeT>
284  ComponentTypeT IGN_DEPRECATED(6) * Component(
285  const ComponentKey &_key);
286 
295  public: template<typename ComponentTypeT>
296  ComponentTypeT *ComponentDefault(Entity _entity,
297  const typename ComponentTypeT::Type &_default =
298  typename ComponentTypeT::Type());
299 
308  public: template<typename ComponentTypeT>
309  std::optional<typename ComponentTypeT::Type> ComponentData(
310  const Entity _entity) const;
311 
322  public: template<typename ComponentTypeT>
323  bool SetComponentData(const Entity _entity,
324  const typename ComponentTypeT::Type &_data);
325 
329  public: std::unordered_set<ComponentTypeId> ComponentTypes(
330  Entity _entity) const;
331 
335  public: template<typename ComponentTypeT>
336  const ComponentTypeT IGN_DEPRECATED(6) * First() const;
337 
341  public: template<typename ComponentTypeT>
342  ComponentTypeT IGN_DEPRECATED(6) * First();
343 
355  public: template<typename ...ComponentTypeTs>
356  Entity EntityByComponents(
357  const ComponentTypeTs &..._desiredComponents) const;
358 
371  public: template<typename ...ComponentTypeTs>
372  std::vector<Entity> EntitiesByComponents(
373  const ComponentTypeTs &..._desiredComponents) const;
374 
390  public: template<typename ...ComponentTypeTs>
391  std::vector<Entity> ChildrenByComponents(Entity _parent,
392  const ComponentTypeTs &..._desiredComponents) const;
393 
395  private: template <typename T>
396  struct identity; // NOLINT
397 
410  private: Entity CloneImpl(Entity _entity, Entity _parent,
411  const std::string &_name, bool _allowRename);
412 
425  public: template<typename ...ComponentTypeTs>
426  void EachNoCache(typename identity<std::function<
427  bool(const Entity &_entity,
428  const ComponentTypeTs *...)>>::type _f) const;
429 
442  public: template<typename ...ComponentTypeTs>
443  void EachNoCache(typename identity<std::function<
444  bool(const Entity &_entity,
445  ComponentTypeTs *...)>>::type _f);
446 
459  public: template<typename ...ComponentTypeTs>
460  void Each(typename identity<std::function<
461  bool(const Entity &_entity,
462  const ComponentTypeTs *...)>>::type _f) const;
463 
476  public: template<typename ...ComponentTypeTs>
477  void Each(typename identity<std::function<
478  bool(const Entity &_entity,
479  ComponentTypeTs *...)>>::type _f);
480 
485  public: template <class Function, class... ComponentTypeTs>
486  static void ForEach(Function _f, const ComponentTypeTs &... _components);
487 
502  public: template <typename... ComponentTypeTs>
503  void EachNew(typename identity<std::function<
504  bool(const Entity &_entity,
505  ComponentTypeTs *...)>>::type _f);
506 
519  public: template <typename... ComponentTypeTs>
520  void EachNew(typename identity<std::function<
521  bool(const Entity &_entity,
522  const ComponentTypeTs *...)>>::type _f) const;
523 
534  public: template<typename ...ComponentTypeTs>
535  void EachRemoved(typename identity<std::function<
536  bool(const Entity &_entity,
537  const ComponentTypeTs *...)>>::type _f) const;
538 
542  public: const EntityGraph &Entities() const;
543 
549  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
550 
559  public: msgs::SerializedState State(
560  const std::unordered_set<Entity> &_entities = {},
561  const std::unordered_set<ComponentTypeId> &_types = {}) const;
562 
575  public: msgs::SerializedState ChangedState() const;
576 
579  public: bool HasNewEntities() const;
580 
583  public: bool HasEntitiesMarkedForRemoval() const;
584 
588  public: bool HasOneTimeComponentChanges() const;
589 
594  ComponentTypesWithPeriodicChanges() const;
595 
604  public: void SetState(const msgs::SerializedState &_stateMsg);
605 
617  public: void State(
618  msgs::SerializedStateMap &_state,
619  const std::unordered_set<Entity> &_entities = {},
620  const std::unordered_set<ComponentTypeId> &_types = {},
621  bool _full = false) const;
622 
636  public: void ChangedState(msgs::SerializedStateMap &_state) const;
637 
646  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
647 
652  public: void SetChanged(
653  const Entity _entity, const ComponentTypeId _type,
655 
660  public: gazebo::ComponentState ComponentState(const Entity _entity,
661  const ComponentTypeId _typeId) const;
662 
667  public: void SetEntityCreateOffset(uint64_t _offset);
668 
672  protected: void ClearNewlyCreatedEntities();
673 
676  protected: void ClearRemovedComponents();
677 
681  protected: void ProcessRemoveEntityRequests();
682 
684  protected: void SetAllComponentsUnchanged();
685 
692  private: bool IsNewEntity(const Entity _entity) const;
693 
697  private: bool IsMarkedForRemoval(const Entity _entity) const;
698 
706  private: bool CreateComponentImplementation(
707  const Entity _entity,
708  const ComponentTypeId _componentTypeId,
709  const components::BaseComponent *_data);
710 
716  private: const components::BaseComponent *ComponentImplementation(
717  const Entity _entity,
718  const ComponentTypeId _type) const;
719 
725  private: components::BaseComponent *ComponentImplementation(
726  const Entity _entity,
727  const ComponentTypeId _type);
728 
733  private: template<typename ...ComponentTypeTs>
734  detail::View<ComponentTypeTs...> *FindView() const;
735 
744  const std::vector<ComponentTypeId> &_types) const;
745 
751  private: detail::BaseView *AddView(
752  const detail::ComponentTypeKey &_types,
754 
760  private: void AddEntityToMessage(msgs::SerializedState &_msg,
761  Entity _entity,
762  const std::unordered_set<ComponentTypeId> &_types = {}) const;
763 
766 
776  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
777  Entity _entity,
778  const std::unordered_set<ComponentTypeId> &_types = {},
779  bool _full = false) const;
780 
789  private: void LockAddingEntitiesToViews(bool _lock);
790 
795  private: bool LockAddingEntitiesToViews() const;
796 
797  // Make runners friends so that they can manage entity creation and
798  // removal. This should be safe since runners are internal
799  // to Gazebo.
800  friend class GuiRunner;
801  friend class SimulationRunner;
802 
803  // Make network managers friends so they have control over component
804  // states. Like the runners, the managers are internal.
805  friend class NetworkManagerPrimary;
806  friend class NetworkManagerSecondary;
807  };
808  }
809  }
810 }
811 
812 #include "ignition/gazebo/detail/EntityComponentManager.hh"
813 
814 #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: 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: 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...