Go to the documentation of this file.
17 #ifndef _GZ_PLUGIN_HH_
18 #define _GZ_PLUGIN_HH_
33 #include <sys/types.h>
36 #include <gazebo/gazebo_config.h>
43 #include <boost/filesystem.hpp>
87 public:
typedef boost::shared_ptr<T>
TPtr;
92 this->dlHandle =
nullptr;
123 public:
static TPtr
Create(
const std::string &_filename,
124 const std::string &_name)
130 std::string fullname,
filename(_filename);
131 std::list<std::string>::iterator iter;
132 std::list<std::string> pluginPaths =
139 size_t soSuffix =
filename.rfind(
".so");
140 if (soSuffix != std::string::npos)
142 const std::string macSuffix(
".dylib");
143 filename.replace(soSuffix, macSuffix.length(), macSuffix);
150 size_t soSuffix =
filename.rfind(
".so");
151 if (soSuffix != std::string::npos)
153 const std::string winSuffix(
".dll");
154 filename.replace(soSuffix, winSuffix.length(), winSuffix);
156 size_t libPrefix =
filename.find(
"lib");
163 #endif // ifdef __APPLE__
165 for (iter = pluginPaths.begin();
166 iter!= pluginPaths.end(); ++iter)
168 fullname = (*iter)+std::string(
"/")+
filename;
169 fullname = boost::filesystem::path(fullname)
170 .make_preferred().string();
171 if (stat(fullname.c_str(), &st) == 0)
181 fptr_union_t registerFunc;
182 std::string registerName =
"RegisterPlugin";
184 void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
187 gzerr <<
"Failed to load plugin " << fullname <<
": "
188 << dlerror() <<
"\n";
192 registerFunc.ptr = dlsym(dlHandle, registerName.c_str());
194 if (!registerFunc.ptr)
196 gzerr <<
"Failed to resolve " << registerName
197 <<
": " << dlerror();
202 result.reset(registerFunc.func());
203 result->dlHandle = dlHandle;
205 result->handleName = _name;
226 protected:
template <
typename V>
void LoadParam(
const sdf::ElementPtr &_sdf,
227 const std::string &_name, V &_target,
228 V _defaultValue = V())
const
230 auto result = _sdf->Get<V>(_name, _defaultValue);
235 << _name.c_str() <<
">, defaults to "
236 << result.first << std::endl;
241 << _name.c_str() <<
"> set to "
242 << result.first << std::endl;
244 _target = result.first;
257 const std::string &_name, std::string &_target,
258 const char* _defaultValue)
const
260 this->LoadParam<std::string>(_sdf, _name, _target, _defaultValue);
273 private:
typedef union
280 private:
void *dlHandle;
303 sdf::ElementPtr _sdf) = 0;
328 sdf::ElementPtr _sdf) = 0;
357 sdf::ElementPtr _sdf) = 0;
384 public:
virtual void Load(
int _argc = 0,
char **_argv =
nullptr) = 0;
410 sdf::ElementPtr _sdf) = 0;
428 #define GZ_REGISTER_MODEL_PLUGIN(classname) \
429 extern "C" GZ_PLUGIN_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
430 gazebo::ModelPlugin *RegisterPlugin() \
432 return new classname();\
439 #define GZ_REGISTER_WORLD_PLUGIN(classname) \
440 extern "C" GZ_PLUGIN_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
441 gazebo::WorldPlugin *RegisterPlugin() \
443 return new classname();\
450 #define GZ_REGISTER_SENSOR_PLUGIN(classname) \
451 extern "C" GZ_PLUGIN_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
452 gazebo::SensorPlugin *RegisterPlugin() \
454 return new classname();\
461 #define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
462 extern "C" GZ_PLUGIN_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
463 gazebo::SystemPlugin *RegisterPlugin() \
465 return new classname();\
472 #define GZ_REGISTER_VISUAL_PLUGIN(classname) \
473 extern "C" GZ_PLUGIN_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
474 gazebo::VisualPlugin *RegisterPlugin() \
476 return new classname();\
ModelPlugin()
Constructor.
Definition: Plugin.hh:315
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
@ MODEL_PLUGIN
A Model plugin.
Definition: Plugin.hh:69
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:334
Forward declarations for the common classes.
Definition: Animation.hh:27
static TPtr Create(const std::string &_filename, const std::string &_name)
a class method that creates a plugin from a file name.
Definition: Plugin.hh:123
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:294
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:415
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
std::string filename
Path to the shared library file.
Definition: Plugin.hh:267
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:113
PluginType type
Type of plugin.
Definition: Plugin.hh:264
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:107
Forward declarations and typedefs for sensors.
virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)=0
Load function.
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:331
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:389
A plugin with access to physics::World. See reference.
Definition: Plugin.hh:288
default namespace for gazebo
#define gzerr
Output an error message.
Definition: Console.hh:50
WorldPlugin()
Constructor.
Definition: Plugin.hh:290
VisualPlugin()
Definition: Plugin.hh:400
A class which all plugins must inherit from.
Definition: Plugin.hh:85
SensorPlugin()
Constructor.
Definition: Plugin.hh:344
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:65
A plugin with access to physics::Sensor. See reference.
Definition: Plugin.hh:342
A plugin with access to rendering::Visual.
Definition: Plugin.hh:399
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:96
@ GUI_PLUGIN
A GUI plugin.
Definition: Plugin.hh:77
@ VISUAL_PLUGIN
A Visual plugin.
Definition: Plugin.hh:75
SystemPlugin()
Constructor.
Definition: Plugin.hh:373
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:371
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:87
@ WORLD_PLUGIN
A World plugin.
Definition: Plugin.hh:67
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:392
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:360
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:213
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:418
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)=0
Load function.
@ SENSOR_PLUGIN
A Sensor plugin.
Definition: Plugin.hh:71
virtual void Init()
Definition: Plugin.hh:305
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
virtual void Reset()
Definition: Plugin.hh:306
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:319
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)=0
Load function.
boost::shared_ptr< World > WorldPtr
Definition: PhysicsTypes.hh:89
A plugin with access to physics::Model.
Definition: Plugin.hh:313
@ SYSTEM_PLUGIN
A System plugin.
Definition: Plugin.hh:73
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:363
virtual void Load(rendering::VisualPtr _visual, sdf::ElementPtr _sdf)=0
Load function.
void LoadParam(const sdf::ElementPtr &_sdf, const std::string &_name, V &_target, V _defaultValue=V()) const
Load parameter value from _sdf and store it to the given reference, using the supplied default value ...
Definition: Plugin.hh:226
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:377
PluginT()
Constructor.
Definition: Plugin.hh:90
#define gzmsg
Output a message.
Definition: Console.hh:41
virtual void Load(int _argc=0, char **_argv=nullptr)=0
Load function.
std::string handleName
Short name.
Definition: Plugin.hh:270
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:348
void LoadParam(sdf::ElementPtr &_sdf, const std::string &_name, std::string &_target, const char *_defaultValue) const
Load parameter value from _sdf and store it to the given reference, using the supplied default value ...
Definition: Plugin.hh:256