index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Basic Stack Interaction - Luwra</title>
  9. <link rel="shortcut icon" href="../img/favicon.ico">
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
  11. <link rel="stylesheet" href="../css/theme.css" type="text/css" />
  12. <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
  13. <link rel="stylesheet" href="../css/highlight.css">
  14. <script>
  15. // Current page data
  16. var mkdocs_page_name = "Basic Stack Interaction";
  17. var mkdocs_page_input_path = "basics.md";
  18. var mkdocs_page_url = "/basics/";
  19. </script>
  20. <script src="../js/jquery-2.1.1.min.js"></script>
  21. <script src="../js/modernizr-2.8.3.min.js"></script>
  22. <script type="text/javascript" src="../js/highlight.pack.js"></script>
  23. <script src="../js/theme.js"></script>
  24. </head>
  25. <body class="wy-body-for-nav" role="document">
  26. <div class="wy-grid-for-nav">
  27. <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
  28. <div class="wy-side-nav-search">
  29. <a href=".." class="icon icon-home"> Luwra</a>
  30. <div role="search">
  31. <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
  32. <input type="text" name="q" placeholder="Search docs" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <ul class="current">
  38. <li>
  39. <li class="toctree-l1 ">
  40. <a class="" href="..">Home</a>
  41. </li>
  42. <li>
  43. <li>
  44. <li class="toctree-l1 current">
  45. <a class="current" href="./">Basic Stack Interaction</a>
  46. <ul>
  47. <li class="toctree-l3"><a href="#integration">Integration</a></li>
  48. <li class="toctree-l3"><a href="#stack-interaction">Stack Interaction</a></li>
  49. <li><a class="toctree-l4" href="#pushing-c-values">Pushing C++ values</a></li>
  50. <li><a class="toctree-l4" href="#reading-lua-values">Reading Lua values</a></li>
  51. <li><a class="toctree-l4" href="#read-and-type-errors">Read and type errors</a></li>
  52. </ul>
  53. </li>
  54. <li>
  55. <li>
  56. <li class="toctree-l1 ">
  57. <a class="" href="../advanced/">Advanced Stack Interaction</a>
  58. </li>
  59. <li>
  60. <li>
  61. <li class="toctree-l1 ">
  62. <a class="" href="../wrapping/">Function Wrapping</a>
  63. </li>
  64. <li>
  65. <li>
  66. <li class="toctree-l1 ">
  67. <a class="" href="../user-types/">User Types</a>
  68. </li>
  69. <li>
  70. </ul>
  71. </div>
  72. &nbsp;
  73. </nav>
  74. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  75. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  76. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  77. <a href="..">Luwra</a>
  78. </nav>
  79. <div class="wy-nav-content">
  80. <div class="rst-content">
  81. <div role="navigation" aria-label="breadcrumbs navigation">
  82. <ul class="wy-breadcrumbs">
  83. <li><a href="..">Docs</a> &raquo;</li>
  84. <li>Basic Stack Interaction</li>
  85. <li class="wy-breadcrumbs-aside">
  86. <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
  87. </li>
  88. </ul>
  89. <hr/>
  90. </div>
  91. <div role="main">
  92. <div class="section">
  93. <h1 id="integration">Integration</h1>
  94. <p>Luwra does not provide a standalone version of Lua nor does it isolate its features. This means that
  95. all functions and classes operate on
  96. <a href="http://www.lua.org/manual/5.3/manual.html#lua_State">lua_State</a> (or the alias
  97. <a href="../reference/namespaceluwra.html#a2c037b44385367826eb4e931b5b8197d">State</a>). Doing this allows you to
  98. integrate Luwra however you like.</p>
  99. <h1 id="stack-interaction">Stack Interaction</h1>
  100. <p>Although Luwra provides a variety of features, its main concern is efficient and safe interaction
  101. with the Lua stack.</p>
  102. <p>A fundamental aspect of this is the abstract template <a href="../reference/structluwra_1_1Value.html">Value</a>.
  103. Every type which can be pushed onto or read from the stack has a specialization of it.
  104. Useful implementations are provided out of the box:</p>
  105. <table>
  106. <thead>
  107. <tr>
  108. <th>C++ type</th>
  109. <th>Pushable</th>
  110. <th>Readable</th>
  111. <th>Lua type</th>
  112. </tr>
  113. </thead>
  114. <tbody>
  115. <tr>
  116. <td>bool</td>
  117. <td>yes</td>
  118. <td>yes</td>
  119. <td>boolean</td>
  120. </tr>
  121. <tr>
  122. <td>signed char</td>
  123. <td>yes</td>
  124. <td>yes</td>
  125. <td>number (integer since 5.3)</td>
  126. </tr>
  127. <tr>
  128. <td>signed short</td>
  129. <td>yes</td>
  130. <td>yes</td>
  131. <td>number (integer since 5.3)</td>
  132. </tr>
  133. <tr>
  134. <td>signed int</td>
  135. <td>yes</td>
  136. <td>yes</td>
  137. <td>number (integer since 5.3)</td>
  138. </tr>
  139. <tr>
  140. <td>signed long int</td>
  141. <td>yes</td>
  142. <td>yes</td>
  143. <td>number (integer since 5.3)</td>
  144. </tr>
  145. <tr>
  146. <td>signed long long int</td>
  147. <td>yes</td>
  148. <td>yes</td>
  149. <td>number (integer since 5.3)</td>
  150. </tr>
  151. <tr>
  152. <td>unsigned char</td>
  153. <td>yes</td>
  154. <td>yes</td>
  155. <td>number (integer since 5.3)</td>
  156. </tr>
  157. <tr>
  158. <td>unsigned short</td>
  159. <td>yes</td>
  160. <td>yes</td>
  161. <td>number (integer since 5.3)</td>
  162. </tr>
  163. <tr>
  164. <td>unsigned int</td>
  165. <td>yes</td>
  166. <td>yes</td>
  167. <td>number (integer since 5.3)</td>
  168. </tr>
  169. <tr>
  170. <td>unsigned long int</td>
  171. <td>yes</td>
  172. <td>yes</td>
  173. <td>number (integer since 5.3)</td>
  174. </tr>
  175. <tr>
  176. <td>unsigned long long int</td>
  177. <td>yes</td>
  178. <td>yes</td>
  179. <td>number (integer since 5.3)</td>
  180. </tr>
  181. <tr>
  182. <td>float</td>
  183. <td>yes</td>
  184. <td>yes</td>
  185. <td>number</td>
  186. </tr>
  187. <tr>
  188. <td>double</td>
  189. <td>yes</td>
  190. <td>yes</td>
  191. <td>number</td>
  192. </tr>
  193. <tr>
  194. <td>long double</td>
  195. <td>yes</td>
  196. <td>yes</td>
  197. <td>number</td>
  198. </tr>
  199. <tr>
  200. <td>const char*</td>
  201. <td>yes</td>
  202. <td>yes</td>
  203. <td>string</td>
  204. </tr>
  205. <tr>
  206. <td>std::string</td>
  207. <td>yes</td>
  208. <td>yes</td>
  209. <td>string</td>
  210. </tr>
  211. <tr>
  212. <td>std::nullptr_t</td>
  213. <td>yes</td>
  214. <td>yes</td>
  215. <td>nil</td>
  216. </tr>
  217. <tr>
  218. <td>std::tuple&lt;T&gt;</td>
  219. <td>yes</td>
  220. <td>no</td>
  221. <td><em>depends on the tuple contents</em></td>
  222. </tr>
  223. <tr>
  224. <td><a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a></td>
  225. <td>yes</td>
  226. <td>no</td>
  227. <td>function</td>
  228. </tr>
  229. <tr>
  230. <td><a href="../reference/structluwra_1_1NativeFunction_3_01R_07A_8_8_8_08_4.html">NativeFunction&lt;R(A...)&gt;</a></td>
  231. <td>no</td>
  232. <td>yes</td>
  233. <td>function</td>
  234. </tr>
  235. <tr>
  236. <td><a href="../reference/namespaceluwra.html#ac090722c6d5d6b88b31895aad64788c2">FieldVector</a></td>
  237. <td>yes</td>
  238. <td>no</td>
  239. <td>table</td>
  240. </tr>
  241. </tbody>
  242. </table>
  243. <p><strong>Note:</strong> Some numeric types have a different size than their matching Lua type - they will be
  244. truncated during push or read operations.</p>
  245. <h2 id="pushing-c-values">Pushing C++ values</h2>
  246. <p>When pushing values onto the stack you can either use
  247. <a href="../reference/structluwra_1_1Value.html#aa376d68285606c206562b822e8187384">Value&lt;T&gt;::push</a> or the more
  248. convenient <a href="../reference/namespaceluwra.html#ae8e7eab11fc2cf3f258ffd81571066fa">push</a>.</p>
  249. <pre><code class="c++">// Push an integer
  250. luwra::push(lua, 1338);
  251. // Push a number
  252. luwra::push(lua, 13.37);
  253. // Push a boolean
  254. luwra::push(lua, false);
  255. // Push a string
  256. luwra::push(lua, &quot;Hello World&quot;);
  257. // Push a table
  258. luwra::push(lua, luwra::FieldVector {
  259. {&quot;one&quot;, 1},
  260. {1, &quot;one&quot;},
  261. {&quot;nested&quot;, luwra::FieldVector {
  262. {&quot;more&quot;, &quot;fields&quot;}
  263. }}
  264. });
  265. </code></pre>
  266. <p>This produces the following stack layout:</p>
  267. <table>
  268. <thead>
  269. <tr>
  270. <th>Absolute Position</th>
  271. <th>Relative Position</th>
  272. <th>Value</th>
  273. </tr>
  274. </thead>
  275. <tbody>
  276. <tr>
  277. <td>1</td>
  278. <td>-5</td>
  279. <td><code>1338</code></td>
  280. </tr>
  281. <tr>
  282. <td>2</td>
  283. <td>-4</td>
  284. <td><code>13.37</code></td>
  285. </tr>
  286. <tr>
  287. <td>3</td>
  288. <td>-3</td>
  289. <td><code>false</code></td>
  290. </tr>
  291. <tr>
  292. <td>4</td>
  293. <td>-2</td>
  294. <td><code>"Hello World"</code></td>
  295. </tr>
  296. <tr>
  297. <td>5</td>
  298. <td>-1</td>
  299. <td><code>{one = 1, [1] = "one", nested = {more = "fields"}}</code></td>
  300. </tr>
  301. </tbody>
  302. </table>
  303. <p>It is possible to provide a template parameter to <code>push</code> to enforce pushing a specific type.
  304. In most cases you are probably better off by letting the compiler infer the template parameter.</p>
  305. <h2 id="reading-lua-values">Reading Lua values</h2>
  306. <p>Simple retrieval of Lua values is done using
  307. <a href="../reference/namespaceluwra.html#a4fe4e574680cf54a0f8d958740eb90ab">read&lt;T&gt;</a>. Consider the
  308. stack layout from the previous example. This is how you would retrieve a value from the stack.</p>
  309. <pre><code class="c++">// Retrieve the integer at position 1
  310. int value = luwra::read&lt;int&gt;(lua, 1);
  311. // Similiar with a relative index
  312. int value = luwra::read&lt;int&gt;(lua, -5);
  313. </code></pre>
  314. <h2 id="read-and-type-errors">Read and type errors</h2>
  315. <p>What happens when a value which you are trying to read mismatches the expected type or cannot be
  316. converted to it? Most <code>Value&lt;T&gt;</code> specializations use Lua's <code>luaL_check*</code> functions to retrieve
  317. the values from the stack. This means that no exceptions will be thrown - instead the error handling
  318. is delegated to the Lua VM. Have a look at the
  319. <a href="http://www.lua.org/manual/5.3/manual.html#4.6">error handling documentation</a> for more information.</p>
  320. </div>
  321. </div>
  322. <footer>
  323. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  324. <a href="../advanced/" class="btn btn-neutral float-right" title="Advanced Stack Interaction">Next <span class="icon icon-circle-arrow-right"></span></a>
  325. <a href=".." class="btn btn-neutral" title="Home"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  326. </div>
  327. <hr/>
  328. <div role="contentinfo">
  329. <!-- Copyright etc -->
  330. </div>
  331. 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>.
  332. </footer>
  333. </div>
  334. </div>
  335. </section>
  336. </div>
  337. <div class="rst-versions" role="note" style="cursor: pointer">
  338. <span class="rst-current-version" data-toggle="rst-current-version">
  339. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  340. <span><a href=".." style="color: #fcfcfc;">&laquo; Previous</a></span>
  341. <span style="margin-left: 15px"><a href="../advanced/" style="color: #fcfcfc">Next &raquo;</a></span>
  342. </span>
  343. </div>
  344. </body>
  345. </html>