Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > d8d30ad953f2dbe7e871721325480315 > files > 355

librttr-devel-0.9.6-1.mga7.armv7hl.rpm

<!-- 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: Classes</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&amp;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">Classes </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>For registering classes in RTTR you use a class called <a class="el" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;T&gt;</a>. Its name is supposed to resemble the C++ keyword, to make it look more intuitive. It has member functions for register <a class="el" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructors</a>, <a class="el" href="classrttr_1_1registration_1_1class__.html#a005a60417fb6d183af475cb4059dc0a0">properties</a>, <a class="el" href="classrttr_1_1registration_1_1class__.html#a3997256452d390b54990f202cf768bc7">methods</a> and <a class="el" href="classrttr_1_1registration_1_1class__.html#ad4f37155a55668afd8fcb66441f3ef88">enums</a>. These functions have the same interface and work in the same way like register the global symbols. Every call to these member functions, will return <a class="el" href="classrttr_1_1registration_1_1bind.html">bind</a> object, in order to chain more registration calls.</p>
<p>Let's start with a simple example. Consider the following C++ class:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;rttr/type&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// test class.h</span></div><div class="line"><span class="keyword">struct </span>test_class</div><div class="line">{</div><div class="line">    test_class(<span class="keywordtype">int</span> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a>) : m_value(<a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a>) {}</div><div class="line">    <span class="keywordtype">void</span> print_value()<span class="keyword"> const </span>{ std::cout &lt;&lt; m_value; }</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> m_value;</div><div class="line"></div><div class="line">    <a class="code" href="rttr__enable_8h.html#a848bcae21d3a54e07ca6450689820a59">RTTR_ENABLE</a>()</div><div class="line">};</div></div><!-- fragment --><p>The registration process is now done at global scope in the cpp file.</p>
<div class="fragment"><div class="line"><span class="comment">// test_class.cpp</span></div><div class="line"><span class="preprocessor">#include &lt;rttr/registration&gt;</span></div><div class="line"></div><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;test_class&gt;</a>(<span class="stringliteral">&quot;test_class&quot;</span>)</div><div class="line">                 .<a class="code" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructor</a>&lt;<span class="keywordtype">int</span>&gt;()</div><div class="line">                 .<a class="code" href="classrttr_1_1method.html">method</a>(<span class="stringliteral">&quot;print_value&quot;</span>, &amp;test_class::print_value)</div><div class="line">                 .property(<span class="stringliteral">&quot;value&quot;</span>, &amp;test_class::m_value);</div><div class="line">}</div></div><!-- fragment --><p>This will register the class <code>test_class</code> with a constructor that takes an integer as argument, a member function with the name <code>print_value</code> and a property called <code>value</code>.</p>
<p>The property name has to be unique for this class type, but derived classes can register another property with the same name again. Member functions can be overloaded, so a method can be registered with an already existing name multiple times. However when there exist a method with the same name and signature, then this function will not be registered and discarded.</p>
<h2>Overloaded member functions </h2>
<p>In order to register a overloaded member function, you have to disambiguate the member function pointer you pass to <a class="el" href="classrttr_1_1registration_1_1class__.html#a3997256452d390b54990f202cf768bc7">method</a>. Therefore a helper function can be used: <a class="el" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload&lt;T&gt;(T (ClassType::*func))</a></p>
<p>Here's an example illustrating this:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Foo</div><div class="line">{</div><div class="line">    <span class="keywordtype">void</span> f() {}</div><div class="line">    <span class="keywordtype">void</span> f(<span class="keywordtype">int</span>) {}</div><div class="line">    <span class="keywordtype">void</span> f(<span class="keywordtype">int</span>)<span class="keyword"> const </span>{}</div><div class="line">};</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>(<span class="stringliteral">&quot;Foo&quot;</span>)</div><div class="line">                 .<a class="code" href="classrttr_1_1registration_1_1class__.html#a3997256452d390b54990f202cf768bc7">method</a>(<span class="stringliteral">&quot;f&quot;</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload</a>&lt;<span class="keywordtype">void</span>(<span class="keywordtype">void</span>)&gt;(&amp;Foo::f))</div><div class="line">                 .method(<span class="stringliteral">&quot;f&quot;</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload</a>&lt;<span class="keywordtype">void</span>(<span class="keywordtype">int</span>)&gt;(&amp;Foo::f))</div><div class="line">                 .method(<span class="stringliteral">&quot;f&quot;</span>, <a class="code" href="namespacerttr.html#ab5ab25763f38e207fba4461aeb307e4b">select_const</a>(&amp;Foo::f));                <span class="comment">// register a overloaded const method</span></div><div class="line">}</div></div><!-- fragment --><p>The last function is a overloaded const function, use therefore the <a class="el" href="namespacerttr.html#ab5ab25763f38e207fba4461aeb307e4b">select_const()</a> function.</p>
<h2>Register constructor </h2>
<p>RTTR allows your to register constructors for classes. Because C++ doesn't allow to retrieve the member function pointer of a constructor you have to explicit specify all data types of a constructor.</p>
<p>Consider following class with three constructors:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Foo</div><div class="line">{</div><div class="line">    Foo();</div><div class="line">    Foo(<span class="keywordtype">int</span>, <span class="keywordtype">double</span>);</div><div class="line">    Foo(<span class="keyword">const</span> std::string&amp;);</div><div class="line">    </div><div class="line">    <span class="keyword">static</span> Foo* create();</div><div class="line">};</div></div><!-- fragment --><p>For registering three <code>Foo</code> constructors you now have to specify every parameter as template parameter in the member function <a class="el" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructor()</a>. As second option, it is possible to register a static function as constructor. In order to do this, just forward the function pointer.</p>
<div class="fragment"><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>(<span class="stringliteral">&quot;Foo&quot;</span>)</div><div class="line">                 .<a class="code" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructor</a>&lt;&gt;()</div><div class="line">                 .<a class="code" href="classrttr_1_1constructor.html">constructor&lt;int,double&gt;</a>()</div><div class="line">                 .constructor&lt;<span class="keyword">const</span> std::string&amp;&gt;()</div><div class="line">                 .<a class="code" href="classrttr_1_1constructor.html">constructor</a>(&amp;Foo::create);</div><div class="line">}</div></div><!-- fragment --><p>When a constructor is registered a destructor is registered automatically. The used default policy for creating an instance is <a class="el" href="structrttr_1_1policy_1_1ctor.html#a1e3b8ee81a41ce6b2e075bef1620f28c">policy::ctor::as_object</a>.</p>
<h2>Register class properties </h2>
<p>Register a public property can be easily done, consider following class:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>Foo</div><div class="line">{</div><div class="line">    <span class="keywordtype">int</span> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a>;</div><div class="line">};</div></div><!-- fragment --><p>This class is registered like this:</p>
<div class="fragment"><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>()</div><div class="line">        .<a class="code" href="classrttr_1_1registration_1_1class__.html#a005a60417fb6d183af475cb4059dc0a0">property</a>(<span class="stringliteral">&quot;value&quot;</span>, &amp;<a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">Foo::value</a>);</div><div class="line">}</div></div><!-- fragment --><p>With the <a class="el" href="classrttr_1_1registration_1_1class__.html#a005a60417fb6d183af475cb4059dc0a0">property()</a> member function you will register the member variable <code>Foo::value</code> with read and write access.</p>
<h3>Read Only Properties</h3>
<p>When you want a register a property with <code>read-only</code> access, then this is also possible with <a class="el" href="classrttr_1_1registration_1_1class__.html#a174f7ef563a97b910dbf894694653ce5">property_readonly()</a> member function.</p>
<div class="fragment"><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>()</div><div class="line">        .<a class="code" href="classrttr_1_1registration_1_1class__.html#a174f7ef563a97b910dbf894694653ce5">property_readonly</a>(<span class="stringliteral">&quot;value&quot;</span>, &amp;<a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">Foo::value</a>);</div><div class="line">}</div></div><!-- fragment --><h3>Private Properties</h3>
<p>When you have a class and the property is declared in private scope, then you can still register this property when you insert the macro: <a class="el" href="registration__friend_8h.html#a307051b21da56e1ec8a63f0aa943797e">RTTR_REGISTRATION_FRIEND</a> inside the class. </p><div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div><div class="line">{</div><div class="line"><span class="keyword">private</span>:</div><div class="line">    <span class="keywordtype">int</span> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a>;</div><div class="line"></div><div class="line">    <a class="code" href="registration_8h.html#a307051b21da56e1ec8a63f0aa943797e">RTTR_REGISTRATION_FRIEND</a></div><div class="line">};</div></div><!-- fragment --><p>This will make this class a friend to the registration system.</p>
<h3>Getter Setter For Properties</h3>
<p>You can also register getter and setter functions and make them look as if they were a public data member. Consider the following class: </p><div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">    <span class="keywordtype">void</span> set_value(<span class="keywordtype">int</span> x) { m_value = x; }</div><div class="line">    <span class="keywordtype">int</span> get_value()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> m_value; }</div><div class="line">    </div><div class="line"><span class="keyword">private</span>:</div><div class="line">    <span class="keywordtype">int</span> m_value;</div><div class="line">};</div></div><!-- fragment --><p>This is the registration code:</p>
<div class="fragment"><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></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">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>()</div><div class="line">        .<a class="code" href="classrttr_1_1registration_1_1class__.html#a005a60417fb6d183af475cb4059dc0a0">property</a>(<span class="stringliteral">&quot;value&quot;</span>, &amp;Foo::get_value, &amp;Foo::set_value);</div><div class="line">}</div></div><!-- fragment --><p>This way, accessing the property will now call these functions, instead the property directly. Remark that the getter function needs the be <code>const</code>.</p>
<p>The following sub sections will now show how to retrieve these informations for creating, invoking and setting properties of an instance of this class.</p>
<h2>Access Level Of Class Members </h2>
<p>With RTTR it is also possible to specify on of the three access modifiers during the registration.</p>
<div class="fragment"><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="keyword">struct </span>Foo</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">    Foo() {}</div><div class="line"><span class="keyword">protected</span>:</div><div class="line">    <span class="keywordtype">void</span> func() {}</div><div class="line"><span class="keyword">private</span>:</div><div class="line">    <span class="keywordtype">int</span> <a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">value</a>;</div><div class="line"></div><div class="line">    <a class="code" href="registration_8h.html#a307051b21da56e1ec8a63f0aa943797e">RTTR_REGISTRATION_FRIEND</a></div><div class="line">};</div><div class="line"></div><div class="line"><a class="code" href="registration_8h.html#ac6326400f16225ee15b52eabcaae8130">RTTR_REGISTRATION</a></div><div class="line">{</div><div class="line">    <a class="code" href="classrttr_1_1registration_1_1class__.html">registration::class_&lt;Foo&gt;</a>(<span class="stringliteral">&quot;Foo&quot;</span>)</div><div class="line">                  .<a class="code" href="classrttr_1_1registration_1_1class__.html#a92925cd9adc8c53c6c2df6e5f7f86e8a">constructor</a>&lt;&gt;(<a class="code" href="classrttr_1_1registration.html#a45df26a4b45b6715ac17e9a03594c369">registration::public_access</a>)   <span class="comment">// not necessary, because that is the default</span></div><div class="line">                  .<a class="code" href="classrttr_1_1method.html">method</a>(<span class="stringliteral">&quot;func&quot;</span>, &amp;Foo::func, <a class="code" href="classrttr_1_1registration.html#a5ac82a82df83ce2dd8c34ada18d7b904">registration::protected_access</a>)</div><div class="line">                  .property(<span class="stringliteral">&quot;value&quot;</span>, &amp;<a class="code" href="namespacerttr.html#a54ecd8bad715cbc451e7aa8491667d4a">Foo::value</a>, <a class="code" href="classrttr_1_1registration.html#a8920352ec53e1cd44bdec18ef5e89568">registration::private_access</a>);</div><div class="line">}</div></div><!-- fragment --><p> The access level has to be specified as last argument in the corresponding <a class="el" href="classrttr_1_1registration_1_1class__.html">class_</a> registration function. Use therefore one of the three static member variables in the <a class="el" href="classrttr_1_1registration.html">registration</a> class. This information can be retrieved at runtime for <a class="el" href="classrttr_1_1property.html#ae3d36f39f60d0aca13d4c6b039799c7d">properties</a>, <a class="el" href="classrttr_1_1method.html#ad77b3825aa9e8378d5e05f7fafe01525">methods</a> or <a class="el" href="classrttr_1_1constructor.html#acccb0f27a9d2e4beef2788c4b95baf20">constructors</a>. via the function <code>get_access_level()</code>, which will return a enum value of type <a class="el" href="namespacerttr.html#aa07db0e4984d0c48adc5d61f834d611b">access_levels</a>.</p>
<h2>Create/destroy of classes </h2>
<p>There are two options for creating/destroying a class.</p><ol type="1">
<li>use just the <a class="el" href="classrttr_1_1type.html#ad44572a03514d83035580f516ebaf4e1">type</a> interface or</li>
<li>retrieve a <a class="el" href="classrttr_1_1constructor.html">constructor</a> and <a class="el" href="classrttr_1_1destructor.html">destructor</a> object from the <a class="el" href="classrttr_1_1type.html">type</a> class.</li>
</ol>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> main()</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">    <span class="comment">// option 1</span></div><div class="line">    <a class="code" href="classrttr_1_1type.html">type</a> class_type = <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">&quot;test_class&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (class_type)</div><div class="line">    {</div><div class="line">        <a class="code" href="classrttr_1_1variant.html">variant</a> obj = class_type.<a class="code" href="classrttr_1_1type.html#ad44572a03514d83035580f516ebaf4e1">create</a>({23});</div><div class="line">        <span class="keywordflow">if</span> (obj.<a class="code" href="classrttr_1_1variant.html#a7a72579286ed164225c5be882ab6ea81">get_type</a>().<a class="code" href="classrttr_1_1type.html#a24a00513155d3e678454aa349b0caaa7">is_pointer</a>())</div><div class="line">            class_type.<a class="code" href="classrttr_1_1type.html#a33bc44604dae75f4368428afabf364ac">destroy</a>(obj);</div><div class="line">    }</div><div class="line">    <span class="comment">// option 2</span></div><div class="line">    <span class="keywordflow">if</span> (class_type)</div><div class="line">    {</div><div class="line">       <a class="code" href="classrttr_1_1constructor.html">constructor</a> ctor = class_type.<a class="code" href="classrttr_1_1type.html#ae1ed385e76752e7c02ffd9bf5a0ae410">get_constructor</a>({type::get&lt;int&gt;()});</div><div class="line">       <a class="code" href="classrttr_1_1variant.html">variant</a> obj = ctor.<a class="code" href="classrttr_1_1constructor.html#a9fe7a0a5c6583be4dbf9e7ebf4ca7f5e">invoke</a>(23);</div><div class="line">       <span class="keywordflow">if</span> (obj.<a class="code" href="classrttr_1_1variant.html#a7a72579286ed164225c5be882ab6ea81">get_type</a>().<a class="code" href="classrttr_1_1type.html#a24a00513155d3e678454aa349b0caaa7">is_pointer</a>())</div><div class="line">       {</div><div class="line">            <a class="code" href="classrttr_1_1destructor.html">destructor</a> dtor = class_type.<a class="code" href="classrttr_1_1type.html#a12d900648f71d11873896f58b1f9a3ac">get_destructor</a>();</div><div class="line">            dtor.<a class="code" href="classrttr_1_1destructor.html#a93d528bf4496174e7ab046dc714b2846">invoke</a>(obj);</div><div class="line">       }</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --><p> The objects which are constructed are created on the heap and stored as pointer in the variant object.</p>
<h2>Invoke member functions </h2>
<p>Invoking a member function works in the same way like invoking global function. The only difference is, that you have to provide the instance of the class.</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> main()</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">    test_class obj(42);</div><div class="line">    <a class="code" href="classrttr_1_1type.html">type</a> class_type = <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">&quot;test_class&quot;</span>);</div><div class="line">    <span class="comment">// option 1</span></div><div class="line">    class_type.<a class="code" href="classrttr_1_1type.html#afc7329cac11337f4022d7d10eebe80d3">invoke</a>(<span class="stringliteral">&quot;print_value&quot;</span>, obj, {}); <span class="comment">// print 42</span></div><div class="line">    </div><div class="line">    <span class="comment">// option 2</span></div><div class="line">    <a class="code" href="classrttr_1_1method.html">method</a> print_meth = class_type.<a class="code" href="classrttr_1_1type.html#a4d31b84c050f5b22509c16d0379fe0c7">get_method</a>(<span class="stringliteral">&quot;print_value&quot;</span>);</div><div class="line">    print_meth.<a class="code" href="classrttr_1_1method.html#aea4351075b4cbe6f2f2b6500cf226f20">invoke</a>(obj); <span class="comment">// prints &quot;42&quot;</span></div><div class="line">}</div></div><!-- fragment --><p>The invoke function also except to use variants. So when you create the object via the type constructor you can use the returned variant to invoke a method:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> main()</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">    <a class="code" href="classrttr_1_1variant.html">variant</a> obj_var = <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">&quot;test_class&quot;</span>).<a class="code" href="classrttr_1_1type.html#ad44572a03514d83035580f516ebaf4e1">create</a>({42});</div><div class="line">    <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">&quot;test_class&quot;</span>).<a class="code" href="classrttr_1_1type.html#afc7329cac11337f4022d7d10eebe80d3">invoke</a>(<span class="stringliteral">&quot;print_value&quot;</span>, obj_var, {}); <span class="comment">// print 42</span></div><div class="line">}</div></div><!-- fragment --><p>It is possible to invoke a method, when the instance is wrapped inside a wrapper class, for example <code>std::shared_ptr&lt;T&gt;</code>.</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    std::shared_ptr&lt;test_class&gt; obj = std::make_shared&lt;test_class&gt;(23);</div><div class="line"></div><div class="line">    method meth = type::get_by_name(<span class="stringliteral">&quot;test_class&quot;</span>).get_method(<span class="stringliteral">&quot;print_value&quot;</span>);</div><div class="line"></div><div class="line">    method.<a class="code" href="classrttr_1_1method.html#aea4351075b4cbe6f2f2b6500cf226f20">invoke</a>(obj);         <span class="comment">// successful invoke</span></div><div class="line">    method.invoke(obj.get());   <span class="comment">// successful invoke</span></div><div class="line">    method.invoke(*obj.get());  <span class="comment">// successful invoke</span></div><div class="line"></div><div class="line">    variant var = obj;</div><div class="line">    <span class="comment">// or use the variant</span></div><div class="line">    method.invoke(var);         <span class="comment">// successful invoke</span></div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><p> When you want to use RTTR with a custom wrapper type, you have provide a specialization of the class template <a class="el" href="structrttr_1_1wrapper__mapper.html">wrapper_mapper&lt;T&gt;</a>.</p>
<h2>Set/Get property of a class </h2>
<p>Properties can be also set and get in two steps:</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> main()</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">    test_class obj(0);</div><div class="line">    <a class="code" href="classrttr_1_1type.html">type</a> class_type = <a class="code" href="classrttr_1_1type.html#a7ba79b9f4916c30db74fe65508dca033">type::get_by_name</a>(<span class="stringliteral">&quot;test_class&quot;</span>);</div><div class="line">    <span class="comment">// option 1</span></div><div class="line">    <span class="keywordtype">bool</span> success = class_type.<a class="code" href="classrttr_1_1type.html#af57f575909bf13fa24d03ad14393204a">set_property_value</a>(<span class="stringliteral">&quot;value&quot;</span>, obj, 50);</div><div class="line">    std::cout &lt;&lt; obj.m_value; <span class="comment">// prints &quot;50&quot;</span></div><div class="line">    </div><div class="line">    <span class="comment">// option 2</span></div><div class="line">    <span class="keyword">property</span> prop = class_type.<a class="code" href="classrttr_1_1type.html#ae53c4df812c270f3f74b77210bc6a926">get_property</a>(<span class="stringliteral">&quot;value&quot;</span>);</div><div class="line">    success = prop.<a class="code" href="classrttr_1_1property.html#a1bb74b1d1910edf6cabec250df8da021">set_value</a>(obj, 24);</div><div class="line">    std::cout &lt;&lt; obj.m_value; <span class="comment">// prints &quot;24&quot;</span></div><div class="line">}</div></div><!-- fragment --><p>In difference to the global properties, a valid type object and an instance (object) of the class is now needed to set and get the value. It doesn't matter in what hierarchy level the object is or if its a pointer, an object on the stack or wrapped inside a variant. RTTR will try to cast the given object to the class type where the property was registered to.</p>
<hr/>
<div type="button" class="btn btn-default doxy-button"><a class="el" href="register_variant_page.html">previous</a></div><div class="btn btn-default doxy-button"><a class="el" href="register_metadata_page.html">next</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>