33 #ifndef GSLAM_REGISTRY_H 34 #define GSLAM_REGISTRY_H 35 #include "SharedLibrary.h" 43 typedef std::set<std::string> FilePathList;
48 static Svar load(std::string pluginName){
49 SharedLibraryPtr plugin=
get(pluginName);
52 std::cerr<<
"Unable to load plugin "<<pluginName<<std::endl;
54 for(std::string p:instance()._libraryFilePath)
59 if(!plugin->hasSymbol(
"svarInstance"))
61 std::cerr<<
"Unable to find symbol svarInstance."<<std::endl;
67 std::cerr<<
"No svarInstance found in "<<pluginName<<std::endl;
72 std::cerr<<
"svarInstance returned null.\n";
81 static std::shared_ptr<Registry> reg(
new Registry);
85 static SharedLibraryPtr
get(std::string pluginName)
87 if(pluginName.empty())
return SharedLibraryPtr();
89 pluginName=inst.getPluginName(pluginName);
91 if(inst._registedLibs[pluginName].
is<SharedLibraryPtr>())
92 return inst._registedLibs.Get<SharedLibraryPtr>(pluginName);
95 for(std::string dir:inst._libraryFilePath)
97 std::string pluginPath=dir+
"/libgslam_"+pluginName;
98 if(!fileExists(pluginPath))
continue;
102 inst._registedLibs.
set(pluginName,lib);
108 for(std::string dir:inst._libraryFilePath)
110 std::string pluginPath=dir+
"/"+pluginName;
111 if(!fileExists(pluginPath))
continue;
115 inst._registedLibs.
set(pluginName,lib);
121 for(std::string dir:inst._libraryFilePath)
123 std::string pluginPath=dir+
"/lib"+pluginName;
124 if(!fileExists(pluginPath))
continue;
128 inst._registedLibs.
set(pluginName,lib);
133 return SharedLibraryPtr();
136 static bool erase(std::string pluginName)
138 if(pluginName.empty())
return false;
140 pluginName=inst.getPluginName(pluginName);
141 inst._registedLibs.
set(pluginName,
Svar());
145 std::set<std::string>& paths(){
return _libraryFilePath;}
147 static bool fileExists(
const std::string& filename)
149 return access( filename.c_str(), 0 ) == 0;
152 static void convertStringPathIntoFilePathList(
const std::string& paths,FilePathList& filepath)
154 #if defined(WIN32) && !defined(__CYGWIN__) 155 char delimitor =
';';
156 if(paths.find(delimitor)==std::string::npos) delimitor=
':';
158 char delimitor =
':';
159 if(paths.find(delimitor)==std::string::npos) delimitor=
';';
164 std::string::size_type start = 0;
165 std::string::size_type end;
166 while ((end = paths.find_first_of(delimitor,start))!=std::string::npos)
168 filepath.insert(std::string(paths,start,end-start));
172 std::string lastPath(paths,start,std::string::npos);
173 if (!lastPath.empty())
174 filepath.insert(lastPath);
178 std::string getPluginName(std::string pluginName)
181 size_t idx=pluginName.find_last_of(
'.');
182 if(idx!=std::string::npos)
183 suffix=pluginName.substr(idx);
189 std::string folder=getFolderPath(pluginName);
190 pluginName=getFileName(pluginName);
192 _libraryFilePath.insert(folder);
199 _libraryFilePath.clear();
201 char** argv=svar.get<
char**>(
"argv",
nullptr);
204 _libraryFilePath.insert(getFolderPath(argv[0]));
206 _libraryFilePath.insert(
".");
208 FilePathList envs={
"GSLAM_LIBRARY_PATH",
"GSLAM_LD_LIBRARY_PATH"};
212 #if defined(__ia64__) || defined(__x86_64__) 213 paths.insert(
"/usr/lib/:/usr/lib64/:/usr/local/lib/:/usr/local/lib64/");
215 paths.insert(
"/usr/lib/:/usr/local/lib/");
217 envs.insert(
"LD_LIBRARY_PATH");
218 #elif defined(__CYGWIN__) 220 paths.insert(
"/usr/bin/:/usr/local/bin/");
224 for(std::string env:envs)
226 std::string ptr=svar.GetString(env,
"");
228 convertStringPathIntoFilePathList(ptr,_libraryFilePath);
230 for(std::string ptr:paths)
231 convertStringPathIntoFilePathList(ptr,_libraryFilePath);
234 inline std::string getFolderPath(
const std::string& path) {
235 auto idx = std::string::npos;
236 if ((idx = path.find_last_of(
'/')) == std::string::npos)
237 idx = path.find_last_of(
'\\');
238 if (idx != std::string::npos)
239 return path.substr(0, idx);
244 inline std::string getBaseName(
const std::string& path) {
245 std::string filename = getFileName(path);
246 auto idx = filename.find_last_of(
'.');
247 if (idx == std::string::npos)
250 return filename.substr(0, idx);
253 inline std::string getFileName(
const std::string& path) {
254 auto idx = std::string::npos;
255 if ((idx = path.find_last_of(
'/')) == std::string::npos)
256 idx = path.find_last_of(
'\\');
257 if (idx != std::string::npos)
258 return path.substr(idx + 1);
265 std::set<std::string> _libraryFilePath;
Definition: Registry.h:40
void set(const std::string &name, const T &def, bool parse_dot=false)
Set the child "name" to "create<T>(def)".
bool is() const
Is holding a type T value?
The SharedLibrary class dynamically loads shared libraries at run-time.
Definition: SharedLibrary.h:109
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages.
Definition: Svar.h:561
static std::string suffix()
Returns the path of the library, as specified in a call to load() or the constructor.
Definition: SharedLibrary.h:238