<!-- HTML header for doxygen 1.8.8--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- For Mobile Devices --> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta name="generator" content="Doxygen 1.8.15"/> <script type="text/javascript" src="jquery.min.js"></script> <title>rttr: Register Plugins</title> <!--<link href="tabs.css" rel="stylesheet" type="text/css"/>--> <script type="text/javascript" src="dynsections.js"></script> <link rel = "shortcut icon" type = "image/x-icon" href = "favicon.ico"> <link rel = "stylesheet" href = "fonts/ptsans_regular_macroman/stylesheet.css"> <link rel = "stylesheet" href = "fonts/source_code_pro_regular/stylesheet.css"> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="custom-doxygen.css" rel="stylesheet" type="text/css"/> <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="custom-bootstrap.css"> <script src="bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="doxy-boot.js"></script> </head> <body> <!-- <nav class="navbar navbar-default" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand">rttr 0.9.6</a> </div> </div> </nav> --> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div class="content" id="content"> <div class="container"> <div class="row"> <div class="col-sm-12 panel panel-default" style="padding-bottom: 15px;"> <div style="margin-bottom: 15px;"> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',false,false,'search.php','Search'); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <div class="PageDoc"><div class="header"> <div class="headertitle"> <div class="title">Register Plugins </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><p>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 <a class="el" href="classrttr_1_1library.html">library</a> which wraps the platform dependent calls to load the library.</p> <p>See following example: </p><div class="fragment"><div class="line"><span class="preprocessor">#include <rttr/registration></span></div><div class="line"></div><div class="line"><span class="keyword">struct </span>MyPluginClass</div><div class="line">{</div><div class="line"> MyPluginClass(){}</div><div class="line"></div><div class="line"> <span class="keywordtype">void</span> perform_calculation()</div><div class="line"> {</div><div class="line"> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a> += 12;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keywordtype">void</span> perform_calculation(<span class="keywordtype">int</span> new_value)</div><div class="line"> {</div><div class="line"> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a> += new_value;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordtype">int</span> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a> = 0;</div><div class="line">};</div><div class="line"></div><div class="line"><a class="code" href="registration_8h.html#acd4d632edb955a7664489ae6d90aa8bd">RTTR_PLUGIN_REGISTRATION</a> <span class="comment">// remark the different registration macro!</span></div><div class="line">{</div><div class="line"> <a class="code" href="classrttr_1_1registration_1_1class__.html">rttr::registration::class_<MyPluginClass></a>(<span class="stringliteral">"MyPluginClass"</span>)</div><div class="line"> .<a class="code" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructor</a><>()</div><div class="line"> .property(<span class="stringliteral">"value"</span>, &<a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">MyPluginClass::value</a>)</div><div class="line"> .method(<span class="stringliteral">"perform_calculation"</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">rttr::select_overload</a><<span class="keywordtype">void</span>(<span class="keywordtype">void</span>)>(&MyPluginClass::perform_calculation))</div><div class="line"> .method(<span class="stringliteral">"perform_calculation"</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">rttr::select_overload</a><<span class="keywordtype">void</span>(<span class="keywordtype">int</span>)>(&MyPluginClass::perform_calculation))</div><div class="line"> ;</div><div class="line">}</div></div><!-- fragment --><p> In order to register your types inside a plugin, you have to use the macro <a class="el" href="registration_8h.html#acd4d632edb955a7664489ae6d90aa8bd">RTTR_PLUGIN_REGISTRATION</a>. 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.</p> <p>Now the following code will load the plugin into your application: </p><div class="fragment"><div class="line"><span class="preprocessor">#include <rttr/type></span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line"> <span class="keyword">using namespace </span><a class="code" href="namespacerttr.html">rttr</a>;</div><div class="line"> </div><div class="line"> <span class="comment">// no suffix is needed, RTTR will automatically append the platform specific file suffix</span></div><div class="line"> <a class="code" href="classrttr_1_1library.html">library</a> lib(<span class="stringliteral">"MyPlugin"</span>);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (!lib.load())</div><div class="line"> {</div><div class="line"> std::cerr << lib.get_error_string() << std::endl;</div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// print all classes contained in the library</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">auto</span> t : lib.get_types()) <span class="comment">// returns all registered types from this library</span></div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> (t.is_class() && !t.is_wrapper())</div><div class="line"> std::cout << t.get_name() << std::endl;</div><div class="line"> }</div><div class="line"> <span class="comment">// we cannot use the actual type, to get the type information,</span></div><div class="line"> <span class="comment">// thus we use string to retrieve it</span></div><div class="line"> <span class="keyword">auto</span> t = <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">"MyPluginClass"</span>);</div><div class="line"> </div><div class="line"> <span class="comment">// iterate over all methods of the class</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">auto</span> meth : t.get_methods())</div><div class="line"> {</div><div class="line"> std::cout << meth.get_signature() << std::endl;</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">// work with the new type</span></div><div class="line"> <span class="keyword">auto</span> var = t.create();</div><div class="line"> t.invoke(<span class="stringliteral">"perform_calculation"</span>, var, {});</div><div class="line"> std::cout << t.get_property_value(<span class="stringliteral">"value"</span>, var).to_int() << std::endl; <span class="comment">// prints "12"</span></div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><p> Output: </p><div class="fragment"><div class="line">MyPluginClass</div><div class="line">perform_calculation( )</div><div class="line">perform_calculation( <span class="keywordtype">int</span> )</div><div class="line">12</div></div><!-- fragment --> <dl class="section remark"><dt>Remarks</dt><dd>When you compile your plugin with the <code>gcc</code> toolchain, make sure you use the compiler option: <code>-fno-gnu-unique</code>. otherwise the unregistration will not work properly.</dd></dl> <h2>Summary </h2> <ul> <li>Using plugins you can work with types without having access to the concrete type itself</li> <li>You don't have to explicit export (e.g. using <code>__declspec( dllexport )</code>) your types in the shared library</li> <li>You can export classes, without a generic abstract interface</li> <li>You can export overloaded methods (not possible in C)</li> <li>With all this functionality, it is easily possible to implement <b>hot-reload</b> of shared libraries. You could serialize your object into JSON-Format, unload the library, load the new version and deserialize it again.</li> <li><dl class="section remark"><dt>Remarks</dt><dd>Make sure you throw away all retrieved items (<a class="el" href="classrttr_1_1type.html">types</a>, <a class="el" href="classrttr_1_1property.html">properties</a>, <a class="el" href="classrttr_1_1method.html">methods</a> etc...) of the loaded library when unloading. Otherwise UB may occur. (e.g. Invoking a method of an unloaded library is not possible)</dd></dl> <hr/> </li> </ul> <div type="button" class="btn btn-default doxy-button"><a class="el" href="register_policies_page.html">previous</a></div><div class="btn btn-default doxy-button"><a class="el" href="tutorial_page.html">finished</a></div> </div></div><!-- PageDoc --> </div><!-- contents --> <!-- HTML footer for doxygen 1.8.9.1--> <!-- start footer part --> <hr class="footer"/> <address class="footer"> <small> Generated on Thu Apr 11 2019 20:05:57 for rttr - 0.9.6 by <a href="http://www.doxygen.org/index.html">doxygen</a>. </small> </address> </body> </html>