25 #ifndef PLUGINLOADER_H
26 #define PLUGINLOADER_H
32 #include "../Exceptions.hpp"
42 #define BOOST_FILESYSTEM_VERSION 2
43 #include <boost/filesystem/operations.hpp>
44 #include <boost/filesystem/convenience.hpp>
45 #include <boost/filesystem/fstream.hpp>
46 #include <boost/filesystem/path.hpp>
51 namespace fs = boost::filesystem;
60 map<string, SharedLoader<T> > libs;
62 map<string, T* > plugins;
64 map<string, string> paths;
72 for(
int i=0; i<dirs.size(); i++)
78 void loadDir(
string dir);
84 vector<SharedLoader<T> > & getPlugins();
85 vector<string> getPaths();
86 vector<string> getFileNames();
87 vector<string> getNames();
88 string getFilename(
string plugin_name);
90 string getPath(
string lib);
91 string getPathByFilename(
string lib);
97 T* getPlugin(
string plugin);
99 T* getPluginByFilename(
string plugin);
104 template <
typename T>
107 typedef typename std::map<string, T* >::const_iterator mit;
108 for(mit it = plugins.begin(); it != plugins.end(); ++it)
110 if(it->second != NULL)
119 template <
typename T>
123 full_path = fs::system_complete(fs::path(directory, fs::native));
127 if(!fs::exists(full_path))
129 throw FileException(
string(
"Directory does not exist: ") + full_path.native_file_string());
132 if(!fs::is_directory(full_path))
134 throw FileException(full_path.native_file_string() + string(
" is not a directory. "));
139 fs::directory_iterator end_iter;
140 for (fs::directory_iterator dir_itr( full_path );
146 string file = dir_itr->leaf();
148 if(file.length() > 3)
150 if(file.substr(file.length()-3,3) ==
".so")
152 string name = file.substr(0,file.length()-3);
153 string full_name = full_path.native_file_string() + file;
154 libs[name].LoadLibrary(full_name.c_str());
155 paths[name] = directory;
156 if(libs[name].Loaded())
158 cout <<
"Loaded plugin " << name <<
".so" << endl;
159 plugins[name] = libs[name].New();
169 catch(
const std::exception & ex)
177 template <
typename T>
180 map<string, string>::iterator it;
182 for(it=paths.begin(); it != paths.end(); ++it)
185 ret.push_back(it->second);
192 template <
typename T>
197 typedef typename std::map<string, SharedLoader<T> >::const_iterator mit;
198 for(mit it = libs.begin(); it != libs.end(); ++it)
200 ret.push_back(it->first);
207 template <
typename T>
212 typedef typename std::map<string, T* >::const_iterator mit;
213 for(mit it = plugins.begin(); it != plugins.end(); ++it)
215 ret.push_back(it->second->getName());
225 template <
typename T>
228 typedef typename std::map<string, T* >::const_iterator mit;
229 for(mit it = plugins.begin(); it != plugins.end(); ++it)
231 if(it->second->getName() == name)
241 template <
typename T>
244 if(plugins.count(name) > 0)
246 return plugins[name];
256 template <
typename T>
259 if(paths.count(name) > 0)
270 template <
typename T>
273 return getPluginByFilename(getFilename(name));
277 template <
typename T>
280 return getPathByFilename(getFilename(name));