Plugin.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifndef _GZ_PLUGIN_HH_
18 #define _GZ_PLUGIN_HH_
19 
20 #ifdef _WIN32
21  // Ensure that Winsock2.h is included before Windows.h, which can get
22  // pulled in by anybody (e.g., Boost).
23  // This was put here because all the plugins are going to use it
24  // This doesn't guarantee something else won't cause it,
25  // but this saves putting this in every plugin
26 #include <Winsock2.h>
27 #endif
28 
29 #ifndef _WIN32
30  #include <unistd.h>
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 
36 #include <gazebo/gazebo_config.h>
37 #include <dlfcn.h>
38 
39 #include <list>
40 #include <string>
41 
42 #include <sdf/sdf.hh>
43 
46 #include "gazebo/common/Console.hh"
48 
52 #include "gazebo/util/system.hh"
53 
54 namespace gazebo
55 {
56  class Event;
57 
60 
64  {
77  };
78 
79 
82  template<class T>
83  class PluginT
84  {
86  public: typedef boost::shared_ptr<T> TPtr;
87 
89  public: PluginT()
90  {
91  this->dlHandle = NULL;
92  }
93 
95  public: virtual ~PluginT()
96  {
97  // dlclose has been disabled due to segfaults in the test suite
98  // This workaround is detailed in #1026 and #1066. After the test
99  // or gazebo execution the plugin is not loaded in memory anymore
100  // \todo Figure out the right thing to do.
101 
102  // dlclose(this->dlHandle);
103  }
104 
106  public: std::string GetFilename() const
107  {
108  return this->filename;
109  }
110 
112  public: std::string GetHandle() const
113  {
114  return this->handleName;
115  }
116 
122  public: static TPtr Create(const std::string &_filename,
123  const std::string &_name)
124  {
125  TPtr result;
126  // PluginPtr result;
127  struct stat st;
128  bool found = false;
129  std::string fullname, filename(_filename);
130  std::list<std::string>::iterator iter;
131  std::list<std::string> pluginPaths =
132  common::SystemPaths::Instance()->GetPluginPaths();
133 
134 #ifdef __APPLE__
135  // This is a hack to work around issue #800,
136  // error loading plugin libraries with different extensions
137  {
138  size_t soSuffix = filename.rfind(".so");
139  const std::string macSuffix(".dylib");
140  if (soSuffix != std::string::npos)
141  filename.replace(soSuffix, macSuffix.length(), macSuffix);
142  }
143 #endif // ifdef __APPLE__
144 
145  for (iter = pluginPaths.begin();
146  iter!= pluginPaths.end(); ++iter)
147  {
148  fullname = (*iter)+std::string("/")+filename;
149  if (stat(fullname.c_str(), &st) == 0)
150  {
151  found = true;
152  break;
153  }
154  }
155 
156  if (!found)
157  fullname = filename;
158 
159  fptr_union_t registerFunc;
160  std::string registerName = "RegisterPlugin";
161 
162  void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
163  if (!dlHandle)
164  {
165  gzerr << "Failed to load plugin " << fullname << ": "
166  << dlerror() << "\n";
167  return result;
168  }
169 
170  registerFunc.ptr = dlsym(dlHandle, registerName.c_str());
171 
172  if (!registerFunc.ptr)
173  {
174  gzerr << "Failed to resolve " << registerName
175  << ": " << dlerror();
176  return result;
177  }
178 
179  // Register the new controller.
180  result.reset(registerFunc.func());
181  result->dlHandle = dlHandle;
182 
183  result->handleName = _name;
184  result->filename = filename;
185 
186  return result;
187  }
188 
191  public: PluginType GetType() const
192  {
193  return this->type;
194  }
195 
197  protected: PluginType type;
198 
200  protected: std::string filename;
201 
203  protected: std::string handleName;
204 
206  private: typedef union
207  {
208  T *(*func)();
209  void *ptr;
210  } fptr_union_t;
211 
213  private: void *dlHandle;
214  };
215 
220  class WorldPlugin : public PluginT<WorldPlugin>
221  {
223  public: WorldPlugin()
224  {this->type = WORLD_PLUGIN;}
225 
227  public: virtual ~WorldPlugin() {}
228 
235  public: virtual void Load(physics::WorldPtr _world,
236  sdf::ElementPtr _sdf) = 0;
237 
238  public: virtual void Init() {}
239  public: virtual void Reset() {}
240  };
241 
245  class ModelPlugin : public PluginT<ModelPlugin>
246  {
248  public: ModelPlugin()
249  {this->type = MODEL_PLUGIN;}
250 
252  public: virtual ~ModelPlugin() {}
253 
260  public: virtual void Load(physics::ModelPtr _model,
261  sdf::ElementPtr _sdf) = 0;
262 
264  public: virtual void Init() {}
265 
267  public: virtual void Reset() {}
268  };
269 
274  class SensorPlugin : public PluginT<SensorPlugin>
275  {
277  public: SensorPlugin()
278  {this->type = SENSOR_PLUGIN;}
279 
281  public: virtual ~SensorPlugin() {}
282 
289  public: virtual void Load(sensors::SensorPtr _sensor,
290  sdf::ElementPtr _sdf) = 0;
291 
293  public: virtual void Init() {}
294 
296  public: virtual void Reset() {}
297  };
298 
303  class SystemPlugin : public PluginT<SystemPlugin>
304  {
306  public: SystemPlugin()
307  {this->type = SYSTEM_PLUGIN;}
308 
310  public: virtual ~SystemPlugin() {}
311 
317  public: virtual void Load(int _argc = 0, char **_argv = NULL) = 0;
318 
322  public: virtual void Init() {}
323 
325  public: virtual void Reset() {}
326  };
327 
331  class VisualPlugin : public PluginT<VisualPlugin>
332  {
333  public: VisualPlugin()
334  {this->type = VISUAL_PLUGIN;}
335 
342  public: virtual void Load(rendering::VisualPtr _visual,
343  sdf::ElementPtr _sdf) = 0;
344 
348  public: virtual void Init() {}
349 
351  public: virtual void Reset() {}
352  };
353 
354 
356 
361 #define GZ_REGISTER_MODEL_PLUGIN(classname) \
362  extern "C" GZ_COMMON_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
363  gazebo::ModelPlugin *RegisterPlugin() \
364  {\
365  return new classname();\
366  }
367 
372 #define GZ_REGISTER_WORLD_PLUGIN(classname) \
373  extern "C" GZ_COMMON_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
374  gazebo::WorldPlugin *RegisterPlugin() \
375  {\
376  return new classname();\
377  }
378 
383 #define GZ_REGISTER_SENSOR_PLUGIN(classname) \
384  extern "C" GZ_COMMON_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
385  gazebo::SensorPlugin *RegisterPlugin() \
386  {\
387  return new classname();\
388  }
389 
394 #define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
395  extern "C" GZ_COMMON_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
396  GZ_COMMON_VISIBLE \
397  gazebo::SystemPlugin *RegisterPlugin() \
398  {\
399  return new classname();\
400  }
401 
406 #define GZ_REGISTER_VISUAL_PLUGIN(classname) \
407  extern "C" GZ_COMMON_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
408  gazebo::VisualPlugin *RegisterPlugin() \
409  {\
410  return new classname();\
411  }
412 }
413 
414 #endif
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:191
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:293
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:88
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
A Visual plugin.
Definition: Plugin.hh:74
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:95
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:106
ModelPlugin()
Constructor.
Definition: Plugin.hh:248
virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf)=0
Load function.
virtual void Init()
Definition: Plugin.hh:238
A plugin with access to physics::World.
Definition: Plugin.hh:220
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:296
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:331
std::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:112
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:264
#define gzerr
Output an error message.
Definition: Console.hh:49
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:348
default namespace for gazebo
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:303
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)=0
Load function.
A System plugin.
Definition: Plugin.hh:72
A World plugin.
Definition: Plugin.hh:66
VisualPlugin()
Definition: Plugin.hh:333
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:227
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:267
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:310
virtual void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)=0
Load function.
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:252
std::string filename
Path to the shared library file.
Definition: Plugin.hh:200
PluginT()
Constructor.
Definition: Plugin.hh:89
A Model plugin.
Definition: Plugin.hh:68
virtual void Load(rendering::VisualPtr _visual, sdf::ElementPtr _sdf)=0
Load function.
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:86
A Sensor plugin.
Definition: Plugin.hh:70
virtual void Load(int _argc=0, char **_argv=NULL)=0
Load function.
boost::shared_ptr< World > WorldPtr
Definition: PhysicsTypes.hh:84
#define NULL
Definition: CommonTypes.hh:31
A class which all plugins must inherit from.
Definition: Plugin.hh:83
std::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:63
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:281
Forward declarations and typedefs for sensors.
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:351
A GUI plugin.
Definition: Plugin.hh:76
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:63
WorldPlugin()
Constructor.
Definition: Plugin.hh:223
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:322
PluginType type
Type of plugin.
Definition: Plugin.hh:197
virtual void Reset()
Definition: Plugin.hh:239
A plugin with access to physics::Sensor.
Definition: Plugin.hh:274
SensorPlugin()
Constructor.
Definition: Plugin.hh:277
SystemPlugin()
Constructor.
Definition: Plugin.hh:306
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:112
A plugin with access to physics::Model.
Definition: Plugin.hh:245
std::string handleName
Short name.
Definition: Plugin.hh:203
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:325
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:122