Plugin.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #include <gazebo/gazebo_config.h>
25 #include <dlfcn.h>
26 
27 #include <list>
28 #include <string>
29 
30 #include <sdf/sdf.hh>
31 
34 #include "gazebo/common/Console.hh"
36 
40 #include "gazebo/util/system.hh"
41 
42 namespace gazebo
43 {
44  class Event;
45 
48 
52  {
65  };
66 
67 
70  template<class T>
72  {
74  public: typedef boost::shared_ptr<T> TPtr;
75 
77  public: PluginT()
78  {
79  this->dlHandle = NULL;
80  }
81 
83  public: virtual ~PluginT()
84  {
85  // dlclose has been disabled due to segfaults in the test suite
86  // This workaround is detailed in #1026 and #1066. After the test
87  // or gazebo execution the plugin is not loaded in memory anymore
88  // \todo Figure out the right thing to do.
89 
90  // dlclose(this->dlHandle);
91  }
92 
94  public: std::string GetFilename() const
95  {
96  return this->filename;
97  }
98 
100  public: std::string GetHandle() const
101  {
102  return this->handleName;
103  }
104 
110  public: static TPtr Create(const std::string &_filename,
111  const std::string &_name)
112  {
113  TPtr result;
114  // PluginPtr result;
115  struct stat st;
116  bool found = false;
117  std::string fullname, filename(_filename);
118  std::list<std::string>::iterator iter;
119  std::list<std::string> pluginPaths =
120  common::SystemPaths::Instance()->GetPluginPaths();
121 
122 #ifdef __APPLE__
123  // This is a hack to work around issue #800,
124  // error loading plugin libraries with different extensions
125  {
126  size_t soSuffix = filename.rfind(".so");
127  const std::string macSuffix(".dylib");
128  if (soSuffix != std::string::npos)
129  filename.replace(soSuffix, macSuffix.length(), macSuffix);
130  }
131 #endif // ifdef __APPLE__
132 
133  for (iter = pluginPaths.begin();
134  iter!= pluginPaths.end(); ++iter)
135  {
136  fullname = (*iter)+std::string("/")+filename;
137  if (stat(fullname.c_str(), &st) == 0)
138  {
139  found = true;
140  break;
141  }
142  }
143 
144  if (!found)
145  fullname = filename;
146 
147  fptr_union_t registerFunc;
148  std::string registerName = "RegisterPlugin";
149 
150  void *dlHandle = dlopen(fullname.c_str(), RTLD_LAZY|RTLD_GLOBAL);
151  if (!dlHandle)
152  {
153  gzerr << "Failed to load plugin " << fullname << ": "
154  << dlerror() << "\n";
155  return result;
156  }
157 
158  registerFunc.ptr = dlsym(dlHandle, registerName.c_str());
159 
160  if (!registerFunc.ptr)
161  {
162  gzerr << "Failed to resolve " << registerName
163  << ": " << dlerror();
164  return result;
165  }
166 
167  // Register the new controller.
168  result.reset(registerFunc.func());
169  result->dlHandle = dlHandle;
170 
171  result->handleName = _name;
172  result->filename = filename;
173 
174  return result;
175  }
176 
179  public: PluginType GetType() const
180  {
181  return this->type;
182  }
183 
185  protected: PluginType type;
186 
188  protected: std::string filename;
189 
191  protected: std::string handleName;
192 
194  private: typedef union
195  {
196  T *(*func)();
197  void *ptr;
198  } fptr_union_t;
199 
201  private: void *dlHandle;
202  };
203 
208  class GAZEBO_VISIBLE WorldPlugin : public PluginT<WorldPlugin>
209  {
211  public: WorldPlugin()
212  {this->type = WORLD_PLUGIN;}
213 
215  public: virtual ~WorldPlugin() {}
216 
223  public: virtual void Load(physics::WorldPtr _world,
224  sdf::ElementPtr _sdf) = 0;
225 
226  public: virtual void Init() {}
227  public: virtual void Reset() {}
228  };
229 
233  class GAZEBO_VISIBLE ModelPlugin : public PluginT<ModelPlugin>
234  {
236  public: ModelPlugin()
237  {this->type = MODEL_PLUGIN;}
238 
240  public: virtual ~ModelPlugin() {}
241 
248  public: virtual void Load(physics::ModelPtr _model,
249  sdf::ElementPtr _sdf) = 0;
250 
252  public: virtual void Init() {}
253 
255  public: virtual void Reset() {}
256  };
257 
262  class GAZEBO_VISIBLE SensorPlugin : public PluginT<SensorPlugin>
263  {
265  public: SensorPlugin()
266  {this->type = SENSOR_PLUGIN;}
267 
269  public: virtual ~SensorPlugin() {}
270 
277  public: virtual void Load(sensors::SensorPtr _sensor,
278  sdf::ElementPtr _sdf) = 0;
279 
281  public: virtual void Init() {}
282 
284  public: virtual void Reset() {}
285  };
286 
291  class GAZEBO_VISIBLE SystemPlugin : public PluginT<SystemPlugin>
292  {
294  public: SystemPlugin()
295  {this->type = SYSTEM_PLUGIN;}
296 
298  public: virtual ~SystemPlugin() {}
299 
305  public: virtual void Load(int _argc = 0, char **_argv = NULL) = 0;
306 
310  public: virtual void Init() {}
311 
313  public: virtual void Reset() {}
314  };
315 
319  class GAZEBO_VISIBLE VisualPlugin : public PluginT<VisualPlugin>
320  {
321  public: VisualPlugin()
322  {this->type = VISUAL_PLUGIN;}
323 
330  public: virtual void Load(rendering::VisualPtr _visual,
331  sdf::ElementPtr _sdf) = 0;
332 
336  public: virtual void Init() {}
337 
339  public: virtual void Reset() {}
340  };
341 
342 
344 
349 #define GZ_REGISTER_MODEL_PLUGIN(classname) \
350  extern "C" GAZEBO_VISIBLE gazebo::ModelPlugin *RegisterPlugin(); \
351  GAZEBO_VISIBLE \
352  gazebo::ModelPlugin *RegisterPlugin() \
353  {\
354  return new classname();\
355  }
356 
361 #define GZ_REGISTER_WORLD_PLUGIN(classname) \
362  extern "C" GAZEBO_VISIBLE gazebo::WorldPlugin *RegisterPlugin(); \
363  GAZEBO_VISIBLE \
364  gazebo::WorldPlugin *RegisterPlugin() \
365  {\
366  return new classname();\
367  }
368 
373 #define GZ_REGISTER_SENSOR_PLUGIN(classname) \
374  extern "C" GAZEBO_VISIBLE gazebo::SensorPlugin *RegisterPlugin(); \
375  GAZEBO_VISIBLE \
376  gazebo::SensorPlugin *RegisterPlugin() \
377  {\
378  return new classname();\
379  }
380 
385 #define GZ_REGISTER_SYSTEM_PLUGIN(classname) \
386  extern "C" GAZEBO_VISIBLE gazebo::SystemPlugin *RegisterPlugin(); \
387  GAZEBO_VISIBLE \
388  gazebo::SystemPlugin *RegisterPlugin() \
389  {\
390  return new classname();\
391  }
392 
397 #define GZ_REGISTER_VISUAL_PLUGIN(classname) \
398  extern "C" GAZEBO_VISIBLE gazebo::VisualPlugin *RegisterPlugin(); \
399  GAZEBO_VISIBLE \
400  gazebo::VisualPlugin *RegisterPlugin() \
401  {\
402  return new classname();\
403  }
404 }
405 
406 #endif
PluginType GetType() const
Returns the type of the plugin.
Definition: Plugin.hh:179
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:281
boost::shared_ptr< Model > ModelPtr
Definition: PhysicsTypes.hh:82
static SystemPaths * Instance()
Get an instance of the singleton.
Definition: SingletonT.hh:36
A Visual plugin.
Definition: Plugin.hh:62
virtual ~PluginT()
Destructor.
Definition: Plugin.hh:83
std::string GetFilename() const
Get the name of the handler.
Definition: Plugin.hh:94
ModelPlugin()
Constructor.
Definition: Plugin.hh:236
virtual void Init()
Definition: Plugin.hh:226
A plugin with access to physics::World.
Definition: Plugin.hh:208
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:284
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:319
virtual void Init()
Override this method for custom plugin initialization behavior.
Definition: Plugin.hh:252
#define gzerr
Output an error message.
Definition: Console.hh:49
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:336
default namespace for gazebo
A plugin loaded within the gzserver on startup.
Definition: Plugin.hh:291
A System plugin.
Definition: Plugin.hh:60
A World plugin.
Definition: Plugin.hh:54
VisualPlugin()
Definition: Plugin.hh:321
virtual ~WorldPlugin()
Destructor.
Definition: Plugin.hh:215
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:255
virtual ~SystemPlugin()
Destructor.
Definition: Plugin.hh:298
virtual ~ModelPlugin()
Destructor.
Definition: Plugin.hh:240
std::string filename
Path to the shared library file.
Definition: Plugin.hh:188
PluginT()
Constructor.
Definition: Plugin.hh:77
A Model plugin.
Definition: Plugin.hh:56
boost::shared_ptr< T > TPtr
plugin pointer type definition
Definition: Plugin.hh:74
A Sensor plugin.
Definition: Plugin.hh:58
boost::shared_ptr< World > WorldPtr
Definition: PhysicsTypes.hh:78
#define NULL
Definition: CommonTypes.hh:30
A class which all plugins must inherit from.
Definition: Plugin.hh:71
virtual ~SensorPlugin()
Destructor.
Definition: Plugin.hh:269
boost::shared_ptr< Visual > VisualPtr
Definition: RenderTypes.hh:102
Forward declarations and typedefs for sensors.
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:339
A GUI plugin.
Definition: Plugin.hh:64
PluginType
Used to specify the type of plugin.
Definition: Plugin.hh:51
WorldPlugin()
Constructor.
Definition: Plugin.hh:211
virtual void Init()
Initialize the plugin.
Definition: Plugin.hh:310
PluginType type
Type of plugin.
Definition: Plugin.hh:185
boost::shared_ptr< Sensor > SensorPtr
Definition: SensorTypes.hh:55
virtual void Reset()
Definition: Plugin.hh:227
A plugin with access to physics::Sensor.
Definition: Plugin.hh:262
SensorPlugin()
Constructor.
Definition: Plugin.hh:265
SystemPlugin()
Constructor.
Definition: Plugin.hh:294
std::string GetHandle() const
Get the short name of the handler.
Definition: Plugin.hh:100
A plugin with access to physics::Model.
Definition: Plugin.hh:233
std::string handleName
Short name.
Definition: Plugin.hh:191
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:48
virtual void Reset()
Override this method for custom plugin reset behavior.
Definition: Plugin.hh:313
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:110