瀏覽代碼

docs: Complete function wrapping section

Ole 9 年之前
父節點
當前提交
c1d2acf931

+ 1 - 0
docs/mkdocs/user-types.md

@@ -0,0 +1 @@
+# User Types

+ 52 - 1
docs/mkdocs/wrapping.md

@@ -4,7 +4,7 @@ Luwra provides an easy way to turn any C or C++ function into a
 Lua VM. Note, all parameter types must be readable from the stack (`Value<T>::read` exists for all)
 and the return type must be pushable (`Value<T>::push` exists).
 
-## Wrap C/C++ functions
+## Wrap functions
 Assuming you have a function similiar to this:
 
 ```c++
@@ -32,3 +32,54 @@ Calling the function from Lua is fairly straightforward:
 local my_result = my_function("Hello World", 1337)
 print(my_result)
 ```
+## Wrap methods and fields
+It is also possible to turn C++ field accessors and methods into `lua_CFunction`s. It is a little
+trickier than wrapping normal functions. The resulting Lua functions expect the first (or `self`)
+parameter to be a user type instance of the type which the wrapped field or method belongs to.
+
+**Note:** Before you wrap fields and methods manually, you might want to take a look at the
+[User Types](/user-types/) section.
+
+The next examples will operate on the following structure:
+
+```c++
+struct Point {
+    double x, y;
+
+    // ...
+
+    void scale(double f) {
+        x *= f;
+        y *= f;
+    }
+};
+```
+
+In order to wrap `x`, `y` and `scale` we utilize the `LUWRA_WRAP` macro again:
+
+```c++
+lua_CFunction cfun_x     = LUWRA_WRAP(Point::x),
+              cfun_y     = LUWRA_WRAP(Point::y),
+              cfun_scale = LUWRA_WRAP(Point::scale);
+
+// Register as globals
+luwra::setGlobal(lua, "x", cfun_x);
+luwra::setGlobal(lua, "y", cfun_y);
+luwra::setGlobal(lua, "scale", cfun_scale);
+```
+
+Usage looks like this:
+
+```lua
+local my_point = -- Magic
+
+-- Access 'x' and 'y' field
+print(x(my_point), y(my_point))
+
+-- Set 'x' and 'y' field
+x(my_point, 13.37)
+y(my_point, 73.31)
+
+-- Invoke 'scale' method
+scale(my_point, 2)
+```

+ 7 - 0
docs/output/advanced/index.html

@@ -90,6 +90,13 @@
     <li class="toctree-l1 ">
         <a class="" href="../wrapping/">Function Wrapping</a>
         
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../user-types/">User Types</a>
+        
     </li>
 <li>
           

+ 7 - 0
docs/output/basics/index.html

@@ -95,6 +95,13 @@
     <li class="toctree-l1 ">
         <a class="" href="../wrapping/">Function Wrapping</a>
         
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../user-types/">User Types</a>
+        
     </li>
 <li>
           

+ 8 - 1
docs/output/index.html

@@ -89,6 +89,13 @@
     <li class="toctree-l1 ">
         <a class="" href="wrapping/">Function Wrapping</a>
         
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="user-types/">User Types</a>
+        
     </li>
 <li>
           
@@ -186,5 +193,5 @@ your preference.</p>
 
 <!--
 MkDocs version : 0.15.3
-Build Date UTC : 2016-04-19 14:10:00.197827
+Build Date UTC : 2016-04-19 14:40:21.348085
 -->

File diff suppressed because it is too large
+ 0 - 0
docs/output/mkdocs/search_index.json


+ 7 - 0
docs/output/search.html

@@ -75,6 +75,13 @@
     <li class="toctree-l1 ">
         <a class="" href="wrapping/">Function Wrapping</a>
         
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="user-types/">User Types</a>
+        
     </li>
 <li>
           

+ 8 - 0
docs/output/sitemap.xml

@@ -33,4 +33,12 @@
     </url>
     
 
+    
+    <url>
+     <loc>None/user-types/</loc>
+     <lastmod>2016-04-19</lastmod>
+     <changefreq>daily</changefreq>
+    </url>
+    
+
 </urlset>

+ 179 - 0
docs/output/user-types/index.html

@@ -0,0 +1,179 @@
+<!DOCTYPE html>
+<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
+<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
+<head>
+  <meta charset="utf-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  
+  
+  
+  <title>User Types - Luwra</title>
+  
+
+  <link rel="shortcut icon" href="../img/favicon.ico">
+
+  
+  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
+
+  <link rel="stylesheet" href="../css/theme.css" type="text/css" />
+  <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
+  <link rel="stylesheet" href="../css/highlight.css">
+
+  
+  <script>
+    // Current page data
+    var mkdocs_page_name = "User Types";
+    var mkdocs_page_input_path = "user-types.md";
+    var mkdocs_page_url = "/user-types/";
+  </script>
+  
+  <script src="../js/jquery-2.1.1.min.js"></script>
+  <script src="../js/modernizr-2.8.3.min.js"></script>
+  <script type="text/javascript" src="../js/highlight.pack.js"></script>
+  <script src="../js/theme.js"></script> 
+
+  
+</head>
+
+<body class="wy-body-for-nav" role="document">
+
+  <div class="wy-grid-for-nav">
+
+    
+    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
+      <div class="wy-side-nav-search">
+        <a href=".." class="icon icon-home"> Luwra</a>
+        <div role="search">
+  <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
+    <input type="text" name="q" placeholder="Search docs" />
+  </form>
+</div>
+      </div>
+
+      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
+        <ul class="current">
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="..">Home</a>
+        
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../basics/">Basic Stack Interaction</a>
+        
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../advanced/">Advanced Stack Interaction</a>
+        
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../wrapping/">Function Wrapping</a>
+        
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 current">
+        <a class="current" href="./">User Types</a>
+        
+            <ul>
+            
+                <li class="toctree-l3"><a href="#user-types">User Types</a></li>
+                
+            
+            </ul>
+        
+    </li>
+<li>
+          
+        </ul>
+      </div>
+      &nbsp;
+    </nav>
+
+    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
+
+      
+      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
+        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
+        <a href="..">Luwra</a>
+      </nav>
+
+      
+      <div class="wy-nav-content">
+        <div class="rst-content">
+          <div role="navigation" aria-label="breadcrumbs navigation">
+  <ul class="wy-breadcrumbs">
+    <li><a href="..">Docs</a> &raquo;</li>
+    
+      
+    
+    <li>User Types</li>
+    <li class="wy-breadcrumbs-aside">
+      
+        
+          <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
+        
+      
+    </li>
+  </ul>
+  <hr/>
+</div>
+          <div role="main">
+            <div class="section">
+              
+                <h1 id="user-types">User Types</h1>
+              
+            </div>
+          </div>
+          <footer>
+  
+    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
+      
+      
+        <a href="../wrapping/" class="btn btn-neutral" title="Function Wrapping"><span class="icon icon-circle-arrow-left"></span> Previous</a>
+      
+    </div>
+  
+
+  <hr/>
+
+  <div role="contentinfo">
+    <!-- Copyright etc -->
+    
+  </div>
+
+  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>.
+</footer>
+	  
+        </div>
+      </div>
+
+    </section>
+
+  </div>
+
+<div class="rst-versions" role="note" style="cursor: pointer">
+    <span class="rst-current-version" data-toggle="rst-current-version">
+      
+          <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
+      
+      
+        <span><a href="../wrapping/" style="color: #fcfcfc;">&laquo; Previous</a></span>
+      
+      
+    </span>
+</div>
+
+</body>
+</html>

+ 59 - 2
docs/output/wrapping/index.html

@@ -83,11 +83,20 @@
             
                 <li class="toctree-l3"><a href="#general">General</a></li>
                 
-                    <li><a class="toctree-l4" href="#wrap-cc-functions">Wrap C/C++ functions</a></li>
+                    <li><a class="toctree-l4" href="#wrap-functions">Wrap functions</a></li>
+                
+                    <li><a class="toctree-l4" href="#wrap-methods-and-fields">Wrap methods and fields</a></li>
                 
             
             </ul>
         
+    </li>
+<li>
+          
+            <li>
+    <li class="toctree-l1 ">
+        <a class="" href="../user-types/">User Types</a>
+        
     </li>
 <li>
           
@@ -132,7 +141,7 @@
 <a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">lua_CFunction</a> which can be used by the
 Lua VM. Note, all parameter types must be readable from the stack (<code>Value&lt;T&gt;::read</code> exists for all)
 and the return type must be pushable (<code>Value&lt;T&gt;::push</code> exists).</p>
-<h2 id="wrap-cc-functions">Wrap C/C++ functions</h2>
+<h2 id="wrap-functions">Wrap functions</h2>
 <p>Assuming you have a function similiar to this:</p>
 <pre><code class="c++">int my_function(const char* a, int b) {
     return /* magic */;
@@ -152,6 +161,50 @@ The macros will take care of this themselves. You must provide only the name of
 <p>Calling the function from Lua is fairly straightforward:</p>
 <pre><code class="lua">local my_result = my_function(&quot;Hello World&quot;, 1337)
 print(my_result)
+</code></pre>
+
+<h2 id="wrap-methods-and-fields">Wrap methods and fields</h2>
+<p>It is also possible to turn C++ field accessors and methods into <code>lua_CFunction</code>s. It is a little
+trickier than wrapping normal functions. The resulting Lua functions expect the first (or <code>self</code>)
+parameter to be a user type instance of the type which the wrapped field or method belongs to.</p>
+<p><strong>Note:</strong> Before you wrap fields and methods manually, you might want to take a look at the
+<a href="../user-types/">User Types</a> section.</p>
+<p>The next examples will operate on the following structure:</p>
+<pre><code class="c++">struct Point {
+    double x, y;
+
+    // ...
+
+    void scale(double f) {
+        x *= f;
+        y *= f;
+    }
+};
+</code></pre>
+
+<p>In order to wrap <code>x</code>, <code>y</code> and <code>scale</code> we utilize the <code>LUWRA_WRAP</code> macro again:</p>
+<pre><code class="c++">lua_CFunction cfun_x     = LUWRA_WRAP(Point::x),
+              cfun_y     = LUWRA_WRAP(Point::y),
+              cfun_scale = LUWRA_WRAP(Point::scale);
+
+// Register as globals
+luwra::setGlobal(lua, &quot;x&quot;, cfun_x);
+luwra::setGlobal(lua, &quot;y&quot;, cfun_y);
+luwra::setGlobal(lua, &quot;scale&quot;, cfun_scale);
+</code></pre>
+
+<p>Usage looks like this:</p>
+<pre><code class="lua">local my_point = -- Magic
+
+-- Access 'x' and 'y' field
+print(x(my_point), y(my_point))
+
+-- Set 'x' and 'y' field
+x(my_point, 13.37)
+y(my_point, 73.31)
+
+-- Invoke 'scale' method
+scale(my_point, 2)
 </code></pre>
               
             </div>
@@ -160,6 +213,8 @@ print(my_result)
   
     <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
       
+        <a href="../user-types/" class="btn btn-neutral float-right" title="User Types">Next <span class="icon icon-circle-arrow-right"></span></a>
+      
       
         <a href="../advanced/" class="btn btn-neutral" title="Advanced Stack Interaction"><span class="icon icon-circle-arrow-left"></span> Previous</a>
       
@@ -192,6 +247,8 @@ print(my_result)
         <span><a href="../advanced/" style="color: #fcfcfc;">&laquo; Previous</a></span>
       
       
+        <span style="margin-left: 15px"><a href="../user-types/" style="color: #fcfcfc">Next &raquo;</a></span>
+      
     </span>
 </div>
 

+ 1 - 0
mkdocs.yml

@@ -6,6 +6,7 @@ pages:
   - 'Basic Stack Interaction': basics.md
   - 'Advanced Stack Interaction': advanced.md
   - 'Function Wrapping': wrapping.md
+  - 'User Types': user-types.md
 theme: readthedocs
 docs_dir: docs/mkdocs
 site_dir: docs/output

Some files were not shown because too many files changed in this diff