Gazebo Sim

API Reference

10.5.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 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>
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
44namespace 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
67 class GZ_SIM_VISIBLE EntityComponentManager
68 {
71
74
87 public: void CopyFrom(const EntityComponentManager &_fromEcm);
88
92
125 public: Entity Clone(Entity _entity, Entity _parent,
126 const std::string &_name, bool _allowRename);
127
130 public: size_t EntityCount() const;
131
143 public: void RequestRemoveEntity(const Entity _entity,
144 bool _recursive = true);
145
161 public: void PinEntity(const Entity _entity, bool _recursive = true);
162
169 public: void UnpinEntity(const Entity _entity, bool _recursive = true);
170
173 public: void UnpinAllEntities();
174
178 public: void RequestRemoveEntities();
179
183 public: bool HasEntity(const Entity _entity) const;
184
191 public: Entity ParentEntity(const Entity _entity) const;
192
203 public: bool SetParentEntity(const Entity _child, const Entity _parent);
204
208 public: bool HasComponentType(const ComponentTypeId _typeId) const;
209
215 public: bool EntityHasComponentType(const Entity _entity,
216 const ComponentTypeId &_typeId) const;
217
222 public: bool EntityMatches(Entity _entity,
223 const std::set<ComponentTypeId> &_types) const;
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
282 public: template<typename ComponentTypeT>
283 ComponentTypeT *ComponentDefault(Entity _entity,
284 const typename ComponentTypeT::Type &_default =
285 typename ComponentTypeT::Type());
286
295 public: template<typename ComponentTypeT>
296 std::optional<typename ComponentTypeT::Type> ComponentData(
297 const Entity _entity) const;
298
309 public: template<typename ComponentTypeT>
310 bool SetComponentData(const Entity _entity,
311 const typename ComponentTypeT::Type &_data);
312
317 Entity _entity) const;
318
330 public: template<typename ...ComponentTypeTs>
332 const ComponentTypeTs &..._desiredComponents) const;
333
346 public: template<typename ...ComponentTypeTs>
348 const ComponentTypeTs &..._desiredComponents) const;
349
365 public: template<typename ...ComponentTypeTs>
367 const ComponentTypeTs &..._desiredComponents) const;
368
370 private: template <typename T>
371 struct identity; // NOLINT
372
385 private: Entity CloneImpl(Entity _entity, Entity _parent,
386 const std::string &_name, bool _allowRename);
387
400 public: template<typename ...ComponentTypeTs>
401 void EachNoCache(typename identity<std::function<
402 bool(const Entity &_entity,
403 const ComponentTypeTs *...)>>::type _f) const;
404
417 public: template<typename ...ComponentTypeTs>
418 void EachNoCache(typename identity<std::function<
419 bool(const Entity &_entity,
420 ComponentTypeTs *...)>>::type _f);
421
434 public: template<typename ...ComponentTypeTs>
435 void Each(typename identity<std::function<
436 bool(const Entity &_entity,
437 const ComponentTypeTs *...)>>::type _f) const;
438
451 public: template<typename ...ComponentTypeTs>
452 void Each(typename identity<std::function<
453 bool(const Entity &_entity,
454 ComponentTypeTs *...)>>::type _f);
455
460 public: template <class Function, class... ComponentTypeTs>
461 static void ForEach(Function _f, const ComponentTypeTs &... _components);
462
477 public: template <typename... ComponentTypeTs>
478 void EachNew(typename identity<std::function<
479 bool(const Entity &_entity,
480 ComponentTypeTs *...)>>::type _f);
481
494 public: template <typename... ComponentTypeTs>
495 void EachNew(typename identity<std::function<
496 bool(const Entity &_entity,
497 const ComponentTypeTs *...)>>::type _f) const;
498
509 public: template<typename ...ComponentTypeTs>
510 void EachRemoved(typename identity<std::function<
511 bool(const Entity &_entity,
512 const ComponentTypeTs *...)>>::type _f) const;
513
517 public: const EntityGraph &Entities() const;
518
525
534 public: msgs::SerializedState State(
535 const std::unordered_set<Entity> &_entities = {},
536 const std::unordered_set<ComponentTypeId> &_types = {}) const;
537
550 public: msgs::SerializedState ChangedState() const;
551
554 public: bool HasNewEntities() const;
555
558 public: bool HasEntitiesMarkedForRemoval() const;
559
563 public: bool HasOneTimeComponentChanges() const;
564
568 public: bool HasPeriodicComponentChanges() const;
569
575
585 std::unordered_set<Entity>>&_changes) const;
586
595 public: void SetState(const msgs::SerializedState &_stateMsg);
596
608 public: void State(
609 msgs::SerializedStateMap &_state,
610 const std::unordered_set<Entity> &_entities = {},
611 const std::unordered_set<ComponentTypeId> &_types = {},
612 bool _full = false) const;
613
623 msgs::SerializedStateMap &_state,
625 std::unordered_set<Entity>> &_cache) const;
626
640 public: void ChangedState(msgs::SerializedStateMap &_state) const;
641
650 public: void SetState(const msgs::SerializedStateMap &_stateMsg);
651
656 public: void SetChanged(
657 const Entity _entity, const ComponentTypeId _type,
658 sim::ComponentState _c = ComponentState::OneTimeChange);
659
665 const ComponentTypeId _typeId) const;
666
671 public: void SetEntityCreateOffset(uint64_t _offset);
672
684 public: void ResetTo(const EntityComponentManager &_other);
685
688 public: bool HasRemovedComponents() const;
689
695 public: std::optional<Entity> EntityByName(
696 const std::string &_name) const;
697
701
705
709
712
721 protected: EntityComponentManagerDiff ComputeEntityDiff(
722 const EntityComponentManager &_other) const;
723
730 protected: void ApplyEntityDiff(const EntityComponentManager &_other,
731 const EntityComponentManagerDiff &_diff);
732
739 private: bool IsNewEntity(const Entity _entity) const;
740
744 private: bool IsMarkedForRemoval(const Entity _entity) const;
745
753 private: bool CreateComponentImplementation(
754 const Entity _entity,
755 const ComponentTypeId _componentTypeId,
756 const components::BaseComponent *_data);
757
763 private: const components::BaseComponent *ComponentImplementation(
764 const Entity _entity,
765 const ComponentTypeId _type) const;
766
772 private: components::BaseComponent *ComponentImplementation(
773 const Entity _entity,
774 const ComponentTypeId _type);
775
780 private: template<typename ...ComponentTypeTs>
781 detail::View *FindView() const;
782
791 const std::vector<ComponentTypeId> &_types) const;
792
798 private: detail::BaseView *AddView(
799 const detail::ComponentTypeKey &_types,
801
807 private: void AddEntityToMessage(msgs::SerializedState &_msg,
808 Entity _entity,
809 const std::unordered_set<ComponentTypeId> &_types = {}) const;
810
813
823 private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
824 Entity _entity,
825 const std::unordered_set<ComponentTypeId> &_types = {},
826 bool _full = false) const;
827
836 private: void LockAddingEntitiesToViews(bool _lock);
837
842 private: bool LockAddingEntitiesToViews() const;
843
844 // Make runners friends so that they can manage entity creation and
845 // removal. This should be safe since runners are internal
846 // to Gazebo.
847 friend class GuiRunner;
848 friend class SimulationRunner;
849
850 // Make SystemManager friend so it has access to removals
851 friend class SystemManager;
852
853 // Make network managers friends so they have control over component
854 // states. Like the runners, the managers are internal.
855 friend class NetworkManagerPrimary;
856 friend class NetworkManagerSecondary;
857 };
858 }
859 }
860}
861
862#include "gz/sim/detail/EntityComponentManager.hh"
863
864#endif