index.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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>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 = "Stack Interaction";
  17. var mkdocs_page_input_path = "stack-interaction.md";
  18. var mkdocs_page_url = "/stack-interaction/";
  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="./">Stack Interaction</a>
  46. <ul>
  47. <li class="toctree-l3"><a href="#stack-interaction">Stack interaction</a></li>
  48. <li><a class="toctree-l4" href="#extending-supported-types">Extending supported types</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/">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>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="stack-interaction">Stack interaction</h1>
  94. <p>A fundamental aspect of this is the abstract template <a href="../reference/structluwra_1_1Value.html">Value</a>. Every type which can be
  95. pushed onto or read from the stack has a specialization of it. Useful implementations are provided
  96. out of the box:</p>
  97. <table>
  98. <thead>
  99. <tr>
  100. <th>C++ type</th>
  101. <th>Pushable</th>
  102. <th>Readable</th>
  103. <th>Lua type</th>
  104. </tr>
  105. </thead>
  106. <tbody>
  107. <tr>
  108. <td>bool</td>
  109. <td>yes</td>
  110. <td>yes</td>
  111. <td>boolean</td>
  112. </tr>
  113. <tr>
  114. <td>signed char</td>
  115. <td>yes</td>
  116. <td>yes</td>
  117. <td>number (integer since 5.3)</td>
  118. </tr>
  119. <tr>
  120. <td>signed short</td>
  121. <td>yes</td>
  122. <td>yes</td>
  123. <td>number (integer since 5.3)</td>
  124. </tr>
  125. <tr>
  126. <td>signed int</td>
  127. <td>yes</td>
  128. <td>yes</td>
  129. <td>number (integer since 5.3)</td>
  130. </tr>
  131. <tr>
  132. <td>signed long int</td>
  133. <td>yes</td>
  134. <td>yes</td>
  135. <td>number (integer since 5.3)</td>
  136. </tr>
  137. <tr>
  138. <td>signed long long int</td>
  139. <td>yes</td>
  140. <td>yes</td>
  141. <td>number (integer since 5.3)</td>
  142. </tr>
  143. <tr>
  144. <td>unsigned char</td>
  145. <td>yes</td>
  146. <td>yes</td>
  147. <td>number (integer since 5.3)</td>
  148. </tr>
  149. <tr>
  150. <td>unsigned short</td>
  151. <td>yes</td>
  152. <td>yes</td>
  153. <td>number (integer since 5.3)</td>
  154. </tr>
  155. <tr>
  156. <td>unsigned int</td>
  157. <td>yes</td>
  158. <td>yes</td>
  159. <td>number (integer since 5.3)</td>
  160. </tr>
  161. <tr>
  162. <td>unsigned long int</td>
  163. <td>yes</td>
  164. <td>yes</td>
  165. <td>number (integer since 5.3)</td>
  166. </tr>
  167. <tr>
  168. <td>unsigned long long int</td>
  169. <td>yes</td>
  170. <td>yes</td>
  171. <td>number (integer since 5.3)</td>
  172. </tr>
  173. <tr>
  174. <td>float</td>
  175. <td>yes</td>
  176. <td>yes</td>
  177. <td>number</td>
  178. </tr>
  179. <tr>
  180. <td>double</td>
  181. <td>yes</td>
  182. <td>yes</td>
  183. <td>number</td>
  184. </tr>
  185. <tr>
  186. <td>long double</td>
  187. <td>yes</td>
  188. <td>yes</td>
  189. <td>number</td>
  190. </tr>
  191. <tr>
  192. <td>const char*</td>
  193. <td>yes</td>
  194. <td>yes</td>
  195. <td>string</td>
  196. </tr>
  197. <tr>
  198. <td>std::string</td>
  199. <td>yes</td>
  200. <td>yes</td>
  201. <td>string</td>
  202. </tr>
  203. <tr>
  204. <td>std::nullptr_t</td>
  205. <td>yes</td>
  206. <td>yes</td>
  207. <td>nil</td>
  208. </tr>
  209. <tr>
  210. <td>std::tuple&lt;T...&gt;</td>
  211. <td>yes</td>
  212. <td>no</td>
  213. <td><em>depends on the tuple contents</em></td>
  214. </tr>
  215. <tr>
  216. <td>std::vector&lt;T&gt;</td>
  217. <td>yes</td>
  218. <td>yes</td>
  219. <td>table</td>
  220. </tr>
  221. <tr>
  222. <td>std::list&lt;T&gt;</td>
  223. <td>yes</td>
  224. <td>yes</td>
  225. <td>table</td>
  226. </tr>
  227. <tr>
  228. <td>std::map&lt;K, V&gt;</td>
  229. <td>yes</td>
  230. <td>yes</td>
  231. <td>table</td>
  232. </tr>
  233. <tr>
  234. <td><a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a></td>
  235. <td>yes</td>
  236. <td>no</td>
  237. <td>function, table or userdata</td>
  238. </tr>
  239. <tr>
  240. <td><a href="../reference/structluwra_1_1NativeFunction_3_01R_07A_8_8_8_08_4.html">NativeFunction</a></td>
  241. <td>yes</td>
  242. <td>yes</td>
  243. <td>function</td>
  244. </tr>
  245. <tr>
  246. <td><a href="../reference/structluwra_1_1Table.html">Table</a></td>
  247. <td>yes</td>
  248. <td>yes</td>
  249. <td>table</td>
  250. </tr>
  251. </tbody>
  252. </table>
  253. <p><strong>Note:</strong> Some numeric types have a different size than their matching Lua type - they will be
  254. truncated during push or read operations.</p>
  255. <h2 id="extending-supported-types">Extending supported types</h2>
  256. <p>If you are missing a type that cannot be used as a <a href="../user-types">user type</a>, you can add a
  257. specialization of <a href="../reference/structluwra_1_1Value.html">Value</a>. All you need to do is modify the following snippet for your
  258. type <code>T</code>.</p>
  259. <pre><code class="c++">namespace luwra {
  260. template &lt;&gt;
  261. struct Value&lt;T&gt; {
  262. static inline
  263. T read(State* state, int index) {
  264. return /* Return the instance of T that you have read at the given index */;
  265. }
  266. static inline
  267. size_t push(State* state, const T&amp; value) {
  268. // Push the given value on top of the stack
  269. return /* Return how many values you have pushed onto the stack */;
  270. }
  271. };
  272. }
  273. </code></pre>
  274. <h2 id="pushing-c-values">Pushing C++ values</h2>
  275. <p>When pushing values onto the stack you can either use <a href="../reference/structluwra_1_1Value.html#aa376d68285606c206562b822e8187384">Value&lt;T&gt;::push</a> or
  276. the more convenient <a href="../reference/namespaceluwra.html#ae8e7eab11fc2cf3f258ffd81571066fa">push</a>.</p>
  277. <pre><code class="c++">// Push an integer
  278. luwra::push(lua, 1338);
  279. // Push a number
  280. luwra::push(lua, 13.37);
  281. // Push a boolean
  282. luwra::push(lua, false);
  283. // Push a string
  284. luwra::push(lua, &quot;Hello World&quot;);
  285. // Push a table
  286. luwra::push(lua, luwra::MemberMap {
  287. {&quot;one&quot;, 1},
  288. {1, &quot;one&quot;},
  289. {&quot;nested&quot;, luwra::MemberMap {
  290. {&quot;more&quot;, &quot;fields&quot;}
  291. }}
  292. });
  293. </code></pre>
  294. <p><strong>Note:</strong> <a href="../reference/namespaceluwra.html#a2e12e40b973f0f56cb9a1dc91bef882a">luwra::MemberMap</a> is an alias for
  295. <code>std:map&lt;luwra::Pushable, luwra::Pushable&gt;</code>. Its keys and values are constructible using any
  296. pushable type.</p>
  297. <p>This produces the following stack layout:</p>
  298. <table>
  299. <thead>
  300. <tr>
  301. <th>Absolute Position</th>
  302. <th>Relative Position</th>
  303. <th>Value</th>
  304. </tr>
  305. </thead>
  306. <tbody>
  307. <tr>
  308. <td>1</td>
  309. <td>-5</td>
  310. <td><code>1338</code></td>
  311. </tr>
  312. <tr>
  313. <td>2</td>
  314. <td>-4</td>
  315. <td><code>13.37</code></td>
  316. </tr>
  317. <tr>
  318. <td>3</td>
  319. <td>-3</td>
  320. <td><code>false</code></td>
  321. </tr>
  322. <tr>
  323. <td>4</td>
  324. <td>-2</td>
  325. <td><code>"Hello World"</code></td>
  326. </tr>
  327. <tr>
  328. <td>5</td>
  329. <td>-1</td>
  330. <td><code>{one = 1, [1] = "one", nested = {more = "fields"}}</code></td>
  331. </tr>
  332. </tbody>
  333. </table>
  334. <p>It is possible to provide a template parameter to <code>push</code> to enforce pushing a specific type.
  335. In most cases you are probably better off by letting the compiler infer the template parameter.</p>
  336. <h2 id="reading-lua-values">Reading Lua values</h2>
  337. <p>Simple retrieval of Lua values is done using <a href="../reference/namespaceluwra.html#a4fe4e574680cf54a0f8d958740eb90ab">read&lt;T&gt;</a>. Consider the stack layout
  338. from the previous example. This is how you would retrieve a value from the stack.</p>
  339. <pre><code class="c++">// Retrieve the integer at position 1
  340. int value = luwra::read&lt;int&gt;(lua, 1);
  341. // Similiar with a relative index
  342. int value = luwra::read&lt;int&gt;(lua, -5);
  343. </code></pre>
  344. <h2 id="read-and-type-errors">Read and type errors</h2>
  345. <p>What happens when a value mismatches the expected type or cannot be converted to it? Most
  346. <a href="../reference/structluwra_1_1Value.html">Value</a> specializations use Lua's <code>luaL_check*</code> functions to retrieve the values from
  347. the stack. This means that no exceptions will be thrown - instead the error handling is delegated to
  348. 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
  349. information.</p>
  350. </div>
  351. </div>
  352. <footer>
  353. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  354. <a href="../advanced/" class="btn btn-neutral float-right" title="Advanced Stack Interaction">Next <span class="icon icon-circle-arrow-right"></span></a>
  355. <a href=".." class="btn btn-neutral" title="Home"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  356. </div>
  357. <hr/>
  358. <div role="contentinfo">
  359. <!-- Copyright etc -->
  360. </div>
  361. 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>.
  362. </footer>
  363. </div>
  364. </div>
  365. </section>
  366. </div>
  367. <div class="rst-versions" role="note" style="cursor: pointer">
  368. <span class="rst-current-version" data-toggle="rst-current-version">
  369. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  370. <span><a href=".." style="color: #fcfcfc;">&laquo; Previous</a></span>
  371. <span style="margin-left: 15px"><a href="../advanced/" style="color: #fcfcfc">Next &raquo;</a></span>
  372. </span>
  373. </div>
  374. </body>
  375. </html>