Go to the documentation of this file.
17 #ifndef _GZ_PLUGIN_HH_
18 #define _GZ_PLUGIN_HH_
24 #include <sys/types.h>
27 #include <gazebo/gazebo_config.h>
34 #include <boost/filesystem.hpp>
78 public:
typedef boost::shared_ptr<T>
TPtr;
83 this->dlHandle =
nullptr;
114 public:
static TPtr
Create(
const std::string &_filename,
115 const std::string &_name)
121 std::string fullname,
filename(_filename);
122 std::list<std::string>::iterator iter;
123 std::list<std::string> pluginPaths =
130 size_t soSuffix =
filename.rfind(
".so");
131 if (soSuffix != std::string::npos)
133 const std::string macSuffix(
".dylib");
134 filename.replace(soSuffix, macSuffix.length(), macSuffix);
141 size_t soSuffix =
filename.rfind(
".so");
142 if (soSuffix != std::string::npos)
144 const std::string winSuffix(
".dll");
145 filename.replace(soSuffix, winSuffix.length(), winSuffix);
147 size_t libPrefix =
filename.find(
"lib");
154 #endif // ifdef __APPLE__
156 for (iter = pluginPaths.begin();
157 iter!= pluginPaths.end(); ++iter)
159 fullname = (*iter)+std::string(
"/")+
filename;
160 fullname = boost::filesystem::path(fullname)
161 .make_preferred().string();
162 if (stat(fullname.c_str(), &st) == 0)
172 fptr_union_t registerFunc;
173 std::string registerName =
"RegisterPlugin";
175 void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
178 gzerr <<
"Failed to load plugin " << fullname <<
": "
179 << dlerror() <<
"\n";
183 registerFunc.ptr = dlsym(dlHandle, registerName.c_str());
185 if (!registerFunc.ptr)
187 gzerr <<
"Failed to resolve " << registerName
188 <<
": " << dlerror();
193 result.reset(registerFunc.func());
194 result->dlHandle = dlHandle;
196 result->handleName = _name;
217 protected:
template <
typename V>
void LoadParam(
const sdf::ElementPtr &_sdf,
218 const std::string &_name, V &_target,
219 V _defaultValue = V())
const
221 auto result = _sdf->Get<V>(_name, _defaultValue);
226 << _name.c_str() <<
">, defaults to "
227 << result.first << std::endl;
232 << _name.c_str() <<
"> set to "
233 << result.first << std::endl;
235 _target = result.first;
248 const std::string &_name, std::string &_target,
249 const char* _defaultValue)
const
251 this->LoadParam<std::string>(_sdf, _name, _target, _defaultValue);
264 private:
typedef union
271 private:
void *dlHandle;
294 sdf::ElementPtr _sdf) = 0;
319 sdf::ElementPtr _sdf) = 0;
348 sdf::ElementPtr _sdf) = 0;
375 public:
virtual void Load(
int _argc = 0,
char **_argv =
nullptr) = 0;
401 sdf::ElementPtr _sdf) = 0;
419 #define GZ_REGISTER_MODEL_PLUGIN(classname) \
420 extern "C" GZ_PLUGIN_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
421 gazebo::ModelPlugin *RegisterPlugin() \
423 return new classname();\
430 #define GZ_REGISTER_WORLD_PLUGIN(classname) \
431 extern "C" GZ_PLUGIN_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
432 gazebo::WorldPlugin *RegisterPlugin() \
434 return new classname();\
441 #define GZ_REGISTER_SENSOR_PLUGIN(classname) \
442 extern "C" GZ_PLUGIN_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
443 gazebo::SensorPlugin *RegisterPlugin() \
445 return new classname();\
452 #define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
453 extern "C" GZ_PLUGIN_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
454 gazebo::SystemPlugin *RegisterPlugin() \
456 return new classname();\
463 #define GZ_REGISTER_VISUAL_PLUGIN(classname) \
464 extern "C" GZ_PLUGIN_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
465 gazebo::VisualPlugin *RegisterPlugin() \
467 return new classname();\
ModelPlugin()
Constructor.
Definition: Plugin.hh:306
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
A Model plugin.
Definition: Plugin.hh:60
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:325
Forward declarations for the common classes.
Definition: Animation.hh:26
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:114
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:285
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:406
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
std::string filename
Path to the shared library file.
Definition: Plugin.hh:258
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:104
PluginType type
Type of plugin.
Definition: Plugin.hh:255
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:98
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:322
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:380
A plugin with access to physics::World. See reference.
Definition: Plugin.hh:278
default namespace for gazebo
#define gzerr
Output an error message.
Definition: Console.hh:50
WorldPlugin()
Constructor.
Definition: Plugin.hh:281
VisualPlugin()
Definition: Plugin.hh:391
A class which all plugins must inherit from.
Definition: Plugin.hh:75
SensorPlugin()
Constructor.
Definition: Plugin.hh:335
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:55
A plugin with access to physics::Sensor. See reference.
Definition: Plugin.hh:332
A plugin with access to rendering::Visual.
Definition: Plugin.hh:389
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:87
A GUI plugin.
Definition: Plugin.hh:68
A Visual plugin.
Definition: Plugin.hh:66
SystemPlugin()
Constructor.
Definition: Plugin.hh:364
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:361
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:78
A World plugin.
Definition: Plugin.hh:58
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:383
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:351
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:204
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:409
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)=0
Load function.
A Sensor plugin.
Definition: Plugin.hh:62
virtual void Init()
Definition: Plugin.hh:296
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
virtual void Reset()
Definition: Plugin.hh:297
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:310
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:303
A System plugin.
Definition: Plugin.hh:64
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:354
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:217
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:368
PluginT()
Constructor.
Definition: Plugin.hh:81
#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:261
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:339
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:247