Skip to content

Commit c731941

Browse files
committed
return a filtered list of parsers only
1 parent b0ffca9 commit c731941

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

urdf/include/urdf/sensor.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@
4444

4545
namespace urdf {
4646

47-
/** retrieve all sensor parsers available in the system
48-
through the plugin-lib mechanism */
49-
const urdf::SensorParserMap &getDefaultSensorParserMap();
47+
/** Retrieve sensor parsers available through the plugin-lib mechanism
48+
whose name matches any of the names listed in allowed.
49+
If allowed is empty (the default), all parsers will be returned.
50+
*/
51+
urdf::SensorParserMap getSensorParsers(const std::vector<std::string> &allowed = std::vector<std::string>());
52+
53+
/** Conveniency method returning the SensorParserMap for the given sensor name */
54+
urdf::SensorParserMap getSensorParser(const std::string &name);
5055

5156
/** parse <sensor> tags in URDF document */
5257
SensorMap parseSensors(TiXmlDocument &doc, const urdf::SensorParserMap &parsers);

urdf/src/sensor.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <ros/ros.h>
4040
#include <pluginlib/class_loader.h>
4141
#include <boost/shared_ptr.hpp>
42+
#include <algorithm>
4243
#include <fstream>
4344

4445
namespace urdf {
@@ -86,7 +87,7 @@ SensorMap parseSensors(const std::string &xml_string, const SensorParserMap &par
8687
return parseSensors(xml_doc, parsers);
8788
}
8889

89-
const SensorParserMap& getDefaultSensorParserMap()
90+
SensorParserMap getSensorParsers(const std::vector<std::string> &allowed)
9091
{
9192
static boost::mutex PARSER_PLUGIN_LOCK;
9293
static boost::shared_ptr<pluginlib::ClassLoader<urdf::SensorParser> > PARSER_PLUGIN_LOADER;
@@ -101,24 +102,35 @@ const SensorParserMap& getDefaultSensorParserMap()
101102
const std::vector<std::string> &classes = PARSER_PLUGIN_LOADER->getDeclaredClasses();
102103
for (std::size_t i = 0 ; i < classes.size() ; ++i)
103104
{
104-
urdf::SensorParserSharedPtr parser;
105-
try {
106-
parser = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
107-
} catch(const pluginlib::PluginlibException& ex) {
108-
ROS_ERROR_STREAM("Failed to create sensor parser: " << classes[i] << "\n" << ex.what());
109-
}
110-
defaultParserMap.insert(std::make_pair(classes[i], parser));
111-
ROS_DEBUG_STREAM("added sensor parser: " << classes[i]);
105+
// skip this class if not listed in allowed
106+
if (!allowed.empty() && std::find(allowed.begin(), allowed.end(), classes[i]) == allowed.end())
107+
continue;
108+
109+
urdf::SensorParserSharedPtr parser;
110+
try {
111+
parser = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
112+
} catch(const pluginlib::PluginlibException& ex) {
113+
ROS_ERROR_STREAM("Failed to create sensor parser: " << classes[i] << "\n" << ex.what());
114+
}
115+
defaultParserMap.insert(std::make_pair(classes[i], parser));
116+
ROS_DEBUG_STREAM("added sensor parser: " << classes[i]);
112117
}
113118
if (defaultParserMap.empty())
114119
ROS_WARN_STREAM("No sensor parsers found");
115120
}
116121
}
117122
catch(const pluginlib::PluginlibException& ex)
118123
{
119-
ROS_ERROR_STREAM("Exception while creating sensor plugin loader " << ex.what());
124+
ROS_ERROR_STREAM("Exception while creating sensor plugin loader " << ex.what());
120125
}
121126
return defaultParserMap;
122127
}
123128

129+
SensorParserMap getSensorParser(const std::string &name)
130+
{
131+
std::vector<std::string> allowed;
132+
allowed.push_back(name);
133+
return getSensorParsers(allowed);
134+
}
135+
124136
} // namespace

0 commit comments

Comments
 (0)