Loading...
Searching...
No Matches
by::dlib Class Reference

Dynamic library loading and management class. More...

#include <dlib.hpp>

Public Member Functions

 dlib (const char *path)
 
 dlib (const std::string &path)
 
 dlib (const me &rhs)
 
meoperator= (const me &rhs)
 
const std::string & getPath () const
 
void setPath (const std::string &path)
 
void setPath (const std::string *it) BY_SIDE_FUNC(setPath)
 
tmay< std::string > load ()
 Loads dynamic library into memory.
 
nbool isLoaded () const
 
template<typename F >
tmayFunc< FaccessFunc (const std::string &name)
 Accesses function by name and returns typed function pointer.
 
template<typename F >
tmayFunc< FaccessFunc (const std::string *it) BY_SIDE_FUNC(accessFunc< F >)
 
template<typename F >
tmayFunc< FaccessFunc (const nchar *name)
 
void rel ()
 

Detailed Description

Dynamic library loading and management class.

Abbreviation for "dynamic loading for library". Handles platform-independent dynamic loading. Can load libraries into memory and find functions to convert them into function pointers.

Usage follows these steps:

  1. Create dlib object
  2. Specify the location of the library to load
  3. Find desired function by name and get it as function pointer
Remarks
Requires familiarity with tmay Uses tmay, so it's recommended to familiarize yourself with tmay beforehand.
RAII idiom dlib is implemented using RAII idiom. When the instance is destroyed, function pointers returned externally become unusable.

Usage

Example showing the complete process of loading a dynamic library and calling a function. Note that rel() explicitly releases resources. When an error occurs, the comma operator (rel(), false) is used to clean up the dlib object before returning:

dlib lib = dlib(path); // Performs steps 1 and 2 simultaneously
auto res = lib.load(); // `res` evaluated as true when it has an error
WHEN(res).err("couldn't open %s pod: %d", path, res.get())
.ret((rel(), false));
// Release resources with rel() first, then return false via comma operator
typedef void (*entrypointFunc)(bicontainable*);
constexpr const nchar* ENTRYPOINT_NAME = "byeol_bridge_cpp_entrypoint";
auto info = lib.accessFunc<entrypointFunc>(ENTRYPOINT_NAME); // Result as tmay
WHEN(!info.has()) // Checking result with tmay's has()
.err("couldn't access entrypoint of %s pod: %d", path, info.getErr())
.ret((rel(), false));
(*info)(&tray); // If function is successfully retrieved, it can be called
// Memory is automatically released when lib is destroyed
Dynamic library loading and management class.
Definition dlib.hpp:63
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34

Member Function Documentation

◆ accessFunc()

template<typename F >
tmayFunc< F > by::dlib::accessFunc ( const std::string & name)
inline

Accesses function by name and returns typed function pointer.

Returns
tres containing function pointer on success, or error message on failure
Note
Library must be loaded first. Uses GetProcAddress on Windows, dlsym on POSIX. Automatically calls rel() on failure.

◆ load()

tmay< std::string > by::dlib::load ( )

Loads dynamic library into memory.

Returns
Empty tmay on success, or error message on failure
Note
Uses LoadLibraryA on Windows, dlopen(RTLD_LAZY) on POSIX. Automatically calls rel() on failure.

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