Skip to content

Commit 16014e7

Browse files
committed
Prevent default-constructed variants from holding a type (#371)
Signed-off-by: Ashton Larkin <[email protected]>
1 parent 742bd27 commit 16014e7

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

Migration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ release will remove the deprecated code.
3232
+ Added `Scene::SetCameraPassCountPerGpuFlush`. Setting this value to 0 forces legacy behavior which eases porting.
3333
+ Systems that rely on Graphics components like particle FXs and postprocessing are explicitly affected by Scene's Pre/PostRender. Once `Scene::PostRender` is called, the particle FXs' simulation is moved forward, as well as time values sent to postprocessing shaders. In previous ign-rendering versions each `Camera::Render` call would move the particle simulation forward, which could cause subtle bugs or inconsistencies when Cameras were rendering the same frame from different angles. Setting SetCameraPassCountPerGpuFlush to 0 will also cause these subtle bugs to reappear.
3434
35+
1. **Visual.hh** and **Node.hh**
36+
+ `*UserData` methods and the `Variant` type alias have been moved from the `Visual` class to the `Node` class.
37+
`Node::UserData` now returns no data for keys that don't exist (prior to Rendering 6.x, if
38+
`Visual::UserData` was called with a key that doesn't exist, an `int` was returned by default).
39+
3540
## Ignition Rendering 4.0 to 4.1
3641
3742
## ABI break

include/ignition/rendering/Node.hh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ namespace ignition
3535
{
3636
inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
3737
//
38+
/// \brief Alias for a variant that can hold various types of data.
39+
/// The first type of the variant is std::monostate in order to prevent
40+
/// default-constructed variants from holding a type (a default-constructed
41+
/// variant is returned when a user calls Node::UserData with a key that
42+
/// doesn't exist for the node. In this case, since the key doesn't
43+
/// exist, the variant that is returned shouldn't hold any types - an
44+
/// "empty variant" should be returned for keys that don't exist)
3845
using Variant =
39-
std::variant<int, float, double, std::string, bool, unsigned int>;
46+
std::variant<std::monostate, int, float, double, std::string, bool,
47+
unsigned int>;
4048

4149
/// \class Node Node.hh ignition/rendering/Node.hh
4250
/// \brief Represents a single posable node in the scene graph
@@ -228,12 +236,12 @@ namespace ignition
228236
/// \param[in] _scale Scalars to alter the current scale
229237
public: virtual void Scale(const math::Vector3d &_scale) = 0;
230238

231-
/// \brief Determine if this visual inherits scale from this parent
232-
/// \return True if this visual inherits scale from this parent
239+
/// \brief Determine if this node inherits scale from this parent
240+
/// \return True if this node inherits scale from this parent
233241
public: virtual bool InheritScale() const = 0;
234242

235-
/// \brief Specify if this visual inherits scale from its parent
236-
/// \param[in] _inherit True if this visual inherits scale from its parent
243+
/// \brief Specify if this node inherits scale from its parent
244+
/// \param[in] _inherit True if this node inherits scale from its parent
237245
public: virtual void SetInheritScale(bool _inherit) = 0;
238246

239247
/// \brief Get number of child nodes
@@ -309,15 +317,16 @@ namespace ignition
309317
/// This detaches all the child nodes but does not destroy them
310318
public: virtual void RemoveChildren() = 0;
311319

312-
/// \brief Store any custom data associated with this visual
320+
/// \brief Store any custom data associated with this node
313321
/// \param[in] _key Unique key
314322
/// \param[in] _value Value in any type
315323
public: virtual void SetUserData(
316324
const std::string &_key, Variant _value) = 0;
317325

318-
/// \brief Get custom data stored in this visual
326+
/// \brief Get custom data stored in this node
319327
/// \param[in] _key Unique key
320-
/// \return Value in any type
328+
/// \return Value in any type. If _key does not exist for the node, an
329+
/// empty variant is returned (i.e., no data).
321330
public: virtual Variant UserData(const std::string &_key) const = 0;
322331
};
323332
}

src/Visual_TEST.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,29 @@ void VisualTest::UserData(const std::string &_renderEngine)
350350
value = visual->UserData(stringKey);
351351
EXPECT_EQ(stringValue, std::get<std::string>(value));
352352

353+
// bool
354+
std::string boolKey = "bool";
355+
bool boolValue = true;
356+
visual->SetUserData(boolKey, boolValue);
357+
value = visual->UserData(boolKey);
358+
EXPECT_EQ(boolValue, std::get<bool>(value));
359+
360+
// unsigned int
361+
std::string unsignedIntKey = "unsignedInt";
362+
unsigned int unsignedIntValue = 5u;
363+
visual->SetUserData(unsignedIntKey, unsignedIntValue);
364+
value = visual->UserData(unsignedIntKey);
365+
EXPECT_EQ(unsignedIntValue, std::get<unsigned int>(value));
366+
367+
// test a key that does not exist (should return no data)
368+
value = visual->UserData("invalidKey");
369+
EXPECT_FALSE(std::holds_alternative<int>(value));
370+
EXPECT_FALSE(std::holds_alternative<float>(value));
371+
EXPECT_FALSE(std::holds_alternative<double>(value));
372+
EXPECT_FALSE(std::holds_alternative<std::string>(value));
373+
EXPECT_FALSE(std::holds_alternative<bool>(value));
374+
EXPECT_FALSE(std::holds_alternative<unsigned int>(value));
375+
353376
// test invalid access
354377
EXPECT_THROW(
355378
{

0 commit comments

Comments
 (0)