18 #ifndef GZ_SIM_SYSTEMS_PHYSICS_ENTITY_FEATURE_MAP_HH_
19 #define GZ_SIM_SYSTEMS_PHYSICS_ENTITY_FEATURE_MAP_HH_
22 #include <type_traits>
23 #include <unordered_map>
25 #include <gz/physics/Entity.hh>
26 #include <gz/physics/FindFeatures.hh>
27 #include <gz/physics/FeatureList.hh>
28 #include <gz/physics/RequestFeatures.hh>
34 inline namespace GZ_SIM_VERSION_NAMESPACE {
35 namespace systems::physics_system
60 template <
template <
typename,
typename>
class PhysicsEntityT,
61 typename PolicyT,
typename RequiredFeatureList,
62 typename... OptionalFeatureLists>
67 public:
template <
typename T>
75 public:
template <
typename T>
77 std::disjunction<std::is_same<T, OptionalFeatureLists>...>;
95 public:
template <
typename ToFeatureList>
104 "Trying to cast to a FeatureList not included in the "
105 "optional FeatureLists of this map.");
112 auto castIt = this->castCache.find(_entity);
113 if (castIt != this->castCache.end())
115 auto castEntity = std::get<ToEntityPtr>(castIt->second);
116 if (
nullptr != castEntity)
122 auto reqEntity = this->Get(_entity);
123 if (
nullptr == reqEntity)
129 auto castEntity = physics::RequestFeatures<ToFeatureList>::From(
134 std::get<ToEntityPtr>(this->castCache[_entity]) = castEntity;
149 public:
template <
typename ToFeatureList>
150 PhysicsEntityPtr<ToFeatureList>
153 auto gzEntity = this->Get(_physicsEntity);
158 return this->EntityCast<ToFeatureList>(gzEntity);
168 auto it = this->entityMap.find(_entity);
169 if (it != this->entityMap.end())
183 auto it = this->reverseMap.find(_physEntity);
184 if (it != this->reverseMap.end())
198 auto it = this->physEntityById.find(_id);
199 if (it != this->physEntityById.end())
212 auto it = this->entityByPhysId.find(_id);
213 if (it != this->entityByPhysId.end())
227 return this->entityMap.find(_entity) != this->entityMap.end();
237 return this->reverseMap.find(_physicsEntity) != this->reverseMap.end();
246 this->entityMap[_entity] = _physicsEntity;
247 this->reverseMap[_physicsEntity] = _entity;
248 this->physEntityById[_physicsEntity->EntityID()] = _physicsEntity;
249 this->entityByPhysId[_physicsEntity->EntityID()] = _entity;
257 auto it = this->entityMap.find(_entity);
258 if (it != this->entityMap.end())
260 this->reverseMap.erase(it->second);
261 this->physEntityById.erase(it->second->EntityID());
262 this->entityByPhysId.erase(it->second->EntityID());
263 this->castCache.erase(_entity);
264 this->entityMap.erase(it);
275 auto it = this->reverseMap.find(_physicsEntity);
276 if (it != this->reverseMap.end())
278 this->entityMap.erase(it->second);
279 this->physEntityById.erase(it->first->EntityID());
280 this->entityByPhysId.erase(it->first->EntityID());
281 this->castCache.erase(it->second);
282 this->reverseMap.erase(it);
293 return this->entityMap;
301 return this->entityMap.size() + this->reverseMap.size() +
302 this->castCache.size() + this->physEntityById.size() +
303 this->entityByPhysId.size();
326 template <
template <
typename,
typename>
class PhysicsEntityT,
327 typename RequiredFeatureList,
typename... OptionalFeatureLists>
329 EntityFeatureMap<PhysicsEntityT, physics::FeaturePolicy3d,
330 RequiredFeatureList, OptionalFeatureLists...>;