Ignition Gazebo

API Reference

3.9.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 
122  public: bool SetParentEntity(const Entity _child, const Entity _parent);
123 
127  public: bool HasComponentType(const ComponentTypeId _typeId) const;
128 
133  public: bool EntityHasComponent(const Entity _entity,
134  const ComponentKey &_key) const;
135 
141  public: bool EntityHasComponentType(const Entity _entity,
142  const ComponentTypeId &_typeId) const;
143 
148  public: bool EntityMatches(Entity _entity,
149  const std::set<ComponentTypeId> &_types) const;
150 
156  public: bool RemoveComponent(
157  const Entity _entity, const ComponentKey &_key);
158 
164  public: bool RemoveComponent(
165  const Entity _entity, const ComponentTypeId &_typeId);
166 
172  public: template<typename ComponentTypeT>
173  bool RemoveComponent(Entity _entity);
174 
177  public: void RebuildViews();
178 
185  public: template<typename ComponentTypeT>
186  ComponentKey CreateComponent(const Entity _entity,
187  const ComponentTypeT &_data);
188 
194  public: template<typename ComponentTypeT>
195  const ComponentTypeT *Component(const Entity _entity) const;
196 
202  public: template<typename ComponentTypeT>
203  ComponentTypeT *Component(const Entity _entity);
204 
209  public: template<typename ComponentTypeT>
210  const ComponentTypeT *Component(const ComponentKey &_key) const;
211 
216  public: template<typename ComponentTypeT>
217  ComponentTypeT *Component(const ComponentKey &_key);
218 
227  public: template<typename ComponentTypeT>
228  ComponentTypeT *ComponentDefault(Entity _entity,
229  const typename ComponentTypeT::Type &_default =
230  typename ComponentTypeT::Type());
231 
240  public: template<typename ComponentTypeT>
241  std::optional<typename ComponentTypeT::Type> ComponentData(
242  const Entity _entity) const;
243 
254  public: template<typename ComponentTypeT>
255  bool SetComponentData(const Entity _entity,
256  const typename ComponentTypeT::Type &_data);
257 
261  public: std::unordered_set<ComponentTypeId> ComponentTypes(
262  Entity _entity) const;
263 
267  public: template<typename ComponentTypeT>
268  const ComponentTypeT *First() const;
269 
273  public: template<typename ComponentTypeT>
274  ComponentTypeT *First();
275 
287  public: template<typename ...ComponentTypeTs>
288  Entity EntityByComponents(
289  const ComponentTypeTs &..._desiredComponents) const;
290 
303  public: template<typename ...ComponentTypeTs>
304  std::vector<Entity> EntitiesByComponents(
305  const ComponentTypeTs &..._desiredComponents) const;
306 
322  public: template<typename ...ComponentTypeTs>
323  std::vector<Entity> ChildrenByComponents(Entity _parent,
324  const ComponentTypeTs &..._desiredComponents) const;
325 
327  private: template <typename T>
328  struct identity; // NOLINT
329 
342  public: template<typename ...ComponentTypeTs>
343  void EachNoCache(typename identity<std::function<
344  bool(const Entity &_entity,
345  const ComponentTypeTs *...)>>::type _f) const;
346 
359  public: template<typename ...ComponentTypeTs>
360  void EachNoCache(typename identity<std::function<
361  bool(const Entity &_entity,
362  ComponentTypeTs *...)>>::type _f);
363 
376  public: template<typename ...ComponentTypeTs>
377  void Each(typename identity<std::function<
378  bool(const Entity &_entity,
379  const ComponentTypeTs *...)>>::type _f) const;
380 
393  public: template<typename ...ComponentTypeTs>
394  void Each(typename identity<std::function<
395  bool(const Entity &_entity,
396  ComponentTypeTs *...)>>::type _f);
397 
402  public: template <class Function, class... ComponentTypeTs>
403  static void ForEach(Function _f, const ComponentTypeTs &... _components);
404 
419  public: template <typename... ComponentTypeTs>
420  void EachNew(typename identity<std::function<
421  bool(const Entity &_entity,
422  ComponentTypeTs *...)>>::type _f);
423 
436  public: template <typename... ComponentTypeTs>
437  void EachNew(typename identity<std::function<
438  bool(const Entity &_entity,
439  const ComponentTypeTs *...)>>::type _f) const;
440 
451  public: template<typename ...ComponentTypeTs>
452  void EachRemoved(typename identity<std::function<
453  bool(const Entity &_entity,
454  const ComponentTypeTs *...)>>::type _f) const;
455 
459  public: const EntityGraph &Entities() const;
460 
466  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
467 
476  public: msgs::SerializedState State(
477  const std::unordered_set<Entity> &_entities = {},
478  const std::unordered_set<ComponentTypeId> &_types = {}) const;
479 
494  public: msgs::SerializedState ChangedState() const;
495 
498  public: bool HasNewEntities() const;
499 
502  public: bool HasEntitiesMarkedForRemoval() const;
503 
507  public: bool HasOneTimeComponentChanges() const;
508 
513  ComponentTypesWithPeriodicChanges() const;
514 
523  public: void SetState(const msgs::SerializedState &_stateMsg);
524 
536  public: void State(
537  msgs::SerializedStateMap &_state,
538  const std::unordered_set<Entity> &_entities = {},
539  const std::unordered_set<ComponentTypeId> &_types = {},
540  bool _full = false) const;
541 
557  public: void ChangedState(msgs::SerializedStateMap &_state) const;
558 
567  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
568 
573  public: void SetChanged(
574  const Entity _entity, const ComponentTypeId _type,
576 
581  public: gazebo::ComponentState ComponentState(const Entity _entity,
582  const ComponentTypeId _typeId) const;
583 
588  public: void SetEntityCreateOffset(uint64_t _offset);
589 
593  protected: void ClearNewlyCreatedEntities();
594 
598  protected: void ProcessRemoveEntityRequests();
599 
601  protected: void SetAllComponentsUnchanged();
602 
609  private: bool IsNewEntity(const Entity _entity) const;
610 
614  private: bool IsMarkedForRemoval(const Entity _entity) const;
615 
619  private: bool RemoveEntity(const Entity _entity);
620 
624  private: components::BaseComponent *First(
625  const ComponentTypeId _componentTypeId);
626 
633  private: ComponentKey CreateComponentImplementation(
634  const Entity _entity,
635  const ComponentTypeId _componentTypeId,
636  const components::BaseComponent *_data);
637 
643  private: const components::BaseComponent *ComponentImplementation(
644  const Entity _entity,
645  const ComponentTypeId _type) const;
646 
652  private: components::BaseComponent *ComponentImplementation(
653  const Entity _entity,
654  const ComponentTypeId _type);
655 
660  private: const components::BaseComponent *ComponentImplementation(
661  const ComponentKey &_key) const;
662 
667  private: components::BaseComponent *ComponentImplementation(
668  const ComponentKey &_key);
669 
675  private: template<typename FirstComponent,
676  typename ...RemainingComponents,
677  typename std::enable_if<
678  sizeof...(RemainingComponents) == 0, int>::type = 0>
679  void AddComponentsToView(detail::View &_view,
680  const Entity _entity) const;
681 
687  private: template<typename FirstComponent,
688  typename ...RemainingComponents,
689  typename std::enable_if<
690  sizeof...(RemainingComponents) != 0, int>::type = 0>
691  void AddComponentsToView(detail::View &_view,
692  const Entity _entity) const;
693 
698  private: template<typename ...ComponentTypeTs>
699  detail::View &FindView() const;
700 
707  private: bool FindView(const std::set<ComponentTypeId> &_types,
708  std::map<detail::ComponentTypeKey,
709  detail::View>::iterator &_iter) const; // NOLINT
710 
717  AddView(const std::set<ComponentTypeId> &_types,
718  detail::View &&_view) const;
719 
722  private: void UpdateViews(const Entity _entity);
723 
727  private: ComponentId EntityComponentIdFromType(
728  const Entity _entity, const ComponentTypeId _type) const;
729 
735  private: void AddEntityToMessage(msgs::SerializedState &_msg,
736  Entity _entity,
737  const std::unordered_set<ComponentTypeId> &_types = {}) const;
738 
741 
751  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
752  Entity _entity,
753  const std::unordered_set<ComponentTypeId> &_types = {},
754  bool _full = false) const;
755 
756  // Make runners friends so that they can manage entity creation and
757  // removal. This should be safe since runners are internal
758  // to Gazebo.
759  friend class GuiRunner;
760  friend class SimulationRunner;
761 
762  // Make network managers friends so they have control over component
763  // states. Like the runners, the managers are internal.
764  friend class NetworkManagerPrimary;
765  friend class NetworkManagerSecondary;
766 
767  // Make View a friend so that it can access components.
768  // This should be safe since View is internal to Gazebo.
769  friend class detail::View;
770  };
771  }
772  }
773 }
774 
775 #include "ignition/gazebo/detail/EntityComponentManager.hh"
776 
777 #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...