forked from rttrorg/rttr
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* This adds the functionatliy to load libraries in a simple cross plattform way. The native OS calls are wrapped behind a class interface. Highlights: - handling of file suffixes (.dll or .so) - automatic unload of library on program exit - etrieving of loaded types in library - avoid of unncessary loading calls, when same plugin is loaded multiple times additional: - added unit tests - added example of loading a plugin - added a small tutorial how do develop plugins with RTTR Development notes: * fixed invalid memory access under MacOSX with clang Actually the problem was also present under linux, (with gcc or clang). This situation was following: 1. A plugin was loaded with type "int[100]" and registered 2. The plugin was unloaded => "int[100]" was removed from the type system and deleted 3. The type[100] was used in the unit_test later and was accessing the memory of the already deleted type_data The reason for this behavior lies in the usage of local static member variables (which is used to cache the retrieving and registering of type_data information). This variable was process wide unique, this lead to problems when unloading a plugin. The static was no reinitialized when unloading the plugin. So the variable was pointing to type_data information which was alreadyl deleted. Now we are using RTTR_LOCAL to hide the symbols for exporting. This fixes the issues.
- Loading branch information
Showing
61 changed files
with
2,680 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
Register Plugins {#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 \ref rttr::library "library" | ||
which wraps the platform dependent calls to load the library. | ||
|
||
See following example: | ||
~~~~{.cpp} | ||
#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)) | ||
; | ||
} | ||
~~~~ | ||
In order to register your types inside a plugin, you have to use the macro | ||
\ref 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: | ||
~~~~{.cpp} | ||
#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 | ||
library lib("MyPlugin"); | ||
if (!lib.load()) | ||
{ | ||
std::cerr << lib.get_error_string() << std::endl; | ||
return -1; | ||
} | ||
// print all classes contained in the library | ||
for (auto t : lib.get_types()) // returns all registered types from this library | ||
{ | ||
if (t.is_class() && !t.is_wrapper()) | ||
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 | ||
auto t = type::get_by_name("MyPluginClass"); | ||
// iterate over all methods of the class | ||
for (auto meth : t.get_methods()) | ||
{ | ||
std::cout << meth.get_signature() << std::endl; | ||
} | ||
// work with the new type | ||
auto var = t.create(); | ||
t.invoke("perform_calculation", var, {}); | ||
std::cout << t.get_property_value("value", var).to_int() << std::endl; // prints "12" | ||
return 0; | ||
} | ||
~~~~ | ||
Output: | ||
~~~~{.cpp} | ||
MyPluginClass | ||
perform_calculation( ) | ||
perform_calculation( int ) | ||
12 | ||
~~~~ | ||
\remark 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 \b hot-reload of shared libraries. | ||
You could serialize your object into JSON-Format, unload the library, load the new version and deserialize it again. | ||
- \remark Make sure you throw away all retrieved items (\ref rttr::type "types", \ref rttr::property "properties", \ref rttr::method "methods" etc...) of the loaded library when unloading. | ||
Otherwise UB may occur. (e.g. Invoking a method of an unloaded library is not possible) | ||
|
||
<hr> | ||
|
||
<div type="button" class="btn btn-default doxy-button">[previous](@ref register_policies_page "Register Policies")</div><div class="btn btn-default doxy-button">[finished](@ref tutorial_page "Tutorial")</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#################################################################################### | ||
# # | ||
# Copyright (c) 2014, 2015 - 2017 Axel Menzel <info@rttr.org> # | ||
# # | ||
# This file is part of RTTR (Run Time Type Reflection) # | ||
# License: MIT License # | ||
# # | ||
# Permission is hereby granted, free of charge, to any person obtaining # | ||
# a copy of this software and associated documentation files (the "Software"), # | ||
# to deal in the Software without restriction, including without limitation # | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, # | ||
# and/or sell copies of the Software, and to permit persons to whom the # | ||
# Software is furnished to do so, subject to the following conditions: # | ||
# # | ||
# The above copyright notice and this permission notice shall be included in # | ||
# all copies or substantial portions of the Software. # | ||
# # | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # | ||
# SOFTWARE. # | ||
# # | ||
#################################################################################### | ||
|
||
add_subdirectory (library_loader_example) | ||
add_subdirectory (plugin_example) |
51 changes: 51 additions & 0 deletions
51
src/examples/library_loading/library_loader_example/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#################################################################################### | ||
# # | ||
# Copyright (c) 2014, 2015 - 2017 Axel Menzel <info@rttr.org> # | ||
# # | ||
# This file is part of the examples of RTTR (Run Time Type Reflection) # | ||
# License: MIT License # | ||
# # | ||
# Permission is hereby granted, free of charge, to any person obtaining # | ||
# a copy of this software and associated documentation files (the "Software"), # | ||
# to deal in the Software without restriction, including without limitation # | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, # | ||
# and/or sell copies of the Software, and to permit persons to whom the # | ||
# Software is furnished to do so, subject to the following conditions: # | ||
# # | ||
# The above copyright notice and this permission notice shall be included in # | ||
# all copies or substantial portions of the Software. # | ||
# # | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # | ||
# SOFTWARE. # | ||
# # | ||
#################################################################################### | ||
|
||
project(library_loader_example LANGUAGES CXX) | ||
|
||
message(STATUS "Scanning " ${PROJECT_NAME} " module.") | ||
message(STATUS "===========================") | ||
|
||
generateLibraryVersionVariables(${RTTR_VERSION_MAJOR} ${RTTR_VERSION_MINOR} ${RTTR_VERSION_PATCH} | ||
"RTTR Examples: library_loading" "Copyright (c) 2014, 2015 - 2017 Axel Menzel <info@rttr.org>" "MIT License") | ||
|
||
loadFolder("files" HPP_FILES SRC_FILES) | ||
|
||
if (USE_PCH) | ||
activate_precompiled_headers("pch.h" SRC_FILES) | ||
endif() | ||
|
||
add_executable(library_loader_example ${SRC_FILES} ${HPP_FILES}) | ||
target_link_libraries(library_loader_example RTTR::Core) | ||
add_dependencies(library_loader_example plugin_example) | ||
|
||
set_target_properties(library_loader_example PROPERTIES DEBUG_POSTFIX ${RTTR_DEBUG_POSTFIX} | ||
FOLDER "Examples/library_loading") | ||
|
||
|
||
message(STATUS "Scanning " ${PROJECT_NAME} " module finished!") | ||
message(STATUS "") |
6 changes: 6 additions & 0 deletions
6
src/examples/library_loading/library_loader_example/ReadMe.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Plugin loading | ||
============== | ||
|
||
This example demonstrate the explicit loading of libraries. | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
src/examples/library_loading/library_loader_example/files.cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#################################################################################### | ||
# # | ||
# Copyright (c) 2014, 2015 - 2017 Axel Menzel <info@rttr.org> # | ||
# # | ||
# This file is part of the examples of RTTR (Run Time Type Reflection) # | ||
# License: MIT License # | ||
# # | ||
# Permission is hereby granted, free of charge, to any person obtaining # | ||
# a copy of this software and associated documentation files (the "Software"), # | ||
# to deal in the Software without restriction, including without limitation # | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, # | ||
# and/or sell copies of the Software, and to permit persons to whom the # | ||
# Software is furnished to do so, subject to the following conditions: # | ||
# # | ||
# The above copyright notice and this permission notice shall be included in # | ||
# all copies or substantial portions of the Software. # | ||
# # | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # | ||
# SOFTWARE. # | ||
# # | ||
#################################################################################### | ||
|
||
set(HEADER_FILES version.rc.in | ||
) | ||
|
||
set(SOURCE_FILES main.cpp | ||
) |
Oops, something went wrong.