Gazebo Sim

API Reference

7.7.0
gz/sim/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 GZ_SIM_ENTITYCOMPONENTMANAGER_HH_
18 #define GZ_SIM_ENTITYCOMPONENTMANAGER_HH_
19 
20 #include <gz/msgs/serialized.pb.h>
21 #include <gz/msgs/serialized_map.pb.h>
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 
35 #include <gz/common/Console.hh>
36 #include <gz/math/graph/Graph.hh>
37 #include "gz/sim/Entity.hh"
38 #include "gz/sim/Export.hh"
39 #include "gz/sim/Types.hh"
40 
42 #include "gz/sim/detail/View.hh"
43 
44 namespace gz
45 {
46  namespace sim
47  {
48  // Inline bracket to help doxygen filtering.
49  inline namespace GZ_SIM_VERSION_NAMESPACE {
50  // Forward declarations.
51  class GZ_SIM_HIDDEN EntityComponentManagerPrivate;
52  class EntityComponentManagerDiff;
53 
59 
63  class GZ_SIM_VISIBLE EntityComponentManager
68  {
70  public: EntityComponentManager();
71 
73  public: ~EntityComponentManager();
74 
81  public: void CopyFrom(const EntityComponentManager &_fromEcm);
82 
85  public: Entity CreateEntity();
86 
119  public: Entity Clone(Entity _entity, Entity _parent,
120  const std::string &_name, bool _allowRename);
121 
124  public: size_t EntityCount() const;
125 
137  public: void RequestRemoveEntity(const Entity _entity,
138  bool _recursive = true);
139 
155  public: void PinEntity(const Entity _entity, bool _recursive = true);
156 
163  public: void UnpinEntity(const Entity _entity, bool _recursive = true);
164 
167  public: void UnpinAllEntities();
168 
172  public: void RequestRemoveEntities();
173 
177  public: bool HasEntity(const Entity _entity) const;
178 
185  public: Entity ParentEntity(const Entity _entity) const;
186 
197  public: bool SetParentEntity(const Entity _child, const Entity _parent);
198 
202  public: bool HasComponentType(const ComponentTypeId _typeId) const;
203 
209  public: bool EntityHasComponentType(const Entity _entity,
210  const ComponentTypeId &_typeId) const;
211 
216  public: bool EntityMatches(Entity _entity,
217  const std::set<ComponentTypeId> &_types) const;
218 
224  public: bool RemoveComponent(
225  const Entity _entity, const ComponentTypeId &_typeId);
226 
232  public: template<typename ComponentTypeT>
233  bool RemoveComponent(Entity _entity);
234 
237  public: void RebuildViews();
238 
247  public: template<typename ComponentTypeT>
248  ComponentTypeT *CreateComponent(
249  const Entity _entity,
250  const ComponentTypeT &_data);
251 
257  public: template<typename ComponentTypeT>
258  const ComponentTypeT *Component(const Entity _entity) const;
259 
265  public: template<typename ComponentTypeT>
266  ComponentTypeT *Component(const Entity _entity);
267 
276  public: template<typename ComponentTypeT>
277  ComponentTypeT *ComponentDefault(Entity _entity,
278  const typename ComponentTypeT::Type &_default =
279  typename ComponentTypeT::Type());
280 
289  public: template<typename ComponentTypeT>
290  std::optional<typename ComponentTypeT::Type> ComponentData(
291  const Entity _entity) const;
292 
303  public: template<typename ComponentTypeT>
304  bool SetComponentData(const Entity _entity,
305  const typename ComponentTypeT::Type &_data);
306 
310  public: std::unordered_set<ComponentTypeId> ComponentTypes(
311  Entity _entity) const;
312 
324  public: template<typename ...ComponentTypeTs>
325  Entity EntityByComponents(
326  const ComponentTypeTs &..._desiredComponents) const;
327 
340  public: template<typename ...ComponentTypeTs>
341  std::vector<Entity> EntitiesByComponents(
342  const ComponentTypeTs &..._desiredComponents) const;
343 
359  public: template<typename ...ComponentTypeTs>
360  std::vector<Entity> ChildrenByComponents(Entity _parent,
361  const ComponentTypeTs &..._desiredComponents) const;
362 
364  private: template <typename T>
365  struct identity; // NOLINT
366 
379  private: Entity CloneImpl(Entity _entity, Entity _parent,
380  const std::string &_name, bool _allowRename);
381 
394  public: template<typename ...ComponentTypeTs>
395  void EachNoCache(typename identity<std::function<
396  bool(const Entity &_entity,
397  const ComponentTypeTs *...)>>::type _f) const;
398 
411  public: template<typename ...ComponentTypeTs>
412  void EachNoCache(typename identity<std::function<
413  bool(const Entity &_entity,
414  ComponentTypeTs *...)>>::type _f);
415 
428  public: template<typename ...ComponentTypeTs>
429  void Each(typename identity<std::function<
430  bool(const Entity &_entity,
431  const ComponentTypeTs *...)>>::type _f) const;
432 
445  public: template<typename ...ComponentTypeTs>
446  void Each(typename identity<std::function<
447  bool(const Entity &_entity,
448  ComponentTypeTs *...)>>::type _f);
449 
454  public: template <class Function, class... ComponentTypeTs>
455  static void ForEach(Function _f, const ComponentTypeTs &... _components);
456 
471  public: template <typename... ComponentTypeTs>
472  void EachNew(typename identity<std::function<
473  bool(const Entity &_entity,
474  ComponentTypeTs *...)>>::type _f);
475 
488  public: template <typename... ComponentTypeTs>
489  void EachNew(typename identity<std::function<
490  bool(const Entity &_entity,
491  const ComponentTypeTs *...)>>::type _f) const;
492 
503  public: template<typename ...ComponentTypeTs>
504  void EachRemoved(typename identity<std::function<
505  bool(const Entity &_entity,
506  const ComponentTypeTs *...)>>::type _f) const;
507 
511  public: const EntityGraph &Entities() const;
512 
518  public: std::unordered_set<Entity> Descendants(Entity _entity) const;
519 
528  public: msgs::SerializedState State(
529  const std::unordered_set<Entity> &_entities = {},
530  const std::unordered_set<ComponentTypeId> &_types = {}) const;
531 
544  public: msgs::SerializedState ChangedState() const;
545 
548  public: bool HasNewEntities() const;
549 
552  public: bool HasEntitiesMarkedForRemoval() const;
553 
557  public: bool HasOneTimeComponentChanges() const;
558 
562  public: bool HasPeriodicComponentChanges() const;
563 
568  ComponentTypesWithPeriodicChanges() const;
569 
578  public: void UpdatePeriodicChangeCache(std::unordered_map<ComponentTypeId,
580 
589  public: void SetState(const msgs::SerializedState &_stateMsg);
590 
602  public: void State(
603  msgs::SerializedStateMap &_state,
604  const std::unordered_set<Entity> &_entities = {},
605  const std::unordered_set<ComponentTypeId> &_types = {},
606  bool _full = false) const;
607 
616  public: void PeriodicStateFromCache(
617  msgs::SerializedStateMap &_state,
619  std::unordered_set<Entity>> &_cache) const;
620 
634  public: void ChangedState(msgs::SerializedStateMap &_state) const;
635 
644  public: void SetState(const msgs::SerializedStateMap &_stateMsg);
645 
650  public: void SetChanged(
651  const Entity _entity, const ComponentTypeId _type,
653 
658  public: sim::ComponentState ComponentState(const Entity _entity,
659  const ComponentTypeId _typeId) const;
660 
665  public: void SetEntityCreateOffset(uint64_t _offset);
666 
672  public: void ResetTo(const EntityComponentManager &_other);
673 
676  public: bool HasRemovedComponents() const;
677 
681  protected: void ClearNewlyCreatedEntities();
682 
685  protected: void ClearRemovedComponents();
686 
690  protected: void ProcessRemoveEntityRequests();
691 
693  protected: void SetAllComponentsUnchanged();
694 
703  protected: EntityComponentManagerDiff ComputeEntityDiff(
704  const EntityComponentManager &_other) const;
705 
711  protected: void ApplyEntityDiff(const EntityComponentManager &_other,
712  const EntityComponentManagerDiff &_diff);
713 
720  private: bool IsNewEntity(const Entity _entity) const;
721 
725  private: bool IsMarkedForRemoval(const Entity _entity) const;
726 
734  private: bool CreateComponentImplementation(
735  const Entity _entity,
736  const ComponentTypeId _componentTypeId,
737  const components::BaseComponent *_data);
738 
744  private: const components::BaseComponent *ComponentImplementation(
745  const Entity _entity,
746  const ComponentTypeId _type) const;
747 
753  private: components::BaseComponent *ComponentImplementation(
754  const Entity _entity,
755  const ComponentTypeId _type);
756 
761  private: template<typename ...ComponentTypeTs>
762  detail::View *FindView() const;
763 
772  const std::vector<ComponentTypeId> &_types) const;
773 
779  private: detail::BaseView *AddView(
780  const detail::ComponentTypeKey &_types,
782 
788  private: void AddEntityToMessage(msgs::SerializedState &_msg,
789  Entity _entity,
790  const std::unordered_set<ComponentTypeId> &_types = {}) const;
791 
794 
804  private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
805  Entity _entity,
806  const std::unordered_set<ComponentTypeId> &_types = {},
807  bool _full = false) const;
808 
817  private: void LockAddingEntitiesToViews(bool _lock);
818 
823  private: bool LockAddingEntitiesToViews() const;
824 
825  // Make runners friends so that they can manage entity creation and
826  // removal. This should be safe since runners are internal
827  // to Gazebo.
828  friend class GuiRunner;
829  friend class SimulationRunner;
830 
831  // Make network managers friends so they have control over component
832  // states. Like the runners, the managers are internal.
833  friend class NetworkManagerPrimary;
834  friend class NetworkManagerSecondary;
835  };
836  }
837  }
838 }
839 
840 #include "gz/sim/detail/EntityComponentManager.hh"
841 
842 #endif