fixed todo merge
[profile/ivi/automotive-message-broker.git] / ambd / pluginloader.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef PLUGINLOADER_H
20 #define PLUGINLOADER_H
21
22 #include <string>
23 #include <functional>
24 #include <ltdl.h>
25 #include <iostream>
26
27 #include "abstractsource.h"
28 #include "abstractsink.h"
29 #include "abstractroutingengine.h"
30 #include "debugout.h"
31
32
33
34 using namespace std;
35
36 typedef void* create_t(AbstractRoutingEngine*, map<string, string> );
37
38
39 class PluginLoader
40 {
41
42 public:
43         PluginLoader(string configFile, AbstractRoutingEngine* routingEngine);
44
45         SourceList sources();
46         SinkList sinks();
47
48         std::string errorString();
49         
50         
51 private: ///methods:
52         
53         template<class T>
54         T loadPlugin(string pluginName, map<string, string> config)
55         {
56                 DebugOut()<<"Loading plugin: "<<pluginName<<endl;
57                 
58                 if(lt_dlinit())
59                 {
60                         mErrorString = lt_dlerror();
61                         cerr<<"error initializing libtool: "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
62                         return nullptr;
63                 }
64                 
65                 lt_dlerror();
66                 
67                 lt_dlhandle handle = lt_dlopenext(pluginName.c_str());
68                 
69                 if(!handle)
70                 {
71                         mErrorString = lt_dlerror();
72                         cerr<<"error opening plugin: "<<pluginName<<" in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<" "<<mErrorString<<endl;
73                         return nullptr;
74                 }
75                 
76                 f_create = (create_t *)lt_dlsym(handle, "create");
77                 
78                 //mErrorString = lt_dlerror();
79                 
80                 if(f_create) 
81                 {
82                         void* obj = f_create(routingEngine, config);
83                         return static_cast<T>( obj );
84                 }
85                 
86                 return nullptr;
87         }
88         
89 private:
90         
91         std::string mPluginPath;
92         std::string mErrorString;
93         
94         AbstractRoutingEngine* routingEngine;
95         
96         SourceList mSources;
97         SinkList mSinks;
98         
99         create_t * f_create;
100 };
101
102 #endif // PLUGINLOADER_H