##

Plugin.hh
Go to the documentation of this file.
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _GZ_PLUGIN_HH_
#define _GZ_PLUGIN_HH_
#ifndef _WIN32
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <gazebo/gazebo_config.h>
#include <dlfcn.h>
#include <list>
#include <string>
#include <sdf/sdf.hh>
#include <boost/filesystem.hpp>
#include "gazebo/common/CommonTypes.hh"
#include "gazebo/common/SystemPaths.hh"
#include "gazebo/common/Console.hh"
#include "gazebo/common/Exception.hh"
#include "gazebo/physics/PhysicsTypes.hh"
#include "gazebo/sensors/SensorTypes.hh"
#include "gazebo/rendering/RenderTypes.hh"
#include "gazebo/util/system.hh"
namespace gazebo
{
class Event;
enum PluginType
{
WORLD_PLUGIN,
MODEL_PLUGIN,
SENSOR_PLUGIN,
SYSTEM_PLUGIN,
VISUAL_PLUGIN,
GUI_PLUGIN
};
template<class T>
class PluginT
{
public: typedef boost::shared_ptr<T> TPtr;
public: PluginT()
{
this->dlHandle = nullptr;
}
public: virtual ~PluginT()
{
// dlclose has been disabled due to segfaults in the test suite
// This workaround is detailed in #1026 and #1066. After the test
// or gazebo execution the plugin is not loaded in memory anymore
// \todo Figure out the right thing to do.
// dlclose(this->dlHandle);
}
public: std::string GetFilename() const
{
return this->filename;
}
public: std::string GetHandle() const
{
return this->handleName;
}
public: static TPtr Create(const std::string &_filename,
const std::string &_name)
{
TPtr result;
// PluginPtr result;
struct stat st;
bool found = false;
std::string fullname, filename(_filename);
std::list<std::string>::iterator iter;
std::list<std::string> pluginPaths =
common::SystemPaths::Instance()->GetPluginPaths();
#ifdef __APPLE__
// This is a hack to work around issue #800,
// error loading plugin libraries with different extensions
{
size_t soSuffix = filename.rfind(".so");
if (soSuffix != std::string::npos)
{
const std::string macSuffix(".dylib");
filename.replace(soSuffix, macSuffix.length(), macSuffix);
}
}
#elif _WIN32
// Corresponding windows hack
{
// replace .so with .dll
size_t soSuffix = filename.rfind(".so");
if (soSuffix != std::string::npos)
{
const std::string winSuffix(".dll");
filename.replace(soSuffix, winSuffix.length(), winSuffix);
}
size_t libPrefix = filename.find("lib");
if (libPrefix == 0)
{
// remove the lib prefix
filename.erase(0, 3);
}
}
#endif // ifdef __APPLE__
for (iter = pluginPaths.begin();
iter!= pluginPaths.end(); ++iter)
{
fullname = (*iter)+std::string("/")+filename;
fullname = boost::filesystem::path(fullname)
.make_preferred().string();
if (stat(fullname.c_str(), &st) == 0)
{
found = true;
break;
}
}
if (!found)
fullname = filename;
fptr_union_t registerFunc;
std::string registerName = "RegisterPlugin";
void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (!dlHandle)
{
gzerr << "Failed to load plugin " << fullname << ": "
<< dlerror() << "\n";
return result;
}
registerFunc.ptr = dlsym(dlHandle, registerName.c_str());
if (!registerFunc.ptr)
{
gzerr << "Failed to resolve " << registerName
<< ": " << dlerror();
return result;
}
// Register the new controller.
result.reset(registerFunc.func());
result->dlHandle = dlHandle;
result->handleName = _name;
result->filename = filename;
return result;
}
public: PluginType GetType() const
{
return this->type;
}
protected: template <typename V> void LoadParam(const sdf::ElementPtr &_sdf,
const std::string &_name, V &_target,
V _defaultValue = V()) const
{
auto result = _sdf->Get<V>(_name, _defaultValue);
if (!result.second)
{
gzmsg << this->handleName.c_str() << " Plugin missing <"
<< _name.c_str() << ">, defaults to "
<< result.first << std::endl;
}
else
{
gzmsg << this->handleName.c_str() << " Plugin <"
<< _name.c_str() << "> set to "
<< result.first << std::endl;
}
_target = result.first;
}
protected: void LoadParam(sdf::ElementPtr &_sdf,
const std::string &_name, std::string &_target,
const char* _defaultValue) const
{
this->LoadParam<std::string>(_sdf, _name, _target, _defaultValue);
}
protected: PluginType type;
protected: std::string filename;
protected: std::string handleName;
private: typedef union
{
T *(*func)();
void *ptr;
} fptr_union_t;
private: void *dlHandle;
};
class WorldPlugin : public PluginT<WorldPlugin>
{
public: WorldPlugin()
{this->type = WORLD_PLUGIN;}
public: virtual ~WorldPlugin() {}
public: virtual void Load(physics::WorldPtr _world,
sdf::ElementPtr _sdf) = 0;
public: virtual void Init() {}
public: virtual void Reset() {}
};
class ModelPlugin : public PluginT<ModelPlugin>
{
public: ModelPlugin()
{this->type = MODEL_PLUGIN;}
public: virtual ~ModelPlugin() {}
public: virtual void Load(physics::ModelPtr _model,
sdf::ElementPtr _sdf) = 0;
public: virtual void Init() {}
public: virtual void Reset() {}
};
class SensorPlugin : public PluginT<SensorPlugin>
{
public: SensorPlugin()
{this->type = SENSOR_PLUGIN;}
public: virtual ~SensorPlugin() {}
public: virtual void Load(sensors::SensorPtr _sensor,
sdf::ElementPtr _sdf) = 0;
public: virtual void Init() {}
public: virtual void Reset() {}
};
class SystemPlugin : public PluginT<SystemPlugin>
{
public: SystemPlugin()
{this->type = SYSTEM_PLUGIN;}
public: virtual ~SystemPlugin() {}
public: virtual void Load(int _argc = 0, char **_argv = nullptr) = 0;
public: virtual void Init() {}
public: virtual void Reset() {}
};
class VisualPlugin : public PluginT<VisualPlugin>
{
public: VisualPlugin()
{this->type = VISUAL_PLUGIN;}
public: virtual void Load(rendering::VisualPtr _visual,
sdf::ElementPtr _sdf) = 0;
public: virtual void Init() {}
public: virtual void Reset() {}
};
#define GZ_REGISTER_MODEL_PLUGIN(classname) \
extern "C" GZ_PLUGIN_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
gazebo::ModelPlugin *RegisterPlugin() \
{\
return new classname();\
}
#define GZ_REGISTER_WORLD_PLUGIN(classname) \
extern "C" GZ_PLUGIN_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
gazebo::WorldPlugin *RegisterPlugin() \
{\
return new classname();\
}
#define GZ_REGISTER_SENSOR_PLUGIN(classname) \
extern "C" GZ_PLUGIN_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
gazebo::SensorPlugin *RegisterPlugin() \
{\
return new classname();\
}
#define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
extern "C" GZ_PLUGIN_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
gazebo::SystemPlugin *RegisterPlugin() \
{\
return new classname();\
}
#define GZ_REGISTER_VISUAL_PLUGIN(classname) \
extern "C" GZ_PLUGIN_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
gazebo::VisualPlugin *RegisterPlugin() \
{\
return new classname();\
}
}
#endif
gazebo::ModelPlugin::ModelPlugin
ModelPlugin()
Constructor.
Definition: Plugin.hh:306
gazebo::sensors::SensorPtr
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:64
gazebo::MODEL_PLUGIN
A Model plugin.
Definition: Plugin.hh:60
gazebo::ModelPlugin::Reset
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:325
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
gazebo::PluginT::Create
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
gazebo::WorldPlugin::~WorldPlugin
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:285
Exception.hh
gazebo::VisualPlugin::Init
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:406
gazebo::physics::ModelPtr
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:93
gazebo::PluginT::filename
std::string filename
Path to the shared library file.
Definition: Plugin.hh:258
gazebo::PluginT::GetHandle
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:104
gazebo::PluginT::type
PluginType type
Type of plugin.
Definition: Plugin.hh:255
gazebo::PluginT::GetFilename
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:98
SensorTypes.hh
Forward declarations and typedefs for sensors.
Console.hh
system.hh
gazebo::SensorPlugin::Load
virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)=0
Load function.
CommonTypes.hh
gazebo::ModelPlugin::Init
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:322
gazebo::SystemPlugin::Init
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:380
gazebo::WorldPlugin
A plugin with access to physics::World. See reference.
Definition: Plugin.hh:278
PhysicsTypes.hh
default namespace for gazebo
gzerr
#define gzerr
Output an error message.
Definition: Console.hh:50
gazebo::WorldPlugin::WorldPlugin
WorldPlugin()
Constructor.
Definition: Plugin.hh:281
gazebo::VisualPlugin::VisualPlugin
VisualPlugin()
Definition: Plugin.hh:391
gazebo::PluginT
A class which all plugins must inherit from.
Definition: Plugin.hh:75
gazebo::SensorPlugin::SensorPlugin
SensorPlugin()
Constructor.
Definition: Plugin.hh:335
gazebo::PluginType
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:55
RenderTypes.hh
gazebo::SensorPlugin
A plugin with access to physics::Sensor. See reference.
Definition: Plugin.hh:332
gazebo::VisualPlugin
A plugin with access to rendering::Visual.
Definition: Plugin.hh:389
gazebo::PluginT::~PluginT
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:87
gazebo::GUI_PLUGIN
A GUI plugin.
Definition: Plugin.hh:68
gazebo::VISUAL_PLUGIN
A Visual plugin.
Definition: Plugin.hh:66
gazebo::SystemPlugin::SystemPlugin
SystemPlugin()
Constructor.
Definition: Plugin.hh:364
gazebo::SystemPlugin
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:361
gazebo::PluginT::TPtr
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:78
gazebo::WORLD_PLUGIN
A World plugin.
Definition: Plugin.hh:58
gazebo::SystemPlugin::Reset
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:383
gazebo::SensorPlugin::Init
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:351
gazebo::PluginT::GetType
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:204
gazebo::VisualPlugin::Reset
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:409
gazebo::ModelPlugin::Load
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)=0
Load function.
gazebo::SENSOR_PLUGIN
A Sensor plugin.
Definition: Plugin.hh:62
gazebo::WorldPlugin::Init
virtual void Init()
Definition: Plugin.hh:296
SingletonT< SystemPaths >::Instance
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
gazebo::WorldPlugin::Reset
virtual void Reset()
Definition: Plugin.hh:297
gazebo::rendering::VisualPtr
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:114
gazebo::ModelPlugin::~ModelPlugin
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:310
gazebo::WorldPlugin::Load
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)=0
Load function.
gazebo::physics::WorldPtr
boost::shared_ptr< World > WorldPtr
Definition: PhysicsTypes.hh:89
gazebo::ModelPlugin
A plugin with access to physics::Model.
Definition: Plugin.hh:303
SystemPaths.hh
gazebo::SYSTEM_PLUGIN
A System plugin.
Definition: Plugin.hh:64
gazebo::SensorPlugin::Reset
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:354
gazebo::VisualPlugin::Load
virtual void Load(rendering::VisualPtr _visual, sdf::ElementPtr _sdf)=0
Load function.
gazebo::PluginT::LoadParam
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
gazebo::SystemPlugin::~SystemPlugin
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:368
gazebo::PluginT::PluginT
PluginT()
Constructor.
Definition: Plugin.hh:81
gzmsg
#define gzmsg
Output a message.
Definition: Console.hh:41
gazebo::SystemPlugin::Load
virtual void Load(int _argc=0, char **_argv=nullptr)=0
Load function.
gazebo::PluginT::handleName
std::string handleName
Short name.
Definition: Plugin.hh:261
gazebo::SensorPlugin::~SensorPlugin
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:339
gazebo::PluginT::LoadParam
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