-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.cpp
67 lines (57 loc) · 2.7 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "plugin.hpp"
#include <dlfcn.h> // dlopen, dlsym, dlclose
#include <iostream> // std::cout
/** ***************************************************************************
* en: This is a testing, simple function, overwritten in the plugin. If it works
* then you will see whatever message written by the plugin. If you just see
* MESSAGE, then it isn't working quite well.
* es: Esta es una función simple de prueba, sobreescrita en el plugin. Si funciona,
* entonces se verá el mensaje redefinido por el plugin. Si ves MESSAGE, entonces
* no está funcionando muy bien.
** ***************************************************************************/
void d::Plugin::print(void)
{
std::cout << "MESSAGE" << std::endl;
}
/** ***************************************************************************
* en: This is the factory builder. There is an static factory object called
* pluginFactory inside every plugin (working with this library), and the
* factory get() build the Plugin objects.
* es: Este es el constructor de la factoría. Hay un objeto factoría estático
* llamado pluginFactory en cada plugin que funciona con esta biblioteca, y su
* get() construye los objetos Plugin.
* @return Smart shared pointer to a maybe new plugin object.
* @see d::plugin()
** ***************************************************************************/
std::shared_ptr< d::Plugin > d::PluginFactory::get(void)
{
auto r=std::make_shared< d::Plugin >();
return r;
}
/** ***************************************************************************
* en: This function gives compatibility with C++ strings.
* es: Esta función brinda compatibilidad con strings de C++.
* @param s Path of the library to open
* @return Plugin object. A Plugin may implement a Singleton or not.
** ***************************************************************************/
std::shared_ptr< d::Plugin > d::plugin(const std::string& s)
{
return plugin ( s.c_str() );
}
/** ***************************************************************************
* en: This will open the library, load the factory, and get a Plugin object.
* es: Abre la biblioteca, carga la factoría, y obtiene un objeto Plugin.
* @param s Path of the library to open
* @return Plugin object. A Plugin may implement a Singleton or not.
** ***************************************************************************/
std::shared_ptr< d::Plugin > d::plugin(const char *s)
{
auto lib = dlopen ( s, RTLD_LAZY );
if (!lib)
throw std::runtime_error( dlerror() );
auto factory = (d::PluginFactory*) dlsym ( lib, "pluginFactory" );
if (!factory)
throw std::runtime_error( dlerror() );
auto plugin = factory->get();
return plugin;
}