Sophie

Sophie

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

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: Methods</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">Methods </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>As already mentioned to register global methods to RTTR use the function <a class="el" href="classrttr_1_1registration.html#a5da1c6eb947d700681faa7fec0bf23b7">registration::method()</a>. It has following synopsis: </p><div class="fragment"><div class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> F&gt;</div><div class="line"><a class="code" href="classrttr_1_1registration_1_1bind.html">rttr::registration::bind</a> <a class="code" href="classrttr_1_1registration.html#a5da1c6eb947d700681faa7fec0bf23b7">rttr::registration::method</a>(<a class="code" href="namespacerttr.html#a61265f69675ac438ecfa23b084adaa56">string_view</a> name, F <span class="keyword">function</span>);</div></div><!-- fragment --><ul>
<li><code>name</code> is the name of the function</li>
<li><code>F</code> is the function pointer or std::function your want to register</li>
</ul>
<p>For example when you want to register the byte string to integer conversion function: <code>int atoi (const char * str);</code></p>
<p>Do it in the following way: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;rttr/registration&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;cstdlib&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">    <a class="code" href="classrttr_1_1registration.html#a5da1c6eb947d700681faa7fec0bf23b7">registration::method</a>(<span class="stringliteral">&quot;atoi&quot;</span>, &amp;atoi);</div><div class="line">}</div></div><!-- fragment --><h2>Overloaded Methods </h2>
<p>When you want to register a function which is overloaded (same name, different signature), you should use the helper function: <a class="el" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload&lt;T&gt;(T*)</a></p>
<p>This will avoid the hassle with the function pointer signature. For example the two functions: <code>float sin (float x);</code> and <code>double sin (double x);</code> can be registered like following:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;rttr/registration&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;cmath&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">    <a class="code" href="classrttr_1_1registration.html#a5da1c6eb947d700681faa7fec0bf23b7">registration::method</a>(<span class="stringliteral">&quot;sin&quot;</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload</a>&lt;<span class="keywordtype">float</span>(<span class="keywordtype">float</span>)&gt;(&amp;sin))</div><div class="line">                 .method(<span class="stringliteral">&quot;sin&quot;</span>, <a class="code" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">select_overload</a>&lt;<span class="keywordtype">double</span>(<span class="keywordtype">double</span>)&gt;(&amp;sin));</div><div class="line">}</div></div><!-- fragment --><p> It is possible the chain multiple registration calls, because of the temporary returned <a class="el" href="classrttr_1_1registration_1_1bind.html">bind</a> object.</p>
<dl class="section remark"><dt>Remarks</dt><dd>The select_overload function cannot be used with MSVC x86 compiler, because of the different calling convention for global- and member-functions. See here for a <a class="el" href="namespacerttr.html#a1582d7d5b803f7dd5498a4a0166d382d">workaround</a>.</dd></dl>
<h2>Invoke of methods </h2>
<p>Invoking a method with RTTR can be done in two ways:</p><ul>
<li>calling <a class="el" href="classrttr_1_1type.html#afe763c9f268d4fa897d8e52b89c2e093">type::invoke()</a> from the <a class="el" href="classrttr_1_1type.html">type</a> class.</li>
<li>retrieving first a <a class="el" href="classrttr_1_1method.html">method</a> object from <a class="el" href="classrttr_1_1type.html#ab96e1fba11f481929eda97399aaedf68">type::get_global_method()</a> and then calling invoke</li>
</ul>
<p>The first option needs less typing, but it is slower when you need to invoke the function several times. For the second option you need more code to write, but it invokes the method faster.</p>
<p>The following example demonstrates the possibilities to invoke a method: </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"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">    <span class="comment">// let asume we have registered the pow function: double pow( double base, double exp );</span></div><div class="line">    <span class="comment">// option 1</span></div><div class="line">    <a class="code" href="classrttr_1_1variant.html">variant</a> return_value = <a class="code" href="classrttr_1_1type.html#afc7329cac11337f4022d7d10eebe80d3">type::invoke</a>(<span class="stringliteral">&quot;pow&quot;</span>, {9.0, 2.0});</div><div class="line">    <span class="keywordflow">if</span> (return_value.<a class="code" href="classrttr_1_1variant.html#aed6e6a2e29b4d1ac51a09b7d9037dd44">is_valid</a>() &amp;&amp; return_value.<a class="code" href="classrttr_1_1variant.html#a8a2fe635a8452ac3a2370589d6fc4bfb">is_type</a>&lt;<span class="keywordtype">double</span>&gt;())</div><div class="line">        std::cout &lt;&lt; return_value.<a class="code" href="classrttr_1_1variant.html#aa3a9f7e07cdc4d922a4e09882c5c0574">get_value</a>&lt;<span class="keywordtype">double</span>&gt;() &lt;&lt; std::endl; <span class="comment">// 81</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> meth = <a class="code" href="classrttr_1_1type.html#a15fa4ae4fb0619be68c3b83967fefb51">type::get_global_method</a>(<span class="stringliteral">&quot;pow&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (meth) <span class="comment">// check if the function was found</span></div><div class="line">    {</div><div class="line">        return_value = meth.invoke({}, 9.0, 3.0); <span class="comment">// invoke with empty instance</span></div><div class="line">        <span class="keywordflow">if</span> (return_value.<a class="code" href="classrttr_1_1variant.html#aed6e6a2e29b4d1ac51a09b7d9037dd44">is_valid</a>() &amp;&amp; return_value.<a class="code" href="classrttr_1_1variant.html#a8a2fe635a8452ac3a2370589d6fc4bfb">is_type</a>&lt;<span class="keywordtype">double</span>&gt;())</div><div class="line">            std::cout &lt;&lt; return_value.<a class="code" href="classrttr_1_1variant.html#aa3a9f7e07cdc4d922a4e09882c5c0574">get_value</a>&lt;<span class="keywordtype">double</span>&gt;() &lt;&lt; std::endl; <span class="comment">// 729</span></div><div class="line">        </div><div class="line">    }</div><div class="line">}</div></div><!-- fragment --><p>The <a class="el" href="classrttr_1_1type.html#afe763c9f268d4fa897d8e52b89c2e093">type::invoke()</a> function will internally try to find a function based on the given name and the given types of the arguments. So finding the correct function when overloaded function are registered is automatically done. Notice that you have to provide the arguments as a vector pack. Therefore an initializer list is quite handy to reduce typing. The argument must match 100%, there is at the moment no conversion done. That means, when an integer argument is needed and you forward a double value the function will <b>not</b> be called. The arguments will not be copied, instead they will be wrapped in an internal class and forwarded to the underlying function pointer. This class is called <code>argument</code> do <b>not</b> create this class on your own. The class will implicit wrap your argument value.</p>
<p>The return value of <a class="el" href="classrttr_1_1type.html#afe763c9f268d4fa897d8e52b89c2e093">type::invoke()</a> is a <a class="el" href="classrttr_1_1variant.html">variant</a> object. It indicates whether the function was called or not. RTTR does <b>not</b> use the exception mechanism of C++, therefore you have to check the return values when you are interested in a successful call. The <a class="el" href="classrttr_1_1variant.html">rttr::variant</a> object <a class="el" href="classrttr_1_1variant.html#aed6e6a2e29b4d1ac51a09b7d9037dd44">is valid</a> when it was successfully called. When the function has a return value, then this value is also contained in the variant object.</p>
<p>When you prefer to hold a handle to the function use the getter <a class="el" href="classrttr_1_1type.html#ab96e1fba11f481929eda97399aaedf68">type::get_global_method()</a> to retrieve a <a class="el" href="classrttr_1_1method.html">method</a> object. This has the advantage that you do not need to search for the function every time you want to invoke it. Additionally this class provides <a class="el" href="classrttr_1_1method.html#aea4351075b4cbe6f2f2b6500cf226f20">functions</a> to invoke a function without the need to create a vector of arguments. The method object is lightweight and can be simply copied around different locations. The object stays valid till end of the <code>main()</code> function.</p>
<hr/>
<div class="btn btn-default doxy-button"><a class="el" href="register_hello_world_page.html">previous</a></div><div class="btn btn-default doxy-button"><a class="el" href="register_properties_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>