| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- <!DOCTYPE html>
- <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
- <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
-
-
-
- <title>Basic Stack Interaction - Luwra</title>
-
- <link rel="shortcut icon" href="../img/favicon.ico">
-
- <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
- <link rel="stylesheet" href="../css/theme.css" type="text/css" />
- <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
- <link rel="stylesheet" href="../css/highlight.css">
-
- <script>
- // Current page data
- var mkdocs_page_name = "Basic Stack Interaction";
- var mkdocs_page_input_path = "basics.md";
- var mkdocs_page_url = "/basics/";
- </script>
-
- <script src="../js/jquery-2.1.1.min.js"></script>
- <script src="../js/modernizr-2.8.3.min.js"></script>
- <script type="text/javascript" src="../js/highlight.pack.js"></script>
- <script src="../js/theme.js"></script>
-
- </head>
- <body class="wy-body-for-nav" role="document">
- <div class="wy-grid-for-nav">
-
- <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
- <div class="wy-side-nav-search">
- <a href=".." class="icon icon-home"> Luwra</a>
- <div role="search">
- <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
- <input type="text" name="q" placeholder="Search docs" />
- </form>
- </div>
- </div>
- <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
- <ul class="current">
-
- <li>
- <li class="toctree-l1 ">
- <a class="" href="..">Home</a>
-
- </li>
- <li>
-
- <li>
- <li class="toctree-l1 current">
- <a class="current" href="./">Basic Stack Interaction</a>
-
- <ul>
-
- <li class="toctree-l3"><a href="#integration">Integration</a></li>
-
-
- <li class="toctree-l3"><a href="#stack-interaction">Stack Interaction</a></li>
-
- <li><a class="toctree-l4" href="#pushing-c-values">Pushing C++ values</a></li>
-
- <li><a class="toctree-l4" href="#reading-lua-values">Reading Lua values</a></li>
-
- <li><a class="toctree-l4" href="#read-and-type-errors">Read and type errors</a></li>
-
-
- </ul>
-
- </li>
- <li>
-
- <li>
- <li class="toctree-l1 ">
- <a class="" href="../advanced/">Advanced Stack Interaction</a>
-
- </li>
- <li>
-
- </ul>
- </div>
-
- </nav>
- <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
-
- <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
- <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
- <a href="..">Luwra</a>
- </nav>
-
- <div class="wy-nav-content">
- <div class="rst-content">
- <div role="navigation" aria-label="breadcrumbs navigation">
- <ul class="wy-breadcrumbs">
- <li><a href="..">Docs</a> »</li>
-
-
-
- <li>Basic Stack Interaction</li>
- <li class="wy-breadcrumbs-aside">
-
-
- <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
-
-
- </li>
- </ul>
- <hr/>
- </div>
- <div role="main">
- <div class="section">
-
- <h1 id="integration">Integration</h1>
- <p>Luwra does not provide a standalone version of Lua nor does it isolate its features. This means that
- all functions and classes operate on
- <a href="http://www.lua.org/manual/5.3/manual.html#lua_State">lua_State</a> (or the alias
- <a href="../reference/namespaceluwra.html#a2c037b44385367826eb4e931b5b8197d">State</a>). Doing this allows you to
- integrate Luwra however you like.</p>
- <h1 id="stack-interaction">Stack Interaction</h1>
- <p>Although Luwra provides a variety of features, its main concern is efficient and safe interaction
- with the Lua stack.</p>
- <p>A fundamental aspect of this is the abstract template <a href="../reference/structluwra_1_1Value.html">Value</a>.
- Every type which can be pushed onto or read from the stack has a specialization of it.
- Useful implementations are provided out of the box:</p>
- <table>
- <thead>
- <tr>
- <th>C++ type</th>
- <th>Pushable</th>
- <th>Readable</th>
- <th>Lua type</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>bool</td>
- <td>yes</td>
- <td>yes</td>
- <td>boolean</td>
- </tr>
- <tr>
- <td>signed char</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>signed short</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>signed int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>signed long int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>signed long long int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>unsigned char</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>unsigned short</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>unsigned int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>unsigned long int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>unsigned long long int</td>
- <td>yes</td>
- <td>yes</td>
- <td>number (integer since 5.3)</td>
- </tr>
- <tr>
- <td>float</td>
- <td>yes</td>
- <td>yes</td>
- <td>number</td>
- </tr>
- <tr>
- <td>double</td>
- <td>yes</td>
- <td>yes</td>
- <td>number</td>
- </tr>
- <tr>
- <td>long double</td>
- <td>yes</td>
- <td>yes</td>
- <td>number</td>
- </tr>
- <tr>
- <td>const char*</td>
- <td>yes</td>
- <td>yes</td>
- <td>string</td>
- </tr>
- <tr>
- <td>std::string</td>
- <td>yes</td>
- <td>yes</td>
- <td>string</td>
- </tr>
- <tr>
- <td>std::nullptr_t</td>
- <td>yes</td>
- <td>yes</td>
- <td>nil</td>
- </tr>
- <tr>
- <td>std::tuple<T></td>
- <td>yes</td>
- <td>no</td>
- <td><em>depends on the tuple contents</em></td>
- </tr>
- <tr>
- <td><a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a></td>
- <td>yes</td>
- <td>no</td>
- <td>function</td>
- </tr>
- <tr>
- <td><a href="../reference/structluwra_1_1NativeFunction_3_01R_07A_8_8_8_08_4.html">NativeFunction<R(A...)></a></td>
- <td>no</td>
- <td>yes</td>
- <td>function</td>
- </tr>
- <tr>
- <td><a href="../reference/namespaceluwra.html#ac090722c6d5d6b88b31895aad64788c2">FieldVector</a></td>
- <td>yes</td>
- <td>no</td>
- <td>table</td>
- </tr>
- </tbody>
- </table>
- <p><strong>Note:</strong> Some numeric types have a different size than their matching Lua type - they will be
- truncated during push or read operations.</p>
- <h2 id="pushing-c-values">Pushing C++ values</h2>
- <p>When pushing values onto the stack you can either use
- <a href="../reference/structluwra_1_1Value.html#aa376d68285606c206562b822e8187384">Value<T>::push</a> or the more
- convenient <a href="../reference/namespaceluwra.html#ae8e7eab11fc2cf3f258ffd81571066fa">push</a>.</p>
- <pre><code class="c++">// Push an integer
- luwra::push(lua, 1338);
- // Push a number
- luwra::push(lua, 13.37);
- // Push a boolean
- luwra::push(lua, false);
- // Push a string
- luwra::push(lua, "Hello World");
- </code></pre>
- <p>This produces the following stack layout:</p>
- <table>
- <thead>
- <tr>
- <th>Absolute Position</th>
- <th>Relative Position</th>
- <th>Value</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td>1</td>
- <td>-4</td>
- <td><code>1338</code></td>
- </tr>
- <tr>
- <td>2</td>
- <td>-3</td>
- <td><code>13.37</code></td>
- </tr>
- <tr>
- <td>3</td>
- <td>-2</td>
- <td><code>false</code></td>
- </tr>
- <tr>
- <td>4</td>
- <td>-1</td>
- <td><code>"Hello World"</code></td>
- </tr>
- </tbody>
- </table>
- <p>It is possible to provide a template parameter to <code>push</code> to enforce pushing a specific type.
- In most cases you are probably better off by letting the compiler infer the template parameter.</p>
- <h2 id="reading-lua-values">Reading Lua values</h2>
- <p>Simple retrieval of Lua values is done using
- <a href="../reference/namespaceluwra.html#a4fe4e574680cf54a0f8d958740eb90ab">read<T></a>. Consider the
- stack layout from the previous example. This is how you would retrieve a value from the stack.</p>
- <pre><code class="c++">// Retrieve the integer at position 1
- int value = luwra::read<int>(lua, 1);
- // Similiar with a relative index
- int value = luwra::read<int>(lua, -4);
- </code></pre>
- <h2 id="read-and-type-errors">Read and type errors</h2>
- <p>What happens when a value which you are trying to read mismatches the expected type or cannot be
- converted to it? Most <code>Value<T></code> specializations use Lua's <code>luaL_check*</code> functions to retrieve
- the values from the stack. This means that no exceptions will be thrown - instead the error handling
- is delegated to the Lua VM. Have a look at the
- <a href="http://www.lua.org/manual/5.3/manual.html#4.6">error handling documentation</a> for more information.</p>
-
- </div>
- </div>
- <footer>
-
- <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
-
- <a href="../advanced/" class="btn btn-neutral float-right" title="Advanced Stack Interaction">Next <span class="icon icon-circle-arrow-right"></span></a>
-
-
- <a href=".." class="btn btn-neutral" title="Home"><span class="icon icon-circle-arrow-left"></span> Previous</a>
-
- </div>
-
- <hr/>
- <div role="contentinfo">
- <!-- Copyright etc -->
-
- </div>
- Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
- </footer>
-
- </div>
- </div>
- </section>
- </div>
- <div class="rst-versions" role="note" style="cursor: pointer">
- <span class="rst-current-version" data-toggle="rst-current-version">
-
- <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
-
-
- <span><a href=".." style="color: #fcfcfc;">« Previous</a></span>
-
-
- <span style="margin-left: 15px"><a href="../advanced/" style="color: #fcfcfc">Next »</a></span>
-
- </span>
- </div>
- </body>
- </html>
|