PIP 5.9.0
Platform-Independent Primitives
Loading...
Searching...
No Matches
PIPluginLoader Class Reference

Plugin loader. Handles dynamic library loading, symbol resolution, and plugin lifecycle management. More...

#include <piplugin.h>

Public Types

enum  Error { Unknown , NoError , NoSuchFile , InvalidUserVersion }
 Possible load plugin error. More...
typedef int(*) FunctionLoaderVersion()
 Signature of the exported loader version function.
typedef void(*) FunctionStaticMerge(int, void *, void *)
 Signature of the exported static merge function.

Public Member Functions

 PIPluginLoader (const PIString &name=PIString())
 Contruct loader with base filename "name" and call load() if "name" not empty.
 ~PIPluginLoader ()
 Destructor, unload library.
bool load (const PIString &name)
 Loads plugin by base filename name and updates error state on failure.
void unload ()
 Unload plugin and free library.
bool isLoaded () const
 Returns if plugin is successfully loaded.
Error lastError () const
 Returns error of last load() call.
PIString lastErrorText () const
 Returns error message of last load() call.
void setMessages (bool yes)
 Set if PIPluginLoader should print load messages, true by default.
bool isMessages () const
 Returns if PIPluginLoader should print load messages, true by default.
PIString libPath ()
 Returns loaded plugin library path.
void * resolve (const char *name)
 Obtain exported library method with name "symbol".
void mergeStatic ()
 Invoke plugin PIP_PLUGIN_STATIC_SECTION_MERGE Calls the plugin's static section merge function to synchronize application and plugin data.

Static Public Member Functions

static PIStringList pluginsDirectories (const PIString &name)
 Returns existing plugin directories for subdirectory name.

(Note that these are not member symbols.)

#define PIP_PLUGIN
 Declares plugin entry points and should be used before other PIP_PLUGIN_* macros.
#define PIP_PLUGIN_SET_USER_VERSION(version)
 Sets user version checked during loading, "version" is quoted string.
#define PIP_PLUGIN_ADD_STATIC_SECTION(type, ptr)
 Registers a static section pointer for future merge. type is an integer key.
#define PIP_PLUGIN_STATIC_SECTION_MERGE
 Declares a function that merges plugin and application static sections.
#define PIP_PLUGIN_EXPORT
 Marks a plugin function for export.

Detailed Description

Plugin loader. Handles dynamic library loading, symbol resolution, and plugin lifecycle management.

Synopsis

This file provides several macro to define plugin and PIPluginLoader - class to load and check plugin.

Plugin side

Plugin is a shared library that can be loaded in run-time. This is only PIP_PLUGIN macro necessary to define plugin. If you want to set and check some version, use macro PIP_PLUGIN_SET_USER_VERSION(version). Also you can define a function to merge static sections between application and plugin with macro PIP_PLUGIN_STATIC_SECTION_MERGE. Before merge, you should set pointers to that sections with macro PIP_PLUGIN_ADD_STATIC_SECTION(type, ptr).

Application side

Application should use class PIPluginLoader to load plugin. Main function is load(PIString name). "name" is base name of library, PIPluginLoader try to use sevaral names, <name>, lib<name> and "dll", "so" and "dylib" extensions, depends on system.
For example:

PIPluginLoader l;
l.load("foo");
bool load(const PIString &name)
Loads plugin by base filename name and updates error state on failure.
Definition piplugin.cpp:327

On Windows, try to open "foo", "libfoo", "foo.dll" and "libfoo.dll".
If you using user version check, you should set it with macro PIP_PLUGIN_SET_USER_VERSION(version). When plugin is successfully loaded and checked, you can load your custom symbols with function resolve(name), similar to PILibrary.

Note
You should use PIP_PLUGIN_EXPORT and C-linkage with functions you want to use with resolve(name)!

Static sections

Macro PIP_PLUGIN_STATIC_SECTION_MERGE defines function with arguments (int type, void * from, void * to), so you can leave this macro as declaration or define its body next:

switch (type) {
...
}
}
#define PIP_PLUGIN_STATIC_SECTION_MERGE
Declares a function that merges plugin and application static sections.
Definition piplugin.h:77
Note
If you using singletones, remember that cpp-defined singletones in shared libraries are single for whole application, including plugins! But if you use h-defined singletones or static linking, there are many objects in application and you should merge their content with this macro.

Anyway, if this is macro PIP_PLUGIN_STATIC_SECTION_MERGE, it called once while loading plugin with "from" - plugin side and "to" - application side, and second (optionally) on method mergeStatic() with "from" - application side and "to" - plugin side.
First direction allow you to copy all defined static content from plugin to application, and second - after loading all plugins (for example) to copy static content from application (and all other plugins) to plugin.

Examples

Simple plugin:

#include <piplugin.h>
extern "C" {
PIP_PLUGIN_EXPORT void myFunc() {
piCout << "Hello plugin!";
}
}
#define PIP_PLUGIN_EXPORT
Marks a plugin function for export.
Definition piplugin.h:83
#define PIP_PLUGIN
Declares plugin entry points and should be used before other PIP_PLUGIN_* macros.
Definition piplugin.h:43
#define piCout
Macro used for conditional (piDebug) output to PICout(StdOut).
Definition picout.h:36
Plugin control.

Application:

#include <piplugin.h>
int main() {
PIPluginLoader pl;
pl.load("your_lib");
if (pl.isLoaded()) {
typedef void(*MyFunc)();
MyFunc f = (MyFunc)pl.resolve("myFunc");
if (f) f();
}
return 0;
}
void * resolve(const char *name)
Obtain exported library method with name "symbol".
Definition piplugin.cpp:430
bool isLoaded() const
Returns if plugin is successfully loaded.
Definition piplugin.cpp:400
s v file v filter f
Restore operator.
Definition pivarianttypes.h:414

Complex plugin:

#include <piplugin.h>
PIStringList global_list;
global_list << "plugin_init";
PIStringList * sfrom = (PIStringList*)from, * sto = (PIStringList*)to;
*sto << *sfrom;
}
#define PIP_PLUGIN_SET_USER_VERSION(version)
Sets user version checked during loading, "version" is quoted string.
Definition piplugin.h:49
#define PIP_PLUGIN_ADD_STATIC_SECTION(type, ptr)
Registers a static section pointer for future merge. type is an integer key.
Definition piplugin.h:55
Based on PIDeque<PIString> strings list.
Definition pistringlist.h:36
PIStringList & removeDuplicates()
Remove duplicated strings and returns reference to this.
Definition pistringlist.cpp:75
#define STATIC_INITIALIZER_BEGIN
Macro to start static initializer.
Definition pibase_macros.h:157
#define STATIC_INITIALIZER_END
Macro to end static initializer.
Definition pibase_macros.h:161

Application:

#include <piplugin.h>
PIStringList global_list;
int main() {
global_list << "app";
PIPluginLoader pl;
pl.load("your_lib");
piCout << "list =" << global_list;
return 0;
}
void mergeStatic()
Invoke plugin PIP_PLUGIN_STATIC_SECTION_MERGE Calls the plugin's static section merge function to syn...
Definition piplugin.cpp:443

Member Enumeration Documentation

◆ Error

Possible load plugin error.

Enumerator
Unknown 

No load() call yet

NoError 

No error

NoSuchFile 

Cant find library file \~russian Не найден файл библиотеки */, LibraryLoadError /** \~english System cant load library Can`t find necessary symbols Internal version mismatch

InvalidUserVersion 

User version mismatch

Member Function Documentation

◆ load()

bool PIPluginLoader::load ( const PIString & name)

Loads plugin by base filename name and updates error state on failure.

Loader try prefix "lib" and suffix ".dll", ".so" or ".dylib", depends on platform.

◆ resolve()

void * PIPluginLoader::resolve ( const char * name)

Obtain exported library method with name "symbol".

See also
PILibrary::resolve()

◆ mergeStatic()

void PIPluginLoader::mergeStatic ( )

Invoke plugin PIP_PLUGIN_STATIC_SECTION_MERGE Calls the plugin's static section merge function to synchronize application and plugin data.

Call PIP_PLUGIN_STATIC_SECTION_MERGE function on every common type, with "from" - application scope, "to" - plugin scope

◆ PIP_PLUGIN_STATIC_SECTION_MERGE

#define PIP_PLUGIN_STATIC_SECTION_MERGE
related

Declares a function that merges plugin and application static sections.

This is functions with 3 arguments: (int type, void * from, void * to). This function invoked first while loading plugin with "from" - plugin scope, "to" - application scope, and second (optionally) on PIPluginLoader::mergeStatic() method with "from" - application scope, "to" - plugin scope. So with macro you can merge application and plugin static data.