Gazebo Sim

API Reference

9.0.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
81 public: void CopyFrom(const EntityComponentManager &_fromEcm);
82
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
311 Entity _entity) const;
312
324 public: template<typename ...ComponentTypeTs>
326 const ComponentTypeTs &..._desiredComponents) const;
327
340 public: template<typename ...ComponentTypeTs>
342 const ComponentTypeTs &..._desiredComponents) const;
343
359 public: template<typename ...ComponentTypeTs>
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
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
569
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
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,
652 sim::ComponentState _c = ComponentState::OneTimeChange);
653
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
683 public: std::optional<Entity> EntityByName(
684 const std::string &_name) const;
685
689 protected: void ClearNewlyCreatedEntities();
690
693 protected: void ClearRemovedComponents();
694
699
701 protected: void SetAllComponentsUnchanged();
702
711 protected: EntityComponentManagerDiff ComputeEntityDiff(
712 const EntityComponentManager &_other) const;
713
719 protected: void ApplyEntityDiff(const EntityComponentManager &_other,
720 const EntityComponentManagerDiff &_diff);
721
728 private: bool IsNewEntity(const Entity _entity) const;
729
733 private: bool IsMarkedForRemoval(const Entity _entity) const;
734
742 private: bool CreateComponentImplementation(
743 const Entity _entity,
744 const ComponentTypeId _componentTypeId,
745 const components::BaseComponent *_data);
746
752 private: const components::BaseComponent *ComponentImplementation(
753 const Entity _entity,
754 const ComponentTypeId _type) const;
755
761 private: components::BaseComponent *ComponentImplementation(
762 const Entity _entity,
763 const ComponentTypeId _type);
764
769 private: template<typename ...ComponentTypeTs>
770 detail::View *FindView() const;
771
780 const std::vector<ComponentTypeId> &_types) const;
781
787 private: detail::BaseView *AddView(
788 const detail::ComponentTypeKey &_types,
790
796 private: void AddEntityToMessage(msgs::SerializedState &_msg,
797 Entity _entity,
798 const std::unordered_set<ComponentTypeId> &_types = {}) const;
799
802
812 private: void AddEntityToMessage(msgs::SerializedStateMap &_msg,
813 Entity _entity,
814 const std::unordered_set<ComponentTypeId> &_types = {},
815 bool _full = false) const;
816
825 private: void LockAddingEntitiesToViews(bool _lock);
826
831 private: bool LockAddingEntitiesToViews() const;
832
833 // Make runners friends so that they can manage entity creation and
834 // removal. This should be safe since runners are internal
835 // to Gazebo.
836 friend class GuiRunner;
837 friend class SimulationRunner;
838
839 // Make SystemManager friend so it has access to removals
840 friend class SystemManager;
841
842 // Make network managers friends so they have control over component
843 // states. Like the runners, the managers are internal.
844 friend class NetworkManagerPrimary;
845 friend class NetworkManagerSecondary;
846 };
847 }
848 }
849}
850
851#include "gz/sim/detail/EntityComponentManager.hh"
852
853#endif