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)
120 std::list<std::string> pluginPaths =
122 std::ostringstream errorStream;
126 auto findAndDlopenPluginFile = [](
127 const std::string &_pluginFilename,
128 const std::list<std::string> &_pluginPaths,
129 std::ostringstream &_errorStream) ->
void *
133 std::string fullname;
134 std::list<std::string>::const_iterator iter;
136 for (iter = _pluginPaths.begin();
137 iter!= _pluginPaths.end(); ++iter)
139 fullname = (*iter)+std::string(
"/")+_pluginFilename;
140 fullname = boost::filesystem::path(fullname)
141 .make_preferred().string();
142 if (stat(fullname.c_str(), &st) == 0)
150 fullname = _pluginFilename;
152 void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
155 _errorStream <<
"Failed to load plugin " << fullname <<
": "
156 << dlerror() <<
"\n";
174 size_t soSuffix =
filename.rfind(
".so");
175 if (soSuffix != std::string::npos)
177 const std::string winSuffix(
".dll");
178 filename.replace(soSuffix, winSuffix.length(), winSuffix);
180 size_t libPrefix =
filename.find(
"lib");
193 void *dlHandle = findAndDlopenPluginFile(
filename, pluginPaths,
199 size_t soSuffix =
filename.rfind(
".so");
200 if (soSuffix != std::string::npos)
202 const std::string macSuffix(
".dylib");
203 filename.replace(soSuffix, macSuffix.length(), macSuffix);
207 dlHandle = findAndDlopenPluginFile(
filename, pluginPaths, errorStream);
212 gzerr << errorStream.str();
216 fptr_union_t registerFunc;
217 const char *registerName =
"RegisterPlugin";
219 registerFunc.ptr = dlsym(dlHandle, registerName);
221 if (!registerFunc.ptr)
223 gzerr <<
"Failed to resolve " << registerName
224 <<
": " << dlerror();
229 result.reset(registerFunc.func());
230 result->dlHandle = dlHandle;
232 result->handleName = _name;
253 protected:
template <
typename V>
void LoadParam(
const sdf::ElementPtr &_sdf,
254 const std::string &_name, V &_target,
255 V _defaultValue = V())
const
257 auto result = _sdf->Get<V>(_name, _defaultValue);
262 << _name.c_str() <<
">, defaults to "
263 << result.first << std::endl;
268 << _name.c_str() <<
"> set to "
269 << result.first << std::endl;
271 _target = result.first;
284 const std::string &_name, std::string &_target,
285 const char* _defaultValue)
const
287 this->LoadParam<std::string>(_sdf, _name, _target, _defaultValue);
300 private:
typedef union
307 private:
void *dlHandle;
330 sdf::ElementPtr _sdf) = 0;
355 sdf::ElementPtr _sdf) = 0;
384 sdf::ElementPtr _sdf) = 0;
411 public:
virtual void Load(
int _argc = 0,
char **_argv =
nullptr) = 0;
437 sdf::ElementPtr _sdf) = 0;
455 #define GZ_REGISTER_MODEL_PLUGIN(classname) \
456 extern "C" GZ_PLUGIN_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
457 gazebo::ModelPlugin *RegisterPlugin() \
459 return new classname();\
466 #define GZ_REGISTER_WORLD_PLUGIN(classname) \
467 extern "C" GZ_PLUGIN_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
468 gazebo::WorldPlugin *RegisterPlugin() \
470 return new classname();\
477 #define GZ_REGISTER_SENSOR_PLUGIN(classname) \
478 extern "C" GZ_PLUGIN_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
479 gazebo::SensorPlugin *RegisterPlugin() \
481 return new classname();\
488 #define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
489 extern "C" GZ_PLUGIN_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
490 gazebo::SystemPlugin *RegisterPlugin() \
492 return new classname();\
499 #define GZ_REGISTER_VISUAL_PLUGIN(classname) \
500 extern "C" GZ_PLUGIN_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
501 gazebo::VisualPlugin *RegisterPlugin() \
503 return new classname();\
default namespace for gazebo
Forward declarations and typedefs for sensors.
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
A plugin with access to physics::Model.
Definition: Plugin.hh:340
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)=0
Load function.
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:358
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:361
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:346
ModelPlugin()
Constructor.
Definition: Plugin.hh:342
A class which all plugins must inherit from.
Definition: Plugin.hh:76
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:98
std::string handleName
Short name.
Definition: Plugin.hh:297
PluginType type
Type of plugin.
Definition: Plugin.hh:291
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:78
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:253
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:240
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
PluginT()
Constructor.
Definition: Plugin.hh:81
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:104
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:283
std::string filename
Path to the shared library file.
Definition: Plugin.hh:294
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:87
A plugin with access to physics::Sensor.
Definition: Plugin.hh:369
SensorPlugin()
Constructor.
Definition: Plugin.hh:371
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:387
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:390
virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)=0
Load function.
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:375
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:398
SystemPlugin()
Constructor.
Definition: Plugin.hh:400
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:416
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:404
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:419
virtual void Load(int _argc=0, char **_argv=nullptr)=0
Load function.
A plugin with access to rendering::Visual.
Definition: Plugin.hh:426
virtual void Load(rendering::VisualPtr _visual, sdf::ElementPtr _sdf)=0
Load function.
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:442
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:445
VisualPlugin()
Definition: Plugin.hh:427
A plugin with access to physics::World.
Definition: Plugin.hh:315
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:321
virtual void Init()
Definition: Plugin.hh:332
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)=0
Load function.
WorldPlugin()
Constructor.
Definition: Plugin.hh:317
virtual void Reset()
Definition: Plugin.hh:333
#define gzmsg
Output a message.
Definition: Console.hh:41
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:56
#define gzerr
Output an error message.
Definition: Console.hh:50
@ SYSTEM_PLUGIN
A System plugin.
Definition: Plugin.hh:64
@ GUI_PLUGIN
A GUI plugin.
Definition: Plugin.hh:68
@ VISUAL_PLUGIN
A Visual plugin.
Definition: Plugin.hh:66
@ SENSOR_PLUGIN
A Sensor plugin.
Definition: Plugin.hh:62
@ MODEL_PLUGIN
A Model plugin.
Definition: Plugin.hh:60
@ WORLD_PLUGIN
A World plugin.
Definition: Plugin.hh:58
boost::shared_ptr< World > WorldPtr
Definition: PhysicsTypes.hh:90
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:94
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
Forward declarations for the common classes.
Definition: Animation.hh:27