Gazebo Physics

API Reference

5.3.2
gz/physics/Entity.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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 
18 #ifndef GZ_PHYSICS_ENTITY_HH_
19 #define GZ_PHYSICS_ENTITY_HH_
20 
21 #include <optional>
22 #include <limits>
23 #include <memory>
24 
25 #include <gz/physics/Export.hh>
26 #include <gz/physics/detail/Identity.hh>
27 
28 namespace ignition
29 {
30  namespace physics
31  {
32  // Forward declaration
33  namespace detail { template <typename, typename> struct DeterminePlugin; }
34  template <typename> struct RequestFeatures;
35 
40 
41  template <typename EntityT>
42  class EntityPtr
43  {
44  public: EntityPtr() = default;
45  public: EntityPtr(EntityPtr&&) = default;
46  public: EntityPtr &operator=(EntityPtr&&) = default;
47  public: ~EntityPtr() = default;
48  // Special handling for copy construction and copy assignment
49  public: EntityPtr(const EntityPtr&);
50  public: EntityPtr &operator=(const EntityPtr&);
51 
53  public: EntityPtr(std::nullptr_t);
54 
56  public: EntityPtr(std::nullopt_t);
57 
60 
62  public: EntityPtr &operator=(std::nullopt_t);
63 
69  public: template <typename Pimpl>
70  EntityPtr(const std::shared_ptr<Pimpl> &_pimpl,
71  const Identity &_identity);
72 
77  public: template <typename OtherEntityT>
78  EntityPtr(const EntityPtr<OtherEntityT> &_other);
79 
84  public: template <typename OtherEntityT>
86 
92  public: EntityT * operator->() const;
93 
100  public: EntityT & operator*() const;
101 
104  public: bool Valid() const;
105 
108  public: operator bool() const;
109 
115  public: std::size_t Hash() const;
116 
125  public: template <typename OtherEntityT>
126  bool operator ==(const EntityPtr<OtherEntityT> &_other) const;
127 
136  public: template <typename OtherEntityT>
137  bool operator <(const EntityPtr<OtherEntityT> &_other) const;
138 
147  public: template <typename OtherEntityT>
148  bool operator >(const EntityPtr<OtherEntityT> &_other) const;
149 
158  public: template <typename OtherEntityT>
159  bool operator !=(const EntityPtr<OtherEntityT> &_other) const;
160 
169  public: template <typename OtherEntityT>
170  bool operator <=(const EntityPtr<OtherEntityT> &_other) const;
171 
180  public: template <typename OtherEntityT>
181  bool operator >=(const EntityPtr<OtherEntityT> &_other) const;
182 
197  private: mutable std::optional<std::remove_const_t<EntityT>> entity;
198 
199 
200  // Declare these friendships so we can cast between different Entity types
201  template <typename> friend class EntityPtr;
202  template <typename> friend struct RequestFeatures;
203  };
204 
215  template <typename PolicyT, typename FeaturesT>
216  class Entity
217  {
218  public: using Policy = PolicyT;
219  public: using Features = FeaturesT;
220  public: using Pimpl =
221  typename detail::DeterminePlugin<Policy, Features>::type;
222 
224  public: const Identity &FullIdentity() const;
225 
227  public: std::size_t EntityID() const;
228 
231  public: const std::shared_ptr<void> &EntityReference() const;
232 
240  // Notes for developers:
241  // - We provide a default constructor for this class so that the feature
242  // entity classes (which get virtually inherited) do not each need to
243  // call on the constructor of Entity. That would make it difficult
244  // to implement and maintain all the constructors of the different object
245  // feature classes.
246  // - Since all the features are virtually inherited, only the "final"
247  // inheriting class constructor needs to actually call this constructor.
248  // - The default argument for the identity will have an INVALID_ENTITY_ID
249  // value (which is the result of default-constructing Identity). If the
250  // Identity of an Entity is invalid, that implies that there is a bug in
251  // the construction of that Entity.
252  protected: Entity(
253  const std::shared_ptr<Pimpl> &_pimpl = nullptr,
254  const Identity &_identity = Identity());
255 
264  protected: Entity(
265  std::shared_ptr<Pimpl> &&_pimpl,
266  const Identity &_identity);
267 
275  protected: template <typename FeatureT>
276  typename FeatureT::template Implementation<PolicyT> *Interface();
277 
282  protected: template <typename FeatureT>
283  const typename FeatureT::template Implementation<PolicyT> *Interface()
284  const;
285 
290 
292  protected: Identity identity;
293 
295  public: virtual ~Entity() = default;
296 
297  // Allow EntityPtr to cast between EntityTypes
298  template <typename> friend class EntityPtr;
299  template <typename> friend struct RequestFeatures;
300  };
301  }
302 }
303 
304 #include <gz/physics/detail/Entity.hh>
305 
306 #endif
Policy Policy
Definition: gz/physics/Entity.hh:218
This is the base class of all "proxy objects". The "proxy objects" are essentially interfaces into th...
Definition: gz/physics/Entity.hh:216
FeatureT::template Implementation< PolicyT > * Interface()
Get a pointer to the implementation of FeatureT.
bool operator==(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
bool operator>=(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
bool operator<=(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
EntityPtr & operator=(EntityPtr &&)=default
const Identity & FullIdentity() const
Get the Identity object of this Entity.
Entity(const std::shared_ptr< Pimpl > &_pimpl=nullptr, const Identity &_identity=Identity())
Constructor for the Entity.
EntityT & operator*() const
Dereference operator. Access a reference to the Entity being pointed to. This does NOT check whether ...
virtual ~Entity()=default
Virtual destructor.
This class can be used to request features from an entity, or identify what features are missing from...
Definition: gz/physics/Entity.hh:34
friend class EntityPtr
Definition: gz/physics/Entity.hh:201
Identity identity
This field contains information to identify the entity.
Definition: gz/physics/Entity.hh:292
const std::shared_ptr< void > & EntityReference() const
Get a reference-counting std::shared_ptr to the object inside the implementation that this object pro...
bool operator>(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
bool operator<(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
bool Valid() const
Check whether this is pointing at a valid Entity.
typename detail::DeterminePlugin< Policy, Features >::type Pimpl
Definition: gz/physics/Entity.hh:221
std::size_t Hash() const
Produces a hash for the Entity that this EntityPtr is referring to. This function allows EntityPtr in...
Definition: gz/physics/Entity.hh:42
const std::size_t INVALID_ENTITY_ID
This constant-value should be used to indicate that an Entity ID is invalid (i.e. does not refer to a...
Definition: gz/physics/Entity.hh:38
bool operator!=(const EntityPtr< OtherEntityT > &_other) const
Comparison operator.
T max(T... args)
EntityT * operator->() const
Drill operator. Access members of the Entity being pointed to. This does NOT check whether the Entity...
FeaturesT Features
Definition: gz/physics/Entity.hh:219
std::size_t EntityID() const
Get the unique ID value of this Entity.
std::shared_ptr< Pimpl > pimpl
This is a pointer to the physics engine implementation, and it can be used by the object features to ...
Definition: gz/physics/Entity.hh:289