*** empty log message ***
[luabind.git] / doc / docs.html
blob7efc71688fd9a58bce9a68395b98095db11aca77
1 <?xml version="1.0" encoding="Latin1" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=Latin1" />
6 <meta name="generator" content="Docutils 0.3.1: http://docutils.sourceforge.net/" />
7 <title>luabind</title>
8 <meta name="author" content="Daniel Wallin, Arvid Norberg" />
9 <meta name="copyright" content="Copyright Daniel Wallin, Arvid Norberg 2003." />
10 <meta name="date" content="2004-08-08" />
11 <link rel="stylesheet" href="style.css" type="text/css" />
12 </head>
13 <body>
14 <div class="document" id="luabind">
15 <h1 class="title">luabind</h1>
16 <table class="docinfo" frame="void" rules="none">
17 <col class="docinfo-name" />
18 <col class="docinfo-content" />
19 <tbody valign="top">
20 <tr><th class="docinfo-name">Author:</th>
21 <td>Daniel Wallin, Arvid Norberg</td></tr>
22 <tr><th class="docinfo-name">Copyright:</th>
23 <td>Copyright Daniel Wallin, Arvid Norberg 2003.</td></tr>
24 <tr><th class="docinfo-name">Date:</th>
25 <td>2004-08-08</td></tr>
26 <tr><th class="docinfo-name">Revision:</th>
27 <td>1.21</td></tr>
28 <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 a
29 copy of this software and associated documentation files (the &quot;Software&quot;),
30 to deal in the Software without restriction, including without limitation
31 the rights to use, copy, modify, merge, publish, distribute, sublicense,
32 and/or sell copies of the Software, and to permit persons to whom the
33 Software is furnished to do so, subject to the following conditions:</p>
34 <p>The above copyright notice and this permission notice shall be included
35 in all copies or substantial portions of the Software.</p>
36 <p class="last">THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF
37 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
38 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
40 SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
41 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
42 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
44 OR OTHER DEALINGS IN THE SOFTWARE.</p>
45 </td>
46 </tr>
47 </tbody>
48 </table>
49 <p>Note: This library is currently in public beta phase. This documentation
50 should be considered beta as well. Please report any grammatical
51 corrections/spelling corrections.</p>
52 <div class="contents topic" id="contents">
53 <p class="topic-title"><a name="contents">Contents</a></p>
54 <ul class="auto-toc simple">
55 <li><a class="reference" href="#introduction" id="id44" name="id44">1   Introduction</a></li>
56 <li><a class="reference" href="#features" id="id45" name="id45">2   Features</a></li>
57 <li><a class="reference" href="#portability" id="id46" name="id46">3   Portability</a></li>
58 <li><a class="reference" href="#building-luabind" id="id47" name="id47">4   Building luabind</a></li>
59 <li><a class="reference" href="#basic-usage" id="id48" name="id48">5   Basic usage</a><ul class="auto-toc">
60 <li><a class="reference" href="#hello-world" id="id49" name="id49">5.1   Hello world</a></li>
61 </ul>
62 </li>
63 <li><a class="reference" href="#scopes" id="id50" name="id50">6   Scopes</a></li>
64 <li><a class="reference" href="#binding-functions-to-lua" id="id51" name="id51">7   Binding functions to Lua</a><ul class="auto-toc">
65 <li><a class="reference" href="#overloaded-functions" id="id52" name="id52">7.1   Overloaded functions</a></li>
66 <li><a class="reference" href="#signature-matching" id="id53" name="id53">7.2   Signature matching</a></li>
67 <li><a class="reference" href="#calling-lua-functions" id="id54" name="id54">7.3   Calling Lua functions</a></li>
68 <li><a class="reference" href="#using-lua-threads" id="id55" name="id55">7.4   Using Lua threads</a></li>
69 </ul>
70 </li>
71 <li><a class="reference" href="#binding-classes-to-lua" id="id56" name="id56">8   Binding classes to Lua</a><ul class="auto-toc">
72 <li><a class="reference" href="#properties" id="id57" name="id57">8.1   Properties</a></li>
73 <li><a class="reference" href="#enums" id="id58" name="id58">8.2   Enums</a></li>
74 <li><a class="reference" href="#operators" id="id59" name="id59">8.3   Operators</a></li>
75 <li><a class="reference" href="#nested-scopes-and-static-functions" id="id60" name="id60">8.4   Nested scopes and static functions</a></li>
76 <li><a class="reference" href="#derived-classes" id="id61" name="id61">8.5   Derived classes</a></li>
77 <li><a class="reference" href="#smart-pointers" id="id62" name="id62">8.6   Smart pointers</a></li>
78 </ul>
79 </li>
80 <li><a class="reference" href="#object" id="id63" name="id63">9   Object</a><ul class="auto-toc">
81 <li><a class="reference" href="#iterators" id="id64" name="id64">9.1   Iterators</a></li>
82 <li><a class="reference" href="#related-functions" id="id65" name="id65">9.2   Related functions</a></li>
83 <li><a class="reference" href="#functor" id="id66" name="id66">9.3   Functor</a></li>
84 </ul>
85 </li>
86 <li><a class="reference" href="#defining-classes-in-lua" id="id67" name="id67">10   Defining classes in Lua</a><ul class="auto-toc">
87 <li><a class="reference" href="#deriving-in-lua" id="id68" name="id68">10.1   Deriving in lua</a></li>
88 <li><a class="reference" href="#overloading-operators" id="id69" name="id69">10.2   Overloading operators</a></li>
89 <li><a class="reference" href="#finalizers" id="id70" name="id70">10.3   Finalizers</a></li>
90 <li><a class="reference" href="#slicing" id="id71" name="id71">10.4   Slicing</a></li>
91 </ul>
92 </li>
93 <li><a class="reference" href="#exceptions" id="id72" name="id72">11   Exceptions</a></li>
94 <li><a class="reference" href="#policies" id="id73" name="id73">12   Policies</a><ul class="auto-toc">
95 <li><a class="reference" href="#adopt" id="id74" name="id74">12.1   adopt</a></li>
96 <li><a class="reference" href="#dependency" id="id75" name="id75">12.2   dependency</a></li>
97 <li><a class="reference" href="#out-value" id="id76" name="id76">12.3   out_value</a></li>
98 <li><a class="reference" href="#pure-out-value" id="id77" name="id77">12.4   pure_out_value</a></li>
99 <li><a class="reference" href="#return-reference-to" id="id78" name="id78">12.5   return_reference_to</a></li>
100 <li><a class="reference" href="#copy" id="id79" name="id79">12.6   copy</a></li>
101 <li><a class="reference" href="#discard-result" id="id80" name="id80">12.7   discard_result</a></li>
102 <li><a class="reference" href="#return-stl-iterator" id="id81" name="id81">12.8   return_stl_iterator</a></li>
103 <li><a class="reference" href="#raw" id="id82" name="id82">12.9   raw</a></li>
104 <li><a class="reference" href="#yield" id="id83" name="id83">12.10   yield</a></li>
105 </ul>
106 </li>
107 <li><a class="reference" href="#splitting-up-the-registration" id="id84" name="id84">13   Splitting up the registration</a></li>
108 <li><a class="reference" href="#configuration" id="id85" name="id85">14   Configuration</a><ul class="auto-toc">
109 <li><a class="reference" href="#build-options" id="id86" name="id86">14.1   Build options</a></li>
110 </ul>
111 </li>
112 <li><a class="reference" href="#implementation-notes" id="id87" name="id87">15   Implementation notes</a></li>
113 <li><a class="reference" href="#error-messages" id="id88" name="id88">16   Error messages</a></li>
114 <li><a class="reference" href="#faq" id="id89" name="id89">17   FAQ</a></li>
115 <li><a class="reference" href="#known-issues" id="id90" name="id90">18   Known issues</a></li>
116 <li><a class="reference" href="#acknowledgments" id="id91" name="id91">19   Acknowledgments</a></li>
117 </ul>
118 </div>
119 <div class="section" id="introduction">
120 <h1><a name="introduction">1   Introduction</a></h1>
121 <p>Luabind is a library that helps you create bindings between C++ and Lua. It has
122 the ability to expose functions and classes, written in C++, to Lua. It will
123 also supply the functionality to define classes in Lua and let them derive from
124 other Lua classes or C++ classes. Lua classes can override virtual functions
125 from their C++ base classes. It is written towards Lua 5.0, and does not work
126 with Lua 4.</p>
127 <p>It is implemented utilizing template meta programming. That means that you
128 don't need an extra preprocess pass to compile your project (it is done by the
129 compiler). It also means you don't (usually) have to know the exact signatureof
130 each function you register, since the library will generate code depending on
131 the compile-time type of the function (which includes the signature). The main
132 drawback of this approach is that the compilation time will increase for the
133 file that does the registration, it is therefore recommended that you register
134 everything in the same cpp-file.</p>
135 <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>
136 <p>We are very interested in hearing about projects that use luabind, please let
137 us know about your project.</p>
138 </div>
139 <div class="section" id="features">
140 <h1><a name="features">2   Features</a></h1>
141 <p>Luabind supports:</p>
142 <blockquote>
143 <ul class="simple">
144 <li>Overloaded free functions</li>
145 <li>C++ classes in Lua</li>
146 <li>Overloaded member functions</li>
147 <li>Operators</li>
148 <li>Properties</li>
149 <li>Enums</li>
150 <li>Lua functions in C++</li>
151 <li>Lua classes in C++</li>
152 <li>Lua classes (single inheritance)</li>
153 <li>Derives from Lua or C++ classes</li>
154 <li>Override virtual functions from C++ classes</li>
155 <li>Implicit casts between registered types</li>
156 <li>Best match signature matching</li>
157 <li>Return value policies and parameter policies</li>
158 </ul>
159 </blockquote>
160 </div>
161 <div class="section" id="portability">
162 <h1><a name="portability">3   Portability</a></h1>
163 <p>Luabind has been tested to work on the following compilers:</p>
164 <blockquote>
165 <ul class="simple">
166 <li>Visual Studio 7.1</li>
167 <li>Visual Studio 7.0</li>
168 <li>Visual Studio 6.0 (sp 5)</li>
169 <li>Intel C++ 6.0 (Windows)</li>
170 <li>GCC 2.95.3 (cygwin)</li>
171 <li>GCC 3.0.4 (Debian/Linux)</li>
172 <li>GCC 3.1 (SunOS 5.8)</li>
173 <li>GCC 3.2 (cygwin)</li>
174 <li>GCC 3.3.1 (cygwin)</li>
175 </ul>
176 </blockquote>
177 <p>It has been confirmed not to work with:</p>
178 <blockquote>
179 <ul class="simple">
180 <li>GCC 2.95.2 (SunOS 5.8)</li>
181 </ul>
182 </blockquote>
183 <p>Metrowerks 8.3 (Windows) compiles but fails the const-test. This
184 means that const member functions are treated as non-const member
185 functions.</p>
186 <p>If you have tried luabind with a compiler not listed here, let us know
187 your result with it.</p>
188 </div>
189 <div class="section" id="building-luabind">
190 <h1><a name="building-luabind">4   Building luabind</a></h1>
191 <p>To keep down the compilation-time luabind is built as a library. This means you
192 have to either build it and lika against it, or include its source files in
193 your project. You also have to make sure the luabind directory is somewhere in
194 your compiler's include path. It requires <a class="reference" href="http://www.boost.org">Boost</a> 1.31.0 to be installed (only
195 boost headers). It also requires that Lua is installed.</p>
196 <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 build
197 luabind with Boost.Build you need to set two environment variables:</p>
198 <dl>
199 <dt>BOOST_ROOT</dt>
200 <dd>Point this to your Boost installation.</dd>
201 <dt>LUA_PATH</dt>
202 <dd>Point this to your Lua directory. The build system will assume that the
203 include and library files are located in <tt class="literal"><span class="pre">$(LUA_PATH)/include/</span></tt> and
204 <tt class="literal"><span class="pre">$(LUA_PATH)/lib/.</span></tt></dd>
205 </dl>
206 <p>For backward compatibility, there is also a makefile in the root-directory that
207 will build the library and the test program. If you are using a UNIX-system (or
208 cygwin) they will make it easy to build luabind as a static library. If you are
209 using Visual Studio it may be easier to include the files in the src directory
210 in your project.</p>
211 <p>When building luabind you have several options that may streamline the library
212 to better suit your needs. It is extremely important that your application has
213 the same settings as the library was built with. The available options are
214 found in the <a class="reference" href="#configuration">Configuration</a> section.</p>
215 <p>If you want to change the settings to differ from the default, it's recommended
216 that you define the settings on the commandline of all your files (in the
217 project settings in visual studio).</p>
218 </div>
219 <div class="section" id="basic-usage">
220 <h1><a name="basic-usage">5   Basic usage</a></h1>
221 <p>To use luabind, you must include <tt class="literal"><span class="pre">lua.h</span></tt> and luabind's main header file:</p>
222 <pre class="literal-block">
223 extern &quot;C&quot;
225 #include &quot;lua.h&quot;
228 #include &lt;luabind/luabind.hpp&gt;
229 </pre>
230 <p>This includes support for both registering classes and functions. If you just
231 want to have support for functions or classes you can include
232 <tt class="literal"><span class="pre">luabind/function.hpp</span></tt> and <tt class="literal"><span class="pre">luabind/class.hpp</span></tt> separately:</p>
233 <pre class="literal-block">
234 #include &lt;luabind/function.hpp&gt;
235 #include &lt;luabind/class.hpp&gt;
236 </pre>
237 <p>The first thing you need to do is to call <tt class="literal"><span class="pre">luabind::open(lua_State*)</span></tt> which
238 will register the functions to create classes from Lua, and initialize some
239 state-global structures used by luabind. If you don't call this function you
240 will hit asserts later in the library. There is no corresponding close function
241 because once a class has been registered in Lua, there really isn't any good
242 way to remove it. Partly because any remaining instances of that class relies
243 on the class being there. Everything will be cleaned up when the state is
244 closed though.</p>
245 <!-- Isn't this wrong? Don't we include lua.h using lua_include.hpp ? -->
246 <p>Luabind's headers will never include <tt class="literal"><span class="pre">lua.h</span></tt> directly, but through
247 <tt class="literal"><span class="pre">&lt;luabind/lua_include.hpp&gt;</span></tt>. If you for some reason need to include another
248 Lua header, you can modify this file.</p>
249 <div class="section" id="hello-world">
250 <h2><a name="hello-world">5.1   Hello world</a></h2>
251 <pre class="literal-block">
252 #include &lt;iostream&gt;
253 #include &lt;luabind/luabind.hpp&gt;
255 void greet()
257 std::cout &lt;&lt; &quot;hello world!\n&quot;;
260 extern &quot;C&quot; int init(lua_State* L)
262 using namespace luabind;
264 open(L);
266 module(L)
268 def(&quot;greet&quot;, &amp;greet)
271 return 0;
273 </pre>
274 <pre class="literal-block">
275 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
276 &gt; loadlib('hello_world.dll', 'init')()
277 &gt; greet()
278 Hello world!
279 &gt;
280 </pre>
281 </div>
282 </div>
283 <div class="section" id="scopes">
284 <h1><a name="scopes">6   Scopes</a></h1>
285 <p>Everything that gets registered in Lua is registered in a namespace (Lua
286 tables) or in the global scope (called module). All registrations must be
287 surrounded by its scope. To define a module, the <tt class="literal"><span class="pre">luabind::module</span></tt> class is
288 used. It is used like this:</p>
289 <pre class="literal-block">
290 module(L)
292 // declarations
294 </pre>
295 <p>This will register all declared functions or classes in the global namespace in
296 Lua. If you want to have a namespace for your module (like the standard
297 libraries) you can give a name to the constructor, like this:</p>
298 <pre class="literal-block">
299 module(L, &quot;my_library&quot;)
301 // declarations
303 </pre>
304 <p>Here all declarations will be put in the my_library table.</p>
305 <p>If you want nested namespaces you can use the <tt class="literal"><span class="pre">luabind::namespace_</span></tt> class. It
306 works exactly as <tt class="literal"><span class="pre">luabind::module</span></tt> except that it doesn't take a lua_State*
307 in it's constructor. An example of its usage could look like this:</p>
308 <pre class="literal-block">
309 module(L, &quot;my_library&quot;)
311 // declarations
313 namespace_(&quot;detail&quot;)
315 // library-private declarations
318 </pre>
319 <p>As you might have figured out, the following declarations are equivalent:</p>
320 <pre class="literal-block">
321 module(L)
323 namespace_(&quot;my_library&quot;)
325 // declarations
329 </pre>
330 <pre class="literal-block">
331 module(L, &quot;my_library&quot;)
333 // declarations
335 </pre>
336 <p>Each declaration must be separated by a comma, like this:</p>
337 <pre class="literal-block">
338 module(L)
340 def(&quot;f&quot;, &amp;f),
341 def(&quot;g&quot;, &amp;g),
342 class_&lt;A&gt;(&quot;A&quot;)
343 .def(constructor&lt;int, int&gt;),
344 def(&quot;h&quot;, &amp;h)
346 </pre>
347 <p>More about the actual declarations in the <a class="reference" href="#binding-functions-to-lua">Binding functions to Lua</a> and
348 <a class="reference" href="#binding-classes-to-lua">Binding classes to Lua</a> sections.</p>
349 <p>A word of caution, if you are in really bad need for performance, putting your
350 functions in tables will increase the lookup time.</p>
351 </div>
352 <div class="section" id="binding-functions-to-lua">
353 <h1><a name="binding-functions-to-lua">7   Binding functions to Lua</a></h1>
354 <p>To bind functions to Lua you use the function <tt class="literal"><span class="pre">luabind::def()</span></tt>. It has the
355 following synopsis:</p>
356 <pre class="literal-block">
357 template&lt;class F, class policies&gt;
358 void def(const char* name, F f, const Policies&amp;);
359 </pre>
360 <ul class="simple">
361 <li>name is the name the function will have within Lua.</li>
362 <li>F is the function pointer you want to register.</li>
363 <li>The Policies parameter is used to describe how parameters and return values
364 are treated by the function, this is an optional parameter. More on this in
365 the <a class="reference" href="#policies">policies</a> section.</li>
366 </ul>
367 <p>An example usage could be if you want to register the function <tt class="literal"><span class="pre">float</span>
368 <span class="pre">std::sin(float)</span></tt>:</p>
369 <pre class="literal-block">
370 module(L)
372 def(&quot;sin&quot;, &amp;std::sin)
374 </pre>
375 <div class="section" id="overloaded-functions">
376 <h2><a name="overloaded-functions">7.1   Overloaded functions</a></h2>
377 <p>If you have more than one function with the same name, and want to register
378 them in Lua, you have to explicitly give the signature. This is to let C++ know
379 which function you refer to. For example, if you have two functions, <tt class="literal"><span class="pre">int</span>
380 <span class="pre">f(const</span> <span class="pre">char*)</span></tt> and <tt class="literal"><span class="pre">void</span> <span class="pre">f(int)</span></tt>.</p>
381 <pre class="literal-block">
382 module(L)
384 def(&quot;f&quot;, (int(*)(const char*)) &amp;f),
385 def(&quot;f&quot;, (void(*)(int)) &amp;f)
387 </pre>
388 </div>
389 <div class="section" id="signature-matching">
390 <h2><a name="signature-matching">7.2   Signature matching</a></h2>
391 <p>luabind will generate code that checks the Lua stack to see if the values there
392 can match your functions' signatures. It will handle implicit typecasts between
393 derived classes, and it will prefer matches with the least number of implicit
394 casts. In a function call, if the function is overloaded and there's no
395 overload that match the parameters better than the other, you have an
396 ambiguity. This will spawn a run-time error, stating that the function call is
397 ambiguous. A simple example of this is to register one function that takes an
398 int and one that takes a float. Since Lua don't distinguish between floats and
399 integers, both will always match.</p>
400 <p>Since all overloads are tested, it will always find the best match (not the
401 first match). This also means that it can handle situations where the only
402 difference in the signature is that one member function is const and the other
403 isn't.</p>
404 <div class="sidebar">
405 <p class="sidebar-title">Ownership transfer</p>
406 <p>To correctly handle ownership transfer, create_a() would need an adopt
407 return value policy. More on this in the <a class="reference" href="#policies">Policies</a> section.</p>
408 </div>
409 <p>For example, if the following function and class is registered:</p>
410 <pre class="literal-block">
411 struct A
413 void f();
414 void f() const;
417 const A* create_a();
419 struct B: A {};
420 struct C: B {};
422 void g(A*);
423 void g(B*);
424 </pre>
425 <p>And the following Lua code is executed:</p>
426 <pre class="literal-block">
427 a1 = create_a()
428 a1:f() -- the const version is called
430 a2 = A()
431 a2:f() -- the non-const version is called
433 a = A()
434 b = B()
435 c = C()
437 g(a) -- calls g(A*)
438 g(b) -- calls g(B*)
439 g(c) -- calls g(B*)
440 </pre>
441 </div>
442 <div class="section" id="calling-lua-functions">
443 <h2><a name="calling-lua-functions">7.3   Calling Lua functions</a></h2>
444 <p>To call a Lua function, you can either use <tt class="literal"><span class="pre">call_function()</span></tt>,
445 an <tt class="literal"><span class="pre">object</span></tt> or <tt class="literal"><span class="pre">functor</span></tt>.</p>
446 <pre class="literal-block">
447 template&lt;class Ret&gt;
448 Ret call_function(lua_State* L, const char* name, ...)
449 template&lt;class Ret&gt;
450 Ret call_function(object const&amp; obj, ...)
451 </pre>
452 <p>There are two overloads of the <tt class="literal"><span class="pre">call_function</span></tt> function, one that calls
453 a function given its name, and one that takes an object that should be a Lua
454 value that can be called as a function.</p>
455 <p>The overload that takes a name can only call global Lua functions. The ...
456 represents a variable number of parameters that are sent to the Lua
457 function. This function call may throw <tt class="literal"><span class="pre">luabind::error</span></tt> if the function
458 call fails.</p>
459 <p>The return value isn't actually Ret (the template parameter), but a proxy
460 object that will do the function call. This enables you to give policies to the
461 call. You do this with the operator[]. You give the policies within the
462 brackets, like this:</p>
463 <pre class="literal-block">
464 int ret = call_function&lt;int&gt;(
466 , &quot;a_lua_function&quot;
467 , new complex_class()
468 )[ adopt(_1) ];
469 </pre>
470 <p>If you want to pass a parameter as a reference, you have to wrap it with the
471 <a class="reference" href="http://www.boost.org/doc/html/ref.html">Boost.Ref</a>.</p>
472 <p>Like this:</p>
473 <pre class="literal-block">
474 int ret = call_function(L, &quot;fun&quot;, boost::ref(val));
475 </pre>
476 </div>
477 <div class="section" id="using-lua-threads">
478 <h2><a name="using-lua-threads">7.4   Using Lua threads</a></h2>
479 <p>To start a Lua thread, you have to call <tt class="literal"><span class="pre">lua_resume()</span></tt>, this means that you
480 cannot use the previous function <tt class="literal"><span class="pre">call_function()</span></tt> to start a thread. You have
481 to use</p>
482 <pre class="literal-block">
483 template&lt;class Ret&gt;
484 Ret resume_function(lua_State* L, const char* name, ...)
485 template&lt;class Ret&gt;
486 Ret resume_function(object const&amp; obj, ...)
487 </pre>
488 <p>and</p>
489 <pre class="literal-block">
490 template&lt;class Ret&gt;
491 Ret resume(lua_State* L, ...)
492 </pre>
493 <p>The first time you start the thread, you have to give it a function to execute. i.e. you
494 have to use <tt class="literal"><span class="pre">resume_function</span></tt>, when the Lua function yeilds, it will return the first
495 value passed in to <tt class="literal"><span class="pre">lua_yield()</span></tt>. When you want to continue the execution, you just call
496 <tt class="literal"><span class="pre">resume()</span></tt> on your <tt class="literal"><span class="pre">lua_State</span></tt>, since it's already executing a function, you don't pass
497 it one. The parameters to <tt class="literal"><span class="pre">resume()</span></tt> will be returned by <tt class="literal"><span class="pre">yield()</span></tt> on the Lua side.</p>
498 <p>For yielding C++-functions (without the support of passing data back and forth between the
499 Lua side and the c++ side), you can use the <a class="reference" href="#yield">yield</a> policy.</p>
500 <p>With the overload of <tt class="literal"><span class="pre">resume_function</span></tt> that takes an <a class="reference" href="#object">object</a>, it is important that the
501 object was constructed with the thread as its <tt class="literal"><span class="pre">lua_State*</span></tt>. Like this:</p>
502 <pre class="literal-block">
503 lua_State* thread = lua_newthread(L);
504 object fun = get_global(<strong>thread</strong>)[&quot;my_thread_fun&quot;];
505 resume_function(fun);
506 </pre>
507 </div>
508 </div>
509 <div class="section" id="binding-classes-to-lua">
510 <h1><a name="binding-classes-to-lua">8   Binding classes to Lua</a></h1>
511 <p>To register classes you use a class called <tt class="literal"><span class="pre">class_</span></tt>. Its name is supposed to
512 resemble the C++ keyword, to make it look more intuitive. It has an overloaded
513 member function <tt class="literal"><span class="pre">def()</span></tt> that is used to register member functions, operators,
514 constructors, enums and properties on the class. It will return its
515 this-pointer, to let you register more members directly.</p>
516 <p>Let's start with a simple example. Consider the following C++ class:</p>
517 <pre class="literal-block">
518 class testclass
520 public:
521 testclass(const std::string&amp; s): m_string(s) {}
522 void print_string() { std::cout &lt;&lt; m_string &lt;&lt; &quot;\n&quot;; }
524 private:
525 std::string m_string;
527 </pre>
528 <p>To register it with a Lua environment, write as follows (assuming you are using
529 namespace luabind):</p>
530 <pre class="literal-block">
531 module(L)
533 class_&lt;testclass&gt;(&quot;testclass&quot;)
534 .def(constructor&lt;const std::string&amp;&gt;())
535 .def(&quot;print_string&quot;, &amp;testclass::print_string)
537 </pre>
538 <p>This will register the class with the name testclass and constructor that takes
539 a string as argument and one member function with the name <tt class="literal"><span class="pre">print_string</span></tt>.</p>
540 <pre class="literal-block">
541 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
542 &gt; a = testclass('a string')
543 &gt; a:print_string()
544 a string
545 </pre>
546 <p>It is also possible to register free functions as member functions. The
547 requirement on the function is that it takes a pointer, const pointer,
548 reference or const reference to the class type as the first parameter. The rest
549 of the parameters are the ones that are visible in Lua, while the object
550 pointer is given as the first parameter. If we have the following C++ code:</p>
551 <pre class="literal-block">
552 struct A
554 int a;
557 int plus(A* o, int v) { return o-&gt;a + v; }
558 </pre>
559 <p>You can register <tt class="literal"><span class="pre">plus()</span></tt> as if it was a member function of A like this:</p>
560 <pre class="literal-block">
561 class_&lt;A&gt;(&quot;A&quot;)
562 .def(&quot;plus&quot;, &amp;plus)
563 </pre>
564 <p><tt class="literal"><span class="pre">plus()</span></tt> can now be called as a member function on A with one parameter, int.
565 If the object pointer parameter is const, the function will act as if it was a
566 const member function (it can be called on const objects).</p>
567 <div class="section" id="properties">
568 <h2><a name="properties">8.1   Properties</a></h2>
569 <p>To register a global data member with a class is easily done. Consider the
570 following class:</p>
571 <pre class="literal-block">
572 struct A
574 int a;
576 </pre>
577 <p>This class is registered like this:</p>
578 <pre class="literal-block">
579 module(L)
581 class_&lt;A&gt;(&quot;A&quot;)
582 .def_readwrite(&quot;a&quot;, &amp;A::a)
584 </pre>
585 <p>This gives read and write access to the member variable <tt class="literal"><span class="pre">A::a</span></tt>. It is also
586 possible to register attributes with read-only access:</p>
587 <pre class="literal-block">
588 module(L)
590 class_&lt;A&gt;(&quot;A&quot;)
591 .def_readonly(&quot;a&quot;, &amp;A::a)
593 </pre>
594 <p>You can also register getter and setter functions and make them look as if they
595 were a public data member. Consider the following class:</p>
596 <pre class="literal-block">
597 class A
599 public:
600 void set_a(int x) { a = x; }
601 int get_a() const { return a; }
603 private:
604 int a;
606 </pre>
607 <p>It can be registered as if it had a public data member a like this:</p>
608 <pre class="literal-block">
609 class_&lt;A&gt;(&quot;A&quot;)
610 .property(&quot;a&quot;, &amp;A::get_a, &amp;A::set_a)
611 </pre>
612 <p>This way the <tt class="literal"><span class="pre">get_a()</span></tt> and <tt class="literal"><span class="pre">set_a()</span></tt> functions will be called instead of
613 just writing to the data member. If you want to make it read only you can just
614 omit the last parameter.</p>
615 </div>
616 <div class="section" id="enums">
617 <h2><a name="enums">8.2   Enums</a></h2>
618 <p>If your class contains enumerated constants (enums), you can register them as
619 well to make them available in Lua. Note that they will not be type safe, all
620 enums are integers in Lua, and all functions that takes an enum, will accept
621 any integer. You register them like this:</p>
622 <pre class="literal-block">
623 module(L)
625 class_&lt;A&gt;(&quot;A&quot;)
626 .enum_(&quot;constants&quot;)
628 value(&quot;my_enum&quot;, 4),
629 value(&quot;my_2nd_enum&quot;, 7),
630 value(&quot;another_enum&quot;, 6)
633 </pre>
634 <p>In Lua they are accessed like any data member, except that they are read-only
635 and reached on the class itself rather than on an instance of the class.</p>
636 <pre class="literal-block">
637 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
638 &gt; print(A.my_enum)
640 &gt; print(A.another_enum)
642 </pre>
643 </div>
644 <div class="section" id="operators">
645 <h2><a name="operators">8.3   Operators</a></h2>
646 <p>The mechanism for registering operators on your class is pretty simple. You use
647 a global name <tt class="literal"><span class="pre">luabind::self</span></tt> to refer to the class itself and then you just
648 write the operator expression inside the <tt class="literal"><span class="pre">def()</span></tt> call. This class:</p>
649 <pre class="literal-block">
650 struct vec
652 vec operator+(int s);
654 </pre>
655 <p>Is registered like this:</p>
656 <pre class="literal-block">
657 module(L)
659 class_&lt;vec&gt;(&quot;vec&quot;)
660 .def(<strong>self + int()</strong>)
662 </pre>
663 <p>This will work regardless if your plus operator is defined inside your class or
664 as a free function.</p>
665 <p>If you operator is const (or, when defined as a free function, takes a const
666 reference to the class itself) you have to use <tt class="literal"><span class="pre">const_self</span></tt> instead of
667 <tt class="literal"><span class="pre">self</span></tt>. Like this:</p>
668 <pre class="literal-block">
669 module(L)
671 class_&lt;vec&gt;(&quot;vec&quot;)
672 .def(<strong>const_self</strong> + int())
674 </pre>
675 <p>The operators supported are those available in Lua:</p>
676 <pre class="literal-block">
677 + - * / == != &lt; &lt;= &gt; &gt;=
678 </pre>
679 <p>This means, no in-place operators. The equality operator (<tt class="literal"><span class="pre">==</span></tt>) has a little
680 hitch; it will not be called if the references are equal. This means that the
681 <tt class="literal"><span class="pre">==</span></tt> operator has to do pretty much what's it's expected to do.</p>
682 <p>In the above example the other operand type is instantiated by writing
683 <tt class="literal"><span class="pre">int()</span></tt>. If the operand type is a complex type that cannot easily be
684 instantiated you can wrap the type in a class called <tt class="literal"><span class="pre">other&lt;&gt;</span></tt>. For example:</p>
685 <p>To register this class, we don't want to instantiate a string just to register
686 the operator.</p>
687 <pre class="literal-block">
688 struct vec
690 vec operator+(std::string);
692 </pre>
693 <p>Instead we use the <tt class="literal"><span class="pre">other&lt;&gt;</span></tt> wrapper like this:</p>
694 <pre class="literal-block">
695 module(L)
697 class_&lt;vec&gt;(&quot;vec&quot;)
698 .def(self + <strong>other&lt;std::string&gt;()</strong>)
700 </pre>
701 <p>To register an application operator:</p>
702 <pre class="literal-block">
703 module(L)
705 class_&lt;vec&gt;(&quot;vec&quot;)
706 .def( <strong>self(int())</strong> )
708 </pre>
709 <p>There's one special operator. In Lua it's called <tt class="literal"><span class="pre">__tostring</span></tt>, it's not
710 really an operator. It is used for converting objects to strings in a standard
711 way in Lua. If you register this functionality, you will be able to use the lua
712 standard function <tt class="literal"><span class="pre">tostring()</span></tt> for converting you object to a string.</p>
713 <p>To implement this operator in C++ you should supply an <tt class="literal"><span class="pre">operator&lt;&lt;</span></tt> for
714 ostream. Like this example:</p>
715 <pre class="literal-block">
716 class number {};
717 std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, number&amp;);
721 module(L)
723 class_&lt;number&gt;(&quot;number&quot;)
724 .def(<strong>tostring(self)</strong>)
726 </pre>
727 </div>
728 <div class="section" id="nested-scopes-and-static-functions">
729 <h2><a name="nested-scopes-and-static-functions">8.4   Nested scopes and static functions</a></h2>
730 <p>It is possible to add nested scopes to a class. This is useful when you need
731 to wrap a nested class, or a static function.</p>
732 <pre class="literal-block">
733 class_&lt;foo&gt;(&quot;foo&quot;)
734 .def(constructor&lt;&gt;()
735 <strong>.scope
737 class_&lt;inner&gt;(&quot;nested&quot;),
738 def(&quot;f&quot;, &amp;f)
739 ]</strong>;
740 </pre>
741 <p>It's also possible to add namespaces to classes using the same syntax.</p>
742 </div>
743 <div class="section" id="derived-classes">
744 <h2><a name="derived-classes">8.5   Derived classes</a></h2>
745 <p>If you want to register classes that derives from other classes, you can
746 specify a template parameter <tt class="literal"><span class="pre">bases&lt;&gt;</span></tt> to the <tt class="literal"><span class="pre">class_</span></tt> instantiation. The
747 following hierarchy:</p>
748 <pre class="literal-block">
749 struct A {};
750 struct B : A {};
751 </pre>
752 <p>Would be registered like this:</p>
753 <pre class="literal-block">
754 module(L)
756 class_&lt;A&gt;(&quot;A&quot;),
757 class_&lt;B, A&gt;(&quot;B&quot;)
759 </pre>
760 <p>If you have multiple inheritance you can specify more than one base. If B would
761 also derive from a class C, it would be registered like this:</p>
762 <pre class="literal-block">
763 module(L)
765 class_&lt;B, bases&lt;A, C&gt; &gt;(&quot;B&quot;)
767 </pre>
768 <p>Note that you can omit <tt class="literal"><span class="pre">bases&lt;&gt;</span></tt> when using single inheritance.</p>
769 <div class="note">
770 <p class="admonition-title">Note</p>
771 If you don't specify that classes derive from each other, luabind will not
772 be able to implicitly cast pointers between the types.</div>
773 </div>
774 <div class="section" id="smart-pointers">
775 <h2><a name="smart-pointers">8.6   Smart pointers</a></h2>
776 <p>When you register a class you can tell luabind that all instances of that class
777 should be held by some kind of smart pointer (boost::shared_ptr for instance).
778 You do this by giving the holder type as an extra template parameter to
779 the <tt class="literal"><span class="pre">class_</span></tt> you are constructing, like this:</p>
780 <pre class="literal-block">
781 module(L)
783 class_&lt;A, boost::shared_ptr&lt;A&gt; &gt;(&quot;A&quot;)
785 </pre>
786 <p>You also have to supply two functions for your smart pointer. One that returns
787 the type of const version of the smart pointer type (boost::shared_ptr&lt;const A&gt;
788 in this case). And one function that extracts the raw pointer from the smart
789 pointer. The first function is needed because luabind has to allow the
790 non-const -&gt; conversion when passing values from Lua to C++. The second
791 function is needed when Lua calls member functions on held types, the this
792 pointer must be a raw pointer, it is also needed to allow the smart_pointer -&gt;
793 raw_pointer conversion from Lua to C++. They look like this:</p>
794 <pre class="literal-block">
795 namespace luabind {
797 template&lt;class T&gt;
798 T* get_pointer(boost::shared_ptr&lt;T&gt; const&amp; p)
800 return p.get();
803 template&lt;class A&gt;
804 boost::shared_ptr&lt;const A&gt;*
805 get_const_holder(boost::shared_ptr&lt;A&gt;*)
807 return 0;
810 </pre>
811 <p>The conversion that works are (given that B is a base class of A):</p>
812 <div class="topic">
813 <p class="topic-title">From Lua to C++</p>
814 <table border class="table">
815 <colgroup>
816 <col width="51%" />
817 <col width="49%" />
818 </colgroup>
819 <thead valign="bottom">
820 <tr><th>Source</th>
821 <th>Target</th>
822 </tr>
823 </thead>
824 <tbody valign="top">
825 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
826 <td><tt class="literal"><span class="pre">A*</span></tt></td>
827 </tr>
828 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
829 <td><tt class="literal"><span class="pre">B*</span></tt></td>
830 </tr>
831 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
832 <td><tt class="literal"><span class="pre">A</span> <span class="pre">const*</span></tt></td>
833 </tr>
834 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
835 <td><tt class="literal"><span class="pre">B</span> <span class="pre">const*</span></tt></td>
836 </tr>
837 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
838 <td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
839 </tr>
840 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
841 <td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
842 </tr>
843 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
844 <td><tt class="literal"><span class="pre">A</span> <span class="pre">const*</span></tt></td>
845 </tr>
846 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
847 <td><tt class="literal"><span class="pre">B</span> <span class="pre">const*</span></tt></td>
848 </tr>
849 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
850 <td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
851 </tr>
852 </tbody>
853 </table>
854 </div>
855 <div class="topic">
856 <p class="topic-title">From C++ to Lua</p>
857 <table border class="table">
858 <colgroup>
859 <col width="56%" />
860 <col width="44%" />
861 </colgroup>
862 <thead valign="bottom">
863 <tr><th>Source</th>
864 <th>Target</th>
865 </tr>
866 </thead>
867 <tbody valign="top">
868 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
869 <td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
870 </tr>
871 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
872 <td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
873 </tr>
874 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span> <span class="pre">const&amp;</span></tt></td>
875 <td><tt class="literal"><span class="pre">holder_type&lt;A&gt;</span></tt></td>
876 </tr>
877 <tr><td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span> <span class="pre">const&amp;</span></tt></td>
878 <td><tt class="literal"><span class="pre">holder_type&lt;A</span> <span class="pre">const&gt;</span></tt></td>
879 </tr>
880 </tbody>
881 </table>
882 </div>
883 <p>When using a holder type, it can be useful to know if the pointer is valid. For
884 example when using std::auto_ptr, the holder will be invalidated when passed as
885 a parameter to a function. For this purpose there is a member of all object
886 instances in luabind: <tt class="literal"><span class="pre">__ok</span></tt>.</p>
887 <pre class="literal-block">
888 struct X {};
889 void f(std::auto_ptr&lt;X&gt;);
891 module(L)
893 class_&lt;X, std::auto_ptr&lt;X&gt; &gt;(&quot;X&quot;)
894 .def(constructor&lt;&gt;()),
896 def(&quot;f&quot;, &amp;f)
898 </pre>
899 <pre class="literal-block">
900 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
901 &gt; a = X()
902 &gt; f(a)
903 &gt; print a.__ok
904 false
905 </pre>
906 </div>
907 </div>
908 <div class="section" id="object">
909 <h1><a name="object">9   Object</a></h1>
910 <p>Since functions have to be able to take Lua values (of variable type) we need a
911 wrapper around them. This wrapper is called <tt class="literal"><span class="pre">luabind::object</span></tt>. If the
912 function you register takes an object, it will match any Lua value. To use it,
913 you need to include <tt class="literal"><span class="pre">luabind/object.hpp</span></tt>.</p>
914 <div class="topic">
915 <p class="topic-title">Synopsis</p>
916 <pre class="literal-block">
917 class object
919 public:
920 class iterator;
921 class raw_iterator;
922 class array_iterator;
924 template&lt;class T&gt;
925 object(lua_State*, T const&amp; value);
926 object(object const&amp;);
927 object(lua_State*);
928 object();
930 ~object();
932 iterator begin() const;
933 iterator end() const;
934 raw_iterator raw_begin() const;
935 raw_iterator raw_end() const;
936 array_iterator abegin() const;
937 array_iterator aend() const;
939 void set();
940 lua_State* lua_state() const;
941 void pushvalue() const;
942 bool is_valid() const;
943 operator safe_bool_type() const;
945 template&lt;class Key&gt;
946 <em>implementation-defined</em> operator[](Key const&amp;);
948 template&lt;class Key&gt;
949 object at(Key const&amp;) const;
951 template&lt;class Key&gt;
952 object raw_at(Key const&amp;) const;
954 template&lt;class T&gt;
955 object&amp; operator=(T const&amp;);
956 object&amp; operator=(object const&amp;);
958 template&lt;class T&gt;
959 bool operator==(T const&amp;) const;
960 bool operator==(object const&amp;) const;
961 bool operator&lt;(object const&amp;) const;
962 bool operator&lt;=(object const&amp;) const;
963 bool operator&gt;(object const&amp;) const;
964 bool operator&gt;=(object const&amp;) const;
965 bool operator!=(object const&amp;) const;
967 void swap(object&amp;);
968 int type() const;
970 <em>implementation-defined</em> operator()();
972 template&lt;class A0&gt;
973 <em>implementation-defined</em> operator()(A0 const&amp; a0);
975 template&lt;class A0, class A1&gt;
976 <em>implementation-defined</em> operator()(A0 const&amp; a0, A1 const&amp; a1);
978 /* ... */
980 </pre>
981 </div>
982 <p>When you have a Lua object, you can assign it a new value with the assignment
983 operator (=). When you do this, the <tt class="literal"><span class="pre">default_policy</span></tt> will be used to make the
984 conversion from C++ value to Lua. If your <tt class="literal"><span class="pre">luabind::object</span></tt> is a table you
985 can access its members through the operator[] or the iterators. The value
986 returned from the operator[] is a proxy object that can be used both for
987 reading and writing values into the table (using operator=). Note that it is
988 impossible to know if a Lua value is indexable or not (lua_gettable doesn't
989 fail, it succeeds or crashes). This means that if you're trying to index
990 something that cannot be indexed, you're on your own. Lua will call its
991 <tt class="literal"><span class="pre">panic()</span></tt> function (you can define your own panic function using
992 <tt class="literal"><span class="pre">lua_setpanicf</span></tt>). The <tt class="literal"><span class="pre">at()</span></tt> and <tt class="literal"><span class="pre">raw_at()</span></tt> functions returns the value at
993 the given table position (like operator[] but only for reading).</p>
994 <p>The ordinary <tt class="literal"><span class="pre">object::iterator</span></tt> uses lua_gettable to extract the values from
995 the table, the standard way that will invoke metamethods if any. The
996 <tt class="literal"><span class="pre">object::raw_iterator</span></tt> uses lua_rawget and <tt class="literal"><span class="pre">object::array_iterator</span></tt> uses
997 lua_rawgeti. The latter will only iterate over numberical keys starting at 1
998 and continue until the first nil value.</p>
999 <p>The <tt class="literal"><span class="pre">lua_state()</span></tt> function returns the Lua state where this object is stored.
1000 If you want to manipulate the object with Lua functions directly you can push
1001 it onto the Lua stack by calling <tt class="literal"><span class="pre">pushvalue()</span></tt>. And set the object's value by
1002 calling <tt class="literal"><span class="pre">set()</span></tt>, which will pop the top value from the Lua stack and assign
1003 it to the object.</p>
1004 <p>The operator== will call lua_equal() on the operands and return its result.</p>
1005 <p>The <tt class="literal"><span class="pre">type()</span></tt> member function will return the Lua type of the object. It will
1006 return the same values as lua_type().</p>
1007 <p>The <tt class="literal"><span class="pre">is_valid()</span></tt> function tells you whether the object has been initialized
1008 or not. When created with its default constructor, objects are invalid. To make
1009 an object valid, you can assign it a value. If you want to invalidate an object
1010 you can simply assign it an invalid object.</p>
1011 <!-- So what? implementation detail, leave out of docs
1012 isn't really an implicit cast to bool, but an implicit cast
1013 to a member pointer, since member pointers don't have any arithmetic operators
1014 on them (which can cause hard to find errors). The functionality of the cast
1015 operator -->
1016 <p>The <tt class="literal"><span class="pre">operator</span> <span class="pre">safe_bool_type()</span></tt> is equivalent to <tt class="literal"><span class="pre">is_valid()</span></tt>. This means
1017 that these snippets are equivalent:</p>
1018 <pre class="literal-block">
1019 object o;
1020 // ...
1021 if (o)
1023 // ...
1028 object o;
1029 // ...
1030 if (o.is_valid())
1032 // ...
1034 </pre>
1035 <p>The application operator will call the value as if it was a function. You can
1036 give it any number of parameters (currently the <tt class="literal"><span class="pre">default_policy</span></tt> will be used
1037 for the conversion). The returned object refers to the return value (currently
1038 only one return value is supported). This operator may throw <tt class="literal"><span class="pre">luabind::error</span></tt>
1039 if the function call fails. If you want to specify policies to your function
1040 call, you can use index-operator (operator[]) on the function call, and give
1041 the policies within the [ and ]. Like this:</p>
1042 <pre class="literal-block">
1043 my_function_object(
1046 , new my_complex_structure(6)
1047 ) [ adopt(_3) ];
1048 </pre>
1049 <p>This tells luabind to make Lua adopt the ownership and responsibility for the
1050 pointer passed in to the lua-function.</p>
1051 <p>It's important that all instances of object have been destructed by the time
1052 the Lua state is closed. The object will keep a pointer to the lua state and
1053 release its Lua object in its destructor.</p>
1054 <p>Here's an example of how a function can use a table:</p>
1055 <pre class="literal-block">
1056 void my_function(const object&amp; table)
1058 if (table.type() == LUA_TTABLE)
1060 table[&quot;time&quot;] = std::clock();
1061 table[&quot;name&quot;] = std::rand() &lt; 500 ? &quot;unusual&quot; : &quot;usual&quot;;
1063 std::cout &lt;&lt; object_cast&lt;std::string&gt;(table[5]) &lt;&lt; &quot;\n&quot;;
1066 </pre>
1067 <p>If you take a <tt class="literal"><span class="pre">luabind::object</span></tt> as a parameter to a function, any Lua value
1068 will match that parameter. That's why we have to make sure it's a table before
1069 we index into it.</p>
1070 <div class="section" id="iterators">
1071 <h2><a name="iterators">9.1   Iterators</a></h2>
1072 <p>The iterators, that are returned by <tt class="literal"><span class="pre">begin()</span></tt> and <tt class="literal"><span class="pre">end()</span></tt> (and their
1073 variants) are (almost) models of the ForwardIterator concept. The exception
1074 is that post increment doesn't exist on them.</p>
1075 <p>They look like this</p>
1076 <pre class="literal-block">
1077 class object::iterator
1079 iterator();
1080 iterator(const iterator&amp;);
1082 iterator&amp; operator++();
1083 bool operator!=(const iterator&amp;) const;
1084 iterator&amp; operator=(const iterator&amp;);
1086 object key() const;
1088 <em>implementation-defined</em> operator*();
1090 </pre>
1091 <p>The implementation defined return value from the dereference operator is a
1092 proxy object that can be used as if it was an object, it can also be used to
1093 assign the specific table entry with a new value. If you want to assign a value
1094 to an entry pointed to by an iterator, just use the assignment operator on the
1095 dereferenced iterator:</p>
1096 <pre class="literal-block">
1097 *iter = 5;
1098 </pre>
1099 <p>The <tt class="literal"><span class="pre">key()</span></tt> member returns the key used by the iterator when indexing the
1100 associated Lua table.</p>
1101 </div>
1102 <div class="section" id="related-functions">
1103 <h2><a name="related-functions">9.2   Related functions</a></h2>
1104 <p>There are a couple of functions related to objects and tables.</p>
1105 <pre class="literal-block">
1106 T object_cast&lt;T&gt;(object const&amp;);
1107 T object_cast&lt;T&gt;(object const&amp;, Policies);
1109 boost::optional&lt;T&gt; object_cast_nothrow&lt;T&gt;(object const&amp;);
1110 boost::optional&lt;T&gt; object_cast_nothrow&lt;T&gt;(object const&amp;, Policies);
1111 </pre>
1112 </div>
1113 <div class="section" id="functor">
1114 <h2><a name="functor">9.3   Functor</a></h2>
1115 <p>The <tt class="literal"><span class="pre">functor</span></tt> class is similar to object, with the exception that it can only
1116 be used to store functions. If you take it as a parameter, it will only match
1117 functions.</p>
1118 <p>To use it you need to include its header:</p>
1119 <pre class="literal-block">
1120 #include &lt;luabind/functor.hpp&gt;
1121 </pre>
1122 <p>It takes one template parameter, the return value of the Lua function it
1123 represents. Currently the functor can have at most one return value (unlike Lua
1124 functions).</p>
1125 <div class="topic">
1126 <p class="topic-title">Synopsis</p>
1127 <pre class="literal-block">
1128 template&lt;class Ret&gt;
1129 class functor
1131 public:
1133 functor(lua_State*, char const* name);
1134 functor(functor const&amp;);
1135 ~functor();
1137 bool is_valid() const;
1138 operator safe_bool_type() const;
1139 void reset();
1141 lua_State* lua_state() const;
1142 void pushvalue() const;
1144 bool operator==(functor&lt;Ret&gt; const&amp;);
1145 bool operator!=(functor&lt;Ret&gt; const&amp;);
1147 <em>implementation-defined</em> operator()() const;
1149 template&lt;class A0&gt;
1150 <em>implementation-defined</em> operator()(A0 const&amp;) const;
1152 template&lt;class A0, class A1&gt;
1153 <em>implementation-defined</em> operator()(A0 const&amp;, A1 const&amp;) const;
1155 /* ... */
1157 </pre>
1158 </div>
1159 <p>The application operator takes any parameters. The parameters are converted
1160 into Lua and the function is called. The return value will act as if it was the
1161 type Ret, with the exception that you can use the return value to give policies
1162 to the call. You do this the same way as you do with objects, using the
1163 operator[], and giving the policies inside the brackets.</p>
1164 <p>The <tt class="literal"><span class="pre">is_valid()</span></tt> function works just like the one on object, it tells you if
1165 the functor has been assigned with a valid Lua function. The <tt class="literal"><span class="pre">operator</span>
1166 <span class="pre">safe_bool_type()</span></tt> is an alias for this member function and also works just as
1167 the one found in object.</p>
1168 <p>For example, if you have the following Lua function:</p>
1169 <pre class="literal-block">
1170 function f(a, b)
1171 return a + b
1173 </pre>
1174 <p>You can expose it to C++ like this:</p>
1175 <pre class="literal-block">
1176 functor&lt;int&gt; f(L, &quot;f&quot;);
1178 std::cout &lt;&lt; f(3, 5) &lt;&lt; &quot;\n&quot;;
1179 </pre>
1180 <p>This will print out the sum of 3 and 5. Note that you can pass any parameters
1181 to the application operator of <tt class="literal"><span class="pre">luabind::functor</span></tt>, this is because lua
1182 doesn't have signatures for its functions. All Lua functions take any number of
1183 parameters of any type.</p>
1184 <p>If we have a C++ function that takes a <tt class="literal"><span class="pre">luabind::functor</span></tt> and registers it,
1185 it will accept Lua functions passed to it. This enables us to expose APIs that
1186 requires you to register callbacks. For example, if your C++ API looks like
1187 this:</p>
1188 <pre class="literal-block">
1189 void set_callback(void(*)(int, int));
1190 </pre>
1191 <p>And you want to expose it to Lua, you have to wrap the call to the Lua
1192 function inside a real C++ function, like this:</p>
1193 <pre class="literal-block">
1194 functor&lt;void&gt; lua_callback;
1196 void callback_wrapper(int a, int b)
1198 lua_callback(a, b);
1201 void set_callback_wrapper(const functor&lt;void&gt;&amp; f)
1203 lua_callback = f;
1204 set_callback(&amp;callback_wrapper);
1206 </pre>
1207 <p>And then register <tt class="literal"><span class="pre">set_callback_wrapper</span></tt> instead of registering
1208 <tt class="literal"><span class="pre">set_callback</span></tt>. This will have the effect that when one tries to register the
1209 callback from Lua, your <tt class="literal"><span class="pre">set_callback_wrapper</span></tt> will be called instead and
1210 first set the Lua functor to the given function. It will then call the real
1211 <tt class="literal"><span class="pre">set_callback</span></tt> with the <tt class="literal"><span class="pre">callback_wrapper</span></tt>. The <tt class="literal"><span class="pre">callback_wrapper</span></tt> will
1212 be called whenever the callback should be called, and it will simply call the
1213 Lua function that we registered.</p>
1214 <p>You can also use <tt class="literal"><span class="pre">object_cast</span></tt> to cast an object to a functor.</p>
1215 <p><tt class="literal"><span class="pre">reset</span></tt> on <tt class="literal"><span class="pre">functor</span></tt> will invalidate the functor (and remove any references
1216 to its Lua value). If the functor object has longer lifetime than the lua state
1217 (e.g. if it's a global).</p>
1218 </div>
1219 </div>
1220 <div class="section" id="defining-classes-in-lua">
1221 <h1><a name="defining-classes-in-lua">10   Defining classes in Lua</a></h1>
1222 <p>In addition to binding C++ functions and classes with Lua, luabind also provide
1223 an OO-system in Lua.</p>
1224 <pre class="literal-block">
1225 class 'lua_testclass'
1227 function lua_testclass:__init(name)
1228 self.name = name
1231 function lua_testclass:print()
1232 print(self.name)
1235 a = lua_testclass('example')
1236 a:print()
1237 </pre>
1238 <p>Inheritance can be used between lua-classes:</p>
1239 <pre class="literal-block">
1240 class 'derived' (lua_testclass)
1242 function derived:__init() super('derived name')
1245 function derived:print()
1246 print('Derived:print() -&gt; ')
1247 lua_testclass.print(self)
1249 </pre>
1250 <p>Here the <tt class="literal"><span class="pre">super</span></tt> keyword is used in the constructor to initialize the base
1251 class. The user is required to call <tt class="literal"><span class="pre">super</span></tt> first in the constructor.</p>
1252 <p>As you can see in this example, you can call the base class member functions.
1253 You can find all member functions in the base class, but you will have to give
1254 the this-pointer (<tt class="literal"><span class="pre">self</span></tt>) as first argument.</p>
1255 <div class="section" id="deriving-in-lua">
1256 <h2><a name="deriving-in-lua">10.1   Deriving in lua</a></h2>
1257 <p>It is also possible to derive Lua classes from C++ classes, and override
1258 virtual functions with Lua functions. To do this we have to create a wrapper
1259 class for our C++ base class. This is the class that will hold the Lua object
1260 when we instantiate a Lua class.</p>
1261 <pre class="literal-block">
1262 class base
1264 public:
1265 base(const char* s)
1266 { std::cout &lt;&lt; s &lt;&lt; &quot;\n&quot;; }
1268 virtual void f(int a)
1269 { std::cout &lt;&lt; &quot;f(&quot; &lt;&lt; a &lt;&lt; &quot;)\n&quot;; }
1272 struct base_wrapper : base, luabind::wrap_base
1274 base_wrapper(const char* s)
1275 : base(s)
1278 virtual void f(int a)
1280 call&lt;void&gt;(&quot;f&quot;, a);
1283 static void default_f(base* ptr, int a)
1285 return ptr-&gt;base::f(a);
1291 module(L)
1293 class_&lt;base, base_wrapper&gt;(&quot;base&quot;)
1294 .def(constructor&lt;const char*&gt;())
1295 .def(&quot;f&quot;, &amp;base_wrapper::f, &amp;base_wrapper::default_f)
1297 </pre>
1298 <div class="important">
1299 <p class="admonition-title">Important</p>
1300 Since visual studio 6.5 doesn't support explicit template parameters
1301 to member functions, instead of using the member function <tt class="literal"><span class="pre">call()</span></tt>
1302 you call a free function <tt class="literal"><span class="pre">call_member()</span></tt> and pass the this-pointer
1303 as first parameter.</div>
1304 <p>Note that if you have both base classes and a base class wrapper, you must give
1305 both bases and the base class wrapper type as template parameter to
1306 <tt class="literal"><span class="pre">class_</span></tt> (as done in the example above). The order in which you specify
1307 them is not important. You must also register both the static version and the
1308 virtual version of the function from the wrapper, this is necessary in order
1309 to allow luabind to use both dynamic and static dispatch when calling the function.</p>
1310 <div class="important">
1311 <p class="admonition-title">Important</p>
1312 It is extremely important that the signatures of the static (default) function
1313 is identical to the virtual function. The fact that one of them is a free
1314 function and the other a member function doesn't matter, but the parameters
1315 as seen from lua must match. It would not have worked if the static function
1316 took a <tt class="literal"><span class="pre">base_wrapper*</span></tt> as its first argument, since the virtual function
1317 takes a <tt class="literal"><span class="pre">base*</span></tt> as its first argument (its this pointer). There's currently
1318 no check in luabind to make sure the signatures match.</div>
1319 <p>If we didn't have a class wrapper, it would not be possible to pass a Lua class
1320 back to C++. Since the entry points of the virtual functions would still point
1321 to the C++ base class, and not to the functions defined in Lua. That's why we
1322 need one function that calls the base class' real function (used if the lua
1323 class doesn't redefine it) and one virtual function that dispatches the call
1324 into luabind, to allow it to select if a Lua function should be called, or if
1325 the original function should be called. If you don't intend to derive from a
1326 C++ class, or if it doesn't have any virtual member functions, you can register
1327 it without a class wrapper.</p>
1328 <p>You don't need to have a class wrapper in order to derive from a class, but if
1329 it has virtual functions you may have silent errors.</p>
1330 <!-- Unnecessary? The rule of thumb is:
1331 If your class has virtual functions, create a wrapper type, if it doesn't
1332 don't create a wrapper type. -->
1333 <p>The wrappers must derive from <tt class="literal"><span class="pre">luabind::wrap_base</span></tt>, it contains a Lua reference
1334 that will hold the Lua instance of the object to make it possible to dispatch
1335 virtual function calls into Lua. This is done through an overloaded member function:</p>
1336 <pre class="literal-block">
1337 template&lt;class Ret&gt;
1338 Ret call(char const* name, ...)
1339 </pre>
1340 <p>Its used in a similar way as <tt class="literal"><span class="pre">call_function</span></tt>, with the exception that it doesn't
1341 take a <tt class="literal"><span class="pre">lua_State</span></tt> pointer, and the name is a member function in the Lua class.</p>
1342 <div class="warning">
1343 <p class="admonition-title">Warning</p>
1344 The current implementation of <tt class="literal"><span class="pre">call_member</span></tt> is not able to distinguish const
1345 member functions from non-const. If you have a situation where you have an overloaded
1346 virtual function where the only difference in their signatures is their constness, the
1347 wrong overload will be called by <tt class="literal"><span class="pre">call_member</span></tt>. This is rarely the case though.</div>
1348 <div class="section" id="object-identity">
1349 <h3><a name="object-identity">10.1.1   Object identity</a></h3>
1350 <p>When a pointer or reference to a registered class with a wrapper is passed
1351 to Lua, luabind will query for it's dynamic type. If the dynamic type
1352 inherits from <tt class="literal"><span class="pre">wrap_base</span></tt>, object identity is preserved.</p>
1353 <pre class="literal-block">
1354 struct A { .. };
1355 struct A_wrap : A, wrap_base { .. };
1357 A* f(A* ptr) { return ptr; }
1359 module(L)
1361 class_&lt;A, A_wrap&gt;(&quot;A&quot;),
1362 def(&quot;f&quot;, &amp;f)
1364 </pre>
1365 <pre class="literal-block">
1366 &gt; class 'B' (A)
1367 &gt; x = B()
1368 &gt; assert(x == f(x)) -- object identity is preserved when object is
1369 -- passed through C++
1370 </pre>
1371 <p>This functionality relies on RTTI being enabled (that <tt class="literal"><span class="pre">LUABIND_NO_RTTI</span></tt> is
1372 not defined).</p>
1373 </div>
1374 </div>
1375 <div class="section" id="overloading-operators">
1376 <h2><a name="overloading-operators">10.2   Overloading operators</a></h2>
1377 <p>You can overload most operators in Lua for your classes. You do this by simply
1378 declaring a member function with the same name as an operator (the name of the
1379 metamethods in Lua). The operators you can overload are:</p>
1380 <blockquote>
1381 <ul class="simple">
1382 <li><tt class="literal"><span class="pre">__add</span></tt></li>
1383 <li><tt class="literal"><span class="pre">__sub</span></tt></li>
1384 <li><tt class="literal"><span class="pre">__mul</span></tt></li>
1385 <li><tt class="literal"><span class="pre">__div</span></tt></li>
1386 <li><tt class="literal"><span class="pre">__pow</span></tt></li>
1387 <li><tt class="literal"><span class="pre">__lt</span></tt></li>
1388 <li><tt class="literal"><span class="pre">__le</span></tt></li>
1389 <li><tt class="literal"><span class="pre">__eq</span></tt></li>
1390 <li><tt class="literal"><span class="pre">__call</span></tt></li>
1391 <li><tt class="literal"><span class="pre">__unm</span></tt></li>
1392 <li><tt class="literal"><span class="pre">__tostring</span></tt></li>
1393 </ul>
1394 </blockquote>
1395 <p><tt class="literal"><span class="pre">__tostring</span></tt> isn't really an operator, but it's the metamethod that is called
1396 by the standard library's <tt class="literal"><span class="pre">tostring()</span></tt> function. There's one strange behavior
1397 regarding binary operators. You are not guaranteed that the self pointer you
1398 get actually refers to an instance of your class. This is because Lua doesn't
1399 distinguish the two cases where you get the other operand as left hand value or
1400 right hand value. Consider the following examples:</p>
1401 <pre class="literal-block">
1402 class 'my_class'
1404 function my_class:__init(v)
1405 self.val = v
1408 function my_class:__sub(v)
1409 return my_class(self.val - v.val)
1412 function my_class:__tostring()
1413 return self.val
1415 </pre>
1416 <p>This will work well as long as you only subtracts instances of my_class with
1417 each other. But If you want to be able to subtract ordinary numbers from your
1418 class too, you have to manually check the type of both operands, including the
1419 self object.</p>
1420 <pre class="literal-block">
1421 function my_class:__sub(v)
1422 if (type(self) == 'number') then
1423 return my_class(self - v.val)
1425 elseif (type(v) == 'number') then
1426 return my_class(self.val - v)
1428 else
1429 -- assume both operands are instances of my_class
1430 return my_class(self.val - v.val)
1434 </pre>
1435 <p>The reason why <tt class="literal"><span class="pre">__sub</span></tt> is used as an example is because subtraction is not
1436 commutative (the order of the operands matter). That's why luabind cannot
1437 change order of the operands to make the self reference always refer to the
1438 actual class instance.</p>
1439 <p>If you have two different Lua classes with an overloaded operator, the operator
1440 of the right hand side type will be called. If the other operand is a C++ class
1441 with the same operator overloaded, it will be prioritized over the Lua class'
1442 operator. If none of the C++ overloads matches, the Lua class operator will be
1443 called.</p>
1444 </div>
1445 <div class="section" id="finalizers">
1446 <h2><a name="finalizers">10.3   Finalizers</a></h2>
1447 <p>If an object needs to perform actions when it's collected we provide a
1448 <tt class="literal"><span class="pre">__finalize</span></tt> function that can be overridden in lua-classes. The
1449 <tt class="literal"><span class="pre">__finalize</span></tt> functions will be called on all classes in the inheritance
1450 chain, starting with the most derived type.</p>
1451 <pre class="literal-block">
1454 function lua_testclass:__finalize()
1455 -- called when the an object is collected
1457 </pre>
1458 </div>
1459 <div class="section" id="slicing">
1460 <h2><a name="slicing">10.4   Slicing</a></h2>
1461 <p>If your lua C++ classes don't have wrappers (see <a class="reference" href="#deriving-in-lua">Deriving in lua</a>) and
1462 you derive from them in lua, they may be sliced. Meaning, if an object
1463 is passed into C++ as a pointer to its base class, the lua part will be
1464 separated from the C++ base part. This means that if you call virtual
1465 functions on that C++ object, thyey will not be dispatched to the lua
1466 class. It also means that if you adopt the object, the lua part will be
1467 garbage collected.</p>
1468 <pre class="literal-block">
1469 +--------------------+
1470 | C++ object | &lt;- ownership of this part is transferred
1471 | | to c++ when adopted
1472 +--------------------+
1473 | lua class instance | &lt;- this part is garbage collected when
1474 | and lua members | instance is adopted, since it cannot
1475 +--------------------+ be held by c++.
1476 </pre>
1477 <p>The problem can be illustrated by this example:</p>
1478 <pre class="literal-block">
1479 struct A {};
1481 A* filter_a(A* a) { return a; }
1482 void adopt_a(A* a) { delete a; }
1483 </pre>
1484 <pre class="literal-block">
1485 using namespace luabind;
1487 module(L)
1489 class_&lt;A&gt;(&quot;A&quot;),
1490 def(&quot;filter_a&quot;, &amp;filter_a),
1491 def(&quot;adopt_a&quot;, &amp;adopt_a, adopt(_1))
1493 </pre>
1494 <p>In lua:</p>
1495 <pre class="literal-block">
1496 a = A()
1497 b = filter_a(a)
1498 adopt_a(b)
1499 </pre>
1500 <p>In this example, lua cannot know that <tt class="literal"><span class="pre">b</span></tt> actually is the same object as
1501 <tt class="literal"><span class="pre">a</span></tt>, and it will therefore consider the object to be owned by the C++ side.
1502 When the <tt class="literal"><span class="pre">b</span></tt> pointer then is adopted, a runtime error will be raised because
1503 an object not owned by lua is being adopted to C++.</p>
1504 <p>If you have a wrapper for your class, none of this will happen, see
1505 <a class="reference" href="#object-identity">Object identity</a>.</p>
1506 </div>
1507 </div>
1508 <div class="section" id="exceptions">
1509 <h1><a name="exceptions">11   Exceptions</a></h1>
1510 <p>If any of the functions you register throws an exception when called, that
1511 exception will be caught by luabind and converted to an error string and
1512 <tt class="literal"><span class="pre">lua_error()</span></tt> will be invoked. If the exception is a <tt class="literal"><span class="pre">std::exception</span></tt> or a
1513 <tt class="literal"><span class="pre">const</span> <span class="pre">char*</span></tt> the string that is pushed on the Lua stack, as error message,
1514 will be the string returned by <tt class="literal"><span class="pre">std::exception::what()</span></tt> or the string itself
1515 respectively. If the exception is unknown, a generic string saying that the
1516 function threw an exception will be pushed.</p>
1517 <p>Exceptions thrown from user defined functions have to be caught by luabind. If
1518 they weren't they would be thrown through Lua itself, which is usually compiled
1519 as C code and doesn't support the stack-unwinding that exceptions imply.</p>
1520 <p>Any function that invokes Lua code may throw <tt class="literal"><span class="pre">luabind::error</span></tt>. This exception
1521 means that a Lua run-time error occurred. The error message is found on top of
1522 the Lua stack. The reason why the exception doesn't contain the error string
1523 itself is because it would then require heap allocation which may fail. If an
1524 exception class throws an exception while it is being thrown itself, the
1525 application will be terminated.</p>
1526 <p>Error's synopsis is:</p>
1527 <pre class="literal-block">
1528 class error : public std::exception
1530 public:
1531 error(lua_State*);
1532 lua_State* state() const throw();
1533 virtual const char* what() const throw();
1535 </pre>
1536 <p>The state function returns a pointer to the Lua state in which the error was
1537 thrown. This pointer may be invalid if you catch this exception after the lua
1538 state is destructed. If the Lua state is valid you can use it to retrieve the
1539 error message from the top of the Lua stack.</p>
1540 <p>An example of where the Lua state pointer may point to an invalid state
1541 follows:</p>
1542 <pre class="literal-block">
1543 struct lua_state
1545 lua_state(lua_State* L): m_L(L) {}
1546 ~lua_state() { lua_close(m_L); }
1547 operator lua_State*() { return m_L; }
1548 lua_State* m_L;
1551 int main()
1555 lua_state L = lua_open();
1556 /* ... */
1558 catch(luabind::error&amp; e)
1560 lua_State* L = e.state();
1561 // L will now point to the destructed
1562 // Lua state and be invalid
1563 /* ... */
1566 </pre>
1567 <p>There's another exception that luabind may throw: <tt class="literal"><span class="pre">luabind::cast_failed</span></tt>,
1568 this exception is thrown from <tt class="literal"><span class="pre">call_function&lt;&gt;</span></tt>, <tt class="literal"><span class="pre">call_member&lt;&gt;</span></tt> or when
1569 <tt class="literal"><span class="pre">functor&lt;&gt;</span></tt> is invoked. It means that the return value from the Lua function
1570 couldn't be converted to a C++ value. It is also thrown from <tt class="literal"><span class="pre">object_cast&lt;&gt;</span></tt>
1571 if the cast cannot be made.</p>
1572 <p>The synopsis for <tt class="literal"><span class="pre">luabind::cast_failed</span></tt> is:</p>
1573 <pre class="literal-block">
1574 class cast_failed : public std::exception
1576 public:
1577 cast_failed(lua_State*);
1578 lua_State* state() const throw();
1579 LUABIND_TYPE_INFO info() const throw();
1580 virtual const char* what() const throw();
1582 </pre>
1583 <p>Again, the state member function returns a pointer to the Lua state where the
1584 error occurred. See the example above to see where this pointer may be invalid.</p>
1585 <p>The info member function returns the user defined <tt class="literal"><span class="pre">LUABIND_TYPE_INFO</span></tt>, which
1586 defaults to a <tt class="literal"><span class="pre">const</span> <span class="pre">std::type_info*</span></tt>. This type info describes the type that
1587 we tried to cast a Lua value to.</p>
1588 <p>If you have defined <tt class="literal"><span class="pre">LUABIND_NO_EXCEPTIONS</span></tt> none of these exceptions will be
1589 thrown, instead you can set two callback functions that are called instead.
1590 These two functions are only defined if <tt class="literal"><span class="pre">LUABIND_NO_EXCEPTIONS</span></tt> are defined.</p>
1591 <pre class="literal-block">
1592 luabind::set_error_callback(void(*)(lua_State*))
1593 </pre>
1594 <p>The function you set will be called when a runtime-error occur in Lua code. You
1595 can find an error message on top of the Lua stack. This function is not
1596 expected to return, if it does luabind will call <tt class="literal"><span class="pre">std::terminate()</span></tt>.</p>
1597 <pre class="literal-block">
1598 luabind::set_cast_failed_callback(void(*)(lua_State*, LUABIND_TYPE_INFO))
1599 </pre>
1600 <p>The function you set is called instead of throwing <tt class="literal"><span class="pre">cast_failed</span></tt>. This function
1601 is not expected to return, if it does luabind will call <tt class="literal"><span class="pre">std::terminate()</span></tt>.</p>
1602 </div>
1603 <div class="section" id="policies">
1604 <h1><a name="policies">12   Policies</a></h1>
1605 <p>Sometimes it is necessary to control how luabind passes arguments and return
1606 value, to do this we have policies. All policies use an index to associate
1607 them with an argument in the function signature. These indices are <tt class="literal"><span class="pre">result</span></tt>
1608 and <tt class="literal"><span class="pre">_N</span></tt> (where <tt class="literal"><span class="pre">N</span> <span class="pre">&gt;=</span> <span class="pre">1</span></tt>). When dealing with member functions <tt class="literal"><span class="pre">_1</span></tt> refers
1609 to the <tt class="literal"><span class="pre">this</span></tt> pointer.</p>
1610 <div class="contents topic" id="policies-currently-implemented">
1611 <p class="topic-title"><a name="policies-currently-implemented">Policies currently implemented</a></p>
1612 <ul class="auto-toc simple">
1613 <li><a class="reference" href="#adopt" id="id92" name="id92">12.1   adopt</a></li>
1614 <li><a class="reference" href="#dependency" id="id93" name="id93">12.2   dependency</a></li>
1615 <li><a class="reference" href="#out-value" id="id94" name="id94">12.3   out_value</a></li>
1616 <li><a class="reference" href="#pure-out-value" id="id95" name="id95">12.4   pure_out_value</a></li>
1617 <li><a class="reference" href="#return-reference-to" id="id96" name="id96">12.5   return_reference_to</a></li>
1618 <li><a class="reference" href="#copy" id="id97" name="id97">12.6   copy</a></li>
1619 <li><a class="reference" href="#discard-result" id="id98" name="id98">12.7   discard_result</a></li>
1620 <li><a class="reference" href="#return-stl-iterator" id="id99" name="id99">12.8   return_stl_iterator</a></li>
1621 <li><a class="reference" href="#raw" id="id100" name="id100">12.9   raw</a></li>
1622 <li><a class="reference" href="#yield" id="id101" name="id101">12.10   yield</a></li>
1623 </ul>
1624 </div>
1625 <div class="section" id="adopt">
1626 <h2><a name="adopt">12.1   adopt</a></h2>
1627 <div class="section" id="motivation">
1628 <h3><a name="motivation">12.1.1   Motivation</a></h3>
1629 <p>Used to transfer ownership across language boundaries.</p>
1630 </div>
1631 <div class="section" id="defined-in">
1632 <h3><a name="defined-in">12.1.2   Defined in</a></h3>
1633 <pre class="literal-block">
1634 #include &lt;luabind/adopt_policy.hpp&gt;
1635 </pre>
1636 </div>
1637 <div class="section" id="synopsis">
1638 <h3><a name="synopsis">12.1.3   Synopsis</a></h3>
1639 <pre class="literal-block">
1640 adopt(index)
1641 </pre>
1642 </div>
1643 <div class="section" id="parameters">
1644 <h3><a name="parameters">12.1.4   Parameters</a></h3>
1645 <table border class="table">
1646 <colgroup>
1647 <col width="17%" />
1648 <col width="83%" />
1649 </colgroup>
1650 <thead valign="bottom">
1651 <tr><th>Parameter</th>
1652 <th>Purpose</th>
1653 </tr>
1654 </thead>
1655 <tbody valign="top">
1656 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
1657 <td>The index which should transfer ownership, <tt class="literal"><span class="pre">_N</span></tt> or <tt class="literal"><span class="pre">result</span></tt></td>
1658 </tr>
1659 </tbody>
1660 </table>
1661 </div>
1662 <div class="section" id="example">
1663 <h3><a name="example">12.1.5   Example</a></h3>
1664 <pre class="literal-block">
1665 X* create()
1667 return new X;
1672 module(L)
1674 def(&quot;create&quot;, &amp;create, <strong>adopt(result)</strong>)
1676 </pre>
1677 </div>
1678 </div>
1679 <div class="section" id="dependency">
1680 <h2><a name="dependency">12.2   dependency</a></h2>
1681 <div class="section" id="id2">
1682 <h3><a name="id2">12.2.1   Motivation</a></h3>
1683 <p>The dependency policy is used to create life-time dependencies between values.
1684 This is needed for example when returning internal references to some class.</p>
1685 </div>
1686 <div class="section" id="id3">
1687 <h3><a name="id3">12.2.2   Defined in</a></h3>
1688 <pre class="literal-block">
1689 #include &lt;luabind/dependency_policy.hpp&gt;
1690 </pre>
1691 </div>
1692 <div class="section" id="id4">
1693 <h3><a name="id4">12.2.3   Synopsis</a></h3>
1694 <pre class="literal-block">
1695 dependency(nurse_index, patient_index)
1696 </pre>
1697 </div>
1698 <div class="section" id="id5">
1699 <h3><a name="id5">12.2.4   Parameters</a></h3>
1700 <table border class="table">
1701 <colgroup>
1702 <col width="23%" />
1703 <col width="77%" />
1704 </colgroup>
1705 <thead valign="bottom">
1706 <tr><th>Parameter</th>
1707 <th>Purpose</th>
1708 </tr>
1709 </thead>
1710 <tbody valign="top">
1711 <tr><td><tt class="literal"><span class="pre">nurse_index</span></tt></td>
1712 <td>The index which will keep the patient alive.</td>
1713 </tr>
1714 <tr><td><tt class="literal"><span class="pre">patient_index</span></tt></td>
1715 <td>The index which will be kept alive.</td>
1716 </tr>
1717 </tbody>
1718 </table>
1719 </div>
1720 <div class="section" id="id6">
1721 <h3><a name="id6">12.2.5   Example</a></h3>
1722 <pre class="literal-block">
1723 struct X
1725 B member;
1726 B&amp; get() { return member; }
1729 module(L)
1731 class_&lt;X&gt;(&quot;X&quot;)
1732 .def(&quot;get&quot;, &amp;X::get, <strong>dependency(result, _1)</strong>)
1734 </pre>
1735 </div>
1736 </div>
1737 <div class="section" id="out-value">
1738 <h2><a name="out-value">12.3   out_value</a></h2>
1739 <div class="section" id="id7">
1740 <h3><a name="id7">12.3.1   Motivation</a></h3>
1741 <p>This policy makes it possible to wrap functions that take non-const references
1742 or pointer to non-const as it's parameters with the intention to write return
1743 values to them.</p>
1744 </div>
1745 <div class="section" id="id8">
1746 <h3><a name="id8">12.3.2   Defined in</a></h3>
1747 <pre class="literal-block">
1748 #include &lt;luabind/out_value_policy.hpp&gt;
1749 </pre>
1750 </div>
1751 <div class="section" id="id9">
1752 <h3><a name="id9">12.3.3   Synopsis</a></h3>
1753 <pre class="literal-block">
1754 out_value(index, policies = none)
1755 </pre>
1756 </div>
1757 <div class="section" id="id10">
1758 <h3><a name="id10">12.3.4   Parameters</a></h3>
1759 <table border class="table">
1760 <colgroup>
1761 <col width="20%" />
1762 <col width="80%" />
1763 </colgroup>
1764 <thead valign="bottom">
1765 <tr><th>Parameter</th>
1766 <th>Purpose</th>
1767 </tr>
1768 </thead>
1769 <tbody valign="top">
1770 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
1771 <td>The index of the parameter to be used as an out parameter.</td>
1772 </tr>
1773 <tr><td><tt class="literal"><span class="pre">policies</span></tt></td>
1774 <td>The policies used internally to convert the out parameter
1775 to/from Lua. <tt class="literal"><span class="pre">_1</span></tt> means <strong>to</strong> C++, <tt class="literal"><span class="pre">_2</span></tt> means <strong>from</strong>
1776 C++.</td>
1777 </tr>
1778 </tbody>
1779 </table>
1780 </div>
1781 <div class="section" id="id11">
1782 <h3><a name="id11">12.3.5   Example</a></h3>
1783 <pre class="literal-block">
1784 void f1(float&amp; val) { val = val + 10.f; }
1785 void f2(float* val) { *val = *val + 10.f; }
1787 module(L)
1789 def(&quot;f&quot;, &amp;f, <strong>out_value(_1)</strong>)
1792 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1793 &gt; print(f1(10))
1795 &gt; print(f2(10))
1797 </pre>
1798 </div>
1799 </div>
1800 <div class="section" id="pure-out-value">
1801 <h2><a name="pure-out-value">12.4   pure_out_value</a></h2>
1802 <div class="section" id="id12">
1803 <h3><a name="id12">12.4.1   Motivation</a></h3>
1804 <p>This works exactly like <tt class="literal"><span class="pre">out_value</span></tt>, except that it will pass a
1805 default constructed object instead of converting an argument from
1806 Lua.</p>
1807 </div>
1808 <div class="section" id="id13">
1809 <h3><a name="id13">12.4.2   Defined in</a></h3>
1810 <pre class="literal-block">
1811 #include &lt;luabind/out_value_policy.hpp&gt;
1812 </pre>
1813 </div>
1814 <div class="section" id="id14">
1815 <h3><a name="id14">12.4.3   Synopsis</a></h3>
1816 <pre class="literal-block">
1817 pure_out_value(index, policies = none)
1818 </pre>
1819 </div>
1820 <div class="section" id="id15">
1821 <h3><a name="id15">12.4.4   Parameters</a></h3>
1822 <table border class="table">
1823 <colgroup>
1824 <col width="20%" />
1825 <col width="80%" />
1826 </colgroup>
1827 <thead valign="bottom">
1828 <tr><th>Parameter</th>
1829 <th>Purpose</th>
1830 </tr>
1831 </thead>
1832 <tbody valign="top">
1833 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
1834 <td>The index of the parameter to be used as an out parameter.</td>
1835 </tr>
1836 <tr><td><tt class="literal"><span class="pre">policies</span></tt></td>
1837 <td>The policies used internally to convert the out parameter
1838 to Lua. <tt class="literal"><span class="pre">_1</span></tt> is used as the interal index.</td>
1839 </tr>
1840 </tbody>
1841 </table>
1842 </div>
1843 <div class="section" id="id16">
1844 <h3><a name="id16">12.4.5   Example</a></h3>
1845 <pre class="literal-block">
1846 void f1(float&amp; val) { val = 10.f; }
1847 void f2(float* val) { *val = 10.f; }
1849 module(L)
1851 def(&quot;f&quot;, &amp;f, <strong>pure_out_value(_1)</strong>)
1854 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1855 &gt; print(f1())
1857 &gt; print(f2())
1859 </pre>
1860 </div>
1861 </div>
1862 <div class="section" id="return-reference-to">
1863 <h2><a name="return-reference-to">12.5   return_reference_to</a></h2>
1864 <div class="section" id="id17">
1865 <h3><a name="id17">12.5.1   Motivation</a></h3>
1866 <p>It is very common to return references to arguments or the this-pointer to
1867 allow for chaining in C++.</p>
1868 <pre class="literal-block">
1869 struct A
1871 float val;
1873 A&amp; set(float v)
1875 val = v;
1876 return *this;
1879 </pre>
1880 <p>When luabind generates code for this, it will create a new object for the
1881 return-value, pointing to the self-object. This isn't a problem, but could be a
1882 bit inefficient. When using the return_reference_to-policy we have the ability
1883 to tell luabind that the return-value is already on the lua stack.</p>
1884 </div>
1885 <div class="section" id="id18">
1886 <h3><a name="id18">12.5.2   Defined in</a></h3>
1887 <pre class="literal-block">
1888 #include &lt;luabind/return_reference_to_policy.hpp&gt;
1889 </pre>
1890 </div>
1891 <div class="section" id="id19">
1892 <h3><a name="id19">12.5.3   Synopsis</a></h3>
1893 <pre class="literal-block">
1894 return_reference_to(index)
1895 </pre>
1896 </div>
1897 <div class="section" id="id20">
1898 <h3><a name="id20">12.5.4   Parameters</a></h3>
1899 <table border class="table">
1900 <colgroup>
1901 <col width="13%" />
1902 <col width="87%" />
1903 </colgroup>
1904 <thead valign="bottom">
1905 <tr><th>Parameter</th>
1906 <th>Purpose</th>
1907 </tr>
1908 </thead>
1909 <tbody valign="top">
1910 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
1911 <td>The argument index to return a reference to, any argument but
1912 not <tt class="literal"><span class="pre">result</span></tt>.</td>
1913 </tr>
1914 </tbody>
1915 </table>
1916 </div>
1917 <div class="section" id="id21">
1918 <h3><a name="id21">12.5.5   Example</a></h3>
1919 <pre class="literal-block">
1920 struct A
1922 float val;
1924 A&amp; set(float v)
1926 val = v;
1927 return *this;
1931 module(L)
1933 class_&lt;A&gt;(&quot;A&quot;)
1934 .def(constructor&lt;&gt;())
1935 .def(&quot;set&quot;, &amp;A::set, <strong>return_reference_to(_1)</strong>)
1937 </pre>
1938 <div class="warning">
1939 <p class="admonition-title">Warning</p>
1940 This policy ignores all type information and should be used only it
1941 situations where the parameter type is a perfect match to the
1942 return-type (such as in the example).</div>
1943 </div>
1944 </div>
1945 <div class="section" id="copy">
1946 <h2><a name="copy">12.6   copy</a></h2>
1947 <div class="section" id="id22">
1948 <h3><a name="id22">12.6.1   Motivation</a></h3>
1949 <p>This will make a copy of the parameter. This is the default behavior when
1950 passing parameters by-value. Note that this can only be used when passing from
1951 C++ to Lua. This policy requires that the parameter type has an accessible copy
1952 constructor.</p>
1953 </div>
1954 <div class="section" id="id23">
1955 <h3><a name="id23">12.6.2   Defined in</a></h3>
1956 <pre class="literal-block">
1957 #include &lt;luabind/copy_policy.hpp&gt;
1958 </pre>
1959 </div>
1960 <div class="section" id="id24">
1961 <h3><a name="id24">12.6.3   Synopsis</a></h3>
1962 <pre class="literal-block">
1963 copy(index)
1964 </pre>
1965 </div>
1966 <div class="section" id="id25">
1967 <h3><a name="id25">12.6.4   Parameters</a></h3>
1968 <table border class="table">
1969 <colgroup>
1970 <col width="17%" />
1971 <col width="83%" />
1972 </colgroup>
1973 <thead valign="bottom">
1974 <tr><th>Parameter</th>
1975 <th>Purpose</th>
1976 </tr>
1977 </thead>
1978 <tbody valign="top">
1979 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
1980 <td>The index to copy. <tt class="literal"><span class="pre">result</span></tt> when used while wrapping C++
1981 functions. <tt class="literal"><span class="pre">_N</span></tt> when passing arguments to Lua.</td>
1982 </tr>
1983 </tbody>
1984 </table>
1985 </div>
1986 <div class="section" id="id26">
1987 <h3><a name="id26">12.6.5   Example</a></h3>
1988 <pre class="literal-block">
1989 X* get()
1991 static X instance;
1992 return &amp;instance;
1997 module(L)
1999 def(&quot;create&quot;, &amp;create, <strong>copy(result)</strong>)
2001 </pre>
2002 </div>
2003 </div>
2004 <div class="section" id="discard-result">
2005 <h2><a name="discard-result">12.7   discard_result</a></h2>
2006 <div class="section" id="id27">
2007 <h3><a name="id27">12.7.1   Motivation</a></h3>
2008 <p>This is a very simple policy which makes it possible to throw away
2009 the value returned by a C++ function, instead of converting it to
2010 Lua.</p>
2011 </div>
2012 <div class="section" id="id28">
2013 <h3><a name="id28">12.7.2   Defined in</a></h3>
2014 <pre class="literal-block">
2015 #include &lt;luabind/discard_result_policy.hpp&gt;
2016 </pre>
2017 </div>
2018 <div class="section" id="id29">
2019 <h3><a name="id29">12.7.3   Synopsis</a></h3>
2020 <pre class="literal-block">
2021 discard_result
2022 </pre>
2023 </div>
2024 <div class="section" id="id30">
2025 <h3><a name="id30">12.7.4   Example</a></h3>
2026 <pre class="literal-block">
2027 struct X
2029 X&amp; set(T n)
2032 return *this;
2038 module(L)
2040 class_&lt;X&gt;(&quot;X&quot;)
2041 .def(&quot;set&quot;, &amp;simple::set, <strong>discard_result</strong>)
2043 </pre>
2044 </div>
2045 </div>
2046 <div class="section" id="return-stl-iterator">
2047 <h2><a name="return-stl-iterator">12.8   return_stl_iterator</a></h2>
2048 <div class="section" id="id31">
2049 <h3><a name="id31">12.8.1   Motivation</a></h3>
2050 <p>This policy converts an STL container to a generator function that can be used
2051 in lua to iterate over the container. It works on any container that defines
2052 <tt class="literal"><span class="pre">begin()</span></tt> and <tt class="literal"><span class="pre">end()</span></tt> member functions (they have to return iterators).</p>
2053 </div>
2054 <div class="section" id="id32">
2055 <h3><a name="id32">12.8.2   Defined in</a></h3>
2056 <pre class="literal-block">
2057 #include &lt;luabind/iterator_policy.hpp&gt;
2058 </pre>
2059 </div>
2060 <div class="section" id="id33">
2061 <h3><a name="id33">12.8.3   Synopsis</a></h3>
2062 <pre class="literal-block">
2063 return_stl_iterator
2064 </pre>
2065 </div>
2066 <div class="section" id="id34">
2067 <h3><a name="id34">12.8.4   Example</a></h3>
2068 <pre class="literal-block">
2069 struct X
2071 std::vector&lt;std::string&gt; names;
2076 module(L)
2078 class_&lt;A&gt;(&quot;A&quot;)
2079 .def_readwrite(&quot;names&quot;, &amp;X::names, <strong>return_stl_iterator</strong>)
2084 &gt; a = A()
2085 &gt; for name in a.names do
2086 &gt; print(name)
2087 &gt; end
2088 </pre>
2089 </div>
2090 </div>
2091 <div class="section" id="raw">
2092 <h2><a name="raw">12.9   raw</a></h2>
2093 <div class="section" id="id35">
2094 <h3><a name="id35">12.9.1   Motivation</a></h3>
2095 <p>This converter policy will pass through the <tt class="literal"><span class="pre">lua_State*</span></tt> unmodified.
2096 This can be useful for example when binding functions that need to
2097 return a <tt class="literal"><span class="pre">luabind::object</span></tt>. The parameter will be removed from the
2098 function signature, decreasing the function arity by one.</p>
2099 </div>
2100 <div class="section" id="id36">
2101 <h3><a name="id36">12.9.2   Defined in</a></h3>
2102 <pre class="literal-block">
2103 #include &lt;luabind/raw_policy.hpp&gt;
2104 </pre>
2105 </div>
2106 <div class="section" id="id37">
2107 <h3><a name="id37">12.9.3   Synopsis</a></h3>
2108 <pre class="literal-block">
2109 raw(index)
2110 </pre>
2111 </div>
2112 <div class="section" id="id38">
2113 <h3><a name="id38">12.9.4   Parameters</a></h3>
2114 <table border class="table">
2115 <colgroup>
2116 <col width="17%" />
2117 <col width="83%" />
2118 </colgroup>
2119 <thead valign="bottom">
2120 <tr><th>Parameter</th>
2121 <th>Purpose</th>
2122 </tr>
2123 </thead>
2124 <tbody valign="top">
2125 <tr><td><tt class="literal"><span class="pre">index</span></tt></td>
2126 <td>The index of the lua_State* parameter.</td>
2127 </tr>
2128 </tbody>
2129 </table>
2130 </div>
2131 <div class="section" id="id39">
2132 <h3><a name="id39">12.9.5   Example</a></h3>
2133 <pre class="literal-block">
2134 void greet(lua_State* L)
2136 lua_pushstring(L, &quot;hello&quot;);
2141 module(L)
2143 def(&quot;greet&quot;, &amp;greet, <strong>raw(_1)</strong>)
2146 &gt; print(greet())
2147 hello
2148 </pre>
2149 </div>
2150 </div>
2151 <div class="section" id="yield">
2152 <h2><a name="yield">12.10   yield</a></h2>
2153 <div class="section" id="id40">
2154 <h3><a name="id40">12.10.1   Motivation</a></h3>
2155 <p>Makes a C++ function yield when returning.</p>
2156 </div>
2157 <div class="section" id="id41">
2158 <h3><a name="id41">12.10.2   Defined in</a></h3>
2159 <pre class="literal-block">
2160 #include &lt;luabind/yield_policy.hpp&gt;
2161 </pre>
2162 </div>
2163 <div class="section" id="id42">
2164 <h3><a name="id42">12.10.3   Synopsis</a></h3>
2165 <pre class="literal-block">
2166 yield
2167 </pre>
2168 </div>
2169 <div class="section" id="id43">
2170 <h3><a name="id43">12.10.4   Example</a></h3>
2171 <pre class="literal-block">
2172 void do_thing_that_takes_time()
2179 module(L)
2181 def(&quot;do_thing_that_takes_time&quot;, &amp;do_thing_that_takes_time, <strong>yield</strong>)
2183 </pre>
2184 <!-- old policies section
2185 ===================================================
2187 Copy
2188 - - - -
2190 This will make a copy of the parameter. This is the default behavior when
2191 passing parameters by-value. Note that this can only be used when passing from
2192 C++ to Lua. This policy requires that the parameter type has a copy
2193 constructor.
2195 To use this policy you need to include ``luabind/copy_policy.hpp``.
2198 Adopt
2199 - - - - -
2201 This will transfer ownership of the parameter.
2203 Consider making a factory function in C++ and exposing it to lua::
2205 base* create_base()
2207 return new base();
2212 module(L)
2214 def("create_base", create_base)
2217 Here we need to make sure Lua understands that it should adopt the pointer
2218 returned by the factory-function. This can be done using the adopt-policy.
2222 module(L)
2224 def(L, "create_base", adopt(return_value))
2227 To specify multiple policies we just separate them with '+'.
2231 base* set_and_get_new(base* ptr)
2233 base_ptrs.push_back(ptr);
2234 return new base();
2237 module(L)
2239 def("set_and_get_new", &set_and_get_new,
2240 adopt(return_value) + adopt(_1))
2243 When Lua adopts a pointer, it will call delete on it. This means that it cannot
2244 adopt pointers allocated with another allocator than new (no malloc for
2245 example).
2247 To use this policy you need to include ``luabind/adopt_policy.hpp``.
2250 Dependency
2251 - - - - - - - - - -
2253 The dependency policy is used to create life-time dependencies between values.
2254 Consider the following example::
2256 struct A
2258 B member;
2260 const B& get_member()
2262 return member;
2266 When wrapping this class, we would do something like::
2268 module(L)
2270 class_<A>("A")
2271 .def(constructor<>())
2272 .def("get_member", &A::get_member)
2276 However, since the return value of get_member is a reference to a member of A,
2277 this will create some life-time issues. For example::
2279 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
2280 a = A()
2281 b = a:get_member() - - b points to a member of a
2282 a = nil
2283 collectgarbage(0) - - since there are no references left to a, it is
2284 - - removed
2285 - - at this point, b is pointing into a removed object
2287 When using the dependency-policy, it is possible to tell luabind to tie the
2288 lifetime of one object to another, like this::
2290 module(L)
2292 class_<A>("A")
2293 .def(constructor<>())
2294 .def("get_member", &A::get_member, dependency(result, _1))
2297 This will create a dependency between the return-value of the function, and the
2298 self-object. This means that the self-object will be kept alive as long as the
2299 result is still alive. ::
2301 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
2302 a = A()
2303 b = a:get_member() - - b points to a member of a
2304 a = nil
2305 collectgarbage(0) - - a is dependent on b, so it isn't removed
2306 b = nil
2307 collectgarbage(0) - - all dependencies to a gone, a is removed
2309 To use this policy you need to include ``luabind/dependency_policy.hpp``.
2312 Return reference to
2313 - - - - - - - - - - - - - - - - - - -
2315 It is very common to return references to arguments or the this-pointer to
2316 allow for chaining in C++.
2320 struct A
2322 float val;
2324 A& set(float v)
2326 val = v;
2327 return *this;
2331 When luabind generates code for this, it will create a new object for the
2332 return-value, pointing to the self-object. This isn't a problem, but could be a
2333 bit inefficient. When using the return_reference_to-policy we have the ability
2334 to tell luabind that the return-value is already on the Lua stack.
2338 module(L)
2340 class_<A>("A")
2341 .def(constructor<>())
2342 .def("set", &A::set, return_reference_to(_1))
2345 Instead of creating a new object, luabind will just copy the object that is
2346 already on the stack.
2348 .. warning::
2349 This policy ignores all type information and should be used only it
2350 situations where the parameter type is a perfect match to the
2351 return-type (such as in the example).
2353 To use this policy you need to include ``luabind/return_reference_to_policy.hpp``.
2356 Out value
2357 - - - - - - - - -
2359 This policy makes it possible to wrap functions that take non const references
2360 as its parameters with the intention to write return values to them.
2364 void f(float& val) { val = val + 10.f; }
2370 void f(float* val) { *val = *val + 10.f; }
2372 Can be wrapped by doing::
2374 module(L)
2376 def("f", &f, out_value(_1))
2379 When invoking this function from Lua it will return the value assigned to its
2380 parameter.
2384 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
2385 > a = f(10)
2386 > print(a)
2389 When this policy is used in conjunction with user define types we often need
2390 to do ownership transfers.
2394 struct A;
2396 void f1(A*& obj) { obj = new A(); }
2397 void f2(A** obj) { *obj = new A(); }
2399 Here we need to make sure luabind takes control over object returned, for
2400 this we use the adopt policy::
2402 module(L)
2404 class_<A>("A"),
2405 def("f1", &f1, out_value(_1, adopt(_2)))
2406 def("f2", &f2, out_value(_1, adopt(_2)))
2409 Here we are using adopt as an internal policy to out_value. The index
2410 specified, _2, means adopt will be used to convert the value back to Lua.
2411 Using _1 means the policy will be used when converting from Lua to C++.
2413 To use this policy you need to include ``luabind/out_value_policy.hpp``.
2415 Pure out value
2416 - - - - - - - - - - - - - -
2418 This policy works in exactly the same way as out_value, except that it
2419 replaces the parameters with default-constructed objects.
2423 void get(float& x, float& y)
2425 x = 3.f;
2426 y = 4.f;
2431 module(L)
2433 def("get", &get,
2434 pure_out_value(_1) + pure_out_value(_2))
2439 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
2440 > x, y = get()
2441 > print(x, y)
2444 Like out_value, it is possible to specify an internal policy used then
2445 converting the values back to Lua.
2449 void get(test_class*& obj)
2451 obj = new test_class();
2456 module(L)
2458 def("get", &get, pure_out_value(_1, adopt(_1)))
2462 Discard result
2463 - - - - - - - - - - - - - -
2465 This is a very simple policy which makes it possible to throw away
2466 the value returned by a C++ function, instead of converting it to
2467 Lua. This example makes sure the this reference never gets converted
2468 to Lua.
2472 struct simple
2474 simple& set_name(const std::string& n)
2476 name = n;
2477 return *this;
2480 std::string name;
2485 module(L)
2487 class_<simple>("simple")
2488 .def("set_name", &simple::set_name, discard_result)
2491 To use this policy you need to include ``luabind/discard_result_policy.hpp``.
2494 Return STL iterator
2495 - - - - - - - - - - - - - - - - - - -
2497 This policy converts an STL container to a generator function that can be used
2498 in Lua to iterate over the container. It works on any container that defines
2499 ``begin()`` and ``end()`` member functions (they have to return iterators). It
2500 can be used like this::
2502 struct A
2504 std::vector<std::string> names;
2508 module(L)
2510 class_<A>("A")
2511 .def_readwrite("names", &A::names, return_stl_iterator)
2514 The Lua code to iterate over the container::
2516 a = A()
2518 for name in a.names do
2519 print(name)
2523 To use this policy you need to include ``luabind/iterator_policy.hpp``.
2526 Yield
2527 - - - - -
2529 This policy will cause the function to always yield the current thread when
2530 returning. See the Lua manual for restrictions on yield. -->
2531 </div>
2532 </div>
2533 </div>
2534 <div class="section" id="splitting-up-the-registration">
2535 <h1><a name="splitting-up-the-registration">13   Splitting up the registration</a></h1>
2536 <p>It is possible to split up a module registration into several
2537 translation units without making each registration dependent
2538 on the module it's being registered in.</p>
2539 <p><tt class="literal"><span class="pre">a.cpp</span></tt>:</p>
2540 <pre class="literal-block">
2541 luabind::scope register_a()
2543 return
2544 class_&lt;a&gt;(&quot;a&quot;)
2545 .def(&quot;f&quot;, &amp;a::f)
2548 </pre>
2549 <p><tt class="literal"><span class="pre">b.cpp</span></tt>:</p>
2550 <pre class="literal-block">
2551 luabind::scope register_b()
2553 return
2554 class_&lt;b&gt;(&quot;b&quot;)
2555 .def(&quot;g&quot;, &amp;b::g)
2558 </pre>
2559 <p><tt class="literal"><span class="pre">module_ab.cpp</span></tt>:</p>
2560 <pre class="literal-block">
2561 luabind::scope register_a();
2562 luabind::scope register_b();
2564 void register_module(lua_State* L)
2566 module(&quot;b&quot;, L)
2568 register_a(),
2569 register_b()
2572 </pre>
2573 </div>
2574 <div class="section" id="configuration">
2575 <h1><a name="configuration">14   Configuration</a></h1>
2576 <p>As mentioned in the <a class="reference" href="http://www.lua.org/manual/5.0/manual.html">Lua documentation</a>, it is possible to pass an
2577 error handler function to <tt class="literal"><span class="pre">lua_pcall()</span></tt>. Luabind makes use of
2578 <tt class="literal"><span class="pre">lua_pcall()</span></tt> internally when calling methods and functions. It is
2579 possible to set the error handler function that Luabind will use
2580 globally:</p>
2581 <pre class="literal-block">
2582 typedef void(*pcall_callback_fun)(lua_State*);
2583 void set_pcall_callback(pcall_callback_fun fn);
2584 </pre>
2585 <p>This is primarily useful for adding more information to the error message
2586 returned by a failed protected call.</p>
2587 <div class="section" id="build-options">
2588 <h2><a name="build-options">14.1   Build options</a></h2>
2589 <p>There are a number of configuration options available when building luabind.
2590 It is very important that your project has the exact same conmfiguration
2591 options as the ones given when the library was build! The exceptions are the
2592 <tt class="literal"><span class="pre">LUABIND_MAX_ARITY</span></tt> and <tt class="literal"><span class="pre">LUABIND_MAX_BASES</span></tt> which are template-based
2593 options and only matters when you use the library (which means they can
2594 differ from the settings of the library).</p>
2595 <p>The default settings which will be used if no other settings are given
2596 can be found in <tt class="literal"><span class="pre">luabind/config.hpp</span></tt>.</p>
2597 <p>If you want to change the settings of the library, you can modify the
2598 config file. It is included and used by all makefiles. You can change paths
2599 to Lua and boost in there as well.</p>
2600 <dl>
2601 <dt>LUABIND_MAX_ARITY</dt>
2602 <dd>Controls the maximum arity of functions that are registered with luabind.
2603 You can't register functions that takes more parameters than the number
2604 this macro is set to. It defaults to 5, so, if your functions have greater
2605 arity you have to redefine it. A high limit will increase compilation time.</dd>
2606 <dt>LUABIND_MAX_BASES</dt>
2607 <dd>Controls the maximum number of classes one class can derive from in
2608 luabind (the number of classes specified within <tt class="literal"><span class="pre">bases&lt;&gt;</span></tt>).
2609 <tt class="literal"><span class="pre">LUABIND_MAX_BASES</span></tt> defaults to 4. A high limit will increase
2610 compilation time.</dd>
2611 <dt>LUABIND_NO_ERROR_CHECKING</dt>
2612 <dd><p class="first">If this macro is defined, all the Lua code is expected only to make legal
2613 calls. If illegal function calls are made (e.g. giving parameters that
2614 doesn't match the function signature) they will not be detected by luabind
2615 and the application will probably crash. Error checking could be disabled
2616 when shipping a release build (given that no end-user has access to write
2617 custom Lua code). Note that function parameter matching will be done if a
2618 function is overloaded, since otherwise it's impossible to know which one
2619 was called. Functions will still be able to throw exceptions when error
2620 checking is disabled.</p>
2621 <p class="last">If a functions throws an exception it will be caught by luabind and
2622 propagated with <tt class="literal"><span class="pre">lua_error()</span></tt>.</p>
2623 </dd>
2624 <dt>LUABIND_NO_EXCEPTIONS</dt>
2625 <dd><p class="first">This define will disable all usage of try, catch and throw in luabind.
2626 This will in many cases disable run-time errors, when performing invalid
2627 casts or calling Lua functions that fails or returns values that cannot
2628 be converted by the given policy. luabind requires that no function called
2629 directly or indirectly by luabind throws an exception (throwing exceptions
2630 through Lua has undefined behavior).</p>
2631 <p class="last">Where exceptions are the only way to get an error report from luabind,
2632 they will be replaced with calls to the callback functions set with
2633 <tt class="literal"><span class="pre">set_error_callback()</span></tt> and <tt class="literal"><span class="pre">set_cast_failed_callback()</span></tt>.</p>
2634 </dd>
2635 <dt>LUA_API</dt>
2636 <dd>If you want to link dynamically against Lua, you can set this define to
2637 the import-keyword on your compiler and platform. On windows in devstudio
2638 this should be <tt class="literal"><span class="pre">__declspec(dllimport)</span></tt> if you want to link against Lua
2639 as a dll.</dd>
2640 <dt>LUABIND_EXPORT, LUABIND_IMPORT</dt>
2641 <dd>If you want to link against luabind as a dll (in devstudio), you can
2642 define <tt class="literal"><span class="pre">LUABIND_EXPORT</span></tt> to <tt class="literal"><span class="pre">__declspec(dllexport)</span></tt> and
2643 <tt class="literal"><span class="pre">LUABIND_IMPORT</span></tt> to <tt class="literal"><span class="pre">__declspec(dllimport)</span></tt>.
2644 Note that you have to link against Lua as a dll aswell, to make it work.</dd>
2645 <dt>LUABIND_NO_RTTI</dt>
2646 <dd>You can define this if you don't want luabind to use <tt class="literal"><span class="pre">dynamic_cast&lt;&gt;</span></tt>.
2647 It will disable <a class="reference" href="#object-identity">Object identity</a>.</dd>
2648 <dt>LUABIND_TYPE_INFO, LUABIND_TYPE_INFO_EQUAL(i1,i2), LUABIND_TYPEID(t), LUABIND_INVALID_TYPE_INFO</dt>
2649 <dd><p class="first">If you don't want to use the RTTI supplied by C++ you can supply your own
2650 type-info structure with the <tt class="literal"><span class="pre">LUABIND_TYPE_INFO</span></tt> define. Your type-info
2651 structure must be copyable and must be able to compare itself against
2652 other type-info structures. You supply the compare function through the
2653 <tt class="literal"><span class="pre">LUABIND_TYPE_INFO_EQUAL()</span></tt> define. It should compare the two type-info
2654 structures it is given and return true if they represent the same type and
2655 false otherwise. You also have to supply a function to generate your
2656 type-info structure. You do this through the <tt class="literal"><span class="pre">LUABIND_TYPEID()</span></tt> define.
2657 It should return your type-info structure and it takes a type as its
2658 parameter. That is, a compile time parameter.
2659 <tt class="literal"><span class="pre">LUABIND_INVALID_TYPE_INFO</span></tt> macro should be defined to an invalid type.
2660 No other type should be able to produce this type info. To use it you
2661 probably have to make a traits class with specializations for all classes
2662 that you have type-info for. Like this:</p>
2663 <pre class="literal-block">
2664 class A;
2665 class B;
2666 class C;
2668 template&lt;class T&gt; struct typeinfo_trait;
2670 template&lt;&gt; struct typeinfo_trait&lt;A&gt; { enum { type_id = 0 }; };
2671 template&lt;&gt; struct typeinfo_trait&lt;B&gt; { enum { type_id = 1 }; };
2672 template&lt;&gt; struct typeinfo_trait&lt;C&gt; { enum { type_id = 2 }; };
2673 </pre>
2674 <p>If you have set up your own RTTI system like this (by using integers to
2675 identify types) you can have luabind use it with the following defines:</p>
2676 <pre class="literal-block">
2677 #define LUABIND_TYPE_INFO const std::type_info*
2678 #define LUABIND_TYPEID(t) &amp;typeid(t)
2679 #define LUABIND_TYPE_INFO_EQUAL(i1, i2) *i1 == *i2
2680 #define LUABIND_INVALID_TYPE_INFO &amp;typeid(detail::null_type)
2681 </pre>
2682 <p class="last">Currently the type given through <tt class="literal"><span class="pre">LUABIND_TYPE_INFO</span></tt> must be less-than
2683 comparable!</p>
2684 </dd>
2685 <dt>NDEBUG</dt>
2686 <dd>This define will disable all asserts and should be defined in a release
2687 build.</dd>
2688 </dl>
2689 </div>
2690 </div>
2691 <div class="section" id="implementation-notes">
2692 <h1><a name="implementation-notes">15   Implementation notes</a></h1>
2693 <p>The classes and objects are implemented as user data in Lua. To make sure that
2694 the user data really is the internal structure it is supposed to be, we tag
2695 their metatables. A user data who's metatable contains a boolean member named
2696 <tt class="literal"><span class="pre">__luabind_classrep</span></tt> is expected to be a class exported by luabind. A user
2697 data who's metatable contains a boolean member named <tt class="literal"><span class="pre">__luabind_class</span></tt> is
2698 expected to be an instantiation of a luabind class.</p>
2699 <p>This means that if you make your own user data and tags its metatable with the
2700 exact same names, you can very easily fool luabind and crash the application.</p>
2701 <p>In the Lua registry, luabind keeps an entry called <tt class="literal"><span class="pre">__luabind_classes</span></tt>. It
2702 should not be removed or overwritten.</p>
2703 <p>In the global table, a variable called <tt class="literal"><span class="pre">super</span></tt> is used every time a
2704 constructor in a lua-class is called. This is to make it easy for that
2705 constructor to call its base class' constructor. So, if you have a global
2706 variable named super it may be overwritten. This is probably not the best
2707 solution, and this restriction may be removed in the future.</p>
2708 <p>Luabind uses two upvalues for functions that it registers. The first is a
2709 userdata containing a list of overloads for the function, the other is a light
2710 userdata with the value 0x1337, this last value is used to identify functions
2711 registered by luabind. It should be virtually impossible to have such a pointer
2712 as secondary upvalue by pure chance. This means, if you are trying to replace
2713 an existing function with a luabind function, luabind will see that the
2714 secondary upvalue isn't the magic id number and replace it. If it can identify
2715 the function to be a luabind function, it won't replace it, but rather add
2716 another overload to it.</p>
2717 <p>Inside the luabind namespace, there's another namespace called detail. This
2718 namespace contains non-public classes and are not supposed to be used directly.</p>
2719 </div>
2720 <div class="section" id="error-messages">
2721 <h1><a name="error-messages">16   Error messages</a></h1>
2722 <ul>
2723 <li><pre class="first literal-block">
2724 the attribute '<em>class-name.attribute-name</em>' is read only
2725 </pre>
2726 <p>There is no data member named <em>attribute-name</em> in the class <em>class-name</em>,
2727 or there's no setter-method registered on that property name. See the
2728 properties section.</p>
2729 </li>
2730 <li><pre class="first literal-block">
2731 the attribute '<em>class-name.attribute-name</em>' is of type: (<em>class-name</em>) and does not match (<em>class_name</em>)
2732 </pre>
2733 <p>This error is generated if you try to assign an attribute with a value
2734 of a type that cannot be converted to the attribute's type.</p>
2735 </li>
2736 <li><pre class="first literal-block">
2737 <em>class-name()</em> threw an exception, <em>class-name:function-name()</em> threw an exception
2738 </pre>
2739 <p>The class' constructor or member function threw an unknown exception.
2740 Known exceptions are const char*, std::exception. See the
2741 <a class="reference" href="#exceptions">exceptions</a> section.</p>
2742 </li>
2743 <li><pre class="first literal-block">
2744 no overload of '<em>class-name:function-name</em>' matched the arguments (<em>parameter-types</em>)
2745 no match for function call '<em>function-name</em>' with the parameters (<em>parameter-types</em>)
2746 no constructor of <em>class-name</em> matched the arguments (<em>parameter-types</em>)
2747 no operator <em>operator-name</em> matched the arguments (<em>parameter-types</em>)
2748 </pre>
2749 <p>No function/operator with the given name takes the parameters you gave
2750 it. You have either misspelled the function name, or given it incorrect
2751 parameters. This error is followed by a list of possible candidate
2752 functions to help you figure out what parameter has the wrong type. If
2753 the candidate list is empty there's no function at all with that name.
2754 See the signature matching section.</p>
2755 </li>
2756 <li><pre class="first literal-block">
2757 call of overloaded '<em>class-name:function-name*(*parameter-types</em>)' is ambiguous
2758 ambiguous match for function call '<em>function-name</em>' with the parameters (<em>parameter-types</em>)
2759 call of overloaded constructor '<em>class-name*(*parameter-types</em>)' is ambiguous
2760 call of overloaded operator <em>operator-name</em> (<em>parameter-types</em>) is ambiguous
2761 </pre>
2762 <p>This means that the function/operator you are trying to call has at least
2763 one other overload that matches the arguments just as good as the first
2764 overload.</p>
2765 </li>
2766 <li><pre class="first literal-block">
2767 cannot derive from C++ class '<em>class-name</em>'. It does not have a wrapped type.
2768 </pre>
2769 </li>
2770 </ul>
2771 </div>
2772 <div class="section" id="faq">
2773 <h1><a name="faq">17   FAQ</a></h1>
2774 <dl>
2775 <dt>What's up with __cdecl and __stdcall?</dt>
2776 <dd>If you're having problem with functions
2777 that cannot be converted from <tt class="literal"><span class="pre">void</span> <span class="pre">(__stdcall</span> <span class="pre">*)(int,int)</span></tt> to
2778 <tt class="literal"><span class="pre">void</span> <span class="pre">(__cdecl*)(int,int)</span></tt>. You can change the project settings to make the
2779 compiler generate functions with __cdecl calling conventions. This is
2780 a problem in developer studio.</dd>
2781 <dt>What's wrong with functions taking variable number of arguments?</dt>
2782 <dd>You cannot register a function with ellipses in its signature. Since
2783 ellipses don't preserve type safety, those should be avoided anyway.</dd>
2784 <dt>Internal structure overflow in VC</dt>
2785 <dd>If you, in visual studio, get fatal error C1204: compiler limit :
2786 internal structure overflow. You should try to split that compilation
2787 unit up in smaller ones.</dd>
2788 </dl>
2789 <!-- the three entries above were removed, why? -->
2790 <dl>
2791 <dt>What's wrong with precompiled headers in VC?</dt>
2792 <dd>Visual Studio doesn't like anonymous namespaces in its precompiled
2793 headers. If you encounter this problem you can disable precompiled
2794 headers for the compilation unit (cpp-file) that uses luabind.</dd>
2795 <dt>error C1076: compiler limit - internal heap limit reached in VC</dt>
2796 <dd>In visual studio you will probably hit this error. To fix it you have to
2797 increase the internal heap with a command-line option. We managed to
2798 compile the test suit with /Zm300, but you may need a larger heap then
2799 that.</dd>
2800 <dt>error C1055: compiler limit : out of keys in VC</dt>
2801 <dd>It seems that this error occurs when too many assert() are used in a
2802 program, or more specifically, the __LINE__ macro. It seems to be fixed by
2803 changing /ZI (Program database for edit and continue) to /Zi
2804 (Program database).</dd>
2805 <dt>How come my executable is huge?</dt>
2806 <dd><p class="first">If you're compiling in debug mode, you will probably have a lot of
2807 debug-info and symbols (luabind consists of a lot of functions). Also,
2808 if built in debug mode, no optimizations were applied, luabind relies on
2809 that the compiler is able to inline functions. If you built in release
2810 mode, try running strip on your executable to remove export-symbols,
2811 this will trim down the size.</p>
2812 <p class="last">Our tests suggests that cygwin's gcc produces much bigger executables
2813 compared to gcc on other platforms and other compilers.</p>
2814 </dd>
2815 </dl>
2816 <!-- HUH?! // check the magic number that identifies luabind's functions -->
2817 <dl>
2818 <dt>Can I register class templates with luabind?</dt>
2819 <dd><p class="first">Yes you can, but you can only register explicit instantiations of the
2820 class. Because there's no Lua counterpart to C++ templates. For example,
2821 you can register an explicit instantiation of std::vector&lt;&gt; like this:</p>
2822 <pre class="last literal-block">
2823 module(L)
2825 class_&lt;std::vector&lt;int&gt; &gt;(&quot;vector&quot;)
2826 .def(constructor&lt;int&gt;)
2827 .def(&quot;push_back&quot;, &amp;std::vector&lt;int&gt;::push_back)
2829 </pre>
2830 </dd>
2831 </dl>
2832 <!-- Again, irrelevant to docs: Note that the space between the two > is required by C++. -->
2833 <dl>
2834 <dt>Do I have to register destructors for my classes?</dt>
2835 <dd><p class="first">No, the destructor of a class is always called by luabind when an
2836 object is collected. Note that Lua has to own the object to collect it.
2837 If you pass it to C++ and gives up ownership (with adopt policy) it will
2838 no longer be owned by Lua, and not collected.</p>
2839 <p class="last">If you have a class hierarchy, you should make the destructor virtual if
2840 you want to be sure that the correct destructor is called (this apply to C++
2841 in general).</p>
2842 </dd>
2843 </dl>
2844 <!-- And again, the above is irrelevant to docs. This isn't a general C++ FAQ. -->
2845 <dl>
2846 <dt>Fatal Error C1063 compiler limit : compiler stack overflow in VC</dt>
2847 <dd>VC6.5 chokes on warnings, if you are getting alot of warnings from your
2848 code try suppressing them with a pragma directive, this should solve the
2849 problem.</dd>
2850 <dt>Crashes when linking against luabind as a dll in windows</dt>
2851 <dd>When you build luabind, Lua and you project, make sure you link against
2852 the runtime dynamically (as a dll).</dd>
2853 <dt>I cannot register a function with a non-const parameter</dt>
2854 <dd>This is because there is no way to get a reference to a Lua value. Have
2855 a look at out_value and pure_out_value policies.</dd>
2856 </dl>
2857 </div>
2858 <div class="section" id="known-issues">
2859 <h1><a name="known-issues">18   Known issues</a></h1>
2860 <ul class="simple">
2861 <li>You cannot use strings with extra nulls in them as member names that refers
2862 to C++ members.</li>
2863 <li>If one class registers two functions with the same name and the same
2864 signature, there's currently no error. The last registered function will
2865 be the one that's used.</li>
2866 <li>In VC7, classes can not be called test.</li>
2867 <li>If you register a function and later rename it, error messages will use the
2868 original function name.</li>
2869 <li>luabind does not support class hierarchies with virtual inheritance. Casts are
2870 done with static pointer offsets.</li>
2871 </ul>
2872 <!-- remove? - Visual studio have problems selecting the correct overload of std::swap()
2873 for luabind::object. -->
2874 </div>
2875 <div class="section" id="acknowledgments">
2876 <h1><a name="acknowledgments">19   Acknowledgments</a></h1>
2877 <p>Written by Daniel Wallin and Arvid Norberg. © Copyright 2003.
2878 All rights reserved.</p>
2879 <p>Evan Wies has contributed with thorough testing, countless bug reports
2880 and feature ideas.</p>
2881 <p>This library was highly inspired by Dave Abrahams' <a class="reference" href="http://www.boost.org/libraries/python">Boost.Python</a> library.</p>
2882 </div>
2883 </div>
2884 <hr class="footer" />
2885 <div class="footer">
2886 <a class="reference" href="docs.rst">View document source</a>.
2887 Generated on: 2004-08-08 14:21 UTC.
2888 Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
2889 </div>
2890 </body>
2891 </html>