CEBL  2.1
SharedLoaderFactory.hpp
Go to the documentation of this file.
1 /*
2 * CEBL : CSU EEG Brain-Computer Interface Lab
3 *
4 * Author: Jeshua Bratman - jeshuabratman@gmail.com
5 *
6 * This file is part of CEBL.
7 *
8 * CEBL is free software; you can redistribute it and/or modify it.
9 * We only ask that if you use our code that you cite the source in
10 * your project or publication.
11 *
12 * EEG Group (www.cs.colostate.edu/eeg)
13 * Department of Computer Science
14 * Colorado State University
15 *
16 */
17 
25 #ifndef SHAREDLOADERFACTORY_H
26 #define SHAREDLAODERFACTORY_H
27 
28 #include "SharedLoader.hpp"
29 #include "../FileUtils.hpp"
30 
31 #include <map>
32 #include <string>
33 #include <sstream>
34 #include <vector>
35 using namespace std;
36 namespace fs = boost::filesystem;
37 
38 template <typename T>
40 {
41 private:
42  map<string, SharedLoader<T> > libs;
43  map<string, string > paths;
44 
45 public:
48 
49  void Scan(std::vector<string> dirs) {
50  for(int i=0; i<dirs.size(); i++)
51  {
52  Scan(dirs[i]);
53  }
54  };
55  void Scan(string);
56  map<string, SharedLoader<T> > &GetMap() { return libs; };
57  map<string, string > &GetPaths() { return paths; };
58 };
59 
60 //DESTRUCTOR
61 template <typename T>
63 {
64 
65 }
66 
67 
68 //Scans a directory for all shared libraries and loads them
69 template <typename T>
70 void SharedLoaderFactory<T>::Scan(string directory)
71 {
72  fs::path full_path;
73  full_path = fs::system_complete(fs::path(directory, fs::native));
74 
75 
76  //make sure directory exists
77  if(!fs::exists(full_path))
78  {
79  std::cout << "Directory does not exist: " << full_path.native_file_string() << std::endl;
80  return;
81  }
82  //make sure it is a directory
83  if(!fs::is_directory(full_path))
84  {
85  std::cout << full_path.native_file_string() << " is not a directory. "<< std::endl;
86  return;
87  }
88 
89 
90  //loop through directory
91  fs::directory_iterator end_iter;
92  for (fs::directory_iterator dir_itr( full_path );
93  dir_itr != end_iter;
94  ++dir_itr )
95  {
96  try
97  {
98  string file = dir_itr->leaf();
99 
100  if(file.length() > 3)
101  {
102  if(file.substr(file.length()-3,3) == ".so")
103  {
104  string name = file.substr(0,file.length()-3);
105  string full_name = full_path.native_file_string() + "/"+file;
106  libs[name].LoadLibrary(full_name.c_str());
107  paths[name] = directory;
108  if(libs[name].Loaded())
109  {
110  //cout << "Loaded shared library: " << name << ".so" << endl;
111  }
112  else
113  {
114  //remove entry
115  libs.erase(name);
116  }
117  }
118  }
119  }
120  catch ( const std::exception & ex )
121  {}
122  }
123 
124 }
125 #endif