Skip to content

Commit 8e89865

Browse files
nkoenigNate Koenig
and
Nate Koenig
authored
Added non-const accessors for Link child objects (#838)
* Added non-const accessors for child objects Signed-off-by: Nate Koenig <nate@openrobotics.org> * Remove helpers Signed-off-by: Nate Koenig <nate@openrobotics.org> Co-authored-by: Nate Koenig <nate@openrobotics.org>
1 parent eed1aa9 commit 8e89865

File tree

3 files changed

+265
-1
lines changed

3 files changed

+265
-1
lines changed

include/sdf/Link.hh

+65
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ namespace sdf
7676
/// \sa uint64_t VisualCount() const
7777
public: const Visual *VisualByIndex(const uint64_t _index) const;
7878

79+
/// \brief Get a mutable visual based on an index.
80+
/// \param[in] _index Index of the visual. The index should be in the
81+
/// range [0..VisualCount()).
82+
/// \return Pointer to the visual. Nullptr if the index does not exist.
83+
/// \sa uint64_t VisualCount() const
84+
public: Visual *VisualByIndex(uint64_t _index);
85+
7986
/// \brief Get whether a visual name exists.
8087
/// \param[in] _name Name of the visual to check.
8188
/// \return True if there exists a visual with the given name.
@@ -86,6 +93,11 @@ namespace sdf
8693
/// \return Pointer to the visual. Nullptr if the name does not exist.
8794
public: const Visual *VisualByName(const std::string &_name) const;
8895

96+
/// \brief Get a mutable visual based on a name.
97+
/// \param[in] _name Name of the visual.
98+
/// \return Pointer to the visual. Nullptr if the name does not exist.
99+
public: Visual *VisualByName(const std::string &_name);
100+
89101
/// \brief Get the number of collisions.
90102
/// \return Number of collisions contained in this Link object.
91103
public: uint64_t CollisionCount() const;
@@ -97,6 +109,13 @@ namespace sdf
97109
/// \sa uint64_t CollisionCount() const
98110
public: const Collision *CollisionByIndex(const uint64_t _index) const;
99111

112+
/// \brief Get a mutable collision based on an index.
113+
/// \param[in] _index Index of the collision. The index should be in the
114+
/// range [0..CollisionCount()).
115+
/// \return Pointer to the collision. Nullptr if the index does not exist.
116+
/// \sa uint64_t CollisionCount() const
117+
public: Collision *CollisionByIndex(uint64_t _index);
118+
100119
/// \brief Get whether a collision name exists.
101120
/// \param[in] _name Name of the collision to check.
102121
/// \return True if there exists a collision with the given name.
@@ -107,6 +126,11 @@ namespace sdf
107126
/// \return Pointer to the collision. Nullptr if the name does not exist.
108127
public: const Collision *CollisionByName(const std::string &_name) const;
109128

129+
/// \brief Get a mutable collision based on a name.
130+
/// \param[in] _name Name of the collision.
131+
/// \return Pointer to the collision. Nullptr if the name does not exist.
132+
public: Collision *CollisionByName(const std::string &_name);
133+
110134
/// \brief Get the number of lights.
111135
/// \return Number of lights contained in this Link object.
112136
public: uint64_t LightCount() const;
@@ -118,6 +142,13 @@ namespace sdf
118142
/// \sa uint64_t LightCount() const
119143
public: const Light *LightByIndex(const uint64_t _index) const;
120144

145+
/// \brief Get a mutable light based on an index.
146+
/// \param[in] _index Index of the light. The index should be in the
147+
/// range [0..LightCount()).
148+
/// \return Pointer to the light. Nullptr if the index does not exist.
149+
/// \sa uint64_t LightCount() const
150+
public: Light *LightByIndex(uint64_t _index);
151+
121152
/// \brief Get whether a light name exists.
122153
/// \param[in] _name Name of the light to check.
123154
/// \return True if there exists a light with the given name.
@@ -128,6 +159,11 @@ namespace sdf
128159
/// \return Pointer to the light. Nullptr if the name does not exist.
129160
public: const Light *LightByName(const std::string &_name) const;
130161

162+
/// \brief Get a mutable light based on a name.
163+
/// \param[in] _name Name of the light.
164+
/// \return Pointer to the light. Nullptr if the name does not exist.
165+
public: Light *LightByName(const std::string &_name);
166+
131167
/// \brief Get the number of sensors.
132168
/// \return Number of sensors contained in this Link object.
133169
public: uint64_t SensorCount() const;
@@ -139,6 +175,13 @@ namespace sdf
139175
/// \sa uint64_t SensorCount() const
140176
public: const Sensor *SensorByIndex(const uint64_t _index) const;
141177

178+
/// \brief Get a mutable sensor based on an index.
179+
/// \param[in] _index Index of the sensor. The index should be in the
180+
/// range [0..SensorCount()).
181+
/// \return Pointer to the sensor. Nullptr if the index does not exist.
182+
/// \sa uint64_t SensorCount() const
183+
public: Sensor *SensorByIndex(uint64_t _index);
184+
142185
/// \brief Get whether a sensor name exists.
143186
/// \param[in] _name Name of the sensor to check.
144187
/// \return True if there exists a sensor with the given name.
@@ -151,6 +194,13 @@ namespace sdf
151194
/// \sa bool SensorNameExists(const std::string &_name) const
152195
public: const Sensor *SensorByName(const std::string &_name) const;
153196

197+
/// \brief Get a mutable sensor based on a name.
198+
/// \param[in] _name Name of the sensor.
199+
/// \return Pointer to the sensor. Nullptr if a sensor with the given name
200+
/// does not exist.
201+
/// \sa bool SensorNameExists(const std::string &_name) const
202+
public: Sensor *SensorByName(const std::string &_name);
203+
154204
/// \brief Get the number of particle emitters.
155205
/// \return Number of particle emitters contained in this Link object.
156206
public: uint64_t ParticleEmitterCount() const;
@@ -164,6 +214,14 @@ namespace sdf
164214
public: const ParticleEmitter *ParticleEmitterByIndex(
165215
const uint64_t _index) const;
166216

217+
/// \brief Get a mutable particle emitter based on an index.
218+
/// \param[in] _index Index of the particle emitter.
219+
/// The index should be in the range [0..ParticleEmitterCount()).
220+
/// \return Pointer to the particle emitter. Nullptr if the index does
221+
/// not exist.
222+
/// \sa uint64_t ParticleEmitterCount() const
223+
public: ParticleEmitter *ParticleEmitterByIndex(uint64_t _index);
224+
167225
/// \brief Get whether a particle emitter name exists.
168226
/// \param[in] _name Name of the particle emitter to check.
169227
/// \return True if there exists a particle emitter with the given name.
@@ -177,6 +235,13 @@ namespace sdf
177235
public: const ParticleEmitter *ParticleEmitterByName(
178236
const std::string &_name) const;
179237

238+
/// \brief Get a mutable particle emitter based on a name.
239+
/// \param[in] _name Name of the particle emitter.
240+
/// \return Pointer to the particle emitter. Nullptr if a particle emitter
241+
/// with the given name does not exist.
242+
/// \sa bool ParticleEmitterNameExists(const std::string &_name) const
243+
public: ParticleEmitter *ParticleEmitterByName(const std::string &_name);
244+
180245
/// \brief Get the inertial value for this link. The inertial object
181246
/// consists of the link's mass, a 3x3 rotational inertia matrix, and
182247
/// a pose for the inertial reference frame. The units for mass is

src/Link.cc

+71-1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ const Visual *Link::VisualByIndex(const uint64_t _index) const
218218
return nullptr;
219219
}
220220

221+
/////////////////////////////////////////////////
222+
Visual *Link::VisualByIndex(uint64_t _index)
223+
{
224+
return const_cast<Visual*>(
225+
static_cast<const Link*>(this)->VisualByIndex(_index));
226+
}
227+
221228
/////////////////////////////////////////////////
222229
bool Link::VisualNameExists(const std::string &_name) const
223230
{
@@ -245,6 +252,13 @@ const Collision *Link::CollisionByIndex(const uint64_t _index) const
245252
return nullptr;
246253
}
247254

255+
/////////////////////////////////////////////////
256+
Collision *Link::CollisionByIndex(uint64_t _index)
257+
{
258+
return const_cast<Collision*>(
259+
static_cast<const Link*>(this)->CollisionByIndex(_index));
260+
}
261+
248262
/////////////////////////////////////////////////
249263
bool Link::CollisionNameExists(const std::string &_name) const
250264
{
@@ -272,6 +286,13 @@ const Light *Link::LightByIndex(const uint64_t _index) const
272286
return nullptr;
273287
}
274288

289+
/////////////////////////////////////////////////
290+
Light *Link::LightByIndex(uint64_t _index)
291+
{
292+
return const_cast<Light*>(
293+
static_cast<const Link*>(this)->LightByIndex(_index));
294+
}
295+
275296
/////////////////////////////////////////////////
276297
bool Link::LightNameExists(const std::string &_name) const
277298
{
@@ -292,6 +313,13 @@ const Sensor *Link::SensorByIndex(const uint64_t _index) const
292313
return nullptr;
293314
}
294315

316+
/////////////////////////////////////////////////
317+
Sensor *Link::SensorByIndex(uint64_t _index)
318+
{
319+
return const_cast<Sensor*>(
320+
static_cast<const Link*>(this)->SensorByIndex(_index));
321+
}
322+
295323
/////////////////////////////////////////////////
296324
bool Link::SensorNameExists(const std::string &_name) const
297325
{
@@ -318,6 +346,13 @@ const Sensor *Link::SensorByName(const std::string &_name) const
318346
return nullptr;
319347
}
320348

349+
/////////////////////////////////////////////////
350+
Sensor *Link::SensorByName(const std::string &_name)
351+
{
352+
return const_cast<Sensor*>(
353+
static_cast<const Link*>(this)->SensorByName(_name));
354+
}
355+
321356
/////////////////////////////////////////////////
322357
uint64_t Link::ParticleEmitterCount() const
323358
{
@@ -332,6 +367,13 @@ const ParticleEmitter *Link::ParticleEmitterByIndex(const uint64_t _index) const
332367
return nullptr;
333368
}
334369

370+
/////////////////////////////////////////////////
371+
ParticleEmitter *Link::ParticleEmitterByIndex(uint64_t _index)
372+
{
373+
return const_cast<ParticleEmitter*>(
374+
static_cast<const Link*>(this)->ParticleEmitterByIndex(_index));
375+
}
376+
335377
/////////////////////////////////////////////////
336378
bool Link::ParticleEmitterNameExists(const std::string &_name) const
337379
{
@@ -359,6 +401,12 @@ const ParticleEmitter *Link::ParticleEmitterByName(
359401
return nullptr;
360402
}
361403

404+
/////////////////////////////////////////////////
405+
ParticleEmitter *Link::ParticleEmitterByName(const std::string &_name)
406+
{
407+
return const_cast<ParticleEmitter *>(
408+
static_cast<const Link*>(this)->ParticleEmitterByName(_name));
409+
}
362410

363411
/////////////////////////////////////////////////
364412
const ignition::math::Inertiald &Link::Inertial() const
@@ -455,10 +503,17 @@ const Visual *Link::VisualByName(const std::string &_name) const
455503
return nullptr;
456504
}
457505

506+
/////////////////////////////////////////////////
507+
Visual *Link::VisualByName(const std::string &_name)
508+
{
509+
return const_cast<Visual *>(
510+
static_cast<const Link*>(this)->VisualByName(_name));
511+
}
512+
458513
/////////////////////////////////////////////////
459514
const Collision *Link::CollisionByName(const std::string &_name) const
460515
{
461-
for (auto const &c : this->dataPtr->collisions)
516+
for (auto &c : this->dataPtr->collisions)
462517
{
463518
if (c.Name() == _name)
464519
{
@@ -468,6 +523,13 @@ const Collision *Link::CollisionByName(const std::string &_name) const
468523
return nullptr;
469524
}
470525

526+
/////////////////////////////////////////////////
527+
Collision *Link::CollisionByName(const std::string &_name)
528+
{
529+
return const_cast<Collision *>(
530+
static_cast<const Link*>(this)->CollisionByName(_name));
531+
}
532+
471533
/////////////////////////////////////////////////
472534
const Light *Link::LightByName(const std::string &_name) const
473535
{
@@ -481,6 +543,14 @@ const Light *Link::LightByName(const std::string &_name) const
481543
return nullptr;
482544
}
483545

546+
/////////////////////////////////////////////////
547+
Light *Link::LightByName(const std::string &_name)
548+
{
549+
return const_cast<Light *>(
550+
static_cast<const Link*>(this)->LightByName(_name));
551+
552+
}
553+
484554
/////////////////////////////////////////////////
485555
sdf::ElementPtr Link::Element() const
486556
{

0 commit comments

Comments
 (0)