| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <!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="../stack-interaction/">Stack Interaction</a>
-
- </li>
- <li>
-
- <li>
- <li class="toctree-l1 ">
- <a class="" href="../advanced-stack-interaction/">Advanced Stack Interaction</a>
-
- </li>
- <li>
-
- <li>
- <li class="toctree-l1 ">
- <a class="" href="../wrapping/">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>
-
- <li><a class="toctree-l4" href="#register-user-type-with-constructor">Register user type with constructor</a></li>
-
- <li><a class="toctree-l4" href="#register-user-type-without-constructor">Register user type without constructor</a></li>
-
- <li><a class="toctree-l4" href="#usage-in-lua">Usage in Lua</a></li>
-
- <li><a class="toctree-l4" href="#manually-constructing-a-user-type">Manually constructing a user type</a></li>
-
- <li><a class="toctree-l4" href="#registry-names">Registry names</a></li>
-
-
- </ul>
-
- </li>
- <li>
-
- </ul>
- </div>
-
- </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> »</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>
- <p>A user type is a collection of class members bundled into a metatable. Before user types can be used
- in Lua, you must register such metatable in Lua's registry.</p>
- <p>The following examples work on this class:</p>
- <pre><code class="c++">struct Point {
- double x, y;
- Point(double x, double y):
- x(x), y(y)
- {
- std::cout << "Construct Point(" << x << ", " << y << ")" << std::endl;
- }
- ~Point() {
- std::cout << "Destruct Point(" << x << ", " << y << ")" << std::endl;
- }
- void scale(double f) {
- x *= f;
- y *= f;
- }
- std::string __tostring() {
- return "<Point(" + std::to_string(x) + ", " + std::to_string(y) + ")>";
- }
- };
- </code></pre>
- <h2 id="register-user-type-with-constructor">Register user type with constructor</h2>
- <p><a href="../reference/namespaceluwra.html#a683f6075862f8fc5cad0d76445f2f607">registerUserType<S></a> allows
- you to register a metatable and constructor in the global namespace. The template parameter to
- <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>
- the parameter types to the constructor which you want to register.</p>
- <p>By default, the function generates a garbage-collector hook and a string representation function.
- If you add a <code>__gc</code> or <code>__tostring</code> meta method to your type, these auto-generated functions will be
- overridden.</p>
- <p>See this example:</p>
- <pre><code class="c++">luwra::registerUserType<Point(double, double)>(
- lua,
- // Constructor name
- "Point",
- // Methods need to be declared here
- {
- LUWRA_MEMBER(Point, scale),
- LUWRA_MEMBER(Point, x),
- LUWRA_MEMBER(Point, y)
- },
- // Meta methods may be registered aswell
- {
- LUWRA_MEMBER(Point, __tostring)
- }
- );
- </code></pre>
- <p>Parameter 3 and 4 are instances of
- <a href="../reference/namespaceluwra.html#ac090722c6d5d6b88b31895aad64788c2">FieldVector</a>. The <code>LUWRA_MEMBER</code> macro
- generates a <code>std::pair<Pushable, Pushable></code> expression.</p>
- <pre><code class="c++">LUWRA_MEMBER(Point, scale) === {"scale", LUWRA_WRAP(Point::scale)}
- </code></pre>
- <p><code>Pushable</code> has an implicit constructor, which makes it convenient to add other types of fields:</p>
- <pre><code class="c++">luwra::registerUserType<Point(double, double)>(
- lua,
- // Constructor name
- "Point",
- // Methods need to be declared here
- {
- {"scale", LUWRA_WRAP(Point::scale)},
- {"x", LUWRA_WRAP(Point::x)},
- {"y", LUWRA_WRAP(Point::y)},
- {"magic", luwra::FieldVector {
- {"number", 1337},
- {"string", "Hello World"}
- }}
- },
- // Meta methods may be registered aswell
- {
- {"__tostring", LUWRA_WRAP(Point::__tostring)}
- }
- );
- </code></pre>
- <h2 id="register-user-type-without-constructor">Register user type without constructor</h2>
- <p>To register only the metatable associated with a user type, simply omit the constructor parameters
- and name from the call to <code>registerUserType</code>.</p>
- <pre><code class="c++">luwra::registerUserType<Point>(
- lua,
- // Methods need to be declared here
- {
- {"scale", LUWRA_WRAP(Point::scale)},
- {"x", LUWRA_WRAP(Point::x)},
- {"y", LUWRA_WRAP(Point::y)},
- {"magic", luwra::FieldVector {
- {"number", 1337},
- {"string", "Hello World"}
- }}
- },
- // Meta methods may be registered aswell
- {
- {"__tostring", LUWRA_WRAP(Point::__tostring)}
- }
- );
- </code></pre>
- <p>It is still possible to provide a constructor using the <code>LUWRA_WRAP_CONSTRUCTOR</code> macro:</p>
- <pre><code class="c++">lua_CFunction ctor = LUWRA_WRAP_CONSTRUCTOR(Point, double, double);
- luwra::setGlobal(lua, "Point", ctor);
- </code></pre>
- <h2 id="usage-in-lua">Usage in Lua</h2>
- <p>After you have registered your user type using one of the given methods, you can start using it in
- Lua:</p>
- <pre><code class="lua">-- Instantiate 'Point'
- local point = Point(13, 37)
- -- Invoke 'scale' method
- point:scale(1.5)
- -- Convert to string via '__tostring' meta method
- print(point)
- -- Read properties 'x' and 'y'
- print(point:x(), point:y())
- -- Set property 'x'
- point:x(point.magic.number)
- </code></pre>
- <h2 id="manually-constructing-a-user-type">Manually constructing a user type</h2>
- <p>Provided you already registered your user type, one can create it from the C++ side aswell.
- <a href="../reference/namespaceluwra.html#af079dcca8e67d88e5cfdc7e8872cf5d7">construct<U></a> provides this
- functionality. Given the user type and constructor parameters, it will construct the user type on
- top of the stack:</p>
- <pre><code class="c++">Point& my_point = luwra::construct<Point>(lua, 13.37, 73.31);
- // Changes on C++ side will be visible in Lua
- my_point.scale(2);
- </code></pre>
- <h2 id="registry-names">Registry names</h2>
- <p>When registering the metatable for a user type, an automatically generated name will be used to
- store it in the registry. When Luwra is used in a single executable or shared library, name
- collisions should not happen. If your application consists of multiple seperate compiled units, it
- is highly recommended to prevent name collisions by defining the <code>LUWRA_REGISTRY_PREFIX</code> macro
- before including the Luwra headers. This macro changes the prefix for auto-generated registry names.</p>
- <pre><code class="c++">#define LUWRA_REGISTRY_PREFIX "MyProject#"
- #include <luwra.hpp>
- </code></pre>
- <p>Another way to prevent collisons is to give each user type its individual registry name. This can be
- done using the <code>LUWRA_DEF_REGISTRY_NAME</code> macro.</p>
- <pre><code class="c++">struct MyUserType {
- // ...
- };
- LUWRA_DEF_REGISTRY_NAME(MyUserType, "MyUserType")
- </code></pre>
- <p>Choosing this method will not prefix the registry name with the value of <code>LUWRA_REGISTRY_PREFIX</code>.</p>
-
- </div>
- </div>
- <footer>
-
- <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
-
-
- <a href="../wrapping/" class="btn btn-neutral" title="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;">« Previous</a></span>
-
-
- </span>
- </div>
- </body>
- </html>
|