LibraryManager.h
Go to the documentation of this file.
1 #ifndef cetlib_LibraryManager_h
2 #define cetlib_LibraryManager_h
3 
4 #include "cetlib/hard_cast.h"
5 #include "cetlib/search_path.h"
6 #include "cetlib/shlib_utils.h"
7 
8 #include <cstring>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13 
14 namespace cet {
15  class LibraryManager;
16 }
17 
19 public:
20  // Create a LibraryManager that searches through a search path
21  // specified by search_path for dynamically loadable libraries
22  // having the given lib_type. Library names are expected to be of
23  // the form:
24  // libaa_bb_cc_xyz_<lib_type>.<ext>
25  // and where <ext> is provided automatically as appropriate for the
26  // platform.
28 
29  explicit LibraryManager(cet::search_path search_path,
30  std::string lib_type,
32 
33  // Use platform-dependent dynamic loader search path
34  explicit LibraryManager(std::string lib_type);
35  explicit LibraryManager(std::string lib_type, std::string pattern);
36 
37  // The d'tor does NOT unload libraries, because that is dangerous to
38  // do in C++. Use the compiler-generated destructor.
39  // ~LibraryManager();
40 
41  // Find and return a symbol named 'sym_name' in the library
42  // identified by 'libspec'. The library is dynamically loaded if
43  // necessary. If more than one library matching libspec is found, an
44  // exception is thrown. If the correct library cannot be found or
45  // loaded, an exception is thrown. If the symbol specified cannot be
46  // loaded, an exception is thrown unless the final argument to the
47  // function call is LibraryManager::nothrow, in which case the
48  // desired function pointer will be NULL.
49 
50  template <typename T>
51  T getSymbolByLibspec(std::string const& libspec,
52  std::string const& sym_name) const;
53  template <typename T>
54  void getSymbolByLibspec(std::string const& libspec,
55  std::string const& sym_name,
56  T& sym) const;
57 
58  template <typename T>
59  T getSymbolByPath(std::string const& lib_loc,
60  std::string const& sym_name) const;
61 
62  template <typename T>
63  void getSymbolByPath(std::string const& lib_loc,
64  std::string const& sym_name,
65  T& sym) const;
66 
67  // Versions which won't throw if they can't find the symbol.
68  static struct nothrow_t {
69  } nothrow;
70 
71  template <typename T>
72  T getSymbolByLibspec(std::string const& libspec,
73  std::string const& sym_name,
74  nothrow_t) const;
75  template <typename T>
76  void getSymbolByLibspec(std::string const& libspec,
77  std::string const& sym_name,
78  T& sym,
79  nothrow_t) const;
80  template <typename T>
81  T getSymbolByPath(std::string const& lib_loc,
82  std::string const& sym_name,
83  nothrow_t) const;
84  template <typename T>
85  void getSymbolByPath(std::string const& lib_loc,
86  std::string const& sym_name,
87  T& sym,
88  nothrow_t) const;
89 
90  // Get a list of loadable libraries (full paths). Returns the number
91  // of entries.
92  size_t getLoadableLibraries(std::vector<std::string>& list) const;
93  template <class OutIter>
94  size_t getLoadableLibraries(OutIter dest) const;
95 
96  // Get a list of already-loaded libraries (full paths). Returns the
97  // number of entries.
98  size_t getLoadedLibraries(std::vector<std::string>& list) const;
99  template <class OutIter>
100  size_t getLoadedLibraries(OutIter dest) const;
101 
102  // Get list of valid libspecs. Returns the number of entries.
103  size_t getValidLibspecs(std::vector<std::string>& list) const;
104  template <class OutIter>
105  size_t getValidLibspecs(OutIter dest) const;
106 
107  // Get pair of short and full libspecs corresponding to library full
108  // path.
109  std::pair<std::string, std::string> getSpecsByPath(
110  std::string const& lib_loc) const;
111 
112  // Load all libraries at once.
113  void loadAllLibraries() const;
114 
115  // Check whether libraries are loaded.
116  bool libraryIsLoaded(std::string const& path) const;
117  // Check whether library is loadable. Note this is *not* efficient
118  // and is only intended for diagnostic purposes. If it needs to be
119  // efficient the implementation will need to be improved.
120  bool libraryIsLoadable(std::string const& path) const;
121 
122  // This manager's library type.
124  libType() const
125  {
126  return lib_type_;
127  }
128 
129  // This managers library search pattern.
131  patternStem() const
132  {
133  return pattern_stem_;
134  }
135 
136 private:
137  // Internally-useful typedefs.
138  using lib_loc_map_t = std::map<std::string, std::string>;
139  using spec_trans_map_t = std::map<std::string, std::set<std::string>>;
140  using lib_ptr_map_t = std::map<std::string, void*>;
141  using good_spec_trans_map_t = std::map<std::string, std::string>;
142 
143  // Private helper functions.
144  static std::string dllExtPattern();
145 
146  void lib_loc_map_inserter(std::string const& path);
147  void spec_trans_map_inserter(lib_loc_map_t::value_type const& entry);
148  void good_spec_trans_map_inserter(spec_trans_map_t::value_type const& entry);
149  void* get_lib_ptr(std::string const& lib_loc) const;
150  void* getSymbolByLibspec_(std::string const& libspec,
151  std::string const& sym_name,
152  bool should_throw_on_dlsym = true) const;
153  void* getSymbolByPath_(std::string const& lib_loc,
154  std::string const& sym_name,
155  bool should_throw_on_dlsym = true) const;
156 
158  std::string const lib_type_; // eg _plugin.
159  std::string const pattern_stem_; // Library search pattern stem.
160  // Map of library filename -> full path.
162  // Map of spec -> full path.
164  // Map of only good translations.
166  // Cache of already-loaded libraries.
168 };
169 
170 inline std::string
172 {
173  static std::string const dllExtPatt{"\\" + shlib_suffix()};
174  return dllExtPatt;
175 }
176 
177 template <typename T>
178 inline T
180  std::string const& sym_name) const
181 {
182  return hard_cast<T>(getSymbolByLibspec_(libspec, sym_name));
183 }
184 
185 template <typename T>
186 inline void
188  std::string const& sym_name,
189  T& sym) const
190 {
191  hard_cast<T>(getSymbolByLibspec_(libspec, sym_name), sym);
192 }
193 
194 template <typename T>
195 inline T
197  std::string const& sym_name) const
198 {
199  return hard_cast<T>(getSymbolByPath_(lib_loc, sym_name));
200 }
201 
202 template <typename T>
203 inline void
205  std::string const& sym_name,
206  T& sym) const
207 {
208  hard_cast<T>(getSymbolByPath_(lib_loc, sym_name), sym);
209 }
210 
211 template <class OutIter>
212 size_t
214 {
215  size_t count{};
216  for (auto const& lib_loc : lib_loc_map_) {
217  *dest++ = lib_loc.second;
218  ++count;
219  }
220  return count;
221 }
222 
223 // Versions which won't throw on failure to obtain symbol.
224 template <typename T>
225 inline T
227  std::string const& sym_name,
228  nothrow_t) const
229 {
230  return hard_cast<T>(getSymbolByLibspec_(libspec, sym_name, false));
231 }
232 
233 template <typename T>
234 inline void
236  std::string const& sym_name,
237  T& sym,
238  nothrow_t) const
239 {
240  hard_cast<T>(getSymbolByLibspec_(libspec, sym_name, false), sym);
241 }
242 
243 template <typename T>
244 inline T
246  std::string const& sym_name,
247  nothrow_t) const
248 {
249  return hard_cast<T>(getSymbolByPath_(lib_loc, sym_name, false));
250 }
251 
252 template <typename T>
253 inline void
255  std::string const& sym_name,
256  T& sym,
257  nothrow_t) const
258 {
259  hard_cast<T>(getSymbolByPath_(lib_loc, sym_name, false), sym);
260 }
261 
262 template <class OutIter>
263 size_t
265 {
266  size_t count{};
267  for (auto const& lib_ptr : lib_ptr_map_) {
268  *dest++ = lib_ptr.first;
269  ++count;
270  }
271  return count;
272 }
273 
274 template <class OutIter>
275 size_t
277 {
278  size_t count{};
279  for (auto const& spec_trans : spec_trans_map_) {
280  *dest++ = spec_trans.first;
281  ++count;
282  }
283  return count;
284 }
285 
286 #endif /* cetlib_LibraryManager_h */
287 
288 // Local Variables:
289 // mode: c++
290 // End:
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
std::pair< std::string, std::string > getSpecsByPath(std::string const &lib_loc) const
std::map< std::string, std::string > lib_loc_map_t
T getSymbolByPath(std::string const &lib_loc, std::string const &sym_name) const
void spec_trans_map_inserter(lib_loc_map_t::value_type const &entry)
size_t getLoadedLibraries(std::vector< std::string > &list) const
size_t getValidLibspecs(std::vector< std::string > &list) const
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
bool libraryIsLoadable(std::string const &path) const
void lib_loc_map_inserter(std::string const &path)
LibraryManager(cet::search_path search_path, std::string lib_type)
cet::search_path const search_path_
std::string const pattern_stem_
std::map< std::string, void * > lib_ptr_map_t
std::string shlib_suffix()
static std::string dllExtPattern()
T getSymbolByLibspec(std::string const &libspec, std::string const &sym_name) const
std::string patternStem() const
void * getSymbolByLibspec_(std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
std::map< std::string, std::string > good_spec_trans_map_t
spec_trans_map_t spec_trans_map_
good_spec_trans_map_t good_spec_trans_map_
size_t getLoadableLibraries(std::vector< std::string > &list) const
static struct cet::LibraryManager::nothrow_t nothrow
void * get_lib_ptr(std::string const &lib_loc) const
void loadAllLibraries() const
void good_spec_trans_map_inserter(spec_trans_map_t::value_type const &entry)
bool libraryIsLoaded(std::string const &path) const
const std::string path
Definition: plot_BEN.C:43
lib_ptr_map_t lib_ptr_map_
std::map< std::string, std::set< std::string >> spec_trans_map_t
double T
Definition: Xdiff_gwt.C:5
std::string const lib_type_
lib_loc_map_t lib_loc_map_
std::string libType() const
def entry(str)
Definition: HTMLTools.py:26
enum BeamMode string