Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Root mutable accessors, and Root::Clone function #841

Merged
merged 16 commits into from
Feb 16, 2022
17 changes: 17 additions & 0 deletions include/sdf/Root.hh
Original file line number Diff line number Diff line change
@@ -125,6 +125,13 @@ namespace sdf
/// \sa uint64_t WorldCount() const
public: const World *WorldByIndex(const uint64_t _index) const;

/// \brief Get a mutable world based on an index.
/// \param[in] _index Index of the world. The index should be in the
/// range [0..WorldCount()).
/// \return Pointer to the world. Nullptr if the index does not exist.
/// \sa uint64_t WorldCount() const
public: World *WorldByIndex(const uint64_t _index);

/// \brief Get whether a world name exists.
/// \param[in] _name Name of the world to check.
/// \return True if there exists a world with the given name.
@@ -151,6 +158,16 @@ namespace sdf
/// not been called.
public: sdf::ElementPtr Element() const;

/// \brief Add a world to the root.
/// \param[in] _word World to add.
/// \return True if successful, false if a world with the name already
/// exists.
/// \return True on success, false if the world name already exists.
public: bool AddWorld(const World &_world);

/// \brief Remove all worlds.
public: void ClearWorlds();

/// \brief Private data pointer
IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
};
25 changes: 25 additions & 0 deletions src/Root.cc
Original file line number Diff line number Diff line change
@@ -381,6 +381,14 @@ const World *Root::WorldByIndex(const uint64_t _index) const
return nullptr;
}

/////////////////////////////////////////////////
World *Root::WorldByIndex(const uint64_t _index)
{
if (_index < this->dataPtr->worlds.size())
return &this->dataPtr->worlds[_index];
return nullptr;
}

/////////////////////////////////////////////////
bool Root::WorldNameExists(const std::string &_name) const
{
@@ -417,3 +425,20 @@ sdf::ElementPtr Root::Element() const
{
return this->dataPtr->sdf;
}

/////////////////////////////////////////////////
bool Root::AddWorld(const World &_world)
{
if (!this->WorldNameExists(_world.Name()))
{
this->dataPtr->worlds.push_back(_world);
return true;
}
return false;
}

/////////////////////////////////////////////////
void Root::ClearWorlds()
{
this->dataPtr->worlds.clear();
}
42 changes: 42 additions & 0 deletions src/Root_TEST.cc
Original file line number Diff line number Diff line change
@@ -270,3 +270,45 @@ TEST(DOMRoot, FrameSemanticsOnMove)
testFrame1(root2);
}
}

/////////////////////////////////////////////////
TEST(DOMRoot, AddWorld)
{
sdf::Root root;
EXPECT_EQ(0u, root.WorldCount());

sdf::World world;
world.SetName("world1");
EXPECT_TRUE(root.AddWorld(world));
EXPECT_EQ(1u, root.WorldCount());
EXPECT_FALSE(root.AddWorld(world));
EXPECT_EQ(1u, root.WorldCount());

root.ClearWorlds();
EXPECT_EQ(0u, root.WorldCount());

EXPECT_TRUE(root.AddWorld(world));
EXPECT_EQ(1u, root.WorldCount());
const sdf::World *worldFromRoot = root.WorldByIndex(0);
ASSERT_NE(nullptr, worldFromRoot);
EXPECT_EQ(worldFromRoot->Name(), world.Name());
}

/////////////////////////////////////////////////
TEST(DOMRoot, MutableByIndex)
{
sdf::Root root;
EXPECT_EQ(0u, root.WorldCount());

sdf::World world;
world.SetName("world1");
EXPECT_TRUE(root.AddWorld(world));
EXPECT_EQ(1u, root.WorldCount());

// Modify the world
sdf::World *w = root.WorldByIndex(0);
ASSERT_NE(nullptr, w);
EXPECT_EQ("world1", w->Name());
w->SetName("world2");
EXPECT_EQ("world2", root.WorldByIndex(0)->Name());
}