RTTR has build in support to register your types into shared libraries, which can be loaded and unloaded at runtime. Furthermore, it has a simple wrapper class called library which wraps the platform dependent calls to load the library.
See following example:
In order to register your types inside a plugin, you have to use the macro RTTR_PLUGIN_REGISTRATION. Then the containing code will be executed every time you load the library and it makes sure to unregister yours types, when your library will be unloaded.
Now the following code will load the plugin into your application:
- When you compile your plugin with the
gcctoolchain, make sure you use the compiler option:
-fno-gnu-unique. otherwise the unregistration will not work properly.
- Using plugins you can work with types without having access to the concrete type itself
- You don't have to explicit export (e.g. using
__declspec( dllexport )) your types in the shared library
- You can export classes, without a generic abstract interface
- You can export overloaded methods (not possible in C)
- With all this functionality, it is easily possible to implement hot-reload of shared libraries. You could serialize your object into JSON-Format, unload the library, load the new version and deserialize it again.
- Make sure you throw away all retrieved items (types, properties, methods etc...) of the loaded library when unloading. Otherwise UB may occur. (e.g. Invoking a method of an unloaded library is not possible)