ASL
Loading...
Searching...
No Matches
Library Class Reference

Detailed Description

This class represents a dynamically loadable library (a DLL on Windows, a shared library on Linux).

It can be used to load functions at runtime, or to create objects of classes defined in the library. To create and use objects they need to be derived from known base class (acting as an interface declaration).

Library lib("mathtools"); // will append .dll on Windows, .so on Linux or .dylib on Mac
typedef double (*Function)(double);
Function tanh = lib.get("tanh");
double y = tanh(1.57);
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:69
This class represents a dynamically loadable library (a DLL on Windows, a shared library on Linux).
Definition Library.h:106
A simple function object that can wrap a function pointer, a functor or a lambda.
Definition util.h:140

Make sure the object is alive while functions from it are used. Keep a (smart) pointer instead of a value object if needed.

Use ASL_EXPORT_CLASS to export classes from a library so that they can be easily imported in applications with library.create("ClassName").

Exporting and importing classes needs a public header:

class Animal
{
public:
virtual void speak() {}
};

One implementation built into dynamic library cat.dll:

class Cat : public Animal
{
public:
void speak() { printf("Miaow!\n"); }
};
#define ASL_EXPORT_CLASS(Class)
Export a class to be instantiated with dynamic runtime loading with Library::create("className").
Definition Library.h:211

Then another program at runtime can load the library and create an object of class Cat. It only needs the public base class header ("Animal.h"), not the Cat subclass.

Library lib("plugins/cat");
Animal* cat = lib.create("Cat");
cat->speak();

The class can be exported with an alternative name, for example to have many plugins export classes with the same name so that clients don't need knowledge about what classes are exported.

Animal* cat = lib.create("Animal");
#define ASL_EXPORT_CLASS_AS(Class, Name)
Export a class to be instantiated with dynamic runtime loading but giving a specific name for importi...
Definition Library.h:222

#include <Library.h>

Public Member Functions

 Library (const String &name)
 Creates and opens a dynamic library.
 
void open (String file, bool tryprefix=true)
 Opens a dynamic library.
 
void close ()
 Closes the library.
 
bool loaded () const
 Returns true if the library was loaded correctly.
 
voidget (const char *sym)
 Gets the address of the given symbol name in the library.
 
voidcreate (const String &className)
 Creates an object of class 'className' exported in the library with the ASL_EXPORT_CLASS macro Returns a null pointer if that class is not exported.
 

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