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 convenience constructor to plugin #911

Merged
merged 8 commits into from
Apr 4, 2022
Merged

Conversation

nkoenig
Copy link
Contributor

@nkoenig nkoenig commented Mar 25, 2022

Signed-off-by: Nate Koenig [email protected]

🎉 New feature

A non-trivial amount of code is required to programmatically construct a plugin that has content. For example, this plugin

<plugin  filename="ignition-gazebo-sensors-system"  name="ignition::gazebo::systems::Sensors">
  <render_engine>ogre2</render_engine>
</plugin>

Requires this code:

sdf::Plugin sensorPlugin;
sensorPlugin.SetFilename("ignition-gazebo-sensors-system");
sensorPlugin.SetName("ignition::gazebo::systems::Sensors");
sdf::ElementPtr sensorElem(new sdf::Element);
sensorElem->SetName("render_engine");
sensorElem->AddValue("string", "ogre2", true);
sensorElem->Set<std::string>("ogre2");
sensorPlugin.InsertContent(sensorElem);

This pull request adds an InsertContent function and an initialization constructor to the Plugin class. Now you'll be able to build the same plugin using:

sdf::Plugin sensorPlugin("ignition-gazebo-sensors-system",  "ignition::gazebo::systems::Sensors", "render_engine>ogre2</render_engine>");

I copied the copyChildren function from parser.cc to Utils.cc so that the Plugin class can use it. This shouldn't effect API/ABI.

Test it

New tests have been added to Plugin_TEST.cc

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@nkoenig nkoenig requested review from azeey and scpeters as code owners March 25, 2022 16:50
@github-actions github-actions bot added the 🏯 fortress Ignition Fortress label Mar 25, 2022
@codecov-commenter
Copy link

codecov-commenter commented Mar 25, 2022

Codecov Report

Merging #911 (fe652eb) into sdf12 (3a64d88) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head fe652eb differs from pull request most recent head b8e2d3b. Consider uploading reports for the commit b8e2d3b to get more accurate results

@@            Coverage Diff             @@
##            sdf12     #911      +/-   ##
==========================================
+ Coverage   87.63%   87.65%   +0.02%     
==========================================
  Files         104      104              
  Lines       14996    15024      +28     
==========================================
+ Hits        13142    13170      +28     
  Misses       1854     1854              
Impacted Files Coverage Δ
include/sdf/Plugin.hh 93.75% <ø> (ø)
src/Utils.hh 94.73% <ø> (ø)
src/parser.cc 87.32% <ø> (-0.29%) ⬇️
src/Plugin.cc 100.00% <100.00%> (ø)
src/Utils.cc 86.06% <100.00%> (+2.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3a64d88...b8e2d3b. Read the comment docs.

@@ -118,6 +120,7 @@ target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME}
ignition-math${IGN_MATH_VER}::ignition-math${IGN_MATH_VER}
ignition-utils${IGN_UTILS_VER}::ignition-utils${IGN_UTILS_VER}
PRIVATE
ignition-common${IGN_COMMON_VER}::ignition-common${IGN_COMMON_VER}
Copy link
Collaborator

Choose a reason for hiding this comment

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

we added ignition::common for USD I'm not really sure that you can use this dependency here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dependency doesn't seem bad to me. Here is the code I'd duplicate, which I could trim (haha) down:

/////////////////////////////////////////////////
void ignition::common::ltrim(std::string &_s)
{
  _s.erase(_s.begin(), std::find_if(_s.begin(), _s.end(),
        [](int c) {return !std::isspace(c);}));
}

/////////////////////////////////////////////////
void ignition::common::rtrim(std::string &_s)
{
  _s.erase(std::find_if(_s.rbegin(), _s.rend(),
        [](int c) {return !std::isspace(c);}).base(),
      _s.end());
}

/////////////////////////////////////////////////
void ignition::common::trim(std::string &_s)
{
  ignition::common::ltrim(_s);
  ignition::common::rtrim(_s);
}

/////////////////////////////////////////////////
std::string ignition::common::ltrimmed(std::string _s)
{
  ignition::common::ltrim(_s);
  return _s;
}   

/////////////////////////////////////////////////
std::string ignition::common::rtrimmed(std::string _s)
{
  ignition::common::rtrim(_s);
  return _s;
}

/////////////////////////////////////////////////
std::string ignition::common::trimmed(std::string _s)
{
  ignition::common::trim(_s);
  return _s;
}

Utilizing existing code rather than duplicating it seems like something we should do internally. Otherwise, we should be telling users to copy our code, and not use the libraries.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should add a dependency in a stable version. We already have sdf::trim that can be used instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, this is a stable version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in e6fc820

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding ign-common as a dependency to the core of SDF is something we should think carefully about. That brings a lot of dependencies. An alternative to consider would be to move some of this functionality to ign-utils.

src/Plugin.cc Outdated
{
this->SetFilename(_filename);
this->SetName(_name);
std::string trimmed = ignition::common::trimmed(_xmlContent);
Copy link
Collaborator

Choose a reason for hiding this comment

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

you are only using ignition::common here, can you remove this dependency here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in e6fc820

@nkoenig nkoenig enabled auto-merge (squash) April 4, 2022 17:35
@nkoenig nkoenig merged commit a35251c into sdf12 Apr 4, 2022
@nkoenig nkoenig deleted the plugin_constructor branch April 4, 2022 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏯 fortress Ignition Fortress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants