Skip to content

Commit ccbda48

Browse files
nkoenigNate Koenigmjcarrollahcordeiche033
authored
Use more sdf::Plugin instead of sdf::ElementPtr (#1352)
Signed-off-by: Nate Koenig <[email protected]> Co-authored-by: Nate Koenig <[email protected]> Co-authored-by: Michael Carroll <[email protected]> Co-authored-by: Alejandro Hernández Cordero <[email protected]> Co-authored-by: Ian Chen <[email protected]>
1 parent e0388cf commit ccbda48

28 files changed

+661
-253
lines changed

include/ignition/gazebo/Conversions.hh

+50
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <ignition/msgs/light.pb.h>
2828
#include <ignition/msgs/material.pb.h>
2929
#include <ignition/msgs/particle_emitter.pb.h>
30+
#include <ignition/msgs/plugin.pb.h>
31+
#include <ignition/msgs/plugin_v.pb.h>
3032
#include <ignition/msgs/physics.pb.h>
3133
#include <ignition/msgs/scene.pb.h>
3234
#include <ignition/msgs/sensor.pb.h>
@@ -742,6 +744,54 @@ namespace ignition
742744
/// \return Plugin message.
743745
template<>
744746
msgs::Plugin convert(const sdf::Plugin &_in);
747+
748+
/// \brief Generic conversion from an SDF plugins to another type.
749+
/// \param[in] _in SDF plugins.
750+
/// \return Conversion result.
751+
/// \tparam Out Output type.
752+
template<class Out>
753+
Out convert(const sdf::Plugins &/*_in*/)
754+
{
755+
Out::ConversionNotImplemented;
756+
}
757+
758+
/// \brief Specialized conversion from an SDF plugins to a plugin_v message.
759+
/// \param[in] _in SDF plugins.
760+
/// \return Plugin_V message.
761+
template<>
762+
msgs::Plugin_V convert(const sdf::Plugins &_in);
763+
764+
/// \brief Generic conversion from a msgs::Plugin to another type.
765+
/// \param[in] _in msgs::Plugin.
766+
/// \return Conversion result.
767+
/// \tparam Out Output type.
768+
template<class Out>
769+
Out convert(const msgs::Plugin &/*_in*/)
770+
{
771+
Out::ConversionNotImplemented;
772+
}
773+
774+
/// \brief Specialized conversion from a msgs::Plugin to an sdf::Plugin.
775+
/// \param[in] _in msgs::Plugin.
776+
/// \return sdf::Plugin.
777+
template<>
778+
sdf::Plugin convert(const msgs::Plugin &_in);
779+
780+
/// \brief Generic conversion from a msgs::Plugin_V to another type.
781+
/// \param[in] _in msgs::Plugin_V.
782+
/// \return Conversion result.
783+
/// \tparam Out Output type.
784+
template<class Out>
785+
Out convert(const msgs::Plugin_V &/*_in*/)
786+
{
787+
Out::ConversionNotImplemented;
788+
}
789+
790+
/// \brief Specialized conversion from a msgs::Plugin_V to an sdf::Plugins.
791+
/// \param[in] _in msgs::Plugin_V.
792+
/// \return sdf::Plugins.
793+
template<>
794+
sdf::Plugins convert(const msgs::Plugin_V &_in);
745795
}
746796
}
747797
}

include/ignition/gazebo/Events.hh

+15-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define IGNITION_GAZEBO_EVENTS_HH_
1919

2020
#include <sdf/Element.hh>
21+
#include <sdf/Plugin.hh>
2122

2223
#include <ignition/common/Event.hh>
2324

@@ -53,11 +54,24 @@ namespace ignition
5354
/// \endcode
5455
using Stop = ignition::common::EventT<void(void), struct StopTag>;
5556

56-
/// \brief Event used to load plugins for an entity into simulation.
57+
/// \brief Please use the LoadSdfPlugins event. The LoadPlugins event
58+
/// will be deprecrated in Gazebo 7 (Garden). Also make sure to
59+
/// connect to only LoadSdfPlugins or LoadPlugins, and not both events.
60+
///
61+
/// Event used to load plugins for an entity into simulation.
5762
/// Pass in the entity which will own the plugins, and an SDF element for
5863
/// the entity, which may contain multiple `<plugin>` tags.
64+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
65+
/// sdf::Plugin interface.
5966
using LoadPlugins = common::EventT<void(Entity, sdf::ElementPtr),
6067
struct LoadPluginsTag>;
68+
69+
/// \brief Event used to load plugins for an entity into simulation.
70+
/// Pass in the entity which will own the plugins, and an SDF element for
71+
/// the entity, which may contain multiple `<plugin>` tags.
72+
/// Makre sure that you don't also connect to the LoadPlugins event.
73+
using LoadSdfPlugins = common::EventT<void(Entity, sdf::Plugins),
74+
struct LoadPluginsTag>;
6175
}
6276
} // namespace events
6377
} // namespace gazebo

include/ignition/gazebo/ServerConfig.hh

+39
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string>
2525
#include <vector>
2626
#include <sdf/Element.hh>
27+
#include <sdf/Plugin.hh>
2728
#include <sdf/Root.hh>
2829
#include <ignition/gazebo/config.hh>
2930
#include <ignition/gazebo/Export.hh>
@@ -86,12 +87,26 @@ namespace ignition
8687
/// \param[in] _name Name of the interface within the plugin library
8788
/// to load.
8889
/// \param[in] _sdf Plugin XML elements associated with this plugin.
90+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
91+
/// sdf::Plugin interface.
8992
public: PluginInfo(const std::string &_entityName,
9093
const std::string &_entityType,
9194
const std::string &_filename,
9295
const std::string &_name,
9396
const sdf::ElementPtr &_sdf);
9497

98+
/// \brief Constructor with plugin information specified.
99+
/// \param[in] _entityName Name of the entity which should receive
100+
/// this plugin. The name is used in conjuction with _entityType to
101+
/// uniquely identify an entity.
102+
/// \param[in] _entityType Entity type which should receive this
103+
/// plugin. The type is used in conjuction with _entityName to
104+
/// uniquely identify an entity.
105+
/// \param[in] _plugin SDF Plugin library information.
106+
public: PluginInfo(const std::string &_entityName,
107+
const std::string &_entityType,
108+
const sdf::Plugin &_plugin);
109+
95110
/// \brief Copy constructor.
96111
/// \param[in] _info Plugin to copy.
97112
public: PluginInfo(const PluginInfo &_info);
@@ -127,32 +142,56 @@ namespace ignition
127142

128143
/// \brief Get the plugin library filename.
129144
/// \return Plugin library filename.
145+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
146+
/// sdf::Plugin interface.
130147
public: const std::string &Filename() const;
131148

132149
/// \brief Set the type of the entity which should receive this
133150
/// plugin. The type is used in conjuction with EntityName to
134151
/// uniquely identify an entity.
135152
/// \param[in] _filename Entity type string.
153+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
154+
/// sdf::Plugin interface.
136155
public: void SetFilename(const std::string &_filename);
137156

138157
/// \brief Name of the interface within the plugin library
139158
/// to load.
140159
/// \return Interface name.
160+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
161+
/// sdf::Plugin interface.
141162
public: const std::string &Name() const;
142163

143164
/// \brief Set the name of the interface within the plugin library
144165
/// to load.
145166
/// \param[in] _name Interface name.
167+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
168+
/// sdf::Plugin interface.
146169
public: void SetName(const std::string &_name);
147170

148171
/// \brief Plugin XML elements associated with this plugin.
149172
/// \return SDF pointer.
173+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
174+
/// sdf::Plugin interface.
150175
public: const sdf::ElementPtr &Sdf() const;
151176

152177
/// \brief Set the plugin XML elements associated with this plugin.
153178
/// \param[in] _sdf SDF pointer, it will be cloned.
179+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
180+
/// sdf::Plugin interface.
154181
public: void SetSdf(const sdf::ElementPtr &_sdf);
155182

183+
/// \brief Get the SDF plugin information.
184+
/// \return The SDF Plugin object.
185+
public: const sdf::Plugin &Plugin() const;
186+
187+
/// \brief Get a mutable version of the SDF plugin information.
188+
/// \return The SDF Plugin object.
189+
public: sdf::Plugin &Plugin();
190+
191+
/// \brief Set the SDF plugin information.
192+
/// \param[in] _plugin The SDF Plugin object to use.
193+
public: void SetPlugin(const sdf::Plugin &_plugin) const;
194+
156195
/// \brief Private data pointer
157196
private: std::unique_ptr<ServerConfig::PluginInfoPrivate> dataPtr;
158197
};

include/ignition/gazebo/SystemLoader.hh

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <string>
2323

2424
#include <sdf/Element.hh>
25+
#include <sdf/Plugin.hh>
2526

2627
#include <ignition/gazebo/Export.hh>
2728
#include <ignition/gazebo/System.hh>
@@ -53,6 +54,8 @@ namespace ignition
5354
/// \brief Load and instantiate system plugin from an SDF element.
5455
/// \param[in] _sdf SDF Element describing plugin instance to be loaded.
5556
/// \returns Shared pointer to system instance or nullptr.
57+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
58+
/// sdf::Plugin interface.
5659
public: std::optional<SystemPluginPtr> LoadPlugin(
5760
const sdf::ElementPtr &_sdf);
5861

@@ -61,11 +64,19 @@ namespace ignition
6164
/// \param[in] _name Class name to be instantiated.
6265
/// \param[in] _sdf SDF Element describing plugin instance to be loaded.
6366
/// \returns Shared pointer to system instance or nullptr.
67+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
68+
/// sdf::Plugin interface.
6469
public: std::optional<SystemPluginPtr> LoadPlugin(
6570
const std::string &_filename,
6671
const std::string &_name,
6772
const sdf::ElementPtr &_sdf);
6873

74+
/// \brief Load and instantiate system plugin from name/filename.
75+
/// \param[in] _plugin SDF Plugin to be loaded.
76+
/// \returns Shared pointer to system instance or nullptr.
77+
public: std::optional<SystemPluginPtr> LoadPlugin(
78+
const sdf::Plugin &_plugin);
79+
6980
/// \brief Makes a printable string with info about systems
7081
/// \returns A pretty string
7182
public: std::string PrettyStr() const;

include/ignition/gazebo/gui/GuiEvents.hh

+25
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <ignition/math/Vector3.hh>
3030
#include <ignition/utils/ImplPtr.hh>
3131
#include <sdf/Element.hh>
32+
#include <sdf/Plugin.hh>
3233

3334
#include "ignition/gazebo/gui/Export.hh"
3435
#include "ignition/gazebo/Entity.hh"
@@ -215,6 +216,8 @@ namespace events
215216
};
216217

217218
/// \brief Event that notifies a visual plugin is to be loaded
219+
/// \note This will be deprecated in Gazebo 7 (Garden), please the use
220+
/// VisualPlugins class.
218221
class IGNITION_GAZEBO_GUI_VISIBLE VisualPlugin: public QEvent
219222
{
220223
/// \brief Constructor
@@ -235,6 +238,28 @@ namespace events
235238
/// \brief Private data pointer
236239
IGN_UTILS_IMPL_PTR(dataPtr)
237240
};
241+
242+
/// \brief Event that notifies a visual plugin is to be loaded
243+
class IGNITION_GAZEBO_GUI_VISIBLE VisualPlugins: public QEvent
244+
{
245+
/// \brief Constructor
246+
/// \param[in] _entity Visual entity id
247+
/// \param[in] _plugins SDF plugin object
248+
public: explicit VisualPlugins(ignition::gazebo::Entity _entity,
249+
const sdf::Plugins &_plugins);
250+
251+
/// \brief Get the entity to load the visual plugin for
252+
public: ignition::gazebo::Entity Entity() const;
253+
254+
/// \brief Get the SDF Plugin of the visual plugin
255+
public: const sdf::Plugins &Plugins() const;
256+
257+
static const QEvent::Type kType = QEvent::Type(QEvent::User + 9);
258+
259+
/// \internal
260+
/// \brief Private data pointer
261+
IGN_UTILS_IMPL_PTR(dataPtr)
262+
};
238263
} // namespace events
239264
}
240265
} // namespace gui

src/Conversions.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1792,3 +1792,35 @@ msgs::Plugin ignition::gazebo::convert(const sdf::Plugin &_in)
17921792

17931793
return result;
17941794
}
1795+
1796+
//////////////////////////////////////////////////
1797+
template<>
1798+
IGNITION_GAZEBO_VISIBLE
1799+
msgs::Plugin_V ignition::gazebo::convert(const sdf::Plugins &_in)
1800+
{
1801+
msgs::Plugin_V result;
1802+
for (const sdf::Plugin &plugin : _in)
1803+
{
1804+
result.add_plugins()->CopyFrom(convert<msgs::Plugin>(plugin));
1805+
}
1806+
return result;
1807+
}
1808+
1809+
//////////////////////////////////////////////////
1810+
template<>
1811+
IGNITION_GAZEBO_VISIBLE
1812+
sdf::Plugin ignition::gazebo::convert(const msgs::Plugin &_in)
1813+
{
1814+
return sdf::Plugin(_in.filename(), _in.name(), _in.innerxml());
1815+
}
1816+
1817+
//////////////////////////////////////////////////
1818+
template<>
1819+
IGNITION_GAZEBO_VISIBLE
1820+
sdf::Plugins ignition::gazebo::convert(const msgs::Plugin_V &_in)
1821+
{
1822+
sdf::Plugins result;
1823+
for (int i = 0; i < _in.plugins_size(); ++i)
1824+
result.push_back(convert<sdf::Plugin>(_in.plugins(i)));
1825+
return result;
1826+
}

src/Conversions_TEST.cc

+47
Original file line numberDiff line numberDiff line change
@@ -1109,3 +1109,50 @@ TEST(Conversions, Plugin)
11091109
EXPECT_NE(pluginMsg.innerxml().find("<avocado>0.5</avocado>"),
11101110
std::string::npos) << pluginMsg.innerxml();
11111111
}
1112+
1113+
/////////////////////////////////////////////////
1114+
TEST(Conversions, MsgsPluginToSdf)
1115+
{
1116+
std::string innerXml ="<test>another_test</test>\n";
1117+
std::string innerXml2 ="<peanut>butter</peanut>\n";
1118+
1119+
msgs::Plugin msgPlugin;
1120+
msgPlugin.set_name("foo");
1121+
msgPlugin.set_filename("bar");
1122+
msgPlugin.set_innerxml(innerXml);
1123+
1124+
// Test conversion of a single msgs::Plugin to sdf::Plugin
1125+
sdf::Plugin sdfPlugin = convert<sdf::Plugin>(msgPlugin);
1126+
EXPECT_EQ("foo", sdfPlugin.Name());
1127+
EXPECT_EQ("bar", sdfPlugin.Filename());
1128+
ASSERT_EQ(1u, sdfPlugin.Contents().size());
1129+
EXPECT_EQ(innerXml, sdfPlugin.Contents()[0]->ToString(""));
1130+
1131+
// Test conversion of a msgs::Plugin_V with 1 plugin to sdf::Plugins
1132+
msgs::Plugin_V msgsPlugin;
1133+
msgsPlugin.add_plugins()->CopyFrom(msgPlugin);
1134+
sdf::Plugins sdfPlugins = convert<sdf::Plugins>(msgsPlugin);
1135+
ASSERT_EQ(1u, sdfPlugins.size());
1136+
EXPECT_EQ("foo", sdfPlugins[0].Name());
1137+
EXPECT_EQ("bar", sdfPlugins[0].Filename());
1138+
ASSERT_EQ(1u, sdfPlugins[0].Contents().size());
1139+
EXPECT_EQ(innerXml, sdfPlugins[0].Contents()[0]->ToString(""));
1140+
1141+
// Add another plugin the msgs::Plugin_V
1142+
msgs::Plugin anotherPlugin;
1143+
anotherPlugin.set_name("sandwich");
1144+
anotherPlugin.set_filename("time");
1145+
anotherPlugin.set_innerxml(innerXml + innerXml2);
1146+
msgsPlugin.add_plugins()->CopyFrom(anotherPlugin);
1147+
sdfPlugins = convert<sdf::Plugins>(msgsPlugin);
1148+
ASSERT_EQ(2u, sdfPlugins.size());
1149+
EXPECT_EQ("foo", sdfPlugins[0].Name());
1150+
EXPECT_EQ("bar", sdfPlugins[0].Filename());
1151+
ASSERT_EQ(1u, sdfPlugins[0].Contents().size());
1152+
EXPECT_EQ(innerXml, sdfPlugins[0].Contents()[0]->ToString(""));
1153+
EXPECT_EQ("sandwich", sdfPlugins[1].Name());
1154+
EXPECT_EQ("time", sdfPlugins[1].Filename());
1155+
ASSERT_EQ(2u, sdfPlugins[1].Contents().size());
1156+
EXPECT_EQ(innerXml, sdfPlugins[1].Contents()[0]->ToString(""));
1157+
EXPECT_EQ(innerXml2, sdfPlugins[1].Contents()[1]->ToString(""));
1158+
}

src/LevelManager.cc

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ void LevelManager::ReadLevelPerformerInfo()
229229
this->ConfigureDefaultLevel();
230230

231231
// Load world plugins.
232+
this->runner->EventMgr().Emit<events::LoadSdfPlugins>(this->worldEntity,
233+
this->runner->sdfWorld->Plugins());
234+
// Deprecate this in Garden
232235
this->runner->EventMgr().Emit<events::LoadPlugins>(this->worldEntity,
233236
this->runner->sdfWorld->Element());
234237

0 commit comments

Comments
 (0)