Skip to content

Commit

Permalink
Reduce/Eliminate sdf::Model and sdf::World serialization warnings (
Browse files Browse the repository at this point in the history
…#2742)

- Reduces serialization warnings on `sdf::Model` so it only occurs when
  the `relative_to` attribute is set. This was the intended behavior
  originally, but there was a bug in the code.
- Eliminate `sdf::World` warnings by specializing the `DefaultSerializer` for `sdf::World`.

---------

Signed-off-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
azeey authored Jan 31, 2025
1 parent 1eebaa3 commit 1739bdb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/gz/sim/components/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace serializers
if (modelElem->HasElement("pose"))
{
sdf::ElementPtr poseElem = modelElem->GetElement("pose");
if (poseElem->HasAttribute("relative_to"))
if (poseElem->GetAttribute("relative_to")->GetSet())
{
// Skip serializing models with //pose/@relative_to attribute
// since deserialization will fail. This could be a nested model.
Expand Down
44 changes: 44 additions & 0 deletions include/gz/sim/components/World.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include <sdf/World.hh>

#include <istream>
#include <ostream>

#include <gz/sim/components/Factory.hh>
#include <gz/sim/components/Component.hh>
#include <gz/sim/config.hh>
Expand All @@ -29,6 +32,47 @@ namespace sim
{
// Inline bracket to help doxygen filtering.
inline namespace GZ_SIM_VERSION_NAMESPACE {

// The following is only needed to keep ABI compatibility
// TODO(azeey) Remove in main
namespace traits
{
template<>
class IsOutStreamable<std::ostream, sdf::World>
{
public: static constexpr bool value = false; // NOLINT
};

template<>
class IsInStreamable<std::istream, sdf::World>
{
public: static constexpr bool value = false; // NOLINT
};
}

namespace serializers
{

/// \brief Specialize the DefaultSerializer on sdf::World so we can
/// skip serialization
/// TODO(azeey) Do we ever want to serialize this component?
template <>
class DefaultSerializer<sdf::World>
{
public:
static std::ostream &Serialize(std::ostream &_out, const sdf::World &)
{
return _out;
}

public:
static std::istream &Deserialize(std::istream &_in, sdf::World &)
{
return _in;
}
};
}

namespace components
{
/// \brief A component that identifies an entity as being a world.
Expand Down
2 changes: 1 addition & 1 deletion test/worlds/gpu_lidar.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<resolution>0.01</resolution>
</range>
</ray>
<alwaysOn>1</alwaysOn>
<always_on>1</always_on>
<visualize>true</visualize>
</sensor>
</link>
Expand Down

0 comments on commit 1739bdb

Please sign in to comment.