https://mooseframework.inl.gov
Public Member Functions | Private Attributes | List of all members
DynamicLibraryLoader Class Reference

Wrapper class to facilitate loading and lifetime management of dynamic libraries and obtaining pointers to exported functions. More...

#include <DynamicLibraryLoader.h>

Public Member Functions

 DynamicLibraryLoader (const std::string &library_file)
 
 ~DynamicLibraryLoader ()
 
template<typename T >
getFunction (std::string func, bool hard_fail=true)
 Get a function/data pointer of type T to a function exported from the loaded library. More...
 

Private Attributes

void_handle
 Library handle returned by dlopen. More...
 
const std::string _library_file
 Library file name. More...
 

Detailed Description

Wrapper class to facilitate loading and lifetime management of dynamic libraries and obtaining pointers to exported functions.

Definition at line 24 of file DynamicLibraryLoader.h.

Constructor & Destructor Documentation

◆ DynamicLibraryLoader()

DynamicLibraryLoader::DynamicLibraryLoader ( const std::string &  library_file)

Definition at line 13 of file DynamicLibraryLoader.C.

14  : _library_file(library_file)
15 {
16  MooseUtils::checkFileReadable(_library_file, false, /*throw_on_unreadable=*/true);
17 #ifdef LIBMESH_HAVE_DLOPEN
18  _handle = dlopen(_library_file.c_str(), RTLD_LAZY);
19  if (!_handle)
20  mooseError("Failed to load library '", _library_file, "' in DynamicLibraryLoader: ", dlerror());
21 
22  dlerror();
23 #else
24  // support for the POSIX function 'dlopen' to dynamically load libraries is missing
25  mooseError("Dynamic library loading is not supported on this operating system.");
26 #endif
27 }
void mooseError(Args &&... args)
void * _handle
Library handle returned by dlopen.
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true, bool check_for_git_lfs_pointer=true)
const std::string _library_file
Library file name.

◆ ~DynamicLibraryLoader()

DynamicLibraryLoader::~DynamicLibraryLoader ( )

Definition at line 29 of file DynamicLibraryLoader.C.

30 {
31 #ifdef LIBMESH_HAVE_DLOPEN
32  dlclose(_handle);
33 #endif
34 }
void * _handle
Library handle returned by dlopen.

Member Function Documentation

◆ getFunction()

template<typename T >
T DynamicLibraryLoader::getFunction ( std::string  func,
bool  hard_fail = true 
)

Get a function/data pointer of type T to a function exported from the loaded library.

Parameters
hard_failSet this to false to return a nullptr if a symbol was not found.

Definition at line 47 of file DynamicLibraryLoader.h.

48 {
49 #ifdef LIBMESH_HAVE_DLOPEN
50  // clear error
51  dlerror();
52 
53  // Snag the function pointers from the library
54  void * pointer = dlsym(_handle, func.c_str());
55 
56  // Catch errors
57  const char * dlsym_error = dlerror();
58  if (dlsym_error && hard_fail)
59  {
60  dlclose(_handle);
61  mooseError("DynamicLibraryLoader error: Unable to find symbol '",
62  func,
63  "' in library '",
65  "'. ",
66  std::string(dlsym_error));
67  }
68 
69  return *reinterpret_cast<T *>(&pointer);
70 #else
71  // this can never be reached as the object instantiation would have already failed
72  return nullptr;
73 #endif
74 }
void mooseError(Args &&... args)
void * _handle
Library handle returned by dlopen.
const std::string _library_file
Library file name.

Member Data Documentation

◆ _handle

void* DynamicLibraryLoader::_handle
private

Library handle returned by dlopen.

Definition at line 39 of file DynamicLibraryLoader.h.

Referenced by DynamicLibraryLoader(), getFunction(), and ~DynamicLibraryLoader().

◆ _library_file

const std::string DynamicLibraryLoader::_library_file
private

Library file name.

Definition at line 42 of file DynamicLibraryLoader.h.

Referenced by DynamicLibraryLoader(), and getFunction().


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