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 plugin to SDF DOM #788

Merged
merged 9 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/sdf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set (headers
Pbr.hh
Physics.hh
Plane.hh
Plugin.hh
PrintConfig.hh
Root.hh
Scene.hh
Expand Down
19 changes: 19 additions & 0 deletions include/sdf/Gui.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <ignition/utils/ImplPtr.hh>
#include "sdf/Element.hh"
#include "sdf/Plugin.hh"
#include "sdf/Types.hh"
#include "sdf/sdf_config.h"
#include "sdf/system_util.hh"
Expand Down Expand Up @@ -66,6 +67,24 @@ namespace sdf
/// \return SDF element pointer with updated gui values.
public: sdf::ElementPtr ToElement() const;

/// \brief Get the number of plugins.
/// \return Number of plugins contained in this Gui object.
public: uint64_t PluginCount() const;

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

/// \brief Remove all plugins
public: void ClearPlugins();

/// \brief Add a plugin to the link.
/// \param[in] _plugin Plugin to add.
public: void AddPlugin(const Plugin &_plugin);

/// \brief Private data pointer.
IGN_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
135 changes: 135 additions & 0 deletions include/sdf/Plugin.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef SDF_PLUGIN_HH_
#define SDF_PLUGIN_HH_

#include <memory>
#include <string>
#include <vector>

#include <sdf/Element.hh>
#include <sdf/Error.hh>
#include <sdf/Types.hh>
#include "sdf/sdf_config.h"
#include "sdf/system_util.hh"

#ifdef _WIN32
// Disable warning C4251 which is triggered by
// std::unique_ptr
#pragma warning(push)
#pragma warning(disable: 4251)
#endif

namespace sdf
{
// Inline bracket to help doxygen filtering.
inline namespace SDF_VERSION_NAMESPACE {
//
class PluginPrivate;

class SDFORMAT_VISIBLE Plugin
{
/// \brief Default constructor
public: Plugin();

/// \brief Default destructor
public: ~Plugin();

/// \brief Copy constructor.
/// \param[in] _plugin Plugin to copy.
public: Plugin(const Plugin &_plugin);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to add move constructor and move assignment as well per the rule of five.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was there a reason we didn't use ImplPtr here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it because we need to take care to clone the sdf::ElementPtr contents instead of just copying the shared pointer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/// \brief Move constructor.
/// \param[in] _plugin Plugin to copy.
public: Plugin(Plugin &&_plugin) noexcept;

/// \brief Load the plugin based on a element pointer. This is *not* the
/// usual entry point. Typical usage of the SDF DOM is through the Root
/// object.
/// \param[in] _sdf The SDF Element pointer
/// \return Errors, which is a vector of Error objects. Each Error includes
/// an error code and message. An empty vector indicates no error.
public: Errors Load(ElementPtr _sdf);

/// \brief Get the name of the plugin.
/// The name of the plugin should be unique within the scope of its
/// parent.
/// \return Name of the plugin.
public: std::string Name() const;

/// \brief Set the name of the plugin.
/// The name of the plugin should be unique within the scope of its
/// parent.
/// \param[in] _name Name of the plugin.
public: void SetName(const std::string &_name);

/// \brief Get the filename of the shared library.
/// \return Filename of the shared library associated with the plugin.
public: std::string Filename() const;

/// \brief Remove the contents of the plugin, this is everything that
/// is a child element of the `<plugin>`.
public: void ClearContents();

/// \brief Get the plugin contents. This is all the SDF elements that
/// are children of the `<plugin>`.
/// \return The child elements of this plugin.
public: const std::vector<sdf::ElementPtr> &Contents() const;

/// \brief Insert an element into the plugin content. This does not
/// modify the values in the sdf::ElementPtr returned by the `Element()`
/// function.
/// \param[in] _elem Element to insert.
public: void InsertContent(const sdf::ElementPtr _elem);
azeey marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Set the filename of the shared library.
/// \param[in] _filename Filename of the shared library associated with
/// this plugin.
public: void SetFilename(const std::string &_filename);

/// \brief Get a pointer to the SDF element that was used during
/// load.
/// \return SDF element pointer. The value will be nullptr if Load has
/// not been called.
public: sdf::ElementPtr Element() const;

/// \brief Create and return an SDF element filled with data from this
/// plugin.
/// \return SDF element pointer with updated plugin values.
public: sdf::ElementPtr ToElement() const;

/// \brief Copy assignment operator
/// \param[in] _plugin Plugin to copy
/// \return A reference to this plugin
public: Plugin &operator=(const Plugin &_plugin);

/// \brief Move assignment operator
/// \param[in] _plugin Plugin to move
/// \return A reference to this plugin
public: Plugin &operator=(Plugin &&_plugin) noexcept;

/// \brief Private data pointer.
std::unique_ptr<sdf::PluginPrivate> dataPtr;
};
}
}

#ifdef _WIN32
#pragma warning(pop)
#endif

#endif
2 changes: 1 addition & 1 deletion sdf/1.9/plugin.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<element name="plugin" required="*">
<description>A plugin is a dynamically loaded chunk of code. It can exist as a child of world, model, and sensor.</description>
<attribute name="name" type="string" default="__default__" required="1">
<description>A unique name for the plugin, scoped to its parent.</description>
<description>A name for the plugin.</description>
</attribute>
<attribute name="filename" type="string" default="__default__" required="1">
<description>Name of the shared library to load. If the filename is not a full path name, the file will be searched for in the configuration paths.</description>
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set (sources
Pbr.cc
Physics.cc
Plane.cc
Plugin.cc
PrintConfig.cc
Root.cc
Scene.cc
Expand Down Expand Up @@ -134,6 +135,7 @@ if (BUILD_SDF_TEST)
ParticleEmitter_TEST.cc
Pbr_TEST.cc
Physics_TEST.cc
Plugin_TEST.cc
PrintConfig_TEST.cc
Plane_TEST.cc
Root_TEST.cc
Expand Down
38 changes: 38 additions & 0 deletions src/Gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class sdf::Gui::Implementation

/// \brief The SDF element pointer used during load.
public: sdf::ElementPtr sdf;

/// \brief GUI plugins.
public: std::vector<Plugin> plugins;
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -57,6 +60,10 @@ Errors Gui::Load(ElementPtr _sdf)
this->dataPtr->fullscreen = _sdf->Get<bool>("fullscreen",
this->dataPtr->fullscreen).first;

Errors pluginErrors = loadRepeated<Plugin>(_sdf, "plugin",
this->dataPtr->plugins);
errors.insert(errors.end(), pluginErrors.begin(), pluginErrors.end());

// \todo(nkoenig) Parse all the elements in gui.sdf

return errors;
Expand Down Expand Up @@ -93,5 +100,36 @@ sdf::ElementPtr Gui::ToElement() const
sdf::initFile("gui.sdf", elem);

elem->GetAttribute("fullscreen")->Set(this->dataPtr->fullscreen);

// Add in the plugins
for (const Plugin &plugin : this->dataPtr->plugins)
elem->InsertElement(plugin.ToElement(), true);

return elem;
}

/////////////////////////////////////////////////
uint64_t Gui::PluginCount() const
{
return this->dataPtr->plugins.size();
}

/////////////////////////////////////////////////
const Plugin *Gui::PluginByIndex(const uint64_t _index) const
{
if (_index < this->dataPtr->plugins.size())
return &this->dataPtr->plugins[_index];
return nullptr;
}

/////////////////////////////////////////////////
void Gui::ClearPlugins()
{
this->dataPtr->plugins.clear();
}

/////////////////////////////////////////////////
void Gui::AddPlugin(const Plugin &_plugin)
{
this->dataPtr->plugins.push_back(_plugin);
}
21 changes: 21 additions & 0 deletions src/Gui_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,32 @@ TEST(DOMGui, ToElement)

gui.SetFullscreen(true);

for (int j = 0; j <= 1; ++j)
{
for (int i = 0; i < 3; ++i)
{
sdf::Plugin plugin;
plugin.SetName("name" + std::to_string(i));
plugin.SetFilename("filename" + std::to_string(i));
gui.AddPlugin(plugin);
gui.AddPlugin(plugin);
}
if (j == 0)
{
EXPECT_EQ(6u, gui.PluginCount());
gui.ClearPlugins();
EXPECT_EQ(0u, gui.PluginCount());
}
}


EXPECT_EQ(6u, gui.PluginCount());
sdf::ElementPtr elem = gui.ToElement();
ASSERT_NE(nullptr, elem);

sdf::Gui gui2;
gui2.Load(elem);

EXPECT_EQ(gui.Fullscreen(), gui2.Fullscreen());
EXPECT_EQ(6u, gui2.PluginCount());
}
Loading