index.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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>Usage - 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 = "Usage";
  17. var mkdocs_page_input_path = "usage.md";
  18. var mkdocs_page_url = "/usage/";
  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="./">Usage</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 class="toctree-l3"><a href="#pushing-c-values">Pushing C++ values</a></li>
  50. <li class="toctree-l3"><a href="#reading-lua-values">Reading Lua values</a></li>
  51. <li class="toctree-l3"><a href="#read-and-type-errors">Read and type errors</a></li>
  52. </ul>
  53. </li>
  54. <li>
  55. </ul>
  56. </div>
  57. &nbsp;
  58. </nav>
  59. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  60. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  61. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  62. <a href="..">Luwra</a>
  63. </nav>
  64. <div class="wy-nav-content">
  65. <div class="rst-content">
  66. <div role="navigation" aria-label="breadcrumbs navigation">
  67. <ul class="wy-breadcrumbs">
  68. <li><a href="..">Docs</a> &raquo;</li>
  69. <li>Usage</li>
  70. <li class="wy-breadcrumbs-aside">
  71. <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
  72. </li>
  73. </ul>
  74. <hr/>
  75. </div>
  76. <div role="main">
  77. <div class="section">
  78. <h1 id="integration">Integration</h1>
  79. <p>Luwra does not provide a standalone version of Lua nor does it capsule its features. This means that
  80. all functions and classes operate on
  81. <a href="http://www.lua.org/manual/5.3/manual.html#lua_State">lua_State</a> (or the alias
  82. <a href="../reference/namespaceluwra.html#a2c037b44385367826eb4e931b5b8197d">State</a>). Doing this allows you to
  83. integrate Luwra however you like.</p>
  84. <h1 id="stack-interaction">Stack Interaction</h1>
  85. <p>Although Luwra provides a variety of features, its main concern is efficient and safe interaction
  86. with the Lua stack.</p>
  87. <p>A fundamental aspect of this is the abstract template <a href="../reference/structluwra_1_1Value.html">Value</a>.
  88. Every type which can be pushed onto or read from the stack has a specialization of it.
  89. Useful implementations are provides out of the box:</p>
  90. <table>
  91. <thead>
  92. <tr>
  93. <th>C++ type</th>
  94. <th>Pushable</th>
  95. <th>Readable</th>
  96. <th>Lua type</th>
  97. </tr>
  98. </thead>
  99. <tbody>
  100. <tr>
  101. <td>bool</td>
  102. <td>yes</td>
  103. <td>yes</td>
  104. <td>boolean</td>
  105. </tr>
  106. <tr>
  107. <td>signed char</td>
  108. <td>yes</td>
  109. <td>yes</td>
  110. <td>number (integer since 5.3)</td>
  111. </tr>
  112. <tr>
  113. <td>signed short</td>
  114. <td>yes</td>
  115. <td>yes</td>
  116. <td>number (integer since 5.3)</td>
  117. </tr>
  118. <tr>
  119. <td>signed int</td>
  120. <td>yes</td>
  121. <td>yes</td>
  122. <td>number (integer since 5.3)</td>
  123. </tr>
  124. <tr>
  125. <td>signed long int</td>
  126. <td>yes</td>
  127. <td>yes</td>
  128. <td>number (integer since 5.3)</td>
  129. </tr>
  130. <tr>
  131. <td>signed long long int</td>
  132. <td>yes</td>
  133. <td>yes</td>
  134. <td>number (integer since 5.3)</td>
  135. </tr>
  136. <tr>
  137. <td>unsigned char</td>
  138. <td>yes</td>
  139. <td>yes</td>
  140. <td>number (integer since 5.3)</td>
  141. </tr>
  142. <tr>
  143. <td>unsigned short</td>
  144. <td>yes</td>
  145. <td>yes</td>
  146. <td>number (integer since 5.3)</td>
  147. </tr>
  148. <tr>
  149. <td>unsigned int</td>
  150. <td>yes</td>
  151. <td>yes</td>
  152. <td>number (integer since 5.3)</td>
  153. </tr>
  154. <tr>
  155. <td>unsigned long int</td>
  156. <td>yes</td>
  157. <td>yes</td>
  158. <td>number (integer since 5.3)</td>
  159. </tr>
  160. <tr>
  161. <td>unsigned long long int</td>
  162. <td>yes</td>
  163. <td>yes</td>
  164. <td>number (integer since 5.3)</td>
  165. </tr>
  166. <tr>
  167. <td>float</td>
  168. <td>yes</td>
  169. <td>yes</td>
  170. <td>number</td>
  171. </tr>
  172. <tr>
  173. <td>double</td>
  174. <td>yes</td>
  175. <td>yes</td>
  176. <td>number</td>
  177. </tr>
  178. <tr>
  179. <td>long double</td>
  180. <td>yes</td>
  181. <td>yes</td>
  182. <td>number</td>
  183. </tr>
  184. <tr>
  185. <td>const char*</td>
  186. <td>yes</td>
  187. <td>yes</td>
  188. <td>string</td>
  189. </tr>
  190. <tr>
  191. <td>std::string</td>
  192. <td>yes</td>
  193. <td>yes</td>
  194. <td>string</td>
  195. </tr>
  196. <tr>
  197. <td>std::nullptr_t</td>
  198. <td>yes</td>
  199. <td>yes</td>
  200. <td>nil</td>
  201. </tr>
  202. <tr>
  203. <td>std::tuple&lt;T&gt;</td>
  204. <td>yes</td>
  205. <td>no</td>
  206. <td><em>depends on the tuple contents</em></td>
  207. </tr>
  208. <tr>
  209. <td><a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a></td>
  210. <td>yes</td>
  211. <td>no</td>
  212. <td>function</td>
  213. </tr>
  214. <tr>
  215. <td><a href="../reference/structluwra_1_1NativeFunction_3_01R_07A_8_8_8_08_4.html">NativeFunction&lt;R(A...)&gt;</a></td>
  216. <td>no</td>
  217. <td>yes</td>
  218. <td>function</td>
  219. </tr>
  220. <tr>
  221. <td><a href="../reference/namespaceluwra.html#ac090722c6d5d6b88b31895aad64788c2">FieldVector</a></td>
  222. <td>yes</td>
  223. <td>no</td>
  224. <td>table</td>
  225. </tr>
  226. </tbody>
  227. </table>
  228. <p><strong>Note:</strong> Some numeric types have a different size than their matching Lua type - they will be
  229. truncated during push or read operations.</p>
  230. <h1 id="pushing-c-values">Pushing C++ values</h1>
  231. <p>When pushing values onto the stack you can either use
  232. <a href="../reference/structluwra_1_1Value.html#aa376d68285606c206562b822e8187384">Value&lt;T&gt;::push</a> or the more
  233. convenient <a href="../reference/namespaceluwra.html#ae8e7eab11fc2cf3f258ffd81571066fa">push</a>.</p>
  234. <pre><code class="c++">// Push an integer
  235. luwra::push(lua, 1338);
  236. // Push a number
  237. luwra::push(lua, 13.37);
  238. // Push a boolean
  239. luwra::push(lua, false);
  240. // Push a string
  241. luwra::push(lua, &quot;Hello World&quot;);
  242. </code></pre>
  243. <p>This produces the following stack layout:</p>
  244. <table>
  245. <thead>
  246. <tr>
  247. <th>Absolute Position</th>
  248. <th>Relative Position</th>
  249. <th>Value</th>
  250. </tr>
  251. </thead>
  252. <tbody>
  253. <tr>
  254. <td>1</td>
  255. <td>-4</td>
  256. <td><code>1338</code></td>
  257. </tr>
  258. <tr>
  259. <td>2</td>
  260. <td>-3</td>
  261. <td><code>13.37</code></td>
  262. </tr>
  263. <tr>
  264. <td>3</td>
  265. <td>-2</td>
  266. <td><code>false</code></td>
  267. </tr>
  268. <tr>
  269. <td>4</td>
  270. <td>-1</td>
  271. <td><code>"Hello World"</code></td>
  272. </tr>
  273. </tbody>
  274. </table>
  275. <p>It is possible to provide a template parameter to <code>push</code> to enforce pushing a specific type.
  276. In most cases you are probably better off by letting the compiler infer the template parameter.</p>
  277. <h1 id="reading-lua-values">Reading Lua values</h1>
  278. <p>Simple retrieval of Lua values is done using
  279. <a href="../reference/namespaceluwra.html#a4fe4e574680cf54a0f8d958740eb90ab">read&lt;T&gt;</a>. Consider the
  280. stack layout from the previous example. This is how you would retrieve a value from the stack.</p>
  281. <pre><code class="c++">// Retrieve the integer at position 1
  282. int value = luwra::read&lt;int&gt;(lua, 1);
  283. // Similiar with a relative index
  284. int value = luwra::read&lt;int&gt;(lua, -4);
  285. </code></pre>
  286. <h1 id="read-and-type-errors">Read and type errors</h1>
  287. <p>What happens when a value which you are trying to read mismatches the expected type or cannot be
  288. converted to it? Most <code>Value&lt;T&gt;</code> specializations use Lua's <code>luaL_check*</code> functions to retrieve
  289. the values from the stack. This means that no exceptions will be thrown - instead the error handling
  290. is delegated to the Lua VM. Have a look at the
  291. <a href="http://www.lua.org/manual/5.3/manual.html#4.6">error handling documentation</a> for more information.</p>
  292. </div>
  293. </div>
  294. <footer>
  295. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  296. <a href=".." class="btn btn-neutral" title="Home"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  297. </div>
  298. <hr/>
  299. <div role="contentinfo">
  300. <!-- Copyright etc -->
  301. </div>
  302. 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>.
  303. </footer>
  304. </div>
  305. </div>
  306. </section>
  307. </div>
  308. <div class="rst-versions" role="note" style="cursor: pointer">
  309. <span class="rst-current-version" data-toggle="rst-current-version">
  310. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  311. <span><a href=".." style="color: #fcfcfc;">&laquo; Previous</a></span>
  312. </span>
  313. </div>
  314. </body>
  315. </html>