index.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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>Function Wrapping - 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 = "Function Wrapping";
  17. var mkdocs_page_input_path = "wrapping.md";
  18. var mkdocs_page_url = "/wrapping/";
  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 current">
  55. <a class="current" href="./">Function Wrapping</a>
  56. <ul>
  57. <li class="toctree-l3"><a href="#general">General</a></li>
  58. <li><a class="toctree-l4" href="#wrap-cc-functions">Wrap C/C++ functions</a></li>
  59. </ul>
  60. </li>
  61. <li>
  62. </ul>
  63. </div>
  64. &nbsp;
  65. </nav>
  66. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  67. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  68. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  69. <a href="..">Luwra</a>
  70. </nav>
  71. <div class="wy-nav-content">
  72. <div class="rst-content">
  73. <div role="navigation" aria-label="breadcrumbs navigation">
  74. <ul class="wy-breadcrumbs">
  75. <li><a href="..">Docs</a> &raquo;</li>
  76. <li>Function Wrapping</li>
  77. <li class="wy-breadcrumbs-aside">
  78. <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
  79. </li>
  80. </ul>
  81. <hr/>
  82. </div>
  83. <div role="main">
  84. <div class="section">
  85. <h1 id="general">General</h1>
  86. <p>Luwra provides an easy way to turn any C or C++ function into a
  87. <a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a> which can be used by the
  88. Lua VM. Note, all parameter types must be readable from the stack (<code>Value&lt;T&gt;::read</code> exists for all)
  89. and the return type must be pushable (<code>Value&lt;T&gt;::push</code> exists).</p>
  90. <h2 id="wrap-cc-functions">Wrap C/C++ functions</h2>
  91. <p>Assuming you have a function similiar to this:</p>
  92. <pre><code class="c++">int my_function(const char* a, int b) {
  93. return /* magic */;
  94. }
  95. </code></pre>
  96. <p>You can easily wrap it using the <code>LUWRA_WRAP</code> macro:</p>
  97. <pre><code class="c++">// Convert to lua_CFunction
  98. lua_CFunction cfun = LUWRA_WRAP(my_function);
  99. // Do something with it, for example set it as a Lua global function
  100. luwra::setGlobal(lua, &quot;my_function&quot;, cfun);
  101. </code></pre>
  102. <p><strong>Note:</strong> Do not provide the address of your function (e.g. <code>&amp;my_function</code>) to any wrapping macro.
  103. The macros will take care of this themselves. You must provide only the name of the function.</p>
  104. <p>Calling the function from Lua is fairly straightforward:</p>
  105. <pre><code class="lua">local my_result = my_function(&quot;Hello World&quot;, 1337)
  106. print(my_result)
  107. </code></pre>
  108. </div>
  109. </div>
  110. <footer>
  111. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  112. <a href="../advanced/" class="btn btn-neutral" title="Advanced Stack Interaction"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  113. </div>
  114. <hr/>
  115. <div role="contentinfo">
  116. <!-- Copyright etc -->
  117. </div>
  118. 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>.
  119. </footer>
  120. </div>
  121. </div>
  122. </section>
  123. </div>
  124. <div class="rst-versions" role="note" style="cursor: pointer">
  125. <span class="rst-current-version" data-toggle="rst-current-version">
  126. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  127. <span><a href="../advanced/" style="color: #fcfcfc;">&laquo; Previous</a></span>
  128. </span>
  129. </div>
  130. </body>
  131. </html>