Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
cet::LibraryManager Class Reference

#include <LibraryManager.h>

Classes

struct  nothrow_t
 

Public Member Functions

 LibraryManager (cet::search_path search_path, std::string lib_type)
 
 LibraryManager (cet::search_path search_path, std::string lib_type, std::string pattern)
 
 LibraryManager (std::string lib_type)
 
 LibraryManager (std::string lib_type, std::string pattern)
 
template<typename T >
getSymbolByLibspec (std::string const &libspec, std::string const &sym_name) const
 
template<typename T >
void getSymbolByLibspec (std::string const &libspec, std::string const &sym_name, T &sym) const
 
template<typename T >
getSymbolByPath (std::string const &lib_loc, std::string const &sym_name) const
 
template<typename T >
void getSymbolByPath (std::string const &lib_loc, std::string const &sym_name, T &sym) const
 
template<typename T >
getSymbolByLibspec (std::string const &libspec, std::string const &sym_name, nothrow_t) const
 
template<typename T >
void getSymbolByLibspec (std::string const &libspec, std::string const &sym_name, T &sym, nothrow_t) const
 
template<typename T >
getSymbolByPath (std::string const &lib_loc, std::string const &sym_name, nothrow_t) const
 
template<typename T >
void getSymbolByPath (std::string const &lib_loc, std::string const &sym_name, T &sym, nothrow_t) const
 
size_t getLoadableLibraries (std::vector< std::string > &list) const
 
template<class OutIter >
size_t getLoadableLibraries (OutIter dest) const
 
size_t getLoadedLibraries (std::vector< std::string > &list) const
 
template<class OutIter >
size_t getLoadedLibraries (OutIter dest) const
 
size_t getValidLibspecs (std::vector< std::string > &list) const
 
template<class OutIter >
size_t getValidLibspecs (OutIter dest) const
 
std::pair< std::string, std::stringgetSpecsByPath (std::string const &lib_loc) const
 
void loadAllLibraries () const
 
bool libraryIsLoaded (std::string const &path) const
 
bool libraryIsLoadable (std::string const &path) const
 
std::string libType () const
 
std::string patternStem () const
 

Static Public Attributes

static struct cet::LibraryManager::nothrow_t nothrow
 

Private Types

using lib_loc_map_t = std::map< std::string, std::string >
 
using spec_trans_map_t = std::map< std::string, std::set< std::string >>
 
using lib_ptr_map_t = std::map< std::string, void * >
 
using good_spec_trans_map_t = std::map< std::string, std::string >
 

Private Member Functions

void lib_loc_map_inserter (std::string const &path)
 
void spec_trans_map_inserter (lib_loc_map_t::value_type const &entry)
 
void good_spec_trans_map_inserter (spec_trans_map_t::value_type const &entry)
 
void * get_lib_ptr (std::string const &lib_loc) const
 
void * getSymbolByLibspec_ (std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
 
void * getSymbolByPath_ (std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
 

Static Private Member Functions

static std::string dllExtPattern ()
 

Private Attributes

cet::search_path const search_path_
 
std::string const lib_type_
 
std::string const pattern_stem_
 
lib_loc_map_t lib_loc_map_ {}
 
spec_trans_map_t spec_trans_map_ {}
 
good_spec_trans_map_t good_spec_trans_map_ {}
 
lib_ptr_map_t lib_ptr_map_ {}
 

Detailed Description

Definition at line 19 of file LibraryManager.h.

Member Typedef Documentation

Definition at line 142 of file LibraryManager.h.

Definition at line 139 of file LibraryManager.h.

using cet::LibraryManager::lib_ptr_map_t = std::map<std::string, void*>
private

Definition at line 141 of file LibraryManager.h.

using cet::LibraryManager::spec_trans_map_t = std::map<std::string, std::set<std::string>>
private

Definition at line 140 of file LibraryManager.h.

Constructor & Destructor Documentation

cet::LibraryManager::LibraryManager ( cet::search_path  search_path,
std::string  lib_type 
)
explicit

Definition at line 34 of file LibraryManager.cc.

36  : LibraryManager{std::move(search_path),
37  std::move(lib_type),
38  default_pattern_stem}
39 {}
LibraryManager(cet::search_path search_path, std::string lib_type)
def move(depos, offset)
Definition: depos.py:107
cet::LibraryManager::LibraryManager ( cet::search_path  search_path,
std::string  lib_type,
std::string  pattern 
)
explicit

Definition at line 41 of file LibraryManager.cc.

45  , lib_type_{std::move(lib_type)}
47 {
48  std::vector<std::string> matches;
51 
52  // Note the use of reverse iterators here: files found earlier in
53  // the vector will therefore overwrite those found later, which is
54  // what we want from "search path"-type behavior.
55  std::for_each(matches.rbegin(), matches.rend(), [this](auto const& match) {
56  this->lib_loc_map_inserter(match);
57  });
58 
59  // Build the spec to long library name translation table.
60  for (auto const& p : lib_loc_map_) {
62  }
63 
64  // Build the fast good-translation table.
65  for (auto const& p : spec_trans_map_) {
67  }
68 }
void spec_trans_map_inserter(lib_loc_map_t::value_type const &entry)
void lib_loc_map_inserter(std::string const &path)
cet::search_path const search_path_
std::string const pattern_stem_
std::string shlib_prefix()
static std::string dllExtPattern()
spec_trans_map_t spec_trans_map_
def move(depos, offset)
Definition: depos.py:107
std::size_t find_files(std::string const &filename_pattern, std::vector< std::string > &result) const
Definition: search_path.cc:126
p
Definition: test.py:223
void good_spec_trans_map_inserter(spec_trans_map_t::value_type const &entry)
search_path plugin_search_path()
std::string pattern
Definition: regex_t.cc:35
std::string const lib_type_
lib_loc_map_t lib_loc_map_
cet::LibraryManager::LibraryManager ( std::string  lib_type)
explicit

Definition at line 70 of file LibraryManager.cc.

71  : LibraryManager{std::move(lib_type), default_pattern_stem}
72 {}
LibraryManager(cet::search_path search_path, std::string lib_type)
def move(depos, offset)
Definition: depos.py:107
cet::LibraryManager::LibraryManager ( std::string  lib_type,
std::string  pattern 
)
explicit

Definition at line 74 of file LibraryManager.cc.

75  : LibraryManager{search_path{plugin_libpath(), std::nothrow},
76  std::move(lib_type),
78 {}
LibraryManager(cet::search_path search_path, std::string lib_type)
constexpr char const * plugin_libpath()
def move(depos, offset)
Definition: depos.py:107
std::string pattern
Definition: regex_t.cc:35

Member Function Documentation

std::string cet::LibraryManager::dllExtPattern ( )
inlinestaticprivate

Definition at line 172 of file LibraryManager.h.

173 {
174  static std::string const dllExtPatt{"\\" + shlib_suffix()};
175  return dllExtPatt;
176 }
std::string string
Definition: nybbler.cc:12
std::string shlib_suffix()
void * cet::LibraryManager::get_lib_ptr ( std::string const &  lib_loc) const
private

Definition at line 202 of file LibraryManager.cc.

203 {
204  lib_ptr_map_t::const_iterator const it{lib_ptr_map_.find(lib_loc)};
205  if (it == lib_ptr_map_.cend() || it->second == nullptr) {
206  dlerror();
207  void* ptr = dlopen(lib_loc.c_str(), RTLD_LAZY | RTLD_GLOBAL);
208  lib_ptr_map_[lib_loc] = ptr;
209  return ptr;
210  }
211  return it->second;
212 }
intermediate_table::const_iterator const_iterator
lib_ptr_map_t lib_ptr_map_
size_t cet::LibraryManager::getLoadableLibraries ( std::vector< std::string > &  list) const

Definition at line 81 of file LibraryManager.cc.

82 {
83  return getLoadableLibraries(std::back_inserter(list));
84 }
size_t getLoadableLibraries(std::vector< std::string > &list) const
template<class OutIter >
size_t cet::LibraryManager::getLoadableLibraries ( OutIter  dest) const

Definition at line 214 of file LibraryManager.h.

215 {
216  size_t count{};
217  for (auto const& lib_loc : lib_loc_map_) {
218  *dest++ = lib_loc.second;
219  ++count;
220  }
221  return count;
222 }
lib_loc_map_t lib_loc_map_
size_t cet::LibraryManager::getLoadedLibraries ( std::vector< std::string > &  list) const

Definition at line 87 of file LibraryManager.cc.

88 {
89  return getLoadedLibraries(std::back_inserter(list));
90 }
size_t getLoadedLibraries(std::vector< std::string > &list) const
template<class OutIter >
size_t cet::LibraryManager::getLoadedLibraries ( OutIter  dest) const

Definition at line 265 of file LibraryManager.h.

266 {
267  size_t count{};
268  for (auto const& lib_ptr : lib_ptr_map_) {
269  *dest++ = lib_ptr.first;
270  ++count;
271  }
272  return count;
273 }
lib_ptr_map_t lib_ptr_map_
std::pair< std::string, std::string > cet::LibraryManager::getSpecsByPath ( std::string const &  lib_loc) const

Definition at line 99 of file LibraryManager.cc.

100 {
101  // pair<short_spec,full_spec>
102  std::pair<std::string, std::string> result;
103  for (auto const& entry : spec_trans_map_) {
104  auto const& spec = maybe_trim_shlib_prefix(entry.first);
105  auto const& paths = entry.second;
106 
107  auto const path_iter = paths.find(lib_loc);
108  if (path_iter != paths.end()) {
109  if (spec.find("/") != std::string::npos)
110  result.second = spec;
111  else
112  result.first = spec;
113  }
114  if (!result.first.empty() && !result.second.empty())
115  break;
116  }
117 
118  return result;
119 }
QList< Entry > entry
static QCString result
int find(const type *d) const
Definition: qlist.h:88
QCollection::Item first()
Definition: qglist.cpp:807
spec_trans_map_t spec_trans_map_
template<typename T >
T cet::LibraryManager::getSymbolByLibspec ( std::string const &  libspec,
std::string const &  sym_name 
) const
inline

Definition at line 180 of file LibraryManager.h.

182 {
183  return hard_cast<T>(getSymbolByLibspec_(libspec, sym_name));
184 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByLibspec_(std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
void cet::LibraryManager::getSymbolByLibspec ( std::string const &  libspec,
std::string const &  sym_name,
T &  sym 
) const
inline

Definition at line 188 of file LibraryManager.h.

191 {
192  hard_cast<T>(getSymbolByLibspec_(libspec, sym_name), sym);
193 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByLibspec_(std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
T cet::LibraryManager::getSymbolByLibspec ( std::string const &  libspec,
std::string const &  sym_name,
nothrow_t   
) const
inline

Definition at line 227 of file LibraryManager.h.

230 {
231  return hard_cast<T>(getSymbolByLibspec_(libspec, sym_name, false));
232 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByLibspec_(std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
void cet::LibraryManager::getSymbolByLibspec ( std::string const &  libspec,
std::string const &  sym_name,
T &  sym,
nothrow_t   
) const
inline

Definition at line 236 of file LibraryManager.h.

240 {
241  hard_cast<T>(getSymbolByLibspec_(libspec, sym_name, false), sym);
242 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByLibspec_(std::string const &libspec, std::string const &sym_name, bool should_throw_on_dlsym=true) const
void * cet::LibraryManager::getSymbolByLibspec_ ( std::string const &  libspec,
std::string const &  sym_name,
bool  should_throw_on_dlsym = true 
) const
private

Definition at line 215 of file LibraryManager.cc.

218 {
219  if (libspec.find("_") != std::string::npos) {
220  // Plugin names (and hence the class name) cannot contain an underscore.
221  throw exception("LogicError", "IllegalUnderscore.")
222  << "Library specification \"" << libspec
223  << "\" contains an illegal underscore.\n"
224  << "The class name and path to it may not contain an underscore. "
225  << "If this is a configuration error, plase correct it; "
226  << "if the module's class name or its location within its "
227  << "enclosing package really do have an underscore this situation "
228  << "must be rectified.\n";
229  }
230  auto const trans = good_spec_trans_map_.find(libspec);
231  if (trans == good_spec_trans_map_.cend()) {
232  // No good translation => zero or too many
233  std::ostringstream error_msg;
234  error_msg << "Library specification \"" << libspec << "\"";
235  auto const bad_trans = spec_trans_map_.find(libspec);
236  if (bad_trans != spec_trans_map_.cend()) {
237  error_msg << " corresponds to multiple libraries:\n";
238  copy_all(bad_trans->second,
239  std::ostream_iterator<std::string>(error_msg, "\n"));
240  } else {
241  auto const& path_name = search_path_.showenv();
242  error_msg << " does not correspond to any library";
243  if (!path_name.empty()) {
244  error_msg << " in " << path_name;
245  }
246  error_msg << " of type \"" << lib_type_ << "\"\n";
247  }
248  throw exception("Configuration") << error_msg.str();
249  }
250  return getSymbolByPath_(trans->second, sym_name, should_throw_on_dlsym);
251 }
std::string const & showenv() const
Definition: search_path.h:50
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
cet::search_path const search_path_
spec_trans_map_t spec_trans_map_
good_spec_trans_map_t good_spec_trans_map_
auto copy_all(FwdCont &, FwdIter)
std::string const lib_type_
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<typename T >
T cet::LibraryManager::getSymbolByPath ( std::string const &  lib_loc,
std::string const &  sym_name 
) const
inline

Definition at line 197 of file LibraryManager.h.

199 {
200  return hard_cast<T>(getSymbolByPath_(lib_loc, sym_name));
201 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
void cet::LibraryManager::getSymbolByPath ( std::string const &  lib_loc,
std::string const &  sym_name,
T &  sym 
) const
inline

Definition at line 205 of file LibraryManager.h.

208 {
209  hard_cast<T>(getSymbolByPath_(lib_loc, sym_name), sym);
210 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
T cet::LibraryManager::getSymbolByPath ( std::string const &  lib_loc,
std::string const &  sym_name,
nothrow_t   
) const
inline

Definition at line 246 of file LibraryManager.h.

249 {
250  return hard_cast<T>(getSymbolByPath_(lib_loc, sym_name, false));
251 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
template<typename T >
void cet::LibraryManager::getSymbolByPath ( std::string const &  lib_loc,
std::string const &  sym_name,
T &  sym,
nothrow_t   
) const
inline

Definition at line 255 of file LibraryManager.h.

259 {
260  hard_cast<T>(getSymbolByPath_(lib_loc, sym_name, false), sym);
261 }
PTR_T hard_cast(void *src)
Definition: hard_cast.h:19
void * getSymbolByPath_(std::string const &lib_loc, std::string const &sym_name, bool should_throw_on_dlsym=true) const
void * cet::LibraryManager::getSymbolByPath_ ( std::string const &  lib_loc,
std::string const &  sym_name,
bool  should_throw_on_dlsym = true 
) const
private

Definition at line 254 of file LibraryManager.cc.

257 {
258  void* result = nullptr;
259  void* lib_ptr = get_lib_ptr(lib_loc);
260  if (lib_ptr == nullptr) {
261  throw exception("Configuration")
262  << "Unable to load requested library " << lib_loc << "\n"
263  << demangle_message(dlerror()) << "\n";
264  } else { // Found library
265  dlerror();
266  result = dlsym(lib_ptr, sym_name.c_str());
267  char const* error = dlerror();
268  if (error != nullptr) { // Error message
269  result = nullptr;
270  if (should_throw_on_dlsym) {
271  throw exception("Configuration")
272  << "Unable to load requested symbol " << demangle_symbol(sym_name)
273  << " from library " << lib_loc << "\n"
274  << demangle_message(error) << "\n";
275  }
276  }
277  }
278  return result;
279 }
static QCString result
error
Definition: include.cc:26
void * get_lib_ptr(std::string const &lib_loc) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
size_t cet::LibraryManager::getValidLibspecs ( std::vector< std::string > &  list) const

Definition at line 93 of file LibraryManager.cc.

94 {
95  return getValidLibspecs(std::back_inserter(list));
96 }
size_t getValidLibspecs(std::vector< std::string > &list) const
template<class OutIter >
size_t cet::LibraryManager::getValidLibspecs ( OutIter  dest) const

Definition at line 277 of file LibraryManager.h.

278 {
279  size_t count{};
280  for (auto const& spec_trans : spec_trans_map_) {
281  *dest++ = spec_trans.first;
282  ++count;
283  }
284  return count;
285 }
spec_trans_map_t spec_trans_map_
void cet::LibraryManager::good_spec_trans_map_inserter ( spec_trans_map_t::value_type const &  entry)
private

Definition at line 193 of file LibraryManager.cc.

195 {
196  if (entry.second.size() == 1) {
197  good_spec_trans_map_[entry.first] = *(entry.second.begin());
198  }
199 }
QList< Entry > entry
QCollection::Item first()
Definition: qglist.cpp:807
good_spec_trans_map_t good_spec_trans_map_
void cet::LibraryManager::lib_loc_map_inserter ( std::string const &  path)
private

Definition at line 153 of file LibraryManager.cc.

154 {
155  lib_loc_map_[boost::filesystem::path(path).filename().native()] = path;
156 }
lib_loc_map_t lib_loc_map_
bool cet::LibraryManager::libraryIsLoadable ( std::string const &  path) const

Definition at line 140 of file LibraryManager.cc.

141 {
142  // TODO: If called with any frequency, this should be made more
143  // efficient.
144  for (auto const& lib : lib_loc_map_) {
145  if (path == lib.second) {
146  return true;
147  }
148  }
149  return false;
150 }
lib_loc_map_t lib_loc_map_
bool cet::LibraryManager::libraryIsLoaded ( std::string const &  path) const

Definition at line 134 of file LibraryManager.cc.

135 {
136  return lib_ptr_map_.find(path) != lib_ptr_map_.end();
137 }
lib_ptr_map_t lib_ptr_map_
std::string cet::LibraryManager::libType ( ) const
inline

Definition at line 125 of file LibraryManager.h.

126  {
127  return lib_type_;
128  }
std::string const lib_type_
void cet::LibraryManager::loadAllLibraries ( ) const

Definition at line 122 of file LibraryManager.cc.

123 {
124  for (auto const& lib : lib_loc_map_) {
125  if (get_lib_ptr(lib.second) == nullptr) {
126  throw exception("Configuration")
127  << "Unable to load requested library " << lib.second << "\n"
128  << demangle_message(dlerror()) << "\n";
129  }
130  }
131 }
void * get_lib_ptr(std::string const &lib_loc) const
lib_loc_map_t lib_loc_map_
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::string cet::LibraryManager::patternStem ( ) const
inline

Definition at line 132 of file LibraryManager.h.

133  {
134  return pattern_stem_;
135  }
std::string const pattern_stem_
void cet::LibraryManager::spec_trans_map_inserter ( lib_loc_map_t::value_type const &  entry)
private

Definition at line 159 of file LibraryManager.cc.

161 {
162  // First obtain short spec.
163  boost::regex const e{"([^_]+)_" + lib_type_ + dllExtPattern() + '$'};
164  boost::match_results<std::string::const_iterator> match_results;
165  if (boost::regex_search(entry.first, match_results, e)) {
166  spec_trans_map_[match_results[1]].insert(entry.second);
167  } else {
168  throw exception("LogicError")
169  << "Internal error in spec_trans_map_inserter for entry " << entry.first
170  << " with pattern " << e.str();
171  }
172  // Next, convert library filename to full libspec.
173  std::ostringstream lib_name;
174  std::ostream_iterator<char, char> oi{lib_name};
175  boost::regex_replace(oi,
176  entry.first.begin(),
177  entry.first.end(),
178  boost::regex{"(_+)"},
179  std::string{"(?1/)"},
180  boost::match_default | boost::format_all);
181  boost::regex const stripper{"^lib(.*)/" + lib_type_ + "\\..*$"};
182  std::string const lib_name_str{lib_name.str()};
183  if (boost::regex_search(lib_name_str, match_results, stripper)) {
184  spec_trans_map_[match_results[1]].insert(entry.second);
185  } else {
186  throw exception("LogicError")
187  << "Internal error in spec_trans_map_inserter stripping "
188  << lib_name.str();
189  }
190 }
QList< Entry > entry
std::string string
Definition: nybbler.cc:12
QCollection::Item first()
Definition: qglist.cpp:807
static std::string dllExtPattern()
const double e
spec_trans_map_t spec_trans_map_
std::string const lib_type_
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33

Member Data Documentation

good_spec_trans_map_t cet::LibraryManager::good_spec_trans_map_ {}
private

Definition at line 166 of file LibraryManager.h.

lib_loc_map_t cet::LibraryManager::lib_loc_map_ {}
private

Definition at line 162 of file LibraryManager.h.

lib_ptr_map_t cet::LibraryManager::lib_ptr_map_ {}
mutableprivate

Definition at line 168 of file LibraryManager.h.

std::string const cet::LibraryManager::lib_type_
private

Definition at line 159 of file LibraryManager.h.

struct cet::LibraryManager::nothrow_t cet::LibraryManager::nothrow
static
std::string const cet::LibraryManager::pattern_stem_
private

Definition at line 160 of file LibraryManager.h.

cet::search_path const cet::LibraryManager::search_path_
private

Definition at line 158 of file LibraryManager.h.

spec_trans_map_t cet::LibraryManager::spec_trans_map_ {}
private

Definition at line 164 of file LibraryManager.h.


The documentation for this class was generated from the following files: