docs.html.svn-base

来自「本人找过多个在linux下c++的lua5.1封装库,但很少.luabind已经」· SVN-BASE 代码 · 共 1,535 行 · 第 1/5 页

SVN-BASE
1,535
字号
<body><div class="document" id="luabind"><h1 class="title">luabind</h1><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Author:</th><td>Daniel Wallin, Arvid Norberg</td></tr><tr><th class="docinfo-name">Copyright:</th><td>Copyright Daniel Wallin, Arvid Norberg 2003.</td></tr><tr><th class="docinfo-name">Date:</th><td>2006-03-26</td></tr><tr><th class="docinfo-name">Revision:</th><td>1.28</td></tr><tr class="field"><th class="docinfo-name">License:</th><td class="field-body"><p class="first">Permission is hereby granted, free of charge, to any person obtaining acopy of this software and associated documentation files (the &quot;Software&quot;),to deal in the Software without restriction, including without limitationthe rights to use, copy, modify, merge, publish, distribute, sublicense,and/or sell copies of the Software, and to permit persons to whom theSoftware is furnished to do so, subject to the following conditions:</p><p>The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.</p><p class="last">THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OFANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITEDTO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR APARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENTSHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FORANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN ANACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USEOR OTHER DEALINGS IN THE SOFTWARE.</p></td></tr></tbody></table><p>Note: This library is currently in public beta phase. This documentationshould be considered beta as well. Please report any grammaticalcorrections/spelling corrections.</p><div class="contents topic"><p class="topic-title first"><a id="contents" name="contents">Contents</a></p><ul class="auto-toc simple"><li><a class="reference" href="#introduction" id="id44" name="id44">1&nbsp;&nbsp;&nbsp;Introduction</a></li><li><a class="reference" href="#features" id="id45" name="id45">2&nbsp;&nbsp;&nbsp;Features</a></li><li><a class="reference" href="#portability" id="id46" name="id46">3&nbsp;&nbsp;&nbsp;Portability</a></li><li><a class="reference" href="#building-luabind" id="id47" name="id47">4&nbsp;&nbsp;&nbsp;Building luabind</a></li><li><a class="reference" href="#basic-usage" id="id48" name="id48">5&nbsp;&nbsp;&nbsp;Basic usage</a><ul class="auto-toc"><li><a class="reference" href="#hello-world" id="id49" name="id49">5.1&nbsp;&nbsp;&nbsp;Hello world</a></li></ul></li><li><a class="reference" href="#scopes" id="id50" name="id50">6&nbsp;&nbsp;&nbsp;Scopes</a></li><li><a class="reference" href="#binding-functions-to-lua" id="id51" name="id51">7&nbsp;&nbsp;&nbsp;Binding functions to Lua</a><ul class="auto-toc"><li><a class="reference" href="#overloaded-functions" id="id52" name="id52">7.1&nbsp;&nbsp;&nbsp;Overloaded functions</a></li><li><a class="reference" href="#signature-matching" id="id53" name="id53">7.2&nbsp;&nbsp;&nbsp;Signature matching</a></li><li><a class="reference" href="#calling-lua-functions" id="id54" name="id54">7.3&nbsp;&nbsp;&nbsp;Calling Lua functions</a></li><li><a class="reference" href="#using-lua-threads" id="id55" name="id55">7.4&nbsp;&nbsp;&nbsp;Using Lua threads</a></li></ul></li><li><a class="reference" href="#binding-classes-to-lua" id="id56" name="id56">8&nbsp;&nbsp;&nbsp;Binding classes to Lua</a><ul class="auto-toc"><li><a class="reference" href="#overloaded-member-functions" id="id57" name="id57">8.1&nbsp;&nbsp;&nbsp;Overloaded member functions</a></li><li><a class="reference" href="#properties" id="id58" name="id58">8.2&nbsp;&nbsp;&nbsp;Properties</a></li><li><a class="reference" href="#enums" id="id59" name="id59">8.3&nbsp;&nbsp;&nbsp;Enums</a></li><li><a class="reference" href="#operators" id="id60" name="id60">8.4&nbsp;&nbsp;&nbsp;Operators</a></li><li><a class="reference" href="#nested-scopes-and-static-functions" id="id61" name="id61">8.5&nbsp;&nbsp;&nbsp;Nested scopes and static functions</a></li><li><a class="reference" href="#derived-classes" id="id62" name="id62">8.6&nbsp;&nbsp;&nbsp;Derived classes</a></li><li><a class="reference" href="#smart-pointers" id="id63" name="id63">8.7&nbsp;&nbsp;&nbsp;Smart pointers</a></li><li><a class="reference" href="#splitting-class-registrations" id="id64" name="id64">8.8&nbsp;&nbsp;&nbsp;Splitting class registrations</a></li></ul></li><li><a class="reference" href="#object" id="id65" name="id65">9&nbsp;&nbsp;&nbsp;Object</a><ul class="auto-toc"><li><a class="reference" href="#iterators" id="id66" name="id66">9.1&nbsp;&nbsp;&nbsp;Iterators</a></li><li><a class="reference" href="#related-functions" id="id67" name="id67">9.2&nbsp;&nbsp;&nbsp;Related functions</a></li><li><a class="reference" href="#assigning-nil" id="id68" name="id68">9.3&nbsp;&nbsp;&nbsp;Assigning nil</a></li></ul></li><li><a class="reference" href="#defining-classes-in-lua" id="id69" name="id69">10&nbsp;&nbsp;&nbsp;Defining classes in Lua</a><ul class="auto-toc"><li><a class="reference" href="#deriving-in-lua" id="id70" name="id70">10.1&nbsp;&nbsp;&nbsp;Deriving in lua</a></li><li><a class="reference" href="#overloading-operators" id="id71" name="id71">10.2&nbsp;&nbsp;&nbsp;Overloading operators</a></li><li><a class="reference" href="#finalizers" id="id72" name="id72">10.3&nbsp;&nbsp;&nbsp;Finalizers</a></li><li><a class="reference" href="#slicing" id="id73" name="id73">10.4&nbsp;&nbsp;&nbsp;Slicing</a></li></ul></li><li><a class="reference" href="#exceptions" id="id74" name="id74">11&nbsp;&nbsp;&nbsp;Exceptions</a></li><li><a class="reference" href="#policies" id="id75" name="id75">12&nbsp;&nbsp;&nbsp;Policies</a><ul class="auto-toc"><li><a class="reference" href="#adopt" id="id76" name="id76">12.1&nbsp;&nbsp;&nbsp;adopt</a></li><li><a class="reference" href="#dependency" id="id77" name="id77">12.2&nbsp;&nbsp;&nbsp;dependency</a></li><li><a class="reference" href="#out-value" id="id78" name="id78">12.3&nbsp;&nbsp;&nbsp;out_value</a></li><li><a class="reference" href="#pure-out-value" id="id79" name="id79">12.4&nbsp;&nbsp;&nbsp;pure_out_value</a></li><li><a class="reference" href="#return-reference-to" id="id80" name="id80">12.5&nbsp;&nbsp;&nbsp;return_reference_to</a></li><li><a class="reference" href="#copy" id="id81" name="id81">12.6&nbsp;&nbsp;&nbsp;copy</a></li><li><a class="reference" href="#discard-result" id="id82" name="id82">12.7&nbsp;&nbsp;&nbsp;discard_result</a></li><li><a class="reference" href="#return-stl-iterator" id="id83" name="id83">12.8&nbsp;&nbsp;&nbsp;return_stl_iterator</a></li><li><a class="reference" href="#raw" id="id84" name="id84">12.9&nbsp;&nbsp;&nbsp;raw</a></li><li><a class="reference" href="#yield" id="id85" name="id85">12.10&nbsp;&nbsp;&nbsp;yield</a></li></ul></li><li><a class="reference" href="#splitting-up-the-registration" id="id86" name="id86">13&nbsp;&nbsp;&nbsp;Splitting up the registration</a></li><li><a class="reference" href="#error-handling" id="id87" name="id87">14&nbsp;&nbsp;&nbsp;Error Handling</a><ul class="auto-toc"><li><a class="reference" href="#pcall-errorfunc" id="id88" name="id88">14.1&nbsp;&nbsp;&nbsp;pcall errorfunc</a></li><li><a class="reference" href="#file-and-line-numbers" id="id89" name="id89">14.2&nbsp;&nbsp;&nbsp;file and line numbers</a></li><li><a class="reference" href="#lua-panic" id="id90" name="id90">14.3&nbsp;&nbsp;&nbsp;lua panic</a></li><li><a class="reference" href="#structured-exceptions-msvc" id="id91" name="id91">14.4&nbsp;&nbsp;&nbsp;structured exceptions (MSVC)</a></li><li><a class="reference" href="#error-messages" id="id92" name="id92">14.5&nbsp;&nbsp;&nbsp;Error messages</a></li></ul></li><li><a class="reference" href="#build-options" id="id93" name="id93">15&nbsp;&nbsp;&nbsp;Build options</a></li><li><a class="reference" href="#implementation-notes" id="id94" name="id94">16&nbsp;&nbsp;&nbsp;Implementation notes</a></li><li><a class="reference" href="#faq" id="id95" name="id95">17&nbsp;&nbsp;&nbsp;FAQ</a></li><li><a class="reference" href="#known-issues" id="id96" name="id96">18&nbsp;&nbsp;&nbsp;Known issues</a></li><li><a class="reference" href="#acknowledgments" id="id97" name="id97">19&nbsp;&nbsp;&nbsp;Acknowledgments</a></li></ul></div><div class="section"><h1><a id="introduction" name="introduction">1&nbsp;&nbsp;&nbsp;Introduction</a></h1><p>Luabind is a library that helps you create bindings between C++ and Lua. It hasthe ability to expose functions and classes, written in C++, to Lua. It willalso supply the functionality to define classes in Lua and let them derive fromother Lua classes or C++ classes. Lua classes can override virtual functionsfrom their C++ base classes. It is written towards Lua 5.0, and does not workwith Lua 4.</p><p>It is implemented utilizing template meta programming. That means that youdon't need an extra preprocess pass to compile your project (it is done by thecompiler). It also means you don't (usually) have to know the exact signatureof each function you register, since the library will generate code dependingon the compile-time type of the function (which includes the signature). Themain drawback of this approach is that the compilation time will increase forthe file that does the registration, it is therefore recommended that youregister everything in the same cpp-file.</p><p>Luabind is released under the terms of the <a class="reference" href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</p><p>We are very interested in hearing about projects that use luabind, please letus know about your project.</p><p>The main channel for help and feedback is the <a class="reference" href="https://lists.sourceforge.net/lists/listinfo/luabind-user">luabind mailing list</a>.There's also an IRC channel <tt class="docutils literal"><span class="pre">#luabind</span></tt> on irc.freenode.net.</p></div><div class="section"><h1><a id="features" name="features">2&nbsp;&nbsp;&nbsp;Features</a></h1><p>Luabind supports:</p><blockquote><ul class="simple"><li>Overloaded free functions</li><li>C++ classes in Lua</li><li>Overloaded member functions</li><li>Operators</li><li>Properties</li><li>Enums</li><li>Lua functions in C++</li><li>Lua classes in C++</li><li>Lua classes (single inheritance)</li><li>Derives from Lua or C++ classes</li><li>Override virtual functions from C++ classes</li><li>Implicit casts between registered types</li><li>Best match signature matching</li><li>Return value policies and parameter policies</li></ul></blockquote></div><div class="section"><h1><a id="portability" name="portability">3&nbsp;&nbsp;&nbsp;Portability</a></h1><p>Luabind has been tested to work on the following compilers:</p><blockquote><ul class="simple"><li>Visual Studio 7.1</li><li>Visual Studio 7.0</li><li>Visual Studio 6.0 (sp 5)</li><li>Intel C++ 6.0 (Windows)</li><li>GCC 2.95.3 (cygwin)</li><li>GCC 3.0.4 (Debian/Linux)</li><li>GCC 3.1 (SunOS 5.8)</li><li>GCC 3.2 (cygwin)</li><li>GCC 3.3.1 (cygwin)</li><li>GCC 3.3 (Apple, MacOS X)</li><li>GCC 4.0 (Apple, MacOS X)</li></ul></blockquote><p>It has been confirmed not to work with:</p><blockquote><ul class="simple"><li>GCC 2.95.2 (SunOS 5.8)</li></ul></blockquote><p>Metrowerks 8.3 (Windows) compiles but fails the const-test. Thismeans that const member functions are treated as non-const memberfunctions.</p><p>If you have tried luabind with a compiler not listed here, let us knowyour result with it.</p></div><div class="section"><h1><a id="building-luabind" name="building-luabind">4&nbsp;&nbsp;&nbsp;Building luabind</a></h1><p>To keep down the compilation-time luabind is built as a library. This means youhave to either build it and link against it, or include its source files inyour project. You also have to make sure the luabind directory is somewhere inyour compiler's include path. It requires <a class="reference" href="http://www.boost.org">Boost</a> 1.32.0 or 1.33.0 to beinstalled (only boost headers). It also requires that Lua is installed.</p><p>The official way of building luabind is with <a class="reference" href="http://www.boost.org/tools/build/v2/index_v2.html">Boost.Build V2</a>. To properly buildluabind with Boost.Build you need to set two environment variables:</p><dl class="docutils"><dt>BOOST_ROOT</dt><dd>Point this to your Boost installation.</dd><dt>LUA_PATH</dt><dd>Point this to your Lua directory. The build system will assume that theinclude and library files are located in <tt class="docutils literal"><span class="pre">$(LUA_PATH)/include/</span></tt> and<tt class="docutils literal"><span class="pre">$(LUA_PATH)/lib/.</span></tt>. If this environment variable is not defined, theJamfile will try to invoke <tt class="docutils literal"><span class="pre">pkg-config</span></tt> in order to find lua. It willlook for lua 5.1 (<tt class="docutils literal"><span class="pre">lua5.1</span></tt> as the package is called on debian systems).</dd></dl><p>For backward compatibility, there is also a makefile in the root-directory thatwill build the library and the test programs. If you are using a UNIX-system (orcygwin) they will make it easy to build luabind as a static library. If you areusing Visual Studio it may be easier to include the files in the src directoryin your project.</p><p>When building luabind you have several options that may streamline the libraryto better suit your needs. It is extremely important that your application hasthe same settings as the library was built with. The available options arefound in the <a class="reference" href="#build-options">Build options</a> section.</p><p>If you want to change the settings to differ from the default, it's recommendedthat you define the settings on the command line of all your files (in theproject settings in visual studio).</p></div><div class="section"><h1><a id="basic-usage" name="basic-usage">5&nbsp;&nbsp;&nbsp;Basic usage</a></h1><p>To use luabind, you must include <tt class="docutils literal"><span class="pre">lua.h</span></tt> and luabind's main header file:</p><pre class="literal-block">extern &quot;C&quot;{    #include &quot;lua.h&quot;}#include &lt;luabind/luabind.hpp&gt;</pre><p>This includes support for both registering classes and functions. If you justwant to have support for functions or classes you can include<tt class="docutils literal"><span class="pre">luabind/function.hpp</span></tt> and <tt class="docutils literal"><span class="pre">luabind/class.hpp</span></tt> separately:</p><pre class="literal-block">#include &lt;luabind/function.hpp&gt;#include &lt;luabind/class.hpp&gt;</pre><p>The first thing you need to do is to call <tt class="docutils literal"><span class="pre">luabind::open(lua_State*)</span></tt> whichwill register the functions to create classes from Lua, and initialize somestate-global structures used by luabind. If you don't call this function youwill hit asserts later in the library. There is no corresponding close functionbecause once a class has been registered in Lua, there really isn't any goodway to remove it. Partly because any remaining instances of that class relieson the class being there. Everything will be cleaned up when the state isclosed though.</p><!-- Isn't this wrong? Don't we include lua.h using lua_include.hpp ? --><p>Luabind's headers will never include <tt class="docutils literal"><span class="pre">lua.h</span></tt> directly, but through<tt class="docutils literal"><span class="pre">&lt;luabind/lua_include.hpp&gt;</span></tt>. If you for some reason need to include anotherLua header, you can modify this file.</p><div class="section"><h2><a id="hello-world" name="hello-world">5.1&nbsp;&nbsp;&nbsp;Hello world</a></h2><pre class="literal-block">#include &lt;iostream&gt;#include &lt;luabind/luabind.hpp&gt;void greet(){    std::cout &lt;&lt; &quot;hello world!\n&quot;;}extern &quot;C&quot; int init(lua_State* L){    using namespace luabind;    open(L);    module(L)    [        def(&quot;greet&quot;, &amp;greet)    ];    return 0;}</pre><pre class="literal-block">Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio&gt; loadlib('hello_world.dll', 'init')()&gt; greet()Hello world!&gt;</pre></div></div><div class="section"><h1><a id="scopes" name="scopes">6&nbsp;&nbsp;&nbsp;Scopes</a></h1><p>Everything that gets registered in Lua is registered in a namespace (Luatables) or in the global scope (called module). All registrations must besurrounded by its scope. To define a module, the <tt class="docutils literal"><span class="pre">luabind::module</span></tt> class isused. It is used like this:</p><pre class="literal-block">module(L)[    // declarations];</pre><p>This will register all declared functions or classes in the global namespace inLua. If you want to have a namespace for your module (like the standardlibraries) you can give a name to the constructor, like this:</p><pre class="literal-block">module(L, &quot;my_library&quot;)[    // declarations];</pre><p>Here all declarations will be put in the my_library table.</p><p>If you want nested namespace's you can use the <tt class="docutils literal"><span class="pre">luabind::namespace_</span></tt> class. Itworks exactly as <tt class="docutils literal"><span class="pre">luabind::module</span></tt> except that it doesn't take a lua_State*in it's constructor. An example of its usage could look like this:</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?