*** empty log message ***
[luabind.git] / doc / docs.html
blob5095b6574e61d616ad1e0e143d91e27e94db5caf
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
3 <html>
4 <head>
5 <title>luabind beta documentation</title>
6 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7 <link href="style.css" type="text/css" rel="stylesheet">
8 </head>
10 <body>
12 <p>This library is currently in public beta phase. This documentation should be considered beta as well.
13 Please report any grammatical corrections/spelling corrections.</p>
15 <h1>Table of contents</h1>
17 <ol>
18 <li><a href="#intro">Introduction</a>
19 <ul>
20 <li><a href="#features">features</a>
21 <li><a href="#compilers">tested compilers</a>
22 </ul>
23 <li><a href="#build">Building luabind</a>
24 <li><a href="#basic">Basic usage</a>
25 <li><a href="#scopes">Scopes</a>
26 <li><a href="#fun">Binding functions to lua</a>
27 <ul>
28 <li><a href="#fun_overload">overloaded functions</a>
29 <li><a href="#fun_sigmatch">signature matching</a>
30 <li><a href="#lua_fun">calling lua functions</a>
31 </ul>
32 <li><a href="#bind_classes">Binding classes to lua</a>
33 <ul>
34 <li><a href="#class_property">properties</a>
35 <li><a href="#class_enum">enums</a>
36 <li><a href="#class_operator">operators</a>
37 <li><a href="#class_derived">derived classes</a>
38 <li><a href="#class_smart_pointers">smart_pointers</a>
39 </ul>
40 <li><a href="#object">Object</a>
41 <ul>
42 <li><a href="#object_iterators">iterators</a>
43 <li><a href="#object_functions">related functions</a>
44 <li><a href="#functor">functor</a>
45 </ul>
46 <li><a href="#class_lua" >Defining classes in lua</a>
47 <ul>
48 <li><a href="#class_lua_derive">deriving in lua</a>
49 <li><a href="#class_lua_operators">overloading operators</a>
50 <li><a href="#class_lua_finalizers">finalizers</a>
51 </ul>
52 <li><a href="#exceptions">Exceptions</a>
53 <li><a href="#policies" >Parameter policies</a>
54 <ul>
55 <li><a href="#policies_copy">copy</a>
56 <li><a href="#policies_adopt">adopt</a>
57 <li><a href="#policies_dependency">dependency</a>
58 <li><a href="#policies_return_ref">return_reference_to</a>
59 <li><a href="#policies_out">out_value</a>
60 <li><a href="#policies_pure_out">pure_out_value</a>
61 <li><a href="#policies_discard_result">discard_result</a>
62 <li><a href="#policies_return_stl_iterator">return_stl_iterator</a>
63 <li><a href="#policies_yield">yield</a>
64 <li><a href="#policies_custom">custom</a>
65 <li><a href="#policies_user_converter">user defined converter</a>
66 </ul>
67 <li><a href="#config">Configuration</a>
68 <li><a href="#implementation_notes">Implementation notes</a>
69 <li><a href="#error_messages">Error messages</a>
70 <li><a href="#faq">FAQ</a>
71 <li><a href="#future">Future additions</a>
72 <li><a href="#issues">Known issues</a>
73 <li><a href="#acknowledgments">Acknowledgments</a>
74 </ol>
76 <h1><a name="intro"></a>Introduction</h1>
78 <p>Luabind is a library that helps you create bindings
79 between C++ and <a href="http://www.lua.org">lua</a>. It
80 has the ability to expose functions and classes, written in C++, to lua. It will
81 also supply the functionality to define classes in lua and let them derive from
82 other lua classes or C++ classes. Lua classes can override virtual functions
83 from their C++ base classes. It is written towards lua 5.0, and does not work
84 with lua 4.</p>
86 <p>It is implemented utilizing template meta programming.
87 That means that you don't need an extra preprocess pass to compile your project
88 (it is done by the compiler). It also means you don't (usually) have to know the
89 exact signature of each function you register, since the library will generate
90 code depending on the compile-time type of the function (which includes the
91 signature). The main drawback of this approach is that the compilation time will
92 increase for the file that does the registration, it is therefore recommended
93 that you register everything in the same cpp-file.</p>
95 <p>luabind is released under the terms of the
96 <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</p>
98 <p>We are very interested in hearing about projects that use luabind, please let
99 us know about your project.</p>
101 <h2><a name="features"></a>features</h2>
103 <p>Luabind supports:</p>
104 <ul>
105 <li>overloaded free functions
106 <li>C++ classes in lua
107 <ul>
108 <li>overloaded member functions
109 <li>operators
110 <li>properties
111 <li>enums
112 </ul>
113 <li>lua functions in C++
114 <li>lua classes in C++
115 <li>lua classes (single inheritance)
116 <ul>
117 <li>derives from lua or C++ classes
118 <li>override virtual functions from C++ classes
119 </ul>
120 <li>implicit casts between registered types
121 <li>best match signature matching
122 <li>return value policies and parameter policies
123 </ul>
125 <h2><a name="compilers"></a>tested compilers</h2>
127 <p>Luabind has been tested to work on the following
128 compilers:</p>
130 <ul>
131 <li>Visual Studio 7.1
132 <li>Visual Studio 7.0
133 <li>Visual Studio 6.0 (sp 5)
134 <li>Intel C++ 6.0 (Windows)
135 <li>GCC 2.95.3 (cygwin)
136 <li>GCC 3.0.4 (Debian/Linux)
137 <li>GCC 3.1 (SunOS 5.8)
138 <li>GCC 3.2 (cygwin)
139 </ul>
141 <p>It has been confirmed not to work with:</p>
143 <ul>
144 <li>GCC 2.95.2 (SunOS 5.8)
145 </ul>
147 <p>Metrowerks 8.3 (Windows) compiles but fails the const-test. This means that const member
148 functions are treated as non-const member functions.</p>
150 <p>If you have tried luabind with a compiler not listed here, let us know
151 your result with it.</p>
153 <h1><a name="build"></a>Building luabind</h1>
155 <p>To keep down the compilation-time luabind is built as a library. This means you
156 have to either build it and lika against it, or include its source files in your project.
157 You also have to make sure the <tt>luabind</tt> directory is somewhere in your compiler's
158 include path. It requires <a href="http://www.boost.org">boost</a> 1.30.0 to be installed
159 (only boost headers). It also requires that lua is installed.</p>
161 <p>There is a makefile in the root-directory that will build the library and the test program.
162 If you are using a UNIX-system (or cygwin) they will make it easy to build luabind as a static
163 library. If you are using Visual Studio it may be easier to include the files in the <tt>src</tt>
164 directory in your project.</p>
166 <p>When building luabind you have several options that may streamline the library to better suit
167 your needs. It is extremely important that your application has the same settings as the library
168 was built with. The available options are found in the <a href="#config">Configuration</a> section.</p>
170 <p>If you want to change the settings to differ from the default, it's recommended that you define
171 the settings on the commandline of all your files (in the project settings in visual studio).</p>
173 <h1><a name="basic"></a>Basic usage</h1>
175 <p>To use luabind, you must include <tt>lua.h</tt> and luabind's main header file.</p>
177 <pre>
178 extern "C"
180 #include "lua.h"
183 #include &lt;luabind/luabind.hpp&gt;
184 </pre>
186 <p>This includes support for both registering classes and functions. If you just want to
187 have support for functions or classes you can include <tt>&lt;luabind/function.hpp&gt;</tt>
188 and <tt>&lt;luabind/class.hpp&gt;</tt> separately.</p>
190 <pre>
191 #include &lt;luabind/function.hpp&gt;
192 #include &lt;luabind/class.hpp&gt;
193 </pre>
195 <p>The first thing you need to do is to call <tt>luabind::open(lua_State*)</tt> which will
196 register the functions to create classes from lua, and initialize some state-global
197 structures used by luabind. If you don't call this function you will hit asserts later in
198 the library. There is no corresponding close function because once a class has been registered
199 in lua, there really isn't any good way to remove it. Partly because any remaining instances
200 of that class relies on the class being there. Everything will be cleaned up when the state
201 is closed though.</p>
203 <p>Note that no luabind header will include <tt>"lua.h"</tt>, this is up to you. You have to include
204 it before any luabind header is included.</p>
207 <h1><a name="scopes"></a>Scopes</h1>
209 <p>Everything that gets registered in lua is registered in a namespace (lua tables) or in the global
210 scope (called module). All registrations must be surrounded by its scope. To define a module, the
211 <tt>luabind::module</tt> class is used. It is used like this:</p>
213 <pre>
214 module(L)
216 // declarations
218 </pre>
220 <p>This will register all declared functions or classes in the global namespace in lua. If you want
221 to have a namespace for your module (like the standard libraries) you can give a name to the constructor,
222 like this:</p>
224 <pre>
225 module(L, "my_library")
227 // declarations
229 </pre>
231 <p>Here all declarations will be put in the <tt>my_library</tt> table.</p>
233 <p>If you want nested namespaces you can use the <tt>luabind::namespace_</tt> class. It works exactly
234 as <tt>luabind::module</tt> except that it doesn't take a <tt>lua_State*</tt> in it's constructor.
235 An example of its usage could look like this:</p>
237 <pre>
238 module(L, "my_library")
241 // declarations
243 namespace_("detail")
245 // library-private declarations
249 </pre>
251 <p>As you might have figured out, the following declarations are equivalent:</p>
253 <pre>
254 module(L)
256 namespace_("my_library")
258 // declarations
262 </pre>
264 <pre>
265 module(L, "my_library")
267 // declarations
269 </pre>
271 <p>In the rest of the documentation the <tt>module</tt> around declarations is ommited to make the
272 examples focus on the actual declaration.</p>
274 <p>A word of caution, if you are in <b>really</b> bad need for performance, putting your functions
275 in tables will increase the lookup time.</p>
277 <h1><a name="fun"></a>Binding functions to lua</h1>
279 <p>To bind functions to lua you use the function <tt>luabind::function()</tt>. It has the following
280 synopsis:</p>
282 <pre>
283 template&lt;class F, class policies&gt;
284 void function(lua_State* L, const char* name, F f, const Policies&amp;);
285 </pre>
287 <ul>
288 <li>L is the lua state you want this function registered
289 in.
290 <li>name is the name the function will have within lua.
291 <li>F is the function pointer you want to register.
292 <li>The Policies parameter is used to describe how
293 parameters and return values are treated by the function, this is an optional
294 parameter. More on this in <a href="#policies" >parameter policies</a>.
295 </ul>
297 <p>An example usage could be if you want to register the
298 function <tt>float std::sin(float)</tt>:</p>
300 <pre>
301 luabind::function(L, "sin", &amp;std::sin);
302 </pre>
304 <h2><a name="fun_overload"></a>overloaded functions</h2>
306 <p>If you have more than one function with the same name,
307 and want to register them in lua, you have to explicitly give the signature.
308 This is to let C++ know which function you refer to. For example, if you have
309 two functions, <tt>int&nbsp;f(const&nbsp;char*)</tt> and <tt>void&nbsp;f(int)</tt>.</p>
311 <pre>
312 luabind::function(L, "f", (int(*)(const char*)) &amp;f);
313 luabind::function(L, "f", (void(*)(int)) &amp;f);
314 </pre>
316 <h2><a name="fun_sigmatch"></a>signature matching</h2>
318 <p>luabind will generate code that checks the lua stack to
319 see if the values there can match your functions' signatures. It will handle
320 implicit typecasts between derived classes, and it will prefer matches with the
321 least number of implicit casts. In a function call, if the function is
322 overloaded and there's no overload that match the parameters better than the
323 other, you have an ambiguity. This will spawn a run-time error, stating that the
324 function call is ambiguous. A simple example of this is to register one function
325 that takes an <tt>int</tt> and one that takes a <tt>float</tt>. Since lua don't distinguish between floats and
326 integers, both will always match.</p>
328 <p>Since all overloads are tested, it will always find the
329 best match (not the first match). This also means that it can handle situations
330 where the only difference in the signature is that one member function is const
331 and the other isn't. For example, if the following function and class is
332 registered:</p>
334 <pre>
335 struct A
337 void f();
338 void f() const;
341 const A* create_a();
343 struct B: A {};
344 struct C: B {};
346 void g(A*);
347 void g(B*);
348 </pre>
350 <p>(note that <tt>create_a()</tt> would need an <a href="#policies_adopt" >adopt return value policy</a>. How to register classes is
351 described in <a href="#bind_classes" >Binding classes to lua</a>).</p>
353 <p>And the following lua code is executed:</p>
355 <pre>
356 a1 = create_a()
357 a1:f() -- the const version is called
359 a2 = A()
360 a2:f() -- the non-const version is called
362 a = A()
363 b = B()
364 c = C()
366 g(a) -- calls g(A*)
367 g(b) -- calls g(B*)
368 g(c) -- calls g(B*)
369 </pre>
371 <h2><a name="lua_fun"></a>calling lua functions</h2>
373 <p>To call a lua function, you can either use <tt>call_function()</tt>, <tt>call_member()</tt>, an
374 <a href="#object"><tt>object</tt></a> or <a href="#functor"><tt>functor</tt></a>.</p>
376 <h3><tt>template&lt;class Ret&gt;<br>
377 Ret call_function(lua_State* L, const char* name, ...)</tt></h3>
378 <p>This calls the global function called <tt>name</tt>. This function can only call global lua functions. The
379 ... represents a variable number of parameters that are sent to the lua function. This function call may
380 throw <tt>luabind::error</tt> if the function call fails.</p>
382 <p>The return value isn't actually <tt>Ret</tt> (the template parameter), but a proxy object that will do
383 the function call. This enables you to give policies to the call. You do this with the operator[]. You
384 give the policies within the brackets, like this:</p>
386 <pre>
387 int ret = call_function&lt;int&gt;(L, "a_lua_function", new complex_class())[ adopt(_1) ];
388 </pre>
390 <h3><tt>template&lt;class Ret&gt;<br>
391 Ret call_member(object&amp;, const char* name, ...)</tt></h3>
392 <p>This treats the given <tt>object</tt> as an instance of a class. The given name is the name of a member
393 function to call. The ... represents a variable number of parameters given to the function. This function may
394 throw <tt>luabind::error</tt> if the function call fails.</p>
396 <p>You can give policies to a member function call the same way as you do with <tt>call_function</tt>.</p>
398 <h1><a name="bind_classes"></a>Binding classes to lua</h1>
400 <p>To register classes you use a class called <tt>class_</tt>. Its name is supposed to resemble the C++
401 keyword, to make it look more intuitive. It has an overloaded member function
402 <tt>def()</tt> that is used to register member functions,
403 operators, constructors, enums and properties on the class. It will return its
404 this-pointer, to let you register more members directly.</p>
406 <p>Let's start with a simple example. Consider the
407 following C++ class:</p>
409 <pre>
410 class testclass
412 public:
413 testclass(const std::string&amp; s): m_string(s) {}
414 void print_string() { std::cout &lt;&lt; m_string &lt;&lt; "\n"; }
415 private:
416 std::string m_string;
418 </pre>
420 <p>To register it with a lua environment, write as follows
421 (assuming you are <tt>using namespace luabind</tt>):</p>
423 <pre>
424 class_&lt;testclass&gt;("testclass")
425 .def(constructor&lt;const std::string&amp;&gt;())
426 .def("print_string", &amp;testclass::print_string)
427 .commit(L);
428 </pre>
430 <p>This will register the class with the name <tt>testclass</tt> and constructor that takes a string as
431 argument and one member function with the name <tt>print_string</tt>. The <tt>commit</tt> member function
432 will do the actual registration of the class, therefore, <tt>commit</tt> has to be the last member function called (You cannot add any more attributes or member functions to your class after <tt>commit</tt>).
433 You can use this class in lua like this:</p>
435 <pre>
436 -- instantiate the class
437 a = testclass('a string')
439 -- call the member function print_string on it
440 -- this will print 'a string' on stdout
441 a:print_string()
442 </pre>
444 <p>It is also possible to register free functions as member
445 functions. The requirement on the function is that it takes a pointer, const
446 pointer, reference or const reference to the class type as the first parameter.
447 The rest of the parameters are the ones that are visible in lua, while the
448 object pointer is given as the first parameter. If we have the following C++
449 code:</p>
451 <pre>
452 struct A
454 int a;
457 int plus(A* o, int v) { return o-&gt;a + v; }
458 </pre>
460 <p>You can register the plus, function as if it was a
461 member function on <tt>A</tt> like this:</p>
463 <pre>
464 class_&lt;A&gt;("A")
465 .def("plus", &amp;plus)
466 .commit(L)
468 </pre>
470 <p><tt>plus</tt> can now be called as
471 a member function on A with one parameter, <tt>int</tt>. If
472 the object pointer parameter is const, the function will act as if it was a
473 const member function (it can be called on const objects).</p>
475 <h2><a name="class_property"></a>properties</h2>
477 <p>To register a global data member with a class is easily
478 done. Consider the following class:</p>
480 <pre>
481 struct A
483 int a;
485 </pre>
487 <p>This class is registered like this:</p>
489 <pre>
490 class_&lt;A&gt;("A")
491 .def_readwrite("a", &amp;A::a)
492 .commit(L)
494 </pre>
496 <p>Then it's both readable and writable. You can
497 also register attributes as read-only.</p>
499 <pre>class_&lt;A&gt;("A")
500 .def_readonly("a", &amp;A::a)
501 .commit(L)
503 </pre>
505 <p>You can also register getter and setter functions and
506 make them look as if they were a public data member. Consider the following
507 class:</p>
509 <pre>
510 class A
512 void set_a(int x) { a_ = x; }
513 int get_a() const { return a_; }
514 private:
515 int a_;
517 </pre>
519 <p>It can be registered as if it had a public data member
520 <tt>a</tt> like this:</p>
522 <pre>
523 class_&lt;A&gt;("A")
524 .property("a", &amp;A::get_a, &amp;A::set_a)
525 .commit(L)
527 </pre>
529 <p>This way the <tt>get_a()</tt> and
530 <tt>set_a()</tt> functions will be called instead of just
531 writing to the data member. If you want to make it read only you can just omit
532 the last parameter.</p>
534 <h2><a name="class_enum"></a>enums</h2>
536 <p>If your class contains enumerated constants (enums), you
537 can register them as well to make them available in lua. Note that they will not
538 be type safe, all enums are integers in lua, and all functions that takes an
539 enum, will accept any integer. You register them like this:</p>
541 <pre>
542 class_&lt;A&gt;("A")
543 .enum_("constants")
545 value("my_enum", 4),
546 value("my_2nd_enum", 7),
547 value("another_enum", 6)
549 .commit(L)
551 </pre>
553 <p>In lua they are reached like any data member, except
554 that they are read-only and reached on the class itself rather than on an
555 instance of the class.</p>
557 <pre>
558 print(A.my_enum)
559 print(A.another_enum)
560 </pre>
562 <p>In this example the numbers 4 and 6 are printed.</p>
564 <h2><a name="class_operator"></a>operators</h2>
566 <p>The mechanism for registering operators on your class is
567 pretty simple. You use a global name <tt>luabind::self</tt>
568 to refer to the class itself and then you just write the operator expression
569 inside the <tt>def()</tt> call. This class:</p>
571 <pre>
572 struct vec
574 vec operator+(int s);
576 </pre>
578 <p>Is registered like this:</p>
580 <pre>
581 class_&lt;vec&gt;("vec")
582 .def(self + int())
583 .commit(L)
585 </pre>
587 <p>This will work regardless if your plus operator is
588 defined inside your class or as a free function.</p>
590 <p>If you operator is <tt>const</tt> (or, when defined as a free function,
591 takes a const reference to the class itself) you have to use <tt>const_self</tt>
592 instead of <tt>self</tt>. Like this:</p>
594 <pre>
595 class_&lt;vec&gt;("vec")
596 .def(const_self + int())
597 .commit(L)
599 </pre>
601 <p>The operators supported are those available in lua:</p>
603 <ul>
604 <li>+
605 <li>-
606 <li>*
607 <li>/
608 <li>^
609 <li>&lt;
610 <li>&lt;=
611 <li>==
612 <li>unary -
613 <li>operator()
614 </ul>
616 <p>This means, no in-place operators. The equality operator (==) has a little
617 hatch, it will not be called if the references are equal. This means that
618 the == operator has to do pretty much what's it's expected to do.</p>
620 <p>In the above example the other operand type is
621 instantiated by writing <tt>int()</tt>. If the operand type
622 is a complex type that cannot easily be instantiated you can wrap the type in a
623 class called <tt>other</tt>. For example:</p>
625 <p>To register this class, we don't want to instantiate a
626 string just to register the operator.</p>
628 <pre>
629 struct vec
631 vec operator+(std::string);
633 </pre>
635 <p>Instead we use the <tt>other</tt>
636 wrapper like this:</p>
638 <pre>
639 class_&lt;vec&gt;("vec")
640 .def(self + other&lt;std::string&gt;())
641 .commit(L)
643 </pre>
645 <p>To register an application operator:</p>
647 <pre>
648 class_&lt;vec&gt;("vec")
649 .def( self(int()) )
650 .commit(L)
652 </pre>
654 <p>There's one special operator. In lua it's called <tt>__tostring</tt>,
655 it's not really an operator. It is used for converting objects to strings
656 in a standard way in lua. If you register this functionality, you will
657 be able to use the lua standard function <tt>tostring()</tt> for converting
658 you object to a string.</p>
660 <p>To implement this operator in C++ you should supply an
661 operator<tt>&lt;&lt;</tt> for <tt>ostream</tt>. Like this example:</p>
663 <pre>
664 class number {};
666 std::ostream&amp; operator&lt;&lt;(ostream&amp;, number&amp;);
667 </pre>
669 <pre>
670 class_&lt;number&gt;("number")
671 .def(tostring(self))
672 .commit(L)
674 </pre>
677 <h2><a name="class_derived"></a>derived classes</h2>
679 <p>If you want to register classes that derives from other
680 classes, you can specify a template parameter <tt>bases&lt;&gt;</tt> to
681 the <tt>class_</tt> instantiation. The following hierarchy:</p>
683 <pre>
684 struct A {};
685 struct B: A{};
686 </pre>
688 <p>Would be registered like this:</p>
690 <pre>
691 class_&lt;A&gt;("A")
692 .commit(L)
694 class_&lt;B, A&gt;("B")
695 .commit(L)
697 </pre>
699 <p>If you have multiple inheritance you can specify more
700 than one base. If <tt>B</tt> would also derive from a class
701 <tt>C</tt>, it would be registered like this:</p>
703 <pre>
704 class_&lt;B, bases&lt;A, C&gt; &gt;("B")
705 .commit(L)
707 </pre>
709 <p>Note that you can omit <tt>bases&lt;..&gt;</tt> when using single inheritance.</p>
711 <p>If you don't specify that classes derive from each
712 other, luabind will not be able to implicitly cast pointers between the types.
713 If for example, you forget to tell that <tt>B</tt> derives
714 from <tt>A</tt> and we have a function with the following
715 signature:</p>
717 <pre>
718 void f(A*);
719 </pre>
721 <p>The following code will not run:</p>
723 <pre>
724 b = B()
725 f(b) -- error, no function 'f' matches the arguments (B)
726 </pre>
728 <p>Because luabind doesn't know that a <tt>B*</tt> can be cast to an <tt>A*</tt>.</p>
730 <h2><a name="class_smart_pointers"></a>smart_pointers</h2>
732 <p>When you register a class you can tell luabind that all instances of that class should be held
733 by some kind of smart pointer (<tt>boost::shared_ptr</tt> for instance). You do this by giving
734 the holder type as an extra template-parameter to the <tt>class_</tt> your constructing, like this:</p>
736 <pre>
737 class_&lt;A, boost::shared_ptr&lt;A&gt; &gt;("A");
738 </pre>
740 <p>You also have to supply two functions for your smart pointer. One that returns the type of const
741 version of the smart pointer type (<tt>boost::shared_ptr&lt;const A&gt;</tt> in this case). And one
742 function that extracts the raw pointer from the smart pointer. The first function is needed because
743 luabind has to allow the non-const&nbsp;-&gt;&nbsp;conversion when passing values from lua to C++. The second
744 function is needed when lua calls member functions on held types, the this pointer must be a raw
745 pointer, it is also needed to allow the smart_pointer&nbsp;-&gt;&nbsp;raw_pointer conversion from
746 lua to C++. They look like this:</p>
748 <pre>
749 namespace luabind
751 template&lt;class T&gt;
752 T* get_pointer(boost::shared_ptr&lt;T&gt;&amp; p) { return p.get(); }
754 template&lt;class A&gt;
755 LUABIND_TYPE_INFO get_const_holder(luabind::detail::type&lt;boost::shared_ptr&lt;A&gt; &gt;)
757 return LUABIND_TYPEID(boost::shared_ptr&lt;const A&gt;);
760 </pre>
762 <p>The current implementation requires that the const-version and non-const version of the holder type
763 has the exact same memory layout and can be <tt>reinterpret_cast</tt>:ed between each other.</p>
765 <p>The conversion that works are (given that B is a base class of A):</p>
767 <table>
768 <tr>
769 <td colspan="2"><h3>from lua to C++</h3></td>
770 </tr>
771 <tr>
772 <td>holder_type&lt;A&gt;</td>
773 <td>A*</td>
774 </tr>
775 <tr>
776 <td>holder_type&lt;A&gt;</td>
777 <td>B*</td>
778 </tr>
779 <tr>
780 <td>holder_type&lt;A&gt;</td>
781 <td>const A*</td>
782 </tr>
783 <tr>
784 <td>holder_type&lt;A&gt;</td>
785 <td>const B*</td>
786 </tr>
787 <tr>
788 <td>holder_type&lt;A&gt;</td>
789 <td>holder_type&lt;A&gt;</td>
790 </tr>
791 <tr>
792 <td>holder_type&lt;A&gt;</td>
793 <td>holder_type&lt;const A&gt;</td>
794 </tr>
795 <tr>
796 <td>holder_type&lt;const A&gt;</td>
797 <td>const A*</td>
798 </tr>
799 <tr>
800 <td>holder_type&lt;const A&gt;</td>
801 <td>const B*</td>
802 </tr>
803 <tr>
804 <td>holder_type&lt;const A&gt;</td>
805 <td>holder_type&lt;const A&gt;</td>
806 </tr>
807 <tr>
808 <td colspan="2"><h3>from C++ to lua</h3></td>
809 </tr>
810 <tr>
811 <td>holder_type&lt;A&gt;</td>
812 <td>holder_type&lt;A&gt;</td>
813 </tr>
814 <tr>
815 <td>holder_type&lt;const A&gt;</td>
816 <td>holder_type&lt;const A&gt;</td>
817 </tr>
818 <tr>
819 <td>const holder_type&lt;A&gt;&amp;</td>
820 <td>holder_type&lt;A&gt;</td>
821 </tr>
822 <tr>
823 <td>const holder_type&lt;const A&gt;&amp;</td>
824 <td>holder_type&lt;const A&gt;</td>
825 </tr>
827 </table>
829 <p>When using a holder type, it can be useful to know if the pointer is valid. For example when using
830 std::auto_ptr, the holder will be invalidated when passed as a parameter to a function.
831 For this purpose there is a member of all object instances in luabind: <tt>__ok</tt>.<p>
833 <pre>
835 struct test {};
836 void f(std::auto_ptr&lt;test&gt;);
838 module(L)
840 class_&lt;test&gt;("test")
841 .def(constructor&lt;&gt;()),
843 def("f", &amp;f)
846 a = test()
847 f(a)
848 print a.__ok -- prints false
850 </pre>
852 <h1><a name="object"></a>Object</h1>
854 <p>Since functions have to be able to take lua values (of variable type) we need a wrapper
855 around them. This wrapper is called <tt>luabind::object</tt>. If the function you register
856 takes an <tt>object</tt>, it will match any lua value. To use it, you need to include
857 <tt>&lt;luabind/object.hpp&gt;</tt>. The <tt>object</tt> class has the following synopsis:</p>
859 <pre>
860 class object
862 public:
863 class iterator;
864 class raw_iterator;
865 class array_iterator;
867 template&lt;class T&gt;
868 object(lua_State*, const T&amp; value);
869 object(const object&amp;);
870 object(lua_State*);
871 object();
873 ~object();
875 iterator begin() const;
876 iterator end() const;
877 raw_iterator raw_begin() const;
878 raw_iterator raw_end() const;
879 array_iterator abegin() const;
880 array_iterator aend() const;
882 void set();
883 lua_State* lua_state() const;
884 void pushvalue() const;
885 bool is_valid() const;
886 operator bool() const;
888 template&lt;class Key&gt;
889 <i>&lt;implementation-defined&gt;</i> operator[](const Key&amp;);
891 template&lt;class Key&gt;
892 object at(const Key&amp;) const;
894 template&lt;class Key&gt;
895 object raw_at(const Key&amp;) const;
897 template&lt;class T&gt;
898 object&amp; operator=(const T&amp;);
899 object&amp; operator=(const object&amp;);
901 template&lt;class T&gt;
902 bool operator==(const T&amp;) const;
903 bool operator==(const object&amp;) const;
904 bool operator&lt;(const object&amp;) const;
905 bool operator&lt;=(const object&amp;) const;
906 bool operator&gt;(const object&amp;) const;
907 bool operator&gt;=(const object&amp;) const;
908 bool operator!=(const object&amp;) const;
910 void swap(object&amp;);
911 int type() const;
913 <i>&lt;implementation-defined&gt;</i> operator()();
915 template&lt;class A0&gt;
916 <i>&lt;implementation-defined&gt;</i> operator()(const A0&amp; a0);
918 template&lt;class A0, class A1&gt;
919 <i>&lt;implementation-defined&gt;</i> operator()(const A0&amp; a0, const A1&amp; a1);
921 /* ... */
924 </pre>
926 <p>When you have a lua object, you can assign it a new value with the assignment operator (=). When
927 you do this, the <tt>default_policy</tt> will be used to make the conversion from C++ value to lua.
928 If your <tt>luabind::object</tt> is a table you can access its members through the operator[] or
929 the iterators. The value returned from the operator[] is a proxy object that can be used both
930 for reading and writing values into the table (using operator=). Note that it is impossible
931 to know if a lua value is indexable or not (lua_gettable doesn't fail, it succeeds or crashes).
932 This means that if you're trying to index something that cannot be indexed, you're on your own. Lua
933 will call its <tt>panic()</tt> function (you can define your own panic function using
934 <tt>lua_setpanicf</tt>). The <tt>at()</tt> and <tt>raw_at()</tt> functions returns the value
935 at the given table position (like operator[] but only for reading).<p>
937 <p>The ordinary <tt>object::iterator</tt> uses <tt>lua_gettable</tt> to extract the values from
938 the table, the standard way that will invoke metamethods if any. The <tt>object::raw_iterator</tt>
939 uses <tt>lua_rawget</tt> and <tt>object::array_iterator</tt> uses <tt>lua_rawgeti</tt>. The latter
940 will only iterate over numberical keys starting at 1 and continue until the first nil value.</p>
942 <p>The <tt>lua_state()</tt> function returns the lua state where this object is stored. If you want
943 to manipulate the object with lua functions directly you can push it onto the lua stack by calling
944 <tt>pushvalue()</tt>. And set the object's value by calling <tt>set()</tt>, which will pop the top
945 value from the lua stack and assign it to the object.</p>
947 <p>The operator== will run <tt>lua_equal()</tt>
948 on the operands and return its result.</p>
950 <p>The <tt>int type()</tt> member function will return the lua type of the object. It will return
951 the same values as <tt>lua_type()</tt>.</p>
953 <p>The <tt>is_valid</tt> function tells you whether the object has been initialized or not. When created
954 with its default constructor, objects are invalid. To make an object valid, you can assign it a value. If
955 you want to invalidate an object you can simply assign it an invalid object.</p>
957 <p>The <tt>operator bool()</tt>
958 isn't really an implicit cast to bool, but an implicit cast to a member pointer, since member pointers
959 don't have any arithmetic operators on them (which can cause hard to find errors). The functionality of
960 the cast-operator is equivalent to <tt>is_valid()</tt>. This means that:</p>
962 <pre>
963 object o;
964 // ...
965 if (o.is_valid())
967 // ...
969 </pre>
971 <p>is equivalent to:</p>
973 <pre>
974 object o;
975 // ...
976 if (o)
978 // ...
980 </pre>
982 <p>The application operator will call the value as if it was a function. You can give it any number
983 of parameters (currently the <tt>default_policy</tt> will be used for the conversion). The returned
984 object refers to the return value (currently only one return value is supported). This operator may
985 throw <tt>luabind::error</tt> if the function call fails. If you want to specify
986 <a href="#policies">policies</a> to your function call, you can use index-operator (operator[]) on
987 the function call, and give the policies within the [ and ]. Like this:</p>
989 <pre>
990 my_function_object(2, 8, new my_complex_structure(6)) [ adopt(_3) ];
991 </pre>
993 <p>This tells luabind to make lua adopt the ownership and responsibility for the pointer passed in
994 to the lua-function.</p>
996 <p>It's important that all instances of <tt>object</tt> have been destructed by the time the lua state
997 is closed. The object will keep a pointer to the lua state and release its lua object in its
998 destructor.</p>
1000 <p>Here's an example of how a function can use a table.</p>
1002 <pre>
1003 void my_function(const object&amp; table)
1005 if (table.type() == LUA_TTABLE)
1007 table["time"] = std::clock();
1008 table["name"] = std::rand() &lt; 500 ? "unusual" : "usual";
1010 std::cout &lt;&lt; object_cast&lt;std::string&gt;(table[5]) &lt;&lt; "\n";
1013 </pre>
1015 <p>If you take a <tt>luabind::object</tt> as a parameter to a function, any lua value will match that parameter.
1016 That's why we have to make sure it's a table before we index into it.</p>
1018 <h2><a name="object_iterators"></a>iterators</h2>
1020 <p>The iterators, that are returned by the <tt>begin()</tt> and <tt>end()</tt> (and their variants) are (almost)
1021 models of the <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">
1022 ForwardIterator</a> concept. The exceptions are that operator-&gt; and post increment doesn't exist on
1023 them.</p>
1025 <p>They look like this:</p>
1027 <pre>
1028 class object::iterator
1030 iterator();
1031 iterator(const iterator&amp;);
1033 iterator&amp; operator++();
1034 bool operator!=(const iterator&amp;) const;
1035 iterator&amp; operator=(const iterator&amp;);
1037 object key() const;
1039 <i>implementation-defined</i> operator*();
1041 </pre>
1043 <p>The implementation defined return value from the dereference operator is a proxy object that can be used as if it was
1044 an <tt>object</tt>, it can also be used to assign the specific table entry with a new value. If you want to assign a
1045 value to an entry pointed to by an iterator, just use the assignment operator on the dereferenced iterator:</p>
1047 <pre>
1048 *iter = 5;
1049 </pre>
1051 <p>The <tt>key()</tt> member returns the key used by the iterator when indexing the associated lua table.</p>
1053 <h2><a name="object_functions"></a>related functions</h2>
1056 There are a couple of functions related to objects and tables.
1057 </p>
1059 <h3><tt>T object_cast&lt;<i>Type</i>&gt;(const&nbsp;object&amp;);</tt><br>
1060 <tt>T object_cast&lt;<i>Type</i>&gt;(const&nbsp;object&amp;, const&nbsp;Policies&amp;);</tt><br>
1061 <tt>boost::optional&lt;T&gt; object_cast_nothrow&lt;<i>Type</i>&gt;(const&nbsp;object&amp;);</tt><br>
1062 <tt>boost::optional&lt;T&gt; object_cast_nothrow&lt;<i>Type</i>&gt;(const&nbsp;object&amp;, const&nbsp;Policies&amp;);</tt></h3>
1063 <p>The <tt>object_cast</tt> function casts the value of an <tt>object</tt> to a C++ value. You
1064 can supply a policy to handle the conversion from lua to C++. If the cast cannot be made a
1065 <tt>cast_failed</tt> exception will be thrown. If you have defined <tt>LUABIND_NO_ERROR_CHECKING</tt>
1066 (see <a href="#config">configuration</a>) no checking will occur, and if the cast is invalid the
1067 application may very well crash. The nothrow versions will return an uninitialized <tt>boost::optional&lt;T&gt;</tt>
1068 object, to indicate that the cast could not be performed.</p>
1070 <h3><tt>object get_globals(lua_State*);</tt><br>
1071 <tt>object get_registry(lua_State*);</tt></h3>
1072 <p>These functions return the global environment table and the registry table respectively.</p>
1074 <h3><tt>object newtable(lua_State*);</tt></h3>
1075 <p>This function creates a new table and returns an object to it.</p>
1077 <h2><a name="functor"></a>functor</h2>
1079 <p>The <tt>functor</tt> class is similar to <tt>object</tt>, with the exception that it can
1080 only be used to store functions. If you take it as a parameter, it will only match functions.</p>
1082 <p>To use it you need to include its header:</p>
1084 <pre>
1085 #include &lt;luabind/functor.hpp&gt;
1086 </pre>
1088 <p>It takes one template parameter, the return value of the lua function it represents.
1089 Currently the functor can have at most one return value (unlike lua functions). It has the
1090 following synopsis:</p>
1092 <pre>
1093 template&lt;class Ret&gt;
1094 class functor
1096 public:
1098 functor(lua_State*, const char* name);
1099 functor(const functor&amp;);
1101 ~functor();
1103 bool is_valid() const;
1104 operator bool() const;
1106 lua_State* lua_state() const;
1107 void pushvalue() const;
1109 bool operator==(const functor&lt;Ret&gt;&amp;);
1110 bool operator!=(const functor&lt;Ret&gt;&amp;);
1112 <i>&lt;implementation-defined&gt;</i> operator()() const;
1114 template&lt;class A0&gt;
1115 <i>&lt;implementation-defined&gt;</i> operator()(const A0&amp;) const;
1117 template&lt;class A0, class A1&gt;
1118 <i>&lt;implementation-defined&gt;</i> operator()(const A0&amp;, const A1&amp;) const;
1120 /* ... */
1122 </pre>
1124 <p>The application operator takes any parameters. The parameters are converted into lua and the
1125 function is called. The return value will act as if it was the type <tt>Ret</tt>, with the exception that you can use the
1126 return value to give policies to the call. You do this the same way as you do with <a href="#object">objects</a>,
1127 using the operator[], and giving the policies inside the brackets.</p>
1129 <p>The <tt>is_valid()</tt> function works just like the one on <a href="#object"><tt>object</tt></a>, it tells you if
1130 the functor has been assigned with a valid lua function. The <tt>operator bool()</tt> is an alias for this member function
1131 and also works just as the one found in <a href="#object"><tt>object</tt></a>.</p>
1133 <p>For example, if you have the following lua function:</p>
1135 <pre>
1136 function f(a, b)
1137 return a + b
1139 </pre>
1141 <p>You can expose it to C++ like this:</p>
1143 <pre>
1144 functor&lt;int&gt; f(L, "f");
1146 std::cout &lt;&lt; f(3, 5) &lt;&lt; "\n";
1147 </pre>
1149 <p>This will print out the sum of 3 and 5. Note that you can pass any parameters to the
1150 application operator of <a href="#functor"><tt>luabind::functor</tt></a>, this is because lua doesn't have
1151 signatures for its functions. All lua functions take any number of parameters
1152 of any type.</p>
1154 <p>If we have a C++ function that takes a <a href="#functor"><tt>luabind::functor</tt></a> and registers it, it will accept lua
1155 functions passed to it. This enables us to expose APIs that requires you to
1156 register callbacks. For example, if your C++ API looks like this:</p>
1158 <pre>
1159 void set_callback(void(*)(int, int));
1160 </pre>
1162 <pre>
1163 object o;
1164 // ...
1165 if (o.is_valid())
1167 // ...
1169 </pre>
1172 <p>And you want to expose it to lua, you have to wrap the
1173 call to the lua function inside a real C++ function, like this:</p>
1175 <pre>
1176 functor&lt;void&gt; lua_callback;
1178 void callback_wrapper(int a, int b)
1180 lua_callback(a, b);
1183 void set_callback_wrapper(const functor&lt;void&gt;&amp; f)
1185 lua_callback = f;
1186 set_callback(&amp;callback_wrapper);
1188 </pre>
1190 <p>And then register <tt>set_callback_wrapper</tt> instead of registering <tt>set_callback</tt>.
1191 This will have the effect that when one tries to register the callback from lua, your
1192 <tt>set_callback_wrapper</tt> will be called instead and first set the lua functor to the given function.
1193 It will then call the real <tt>set_callback</tt> with the <tt>callback_wrapper</tt>. The
1194 <tt>callback_wrapper</tt> will be called whenever the callback should be called, and it will simply
1195 call the lua function that we registered.</p>
1197 <p>You can also use <a href="#object_functions"><tt>object_cast</tt></a> to cast an object to a <a href="#functor"><tt>functor</tt></a>.</p>
1199 <h1><a name="class_lua"></a>Defining classes in lua</h1>
1201 <p>In addition to binding C++ functions and classes with
1202 lua, luabind also provide an oo-system in lua.</p>
1204 <pre>
1205 class 'lua_testclass'
1207 function lua_testclass:__init(name)
1208 self.name = name
1211 function lua_testclass:print()
1212 print(self.name)
1215 a = lua_testclass('example')
1216 a:print()
1217 </pre>
1219 <p>Inheritance can be used between lua-classes:</p>
1221 <pre>
1222 class 'derived' (lua_testclass)
1224 function derived:__init() super('derived name')
1227 function derived:print()
1228 print('Derived:print() -&gt; ')
1229 lua_testclass.print(self)
1231 </pre>
1233 <p>Here the <tt>super</tt> keyword is
1234 used in the constructor to initialize the base class. The user is required to
1235 call <tt>super</tt> first in the constructor.</p>
1237 <p>As you can see in this example, you can call the
1238 base class' member functions. You can find all member functions in the base class,
1239 but you will have to give the this-pointer (<tt>self</tt>) as first argument.</p>
1241 <h2><a name="class_lua_derive"></a>deriving in lua</h2>
1243 <p>It is also possible to derive lua classes from C++
1244 classes, and override virtual functions with lua functions. To do this we have
1245 to create a wrapper class for our C++ base class. This is the class that will
1246 hold the lua object when we instantiate a lua class.</p>
1248 <p>The wrapper class has to provide the same constructors
1249 as the base class, with the addition of one extra parameter: <tt>luabind::object</tt>.
1250 This is the reference to the lua object that should be held by the wrapper, and should
1251 be stored in a member variable as done in the sample below.</p>
1253 <pre>
1254 class base class
1256 public:
1257 baseclass(const char* s) { std::cout &lt;&lt; s &lt;&lt; "\n"; }
1258 virtual void f(int a) { std::cout &lt;&lt; "f(" &lt;&lt; a &lt;&lt; ")\n"; }
1261 struct baseclass_wrapper: baseclass
1263 luabind::object m_l;
1264 baseclass_wrapper(luabind::object l, const char* s): baseclass(s), m_l(l) {}
1266 virtual void f(int a) { call_member&lt;void&gt;(m_l, "f", a); }
1267 static void f_static(baseclass* ptr, int a)
1269 return ptr-&gt;baseclass::f(a);
1272 </pre>
1273 <pre>
1274 class_&lt;baseclass, baseclass_wrapper&gt;("baseclass")
1275 .def(constructor&lt;const char*&gt;())
1276 .def("f", &amp;baseclass_wrapper::f_static)
1277 .commit(L)
1279 </pre>
1281 <p>Note that if you have both base classes and a base class
1282 wrapper, you must give both <tt>bases</tt> and the
1283 base class wrapper type as template parameter to <tt>class_</tt>. The order
1284 in which you specify them is not important.</p>
1286 <p>If we didn't have a class wrapper, it would not be possible to pass a lua class
1287 back to C++. Since the entry points of the virtual functions would still point to
1288 the C++ base class, and not to the functions defined in lua. That's why we need
1289 one function that calls the base class' real function (used if the lua class
1290 doesn't redefine it) and one virtual function that dispatches the call into luabind,
1291 to allow it to select if a lua function should be called, or if the original
1292 function should be called. If you don't intend to derive from a C++ class, or if
1293 it doesn't have any virtual member functions, you can register it without a
1294 class wrapper.</p>
1296 <p>You don't need to have a class wrapper in order to derive from a class,
1297 but if it has virtual functions you may have silent errors. The rule of thumb is:
1298 If your class has virtual functions, create a wrapper type, if it doesn't don't create
1299 a wrapper type.</p>
1301 <h2><a name="class_lua_operators"></a>overloading operators</h2>
1303 <p>You can overload most operators in lua for your classes. You do this by simply declaring
1304 a member function with the same name as an operator (the name of the metamethods in lua). The operators
1305 you can overload are:</p>
1307 <ul>
1308 <li><tt>__add</tt>
1309 <li><tt>__sub</tt>
1310 <li><tt>__mul</tt>
1311 <li><tt>__div</tt>
1312 <li><tt>__pow</tt>
1313 <li><tt>__lt</tt>
1314 <li><tt>__le</tt>
1315 <li><tt>__eq</tt>
1316 <li><tt>__call</tt>
1317 <li><tt>__unm</tt>
1318 <li><tt>__tostring</tt>
1319 </ul>
1321 <p><tt>__tostring</tt> isn't really an operator, but it's the metamethod that is called by the standard
1322 library's <tt>tostring()</tt> function. There's one strange behavior regarding binary operators. You are not
1323 guaranteed that the self pointer you get actually refers to an instance of your class. This is because
1324 lua doesn't distinguish the two cases where you get the other operand as left hand value or right hand value.
1325 Consider the following examples:</p>
1327 <pre>
1328 class 'my_class'
1330 function my_class:__init(v)
1331 self.val = v
1334 function my_class:__sub(v)
1335 return my_class(self.val - v.val)
1338 function my_class:__tostring()
1339 return self.val
1341 </pre>
1343 <p>This will work well as long as you only subtracts instances of <tt>my_class</tt> with each other. But
1344 If you want to be able to subtract ordinary numbers from your class too, you have to manually check the type
1345 of <b>both</b> operands, including the self object.</p>
1347 <pre>
1348 function my_class:__sub(v)
1349 if (type(self) == 'number') then
1350 return my_class(self - v.val)
1352 elseif (type(v) == 'number') then
1353 return my_class(self.val - v)
1355 else
1356 -- assume both operands are instances of my_class
1357 return my_class(self.val - v.val)
1361 </pre>
1363 <p>The reason why <tt>__sub</tt> is used as an example is because subtraction is not commutative (the
1364 order of the operands matter). That's why luabind cannot change order of the operands to make the
1365 self reference always refer to the actual class instance.</p>
1367 <p>If you have two different lua classes with an overloaded operator, the operator of the right hand
1368 side type will be called. If the other operand is a C++ class with the same operator overloaded, it
1369 will be prioritized over the lua class' operator. If none of the C++ overloads matches, the lua
1370 class' operator will be called.</p>
1372 <h2><a name="class_lua_finalizers"></a>finalizers</h2>
1374 <p>If an object needs to perform actions when it's collected we provide
1375 a <tt>__finalize</tt> function that can be overridden in lua-classes.
1376 The __finalize functions will be called on all classes in the inheritance
1377 chain, starting with the most derived type.</p>
1379 <pre>
1382 function lua_testclass:__finalize()
1383 -- called when the an object is collected
1385 </pre>
1387 <h1><a name="exceptions"></a>Exceptions</h1>
1388 <p>If any of the functions you register throws an exception when called, that exception will be caught
1389 by luabind and converted to an error string and <tt>lua_error</tt> will be invoked. If the exception
1390 is a <tt>std::exception</tt> or a <tt>const&nbsp;char*</tt> the string that is pushed on the lua stack,
1391 as error message, will be the <tt>what()</tt> or the string itself respectively. If the exception
1392 is unknown, a generic string saying that the function threw an exception will be pushed.</p>
1394 <p>Exceptions thrown from user defined functions have to be caught by luabind. If they weren't they
1395 would be thrown through lua itself, which is usually compiled as C code and doesn't support the
1396 stack-unwinding that exceptions imply.</p>
1398 <p>Any function that invokes lua code may throw <tt>luabind::error</tt>. This exception means that a lua
1399 run-time error occurred. The error message is found on top of the lua stack. The reason why the
1400 exception doesn't contain the error string itself is because it would then require heap allocation
1401 which may fail. If an exception class throws an exception while it is being thrown itself, the
1402 application will be terminated.</p>
1404 <p>Error's synopsis is:</p>
1406 <pre>
1407 class error: std::exception
1409 public:
1410 error(lua_State*);
1411 lua_State* state() const throw();
1412 virtual const char* what() const throw();
1414 </pre>
1416 <p>The state function returns a pointer to the lua state in which the error was thrown. This pointer
1417 may be invalid if you catch this exception after the lua state is destructed. If the lua state is
1418 valid you can use it to retrieve the error message from the top of the lua stack.</p>
1420 <p>An example of where the lua state pointer may point to an invalid state follows:</p>
1422 <pre>
1423 struct lua_state
1425 lua_state(lua_State* L): m_L(L) {}
1426 ~lua_state() { lua_close(m_L); }
1427 operator lua_State*() { return m_L; }
1428 lua_State* m_L;
1431 int main()
1435 lua_state L = lua_open();
1436 /* ... */
1438 catch(luabind::error&amp; e)
1440 lua_State* L = e.state();
1441 // L will now point to the destructed
1442 // lua state and be invalid
1443 /* ... */
1446 </pre>
1448 <p>There's another exception that luabind may throw. <tt>luabind::cast_failed</tt>, this exception
1449 is thrown from <a href="#lua_fun"><tt>call_function</tt></a>, <a href="#lua_fun"><tt>call_member</tt></a> or when <a href="#functor"><tt>functor</tt></a> is invoked.
1450 It means that the return value from the lua function couldn't be converted to a C++ value. It is
1451 also thrown from <a href="#object_functions"><tt>object_cast</tt></a> if the cast cannot be made.</p>
1453 <p>The synopsis for <tt>luabind::cast_failed</tt> is:</p>
1455 <pre>
1456 class cast_failed: std::exception
1458 public:
1459 cast_failed(lua_State*);
1460 lua_State* state() const throw();
1461 LUABIND_TYPE_INFO info() const throw();
1462 virtual const char* what() const throw();
1464 </pre>
1466 <p>Again, the <tt>state</tt> member function returns a pointer to the lua state where the error occurred.
1467 See the example above to see where this pointer may be invalid.</p>
1469 <p>The <tt>info</tt> member function returns the user defined <a href="#luabind_type_info"><tt>LUABIND_TYPE_INFO</tt></a>, which
1470 defaults to a <tt>const&nbsp;std::type_info*</tt>. This type info describes the type that we tried
1471 to cast a lua value to.</p>
1473 <p>If you have defined <a href="#luabind_no_exceptions"><tt>LUABIND_NO_EXCEPTIONS</tt></a> none of these exceptions will be thrown,
1474 instead you can set two callback functions that are called instead. These two functions are only defined if <a href="#luabind_no_exceptions"><tt>LUABIND_NO_EXCEPTIONS</tt></a> are defined.</p>
1476 <h3><a name="set_error_callback"></a><tt>luabind::set_error_callback(void(*)(lua_State*))</tt></h3>
1477 <p>The function you set will be called when a runtime-error occur in lua code. You can find an
1478 error message on top of the lua stack. This function is not expected to return, if it does
1479 luabind will call <tt>std::terminate()</tt>.</p>
1481 <h3><a name="set_cast_failed_callback"></a><tt>luabind::set_cast_failed_callback(void(*)(lua_State*, LUABIND_TYPE_INFO))</tt></h3>
1483 <p>The function you set is called instead of throwing <tt>cast_failed</tt>. This function is not expected to return, if it does luabind will call <tt>std::terminate()</tt>.</p>
1486 <h1><a name="policies"></a>Parameter policies</h1>
1488 <p>Sometimes it is necessary to control how luabind passes
1489 arguments and return value, to do this we have policies. These are the
1490 policies that can be used:</p>
1492 <h2><a name="policies_copy"></a>copy</h2>
1494 <p>This will make a copy of the parameter. This is the
1495 default behavior when passing parameters by-value. Note that this can only be
1496 used when passing from C++ to lua. This policy requires that the parameter
1497 type has a copy constructor.</p>
1499 <p>To use this policy you need to include <tt>&lt;luabind/copy_policy.hpp&gt;</tt>.</p>
1501 <h2><a name="policies_adopt"></a>adopt</h2>
1503 <p>This will transfer ownership of the parameter.</p>
1504 <p>Consider making a factory function in C++ and exposing
1505 it to lua:</p><pre>base* create_base()
1507 return new base();
1509 </pre>
1511 <pre>
1512 function(L, "create_base", create_base);
1513 </pre>
1515 <p>Here we need to make sure lua understands that it should
1516 adopt the pointer returned by the factory-function. This can be done using the
1517 adopt-policy.</p>
1519 <pre>
1520 function(L, "create_base", adopt(return_value));
1521 </pre>
1523 <p>To specify multiple policies we just separate them with '+'.</p>
1525 <pre>
1526 base* set_and_get_new(base* ptr)
1528 base_ptrs.push_back(ptr);
1529 return new base();
1531 </pre>
1533 <pre>
1534 function(L, "set_and_get_new", &amp;set_and_get_new, adopt(return_value) + adopt(_1));
1535 </pre>
1537 <p>When lua adopts a pointer, it will call <tt>delete</tt> on it. This means that it cannot adopt pointers allocated with
1538 another allocator than <tt>new</tt> (no <tt>malloc</tt> for example).</p>
1540 <p>To use this policy you need to include <tt>&lt;luabind/adopt_policy.hpp&gt;</tt>.</p>
1542 <h2><a name="policies_dependency"></a>dependency</h2>
1544 <p>The dependency policy is used to create life-time dependencies between values. Consider the following example:</p>
1546 <pre>
1547 struct A
1549 B m_member;
1551 const B&amp; get_member()
1553 return m_member;
1556 </pre>
1558 <p>When wrapping this class, we would do something like:</p>
1560 <pre>
1561 class_&lt;A&gt;("A")
1562 .def(constructor&lt;&gt;())
1563 .def("get_member", &amp;A::get_member)
1564 .commit(L)
1566 </pre>
1568 <p>However, since the return value of get_member is a reference to a member of A, this will create
1569 some life-time issues. For example:</p>
1571 <pre>
1572 a = A()
1573 b = a:get_member() -- b points to a member of a
1574 a = nil
1575 collectgarbage(0) -- since there are no references left to a, it is removed
1576 -- at this point, b is pointing into a removed object
1577 </pre>
1579 <p>When using the dependency-policy, it is possible to tell luabind to tie the lifetime of one
1580 object to another, like this:</p>
1582 <pre>
1583 class_&lt;A&gt;("A")
1584 .def(constructor&lt;&gt;())
1585 .def("get_member", &amp;A::get_member, dependency(result, self))
1586 .commit(L)
1588 </pre>
1590 <p>This will create a dependency between the return-value of the function, and the self-object. This means
1591 that the self-object will be kept alive as long as the result is still alive.</p>
1593 <pre>
1594 a = A()
1595 b = a:get_member() -- b points to a member of a
1596 a = nil
1597 collectgarbage(0) -- a is dependent on b, so it isn't removed
1598 b = nil
1599 collectgarbage(0) -- all dependencies to a gone, a is removed
1600 </pre>
1602 <p>To use this policy you need to include <tt>&lt;luabind/dependency_policy.hpp&gt;</tt>.</p>
1604 <h2><a name="policies_return_ref"></a>return_reference_to</h2>
1606 <p>It is very common to return references to arguments or the this-pointer to allow for chaining in C++.</p>
1607 <pre>
1608 struct A
1610 float m_val;
1612 A&amp; set(float v)
1614 m_val = v;
1615 return *this;
1618 </pre>
1620 <p>When luabind generates code for this, it will create a new object for the return-value, pointing to the self-object. This
1621 isn't a problem, but could be a bit inefficient. When using the return_reference_to-policy we have the ability to tell
1622 luabind that the return-value is already on the lua stack.</p>
1624 <pre>
1625 class_&lt;A&gt;("A")
1626 .def(constructor&lt;&gt;())
1627 .def("set", &amp;A::set, return_reference_to(self))
1628 .commit(L)
1630 </pre>
1632 <p>Instead of creating a new object, luabind will just copy the object that is already on the stack.</p>
1633 <p>NOTE! This policy ignores all type information and should be used only it situations where the parameter type
1634 is a perfect match to the return-type (such as in the example).</p>
1636 <p>To use this policy you need to include <tt>&lt;luabind/return_reference_to_policy.hpp&gt;</tt>.</p>
1638 <h2><a name="policies_out"></a>out_value</h2>
1640 <p>This policy makes it possible to wrap functions that take non const references as its parameters with the intention to write return values to them.</p>
1642 <pre>
1643 void f(float&amp; val) { val = val + 10.f; }
1644 </pre>
1646 <pre>
1647 void f(float* val) { *val = *val + 10.f; }
1648 </pre>
1650 <p>Can be wrapped by doing:</p>
1652 <pre>
1653 function(L, "f", &amp;f, out_value(_1))
1654 </pre>
1656 <p>When invoking this function from lua it will return the value assigned to its parameter.</p>
1658 <pre>
1659 a = f(10) -- a is now 20
1660 </pre>
1662 <p>When this policy is used in conjunction with user define types we often need to do ownership transfers.</p>
1664 <pre>
1665 struct A
1669 void f(A*&amp; obj) { obj = new A(); }
1671 void f(A** obj) { *obj = new A(); }
1672 </pre>
1674 <p>Here we need to make sure luabind takes control over object returned, for this we use the <tt>adopt</tt> policy</p>
1676 <pre>
1677 class_&lt;A&gt;("A")
1678 .commit(L)
1681 function(L, "f", &amp;f, out_value(_1, adopt(_2)));
1682 </pre>
1684 <p>Here we are using <tt>adopt</tt> as an internal policy to <tt>out_value</tt>. The index specified, <tt>_2</tt>, means adopt will be used to convert the value back to lua. Using <tt>_1</tt> means the policy will be used when converting from lua to C++.</p>
1686 <p>To use this policy you need to include <tt>&lt;luabind/out_value_policy.hpp&gt;</tt>.</p>
1688 <h2><a name="policies_pure_out"></a>pure_out_value</h2>
1690 <p>This policy works in exactly the same way as <tt>out_value</tt>, except that it replaces the parameters with default-constructed
1691 objects.</p>
1693 <pre>
1694 void get(float&amp; x, float&amp; y)
1696 x = 3.f;
1697 y = 4.f;
1700 function(L, "get", &amp;get, pure_out_value(_1) + pure_out_value(_2));
1701 </pre>
1702 <pre>
1703 x,y = get()
1704 print(x,y) -- prints '3 4'
1705 </pre>
1707 <p>Like out_value, it is possible to specify an internal policy used then converting the values back to lua.</p>
1709 <pre>
1710 void get(test_class*&amp; obj)
1712 obj = new test_class();
1715 function(L, "get", &amp;get, pure_out_value(_1, adopt(_1)));
1716 </pre>
1718 <h2><a name="policies_discard_result"></a>discard_result</h2>
1720 <p>This is a very simple policy which makes it possible to throw away the value returned by a C++ function, instead of converting it to lua. This example makes sure the <tt>this</tt> reference never gets converted to lua</p>
1722 <pre>
1723 struct simple
1725 simple&amp; set_name(const std::string&amp; n)
1727 name = n;
1728 return *this;
1731 std::string name;
1734 class_&lt;simple&gt;("simple")
1735 .def("set_name", &amp;simple::set_name, discard_result)
1736 .commit(L)
1738 </pre>
1740 <p>To use this policy you need to include <tt>&lt;luabind/discard_result.hpp&gt;</tt>.</p>
1742 <h2><a name="policies_return_stl_iterator"></a>return_stl_iterator</h2>
1744 <p>This policy converts an STL container to a generator function that can be used in lua to
1745 iterate over the container. It works on any container that defines <tt>begin()</tt> and <tt>end()</tt>
1746 member functions (they have to return iterators). It can be used like this:</p>
1748 <pre>
1749 struct A
1751 std::vector&lt;std::string&gt; names;
1754 class_&lt;A&gt;("A")
1755 .def_readwrite("names", &amp;A::names, return_stl_iterator)
1756 .commit(L)
1758 </pre>
1760 <p>The lua code to iterate over the container:</p>
1762 <pre>
1763 a = A()
1765 for name in a.names do
1766 print(name)
1768 </pre>
1770 <p>To use this policy you need to include <tt>&lt;luabind/iterator_policy.hpp&gt;</tt></p>
1772 <h2><a name="policies_yield"></a>yield</h2>
1774 <p>TODO: document.</p>
1776 <h2><a name="policies_custom"></a>custom</h2>
1778 <p>The policies don't have a stable API yet. See the examples directory.</p>
1780 <h2><a name="policies_user_converter"></a>user defined converter</h2>
1782 <p>The policies don't have a stable API yet. See the examples directory.</p>
1784 <h1><a name="config"></a>Configuration</h1>
1786 <p>There are a number of configuration options available when building luabind. It is very important
1787 that your project has the exact same conmfiguration options as the ones given when the library was build!
1788 The exceptions are the <tt>LUABIND_MAX_ARITY</tt> and <tt>LUABIND_MAX_BASES</tt> which are template-based
1789 options and only matters when you use the library (which means they can differ from the settings of the
1790 library).</p>
1792 <p>The default settings can be found in <tt>luabind/config.hpp</tt> they will be used if no other settings
1793 are given.</p>
1795 <p>If you want to change the settings of the library, you can modify the config-file. It is included and used
1796 by all make-files. You can change paths to lua and boost in there as well.</p>
1798 <h3><tt>LUABIND_MAX_ARITY</tt></h3>
1799 <p>Controls the maximum arity of functions that are registered
1800 with luabind. You can't register functions that takes more
1801 parameters than the number this macro is set to. It defaults
1802 to 5, so, if your functions have greater arity you have to
1803 redefine it. A high limit will increase compilation time.</p>
1804 <h3><tt>LUABIND_MAX_BASES</tt></h3>
1805 <p>Controls the maximum number of classes one class can derive from
1806 in luabind (the number of classes specified within
1807 <tt>bases&lt;&gt;</tt>). <tt>LUABIND_MAX_BASES</tt> defaults to 4.
1808 A high limit will increase compilation time.</p>
1809 <h3><tt>LUABIND_NO_ERROR_CHECKING</tt></h3>
1810 <p>If this macro is defined, all the lua code is expected only to make
1811 legal calls. If illegal function calls are made (e.g. giving parameters
1812 that doesn't match the function signature) they will not be detected
1813 by luabind and the application will probably crash. Error checking
1814 could be disabled when shipping a release build (given that no end-user
1815 has access to write custom lua code). Note that function parameter
1816 matching will be done if a function is overloaded, since otherwise it's
1817 impossible to know which one was called. Functions will still be able
1818 to throw exceptions when error checking is disabled.</p>
1820 <p>Functions will still be able to throw exceptions, they will be
1821 caught by luabind and propagated with <tt>lua_error()</tt>.</p>
1824 <h3><tt>LUABIND_DONT_COPY_STRINGS</tt></h3>
1825 <p>If this macro is defined, luabind will expect that all strings given
1826 to the def() methods are static constant strings (given as string
1827 constants for example). luabind will not copy the strings if you
1828 enable this setting, but just keep the char pointers. This may be
1829 especially useful for embedded systems or consoles where heap
1830 allocations should be minimized.</p>
1833 <a name="luabind_no_exceptions"></a><h3><tt>LUABIND_NO_EXCEPTIONS</tt></h3>
1834 <p>This define will disable all usage of try, catch and throw in
1835 luabind. This will in many cases disable run-time errors, when
1836 performing invalid casts or calling lua-functions that fails or
1837 returns values that cannot be converted by the given policy.
1838 luabind requires that no function called directly or indirectly
1839 by luabind throws an exception (throwing exceptions through
1840 lua has undefined behavior).</p>
1842 <p>Where exceptions are the only way to get an error report from
1843 luabind, they will be replaced with calls to the callback
1844 functions set byt <a href="#set_error_callback"><tt>set_error_callback()</tt></a>
1845 and <a href="#set_cast_failed_callback"><tt>set_cast_failed_callback()</tt></a>.</p>
1847 <a name="luabind_type_info"></a><h3><tt>LUABIND_TYPE_INFO</tt><br>
1848 <tt>LUABIND_TYPE_INFO_EQUAL(i1,i2)</tt><br>
1849 <tt>LUABIND_TYPEID(t)</tt><br>
1850 <tt>LUABIND_INVALID_TYPE_INFO</tt></h3>
1852 <p>If you don't want to use the RTTI supplied by C++
1853 you can supply your own type-info structure with the
1854 LUABIND_TYPE_INFO define. Your type-info structure must
1855 be copyable and must be able to compare itself against
1856 other type-info structures. You supply the compare
1857 function through the LUABIND_TYPE_INFO_EQUAL()
1858 define. It should compare the two type-info structures
1859 it is given and return true if they represent the same type
1860 and false otherwise. You also have to supply a function
1861 to generate your type-info structure. You do this through
1862 the LUABIND_TYPEID() define. It should return your type-info
1863 structure and it takes a type as its parameter. That is,
1864 a compile time parameter. LUABIND_INVALID_TYPE_INFO macro
1865 should be defined to an invalid type. No other type should
1866 be able to produce this type info. To use it you probably
1867 have to make a traits class with specializations for all
1868 classes that you have type-info for. Like this:</p>
1870 <pre>
1871 class A;
1872 class B;
1873 class C;
1875 template&lt;class T&gt; struct typeinfo_trait;
1877 template&lt;&gt; struct typeinfo_trait&lt;A&gt; { enum { type_id = 0 }; };
1878 template&lt;&gt; struct typeinfo_trait&lt;B&gt; { enum { type_id = 1 }; };
1879 template&lt;&gt; struct typeinfo_trait&lt;C&gt; { enum { type_id = 2 }; };
1880 </pre>
1882 <p>If you have set up your own RTTI system like this (by using integers to identify types)
1883 you can have luabind use it with the following defines</p>
1885 <pre>
1886 #define LUABIND_TYPE_INFO int
1887 #define LUABIND_TYPE_INFO_EQUAL(i1, i2) i1 == i2
1888 #define LUABIND_TYPEID(t) typeinfo_trait&lt;t&gt;::type_id
1889 #define LUABIND_INVALID_TYPE_INFO -1
1890 </pre>
1892 <p>The default behavior, if you don't define any of these three, is to use the built-in
1893 RTTI support in C++.</p>
1895 <pre>
1896 #define LUABIND_TYPE_INFO const std::type_info*
1897 #define LUABIND_TYPEID(t) &amp;typeid(t)
1898 #define LUABIND_TYPE_INFO_EQUAL(i1, i2) *i1 == *i2
1899 #define LUABIND_INVALID_TYPE_INFO &amp;typeid(detail::null_type)
1900 </pre>
1902 <p>currently the type given through LUABIND_TYPE_INFO must be less-than comparable!</p>
1903 <h3><tt>NDEBUG</tt></h3>
1904 <p>This define will disable all asserts and should be defined in a release build.</p>
1907 <h1><a name="implementation_notes"></a>Implementation notes</h1>
1909 <p>The classes and objects are implemented as user data in lua. To make sure that
1910 the user data really is the internal structure it is supposed to be, we tag their
1911 metatables. A user data who's metatable contains a boolean member named
1912 <tt>"__luabind_classrep"</tt> is expected to be a class exported by luabind.
1913 A user data who's metatable contains a boolean member named <tt>"__luabind_class"</tt>
1914 is expected to be an instantiation of a luabind class.</p>
1916 <p>This means that if you make your own user data and tags its metatable with the
1917 exact same names, you can very easily fool luabind and crash the application.</p>
1919 <p>In the lua registry, luabind keeps an entry called "<tt>__luabind_classes</tt>"
1920 and "<tt>__luabind_free_functions</tt>". These should not be removed or overwritten.</p>
1922 <p>In the global table, a variable called <tt>super</tt> is used every time a constructor
1923 in a lua-class is called. This is to make it easy for that constructor to call its base
1924 class' constructor. So, if you have a global variable named <tt>super</tt> it may very
1925 well be overwritten. This is probably not the best solution, and this restriction may very
1926 well be removed in the future.</p>
1928 <p>Inside the <tt>luabind</tt> namespace, there's another namespace called <tt>detail</tt>.
1929 This namespace contains non-public classes and are not supposed to be used directly.</p>
1931 <h1><a name="error_messages"></a>Error messages</h1>
1933 <ul>
1934 <li>cannot set attribute '<i>&lt;class-name&gt;</i>.<i>&lt;attribute-name&gt;</i>'
1935 <p>There is no data member named <i>&lt;attribute-name&gt;</i> in the class <i>&lt;class-name&gt;</i>, or
1936 there's no setter-method registered on that property name. See the <a href="#class_property">properties</a> section.</p>
1938 <li><i>&lt;class-name&gt;</i>() threw an exception
1939 <li><i>&lt;class-name&gt;</i>:<i>&lt;function-name&gt;</i>() threw an exception
1940 <p>The class' constructor or member function threw an unknown exception. Known exceptions are <tt>const&nbsp;char*</tt>,
1941 <tt>std::exception</tt>. See the <a href="#exceptions">exceptions</a> section.</p>
1943 <li>no overload of '<i>&lt;class-name&gt;</I>:<i>&lt;function-name&gt;</i>' matched the arguments (&lt;<i>parameter-types&gt;)</i>
1944 <li>no match for function call '<i>&lt;function-name&gt;</i>' with the parameters (<i>&lt;parameter-types&gt;</i>)
1945 <li>no constructor of <i>&lt;class-name&gt;</i> matched the arguments (<i>&lt;parameter-types&gt;</i>)
1946 <li>no operator <i>&lt;operator-name&gt;</i> matched the arguments (<i>&lt;parameter-types&gt;</i>)
1947 <p>No function/operator with the given name takes the parameters you gave it. You have either misspelled the function name,
1948 or given it incorrect parameters. This error is followed by a list of possible candidate functions to help you figure out what
1949 parameter has the wrong type. If the candidate list is empty there's no function at all with that name. See the
1950 <a href="#fun_sigmatch">signature matching</a> section.</p>
1952 <li>call of overloaded '<i>&lt;class-name&gt;</i>:<i>&lt;function-name&gt;</i>(&lt;<i>parameter-types&gt;</i>)' is ambiguous
1953 <li>ambiguous match for function call '<i>&lt;function-name&gt;</i>' with the parameters (<i>&lt;parameter-types&gt;</i>)
1954 <li>call of overloaded constructor '<i>&lt;class-name&gt;</i>(<i>&lt;parameter-types&gt;</i>)' is ambiguous
1955 <li>call of overloaded operator <i>&lt;operator-name&gt;</i> (<i>&lt;parameter-types&gt;</i>) is ambiguous
1956 <p>This means that the function/operator you are trying to call has at least one other overload that matches the arguments
1957 just as good as the first overload.</p>
1959 <li>cannot derive from C++ class '<i>&lt;class-name&gt;</i>'. It does not have a wrapped type.
1960 <p>You are trying to derive a lua class from a C++ class that doesn't have a wrapped type. You have to give
1961 your C++ class a wrapped type when you register it with lua. See the <a href="#class_lua_derive">deriving in lua</a> section.</p>
1963 <li>derived class must call super on base
1964 <li>cannot set property '<i>&lt;class-name&gt;</i>.<i>&lt;attribute_name&gt;</i>' because it's read only
1965 <p>The attribute you are trying to set is registered as read only. If you want it to be writeable you have to change your
1966 class registration and use <tt>def_readwrite()</tt> instead of <tt>def_readonly()</tt>. Alternatively (if your attribute
1967 is a property with getter and setter functions), you have to give a setter function when declaring your attribute. See
1968 the <a href="#class_property">properties</a> section.</p>
1970 <li>no static '<i>&lt;enum-name&gt;</i>' in class '<i>&lt;class-name&gt;</i>'
1971 <p>You will get this error message if you are trying to access an enum that doesn't exist. Read about how to
1972 <a href="#class_enum">declare enums</a>.</p>
1974 <li>expected base class
1975 <p>You have written a malformed <a href="#class_lua">class definition in lua</a>. The format is:
1976 <tt>class&nbsp;'<i>&lt;class-name&gt;</i>'&nbsp;[<i>&lt;base-class&gt;</i>]</tt>. If you don't want to derive from
1977 a base class, you have to break the line directly after the class declaration.</p>
1979 <li>invalid construct, expected class name
1980 <p>You have written a malformed <a href="#class_lua">class definition in lua</a>. The <tt>class</tt> function expects
1981 a string as argument. That string is the name of the lua class to define.</p>
1982 </ul>
1984 <h1><a name="faq"></a>FAQ</h1>
1985 <ul>
1986 <li><h3>What's up with __cdecl and __stdcall?</h3>
1988 <p>If you're having problem with functions that cannot be converted from
1989 <tt>'void&nbsp;(__stdcall&nbsp;*)(int,int)'</tt> to <tt>'void&nbsp;(__cdecl&nbsp;*)(int,int)'</tt>.
1990 You can change the project settings to make the compiler generate functions with
1991 <tt>__cdecl</tt> calling conventions. This is a problem in developer studio.</p>
1993 <li><h3>What's wrong with functions taking variable number of arguments?</h3>
1994 <p>You cannot register a function with ellipses in its signature. Since ellipses don't preserve
1995 type safety, those should be avoided anyway.</p>
1997 <li><h3>Internal structure overflow in VC</h3>
1998 <p>If you, in visual studio, get <tt>fatal error C1204: compiler limit : internal
1999 structure overflow</tt>. You should try to split that compilation unit up
2000 in smaller ones.</p>
2002 <li><h3>What's wrong with precompiled headers in VC?</h3>
2003 <p>Visual Studio doesn't like anonymous namespaces in its precompiled headers. If you encounter this
2004 problem you can disable precompiled headers for the compilation unit (cpp-file) that uses luabind.</p>
2006 <li><h3>error C1076: compiler limit - internal heap limit reached in VC</h3>
2007 <p>In visual studio you will probably hit this error. To fix it you have to increase the internal heap
2008 with a command-line option. We managed to compile the test suit with /Zm300, but you may need a larger
2009 heap then that.</p>
2011 <li><h3>error C1055: compiler limit : out of keys in VC</h3>
2012 <p>It seems that this error occurs when too many <tt>assert()</tt> are used in a program, or more
2013 specifically, the <tt>__LINE__</tt> macro. It seems to be fixed by changing /ZI (Program database
2014 for edit and continue) to /Zi (Program database).</p>
2016 <li><h3>How come my executable is huge?</h3>
2017 <p>If you're compiling in debug mode, you will probably have a lot of debug-info and symbols (luabind
2018 consists of a lot of functions). Also, if built in debug mode, no optimizations were applied, luabind
2019 relies on that the compiler is able to inline functions. If you built in release mode, try running
2020 <tt>strip</tt> on your executable to remove export-symbols, this will trim down the size.</p>
2021 <p>Our tests suggests that cygwin's gcc produces much bigger executables compared to gcc on other
2022 platforms and other compilers.</p>
2024 <li><h3>Can I register template classes with luabind?</h3>
2025 <p>Yes you can, but you can only register explicit instantiations of the class. Because there's no
2026 lua counterpart to C++ templates. For example, you can register an explicit instantiation of
2027 <tt>std::vector</tt> like this:</p>
2028 <pre>
2029 class_&lt;std::vector&lt;int&gt;&nbsp;&gt;("vector")
2030 .def(constructor&lt;int&gt;)
2031 .def("push_back", &amp;std::vector&lt;int&gt;::push_back)
2032 .commit(L)
2034 </pre>
2035 <p>Note that the space between the two &gt; is required by C++.</p>
2037 <li><h3>Do I have to register destructors for my classes?</h3>
2038 <p>No, the destructor of a class is always called by luabind when an object is collected. Note that
2039 lua has to own the object to collect it. If you pass it to C++ and gives up ownership (with
2040 <a href="#policy_adopt">adopt</a> policy) it will no longer be owned by lua, and not collected.</p>
2041 <p>If you have a class hierarchy, you should make the destructor <tt>virtual</tt> if you want to
2042 be sure that the correct destructor is called (this apply to C++ in general).</p>
2044 <li><h3>Fatal Error C1063 compiler limit : compiler stack overflow in VC</h3>
2045 <p>VC6.5 chokes on warnings, if you are getting alot of warnings from your code try suppressing them
2046 with a pragma directive, this should solve the problem.</p>
2048 </ul>
2050 <h1><a name="future"></a>Future additions</h1>
2051 <ul>
2052 <li>A mechanism for wrapping iterators with generators.
2053 </ul>
2055 <h1><a name="issues"></a>Known issues</h1>
2056 <ul>
2057 <li>If one class registers two functions with the same name and the same signature,
2058 there's currently no error. The last registered function will be the one that's used.
2059 <li>In vc7, classes can not be called <tt>test</tt>.
2060 <li>Visual studio have problems selecting the correct overload of <tt>std::swap()</tt> for <tt>luabind::object</tt>.
2061 </ul>
2063 <h1><a name="acknowledgments"></a>Acknowledgments</h1>
2065 <p>This library was written by <a href="mailto:dalwan01@student.umu.se">Daniel Wallin</a>
2066 and <a href="mailto:c99ang@cs.umu.se">Arvid Norberg</a>. &copy; Copyright 2003. All rights reserved.</p>
2068 <p>This library was inspired by Dave Abrahams'
2069 <a href="http://www.boost.org/libs/python/doc/index.html">Boost.Python</a> library which can be found in the
2070 <a href="http://www.boost.org">boost</a> library.</p>
2072 <a href="http://validator.w3.org/check/referer">
2073 <img style="border:0;width:88px;height:31px"
2074 src="http://www.w3.org/Icons/valid-html401"
2075 alt="Valid HTML 4.01!"></a>
2077 <a href="http://jigsaw.w3.org/css-validator/">
2078 <img style="border:0;width:88px;height:31px"
2079 src="http://jigsaw.w3.org/css-validator/images/vcss"
2080 alt="Valid CSS!"></a>
2082 </p>
2084 </body></html>