index.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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>User Types - 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 = "User Types";
  17. var mkdocs_page_input_path = "user-types.md";
  18. var mkdocs_page_url = "/user-types/";
  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 ">
  45. <a class="" href="../basics/">Basic Stack Interaction</a>
  46. </li>
  47. <li>
  48. <li>
  49. <li class="toctree-l1 ">
  50. <a class="" href="../advanced/">Advanced Stack Interaction</a>
  51. </li>
  52. <li>
  53. <li>
  54. <li class="toctree-l1 ">
  55. <a class="" href="../wrapping/">Function Wrapping</a>
  56. </li>
  57. <li>
  58. <li>
  59. <li class="toctree-l1 current">
  60. <a class="current" href="./">User Types</a>
  61. <ul>
  62. <li class="toctree-l3"><a href="#user-types">User Types</a></li>
  63. <li><a class="toctree-l4" href="#register-user-type-with-constructor">Register user type with constructor</a></li>
  64. <li><a class="toctree-l4" href="#register-user-type-without-constructor">Register user type without constructor</a></li>
  65. <li><a class="toctree-l4" href="#usage-in-lua">Usage in Lua</a></li>
  66. <li><a class="toctree-l4" href="#manually-constructing-a-user-type">Manually constructing a user type</a></li>
  67. </ul>
  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>User Types</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="user-types">User Types</h1>
  94. <p>A user type is a collection of class members bundled into a metatable. Before user types can be used
  95. in Lua, you must register such metatable in Lua's registry.</p>
  96. <p>The following examples work on this class:</p>
  97. <pre><code class="c++">struct Point {
  98. double x, y;
  99. Point(double x, double y):
  100. x(x), y(y)
  101. {
  102. std::cout &lt;&lt; &quot;Construct Point(&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
  103. }
  104. ~Point() {
  105. std::cout &lt;&lt; &quot;Destruct Point(&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;
  106. }
  107. void scale(double f) {
  108. x *= f;
  109. y *= f;
  110. }
  111. std::string __tostring() {
  112. return &quot;&lt;Point(&quot; + std::to_string(x) + &quot;, &quot; + std::to_string(y) + &quot;)&gt;&quot;;
  113. }
  114. };
  115. </code></pre>
  116. <h2 id="register-user-type-with-constructor">Register user type with constructor</h2>
  117. <p><a href="../reference/namespaceluwra.html#aae6f45ae03c3bd91321ea19f794cae18">registerUserType&lt;S&gt;</a> allows
  118. you to register a metatable and constructor in the global namespace. The template parameter to
  119. <code>registerUserType</code> is a signature in the form of <code>U(A...)</code> where <code>U</code> is your user type and <code>A...</code>
  120. the parameter types to the constructor which you want to register.</p>
  121. <p>By default, the function generates a garbage-collector hook and a string representation function.
  122. If you add a <code>__gc</code> or <code>__tostring</code> meta method to your type, these auto-generated functions will be
  123. overridden.</p>
  124. <p>See this example:</p>
  125. <pre><code class="c++">luwra::registerUserType&lt;Point(double, double)&gt;(
  126. lua,
  127. // Constructor name
  128. &quot;Point&quot;,
  129. // Methods need to be declared here
  130. {
  131. LUWRA_MEMBER(Point, scale),
  132. LUWRA_MEMBER(Point, x),
  133. LUWRA_MEMBER(Point, y)
  134. },
  135. // Meta methods may be registered aswell
  136. {
  137. LUWRA_MEMBER(Point, __tostring)
  138. }
  139. );
  140. </code></pre>
  141. <p>Parameter 3 and 4 are instances of
  142. <a href="../reference/namespaceluwra.html#a3fbba1f4a5025647470d5942ededa5aa">MemberMap</a>. The <code>LUWRA_MEMBER</code>
  143. macro generates a <code>std::pair&lt;std::string, Pushable&gt;</code> expression.</p>
  144. <pre><code class="c++">LUWRA_MEMBER(Point, scale) === {&quot;scale&quot;, LUWRA_WRAP(Point::scale)}
  145. </code></pre>
  146. <p><code>Pushable</code> has an implicit constructor, which makes it convenient to add other types of fields:</p>
  147. <pre><code class="c++">luwra::registerUserType&lt;Point(double, double)&gt;(
  148. lua,
  149. // Constructor name
  150. &quot;Point&quot;,
  151. // Methods need to be declared here
  152. {
  153. {&quot;scale&quot;, LUWRA_WRAP(Point::scale)},
  154. {&quot;x&quot;, LUWRA_WRAP(Point::x)},
  155. {&quot;y&quot;, LUWRA_WRAP(Point::y)},
  156. {&quot;magicNumber&quot;, 1337},
  157. {&quot;magicString&quot;, &quot;Hello World&quot;}
  158. },
  159. // Meta methods may be registered aswell
  160. {
  161. LUWRA_MEMBER(Point, __tostring)
  162. }
  163. );
  164. </code></pre>
  165. <h2 id="register-user-type-without-constructor">Register user type without constructor</h2>
  166. <p>To register only the metatable associated with a user type, simply omit the constructor parameters
  167. and name from the call to <code>registerUserType</code>.</p>
  168. <pre><code class="c++">luwra::registerUserType&lt;Point&gt;(
  169. lua,
  170. // Methods need to be declared here
  171. {
  172. LUWRA_MEMBER(Point, scale),
  173. LUWRA_MEMBER(Point, x),
  174. LUWRA_MEMBER(Point, y),
  175. {&quot;magicNumber&quot;, 1337}
  176. },
  177. // Meta methods may be registered aswell
  178. {
  179. LUWRA_MEMBER(Point, __tostring)
  180. }
  181. );
  182. </code></pre>
  183. <p>It is still possible to provide a constructor using the <code>LUWRA_WRAP_CONSTRUCTOR</code> macro:</p>
  184. <pre><code class="c++">lua_CFunction ctor = LUWRA_WRAP_CONSTRUCTOR(Point, double, double);
  185. luwra::setGlobal(lua, &quot;Point&quot;, ctor);
  186. </code></pre>
  187. <h2 id="usage-in-lua">Usage in Lua</h2>
  188. <p>After you have registered your user type using one of the given methods, you can start using it in
  189. Lua:</p>
  190. <pre><code class="lua">-- Instantiate 'Point'
  191. local point = Point(13, 37)
  192. -- Invoke 'scale' method
  193. point:scale(1.5)
  194. -- Convert to string via '__tostring' meta method
  195. print(point)
  196. -- Read properties 'x' and 'y'
  197. print(point:x(), point:y())
  198. -- Set property 'x'
  199. point:x(point.magicNumber)
  200. </code></pre>
  201. <h2 id="manually-constructing-a-user-type">Manually constructing a user type</h2>
  202. <p>Provided you already registered your user type, one can create it from the C++ side aswell.
  203. <a href="../reference/namespaceluwra.html#af079dcca8e67d88e5cfdc7e8872cf5d7">construct&lt;U&gt;</a> provides this
  204. functionality. Given the user type and constructor parameters, it will construct the user type on
  205. top of the stack:</p>
  206. <pre><code class="c++">Point&amp; my_point = luwra::construct&lt;Point&gt;(lua, 13.37, 73.31);
  207. // Changes on C++ side will be visible in Lua
  208. my_point.scale(2);
  209. </code></pre>
  210. </div>
  211. </div>
  212. <footer>
  213. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  214. <a href="../wrapping/" class="btn btn-neutral" title="Function Wrapping"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  215. </div>
  216. <hr/>
  217. <div role="contentinfo">
  218. <!-- Copyright etc -->
  219. </div>
  220. 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>.
  221. </footer>
  222. </div>
  223. </div>
  224. </section>
  225. </div>
  226. <div class="rst-versions" role="note" style="cursor: pointer">
  227. <span class="rst-current-version" data-toggle="rst-current-version">
  228. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  229. <span><a href="../wrapping/" style="color: #fcfcfc;">&laquo; Previous</a></span>
  230. </span>
  231. </div>
  232. </body>
  233. </html>