Register Plugins
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:
#include <rttr/registration>
struct MyPluginClass
{
MyPluginClass(){}
void perform_calculation()
{
value += 12;
}
void perform_calculation(int new_value)
{
value += new_value;
}
int value = 0;
};
RTTR_PLUGIN_REGISTRATION // remark the different registration macro!
{
rttr::registration::class_<MyPluginClass>("MyPluginClass")
.constructor<>()
.property("value", &MyPluginClass::value)
.method("perform_calculation", rttr::select_overload<void(void)>(&MyPluginClass::perform_calculation))
.method("perform_calculation", rttr::select_overload<void(int)>(&MyPluginClass::perform_calculation))
;
}
The class_ is used to register classes to RTTR.
Definition registration.h:130
bind< detail::ctor, Class_Type, acc_level, Visitor_List, Args... > constructor(acc_level level=acc_level())
Register a constructor for this class type with or without arguments.
detail::enum_data< Enum_Type > value(string_view, Enum_Type value)
The value function should be used to add a mapping from enum name to value during the registration pr...
Signature * select_overload(Signature *func)
This is a helper function to register overloaded functions.
Definition registration.h:481
#define RTTR_PLUGIN_REGISTRATION
Use this macro to automatically register your reflection information inside a plugin to RTTR.
Definition registration.h:769
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:
#include <rttr/type>
int main(int argc, char** argv)
{
using namespace rttr;
// no suffix is needed, RTTR will automatically append the platform specific file suffix
if (!lib.load())
{
std::cerr << lib.get_error_string() << std::endl;
return -1;
}
// print all classes contained in the library
{
std::cout << t.get_name() << std::endl;
}
// we cannot use the actual type, to get the type information,
// thus we use string to retrieve it
// iterate over all methods of the class
{
std::cout << meth.get_signature() << std::endl;
}
// work with the new type
return 0;
}
The array_range class provides a view into an underlying data structure with lower and upper limits.
Definition array_range.h:64
The library class provides a cross platform way of explicit loading shared objects (....
Definition library.h:100
Definition access_levels.h:34
Output:
- Remarks
- When you compile your plugin with the
gcc
toolchain, make sure you use the compiler option:-fno-gnu-unique
. otherwise the unregistration will not work properly.
Summary
- 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.
- Remarks
- 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) When you want to create dynamic plugins with RTTR, make sure you link dynamically against the host application and the plugin, otherwise you will get UB. Also see
/src/examples/library_loading
for example of hot reloading.
Generated on Fri Jan 26 2024 00:00:00 for rttr - 0.9.7 by doxygen.