5 .. _MIT license: http://www.opensource.org/licenses/mit-license.php
6 .. _Boost: http://www.boost.org
8 Note: This library is currently in public beta phase. This documentation
9 should be considered beta as well. Please report any grammatical
10 corrections/spelling corrections.
14 .. section-numbering::
19 Luabind is a library that helps you create bindings between C++ and lua. It has
20 the ability to expose functions and classes, written in C++, to lua. It will
21 also supply the functionality to define classes in lua and let them derive from
22 other lua classes or C++ classes. Lua classes can override virtual functions
23 from their C++ base classes. It is written towards lua 5.0, and does not work
26 It is implemented utilizing template meta programming. That means that you
27 don't need an extra preprocess pass to compile your project (it is done by the
28 compiler). It also means you don't (usually) have to know the exact signature of
29 each function you register, since the library will generate code depending on
30 the compile-time type of the function (which includes the signature). The main
31 drawback of this approach is that the compilation time will increase for the
32 file that does the registration, it is therefore recommended that you register
33 everything in the same cpp-file.
35 luabind is released under the terms of the `MIT license`_.
37 We are very interested in hearing about projects that use luabind, please let
38 us know about your project.
46 - Overloaded free functions
48 - Overloaded member functions
52 - Lua functions in C++
54 - Lua classes (single inheritance)
55 - Derives from lua or C++ classes
56 - O verride virtual functions from C++ classes
57 - Implicit casts between registered types
58 - Best match signature matching
59 - Return value policies and parameter policies
65 Luabind has been tested to work on the following compilers:
69 - Visual Studio 6.0 (sp 5)
70 - Intel C++ 6.0 (Windows)
72 - GCC 3.0.4 (Debian/Linux)
77 It has been confirmed not to work with:
79 - GCC 2.95.2 (SunOS 5.8)
81 Metrowerks 8.3 (Windows) compiles but fails the const-test. This
82 means that const member functions are treated as non-const member
85 If you have tried luabind with a compiler not listed here, let us know
92 To keep down the compilation-time luabind is built as a library. This means you
93 have to either build it and lika against it, or include its source files in
94 your project. You also have to make sure the luabind directory is somewhere in
95 your compiler's include path. It requires `Boost`_ 1.31.0 to be installed (only
96 boost headers). It also requires that lua is installed.
98 The official way of building luabind is with `Boost.Build V2`_. To properly build
99 luabind with Boost.Build you need to set two environment variables:
102 Point this to your Boost installation.
105 Point this to your lua directory. The build system will assume that the
106 include and library files are located in ``$(LUA_PATH)/include/`` and
107 ``$(LUA_PATH)/lib/.``
109 For backward compatibility, there is also a makefile in the root-directory that
110 will build the library and the test program. If you are using a UNIX-system (or
111 cygwin) they will make it easy to build luabind as a static library. If you are
112 using Visual Studio it may be easier to include the files in the src directory
115 When building luabind you have several options that may streamline the library
116 to better suit your needs. It is extremely important that your application has
117 the same settings as the library was built with. The available options are
118 found in the `Configuration`_ section.
120 If you want to change the settings to differ from the default, it's recommended
121 that you define the settings on the commandline of all your files (in the
122 project settings in visual studio).
124 .. _`Boost.Build V2`: http://www.boost.org/tools/build/v2/index_v2.html
130 To use luabind, you must include ``lua.h`` and luabind's main header file::
137 #include <luabind/luabind.hpp>
139 This includes support for both registering classes and functions. If you just
140 want to have support for functions or classes you can include
141 ``luabind/function.hpp`` and ``luabind/class.hpp`` separately::
143 #include <luabind/function.hpp>
144 #include <luabind/class.hpp>
146 The first thing you need to do is to call ``luabind::open(lua_State*)`` which
147 will register the functions to create classes from lua, and initialize some
148 state-global structures used by luabind. If you don't call this function you
149 will hit asserts later in the library. There is no corresponding close function
150 because once a class has been registered in lua, there really isn't any good
151 way to remove it. Partly because any remaining instances of that class relies
152 on the class being there. Everything will be cleaned up when the state is
155 .. Isn't this wrong? Don't we include lua.h using lua_include.hpp ?
157 Note that no luabind header will include ``lua.h``, this is up to you. You have
158 to include it before any luabind header is included.
167 #include <luabind/luabind.hpp>
171 std::cout << "hello world!\n";
174 extern "C" int init(lua_State* L)
176 using namespace luabind;
190 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
191 > loadlib('hello_world.dll', 'init')()
199 Everything that gets registered in lua is registered in a namespace (lua
200 tables) or in the global scope (called module). All registrations must be
201 surrounded by its scope. To define a module, the ``luabind::module`` class is
202 used. It is used like this::
209 This will register all declared functions or classes in the global namespace in
210 lua. If you want to have a namespace for your module (like the standard
211 libraries) you can give a name to the constructor, like this::
213 module(L, "my_library")
218 Here all declarations will be put in the my_library table.
220 If you want nested namespaces you can use the ``luabind::namespace_`` class. It
221 works exactly as ``luabind::module`` except that it doesn't take a lua_State*
222 in it's constructor. An example of its usage could look like this::
224 module(L, "my_library")
230 // library-private declarations
234 As you might have figured out, the following declarations are equivalent::
238 namespace_("my_library")
247 module(L, "my_library")
252 Each declaration must be separated by a comma, like this::
259 .def(constructor<int, int>),
264 More about the actual declarations in the `Binding functions to lua`_ and
265 `Binding classes to lua`_ sections.
267 A word of caution, if you are in really bad need for performance, putting your
268 functions in tables will increase the lookup time.
271 Binding functions to lua
272 ========================
274 To bind functions to lua you use the function ``luabind::def()``. It has the
277 template<class F, class policies>
278 void def(const char* name, F f, const Policies&);
280 - name is the name the function will have within lua.
281 - F is the function pointer you want to register.
282 - The Policies parameter is used to describe how parameters and return values
283 are treated by the function, this is an optional parameter. More on this in
284 the `policies`_ section.
286 An example usage could be if you want to register the function ``float
291 def("sin", &std::sin)
297 If you have more than one function with the same name, and want to register
298 them in lua, you have to explicitly give the signature. This is to let C++ know
299 which function you refer to. For example, if you have two functions, ``int
300 f(const char*)`` and ``void f(int)``. ::
304 def("f", (int(*)(const char*)) &f),
305 def("f", (void(*)(int)) &f)
311 luabind will generate code that checks the lua stack to see if the values there
312 can match your functions' signatures. It will handle implicit typecasts between
313 derived classes, and it will prefer matches with the least number of implicit
314 casts. In a function call, if the function is overloaded and there's no
315 overload that match the parameters better than the other, you have an
316 ambiguity. This will spawn a run-time error, stating that the function call is
317 ambiguous. A simple example of this is to register one function that takes an
318 int and one that takes a float. Since lua don't distinguish between floats and
319 integers, both will always match.
321 Since all overloads are tested, it will always find the best match (not the
322 first match). This also means that it can handle situations where the only
323 difference in the signature is that one member function is const and the other
326 .. sidebar:: Ownership transfer
328 To correctly handle ownership transfer, create_a() would need an adopt
329 return value policy. More on this in the `Policies`_ section.
331 For example, if the following function and class is registered:
349 And the following lua code is executed::
352 a1:f() -- the const version is called
355 a2:f() -- the non-const version is called
366 Calling lua functions
367 ---------------------
369 To call a lua function, you can either use ``call_function()``,
370 ``call_member()``, an ``object`` or ``functor``.
375 Ret call_function(lua_State* L, const char* name, ...)
377 This calls the global function called name. This function can only call global
378 lua functions. The ... represents a variable number of parameters that are sent
379 to the lua function. This function call may throw ``luabind::error`` if the
382 The return value isn't actually Ret (the template parameter), but a proxy
383 object that will do the function call. This enables you to give policies to the
384 call. You do this with the operator[]. You give the policies within the
385 brackets, like this::
387 int ret = call_function<int>(
390 , new complex_class()
396 Ret call_member(object&, const char* name, ...)
398 This treats the given object as an instance of a class. The given name is the
399 name of a member function to call. The ... represents a variable number of
400 parameters given to the function. This function may throw ``luabind::error`` if
401 the function call fails.
403 You can give policies to a member function call the same way as you do with
410 To start a lua thread, you have to call ``lua_resume()``, this means that you
411 cannot use the previous function ``call_function()`` to start a thread. You have
417 Ret resume_function(lua_State* L, const char* name, ...)
424 Ret resume(lua_State* L, ...)
426 The first time you start the thread, you have to give it a function to execute. i.e. you
427 have to use ``resume_function``, when the lua function yeilds, it will return the first
428 value passed in to ``lua_yield()``. When you want to continue the execution, you just call
429 ``resume()`` on your ``lua_State``, since it's already executing a function, you don't pass
430 it one. The parameters to ``resume()`` will be returned by ``yield()`` on the lua side.
432 For yielding C++-functions (without the support of passing data back and forth between the
433 lua side and the c++ side), you can use the Yield_ policy.
436 Binding classes to lua
437 ======================
439 To register classes you use a class called ``class_``. Its name is supposed to
440 resemble the C++ keyword, to make it look more intuitive. It has an overloaded
441 member function ``def()`` that is used to register member functions, operators,
442 constructors, enums and properties on the class. It will return its
443 this-pointer, to let you register more members directly.
445 Let's start with a simple example. Consider the following C++ class::
450 testclass(const std::string& s): m_string(s) {}
451 void print_string() { std::cout << m_string << "\n"; }
454 std::string m_string;
457 To register it with a lua environment, write as follows (assuming you are using
462 class_<testclass>("testclass")
463 .def(constructor<const std::string&>())
464 .def("print_string", &testclass::print_string)
467 This will register the class with the name testclass and constructor that takes
468 a string as argument and one member function with the name ``print_string``.
472 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
473 > a = testclass('a string')
477 It is also possible to register free functions as member functions. The
478 requirement on the function is that it takes a pointer, const pointer,
479 reference or const reference to the class type as the first parameter. The rest
480 of the parameters are the ones that are visible in lua, while the object
481 pointer is given as the first parameter. If we have the following C++ code::
488 int plus(A* o, int v) { return o->a + v; }
490 You can register ``plus()`` as if it was a member function of A like this::
495 ``plus()`` can now be called as a member function on A with one parameter, int.
496 If the object pointer parameter is const, the function will act as if it was a
497 const member function (it can be called on const objects).
503 To register a global data member with a class is easily done. Consider the
511 This class is registered like this::
516 .def_readwrite("a", &A::a)
519 This gives read and write access to the member variable ``A::a``. It is also
520 possible to register attributes with read-only access::
525 .def_readonly("a", &A::a)
528 You can also register getter and setter functions and make them look as if they
529 were a public data member. Consider the following class::
534 void set_a(int x) { a = x; }
535 int get_a() const { return a; }
541 It can be registered as if it had a public data member a like this::
544 .property("a", &A::get_a, &A::set_a)
546 This way the ``get_a()`` and ``set_a()`` functions will be called instead of
547 just writing to the data member. If you want to make it read only you can just
548 omit the last parameter.
554 If your class contains enumerated constants (enums), you can register them as
555 well to make them available in lua. Note that they will not be type safe, all
556 enums are integers in lua, and all functions that takes an enum, will accept
557 any integer. You register them like this::
565 value("my_2nd_enum", 7),
566 value("another_enum", 6)
570 In lua they are accessed like any data member, except that they are read-only
571 and reached on the class itself rather than on an instance of the class.
575 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
578 > print(A.another_enum)
585 The mechanism for registering operators on your class is pretty simple. You use
586 a global name ``luabind::self`` to refer to the class itself and then you just
587 write the operator expression inside the ``def()`` call. This class::
591 vec operator+(int s);
594 Is registered like this::
602 This will work regardless if your plus operator is defined inside your class or
605 If you operator is const (or, when defined as a free function, takes a const
606 reference to the class itself) you have to use ``const_self`` instead of
607 ``self``. Like this::
612 .def(const_self + int())
615 The operators supported are those available in lua:
624 This means, no in-place operators. The equality operator (==) has a little
625 hatch, it will not be called if the references are equal. This means that the
626 == operator has to do pretty much what's it's expected to do.
628 In the above example the other operand type is instantiated by writing
629 ``int()``. If the operand type is a complex type that cannot easily be
630 instantiated you can wrap the type in a class called ``other<>``. For example:
632 To register this class, we don't want to instantiate a string just to register
639 vec operator+(std::string);
642 Instead we use the other ``wrapper`` like this::
647 .def(self + other<std::string>())
650 To register an application operator::
658 There's one special operator. In lua it's called ``__tostring``, it's not
659 really an operator. It is used for converting objects to strings in a standard
660 way in lua. If you register this functionality, you will be able to use the lua
661 standard function ``tostring()`` for converting you object to a string.
663 To implement this operator in C++ you should supply an ``operator<<`` for
664 ostream. Like this example::
667 std::ostream& operator<<(std::ostream&, number&);
673 class_<number>("number")
678 Nested scopes and static functions
679 ----------------------------------
681 It is possible to add nested scopes to a class. This is useful when you need
682 to wrap a nested class, or a static function. ::
689 class_<inner>("nested"),
693 It's also possible to add namespaces to classes using the same syntax.
699 If you want to register classes that derives from other classes, you can
700 specify a template parameter ``bases<>`` to the ``class_`` instantiation. The
701 following hierarchy::
706 Would be registered like this::
714 If you have multiple inheritance you can specify more than one base. If B would
715 also derive from a class C, it would be registered like this::
719 class_<B, bases<A, C> >("B")
722 Note that you can omit ``bases<>`` when using single inheritance.
725 If you don't specify that classes derive from each other, luabind will not
726 be able to implicitly cast pointers between the types.
732 When you register a class you can tell luabind that all instances of that class
733 should be held by some kind of smart pointer (boost::shared_ptr for instance).
734 You do this by giving the holder type as an extra template parameter to
735 the``class_``your constructing, like this::
739 class_<A, boost::shared_ptr<A> >("A")
742 You also have to supply two functions for your smart pointer. One that returns
743 the type of const version of the smart pointer type (boost::shared_ptr<const A>
744 in this case). And one function that extracts the raw pointer from the smart
745 pointer. The first function is needed because luabind has to allow the
746 non-const -> conversion when passing values from lua to C++. The second
747 function is needed when lua calls member functions on held types, the this
748 pointer must be a raw pointer, it is also needed to allow the smart_pointer ->
749 raw_pointer conversion from lua to C++. They look like this::
754 T* get_pointer(boost::shared_ptr<T>& p)
760 boost::shared_ptr<const A>*
761 get_const_holder(boost::shared_ptr<A>*)
767 The conversion that works are (given that B is a base class of A):
769 +----------------------------------------------+
771 +======================+=======================+
772 | holder_type<A> | A* |
773 +----------------------+-----------------------+
774 | holder_type<A> | A* |
775 +----------------------+-----------------------+
776 | holder_type<A> | const A* |
777 +----------------------+-----------------------+
778 | holder_type<A> | const B* |
779 +----------------------+-----------------------+
780 | holder_type<A> | holder_type<A> |
781 +----------------------+-----------------------+
782 | holder_type<A> | holder_type<const A> |
783 +----------------------+-----------------------+
784 | holder_type<const A> | const A* |
785 +----------------------+-----------------------+
786 | holder_type<const A> | const B* |
787 +----------------------+-----------------------+
788 | holder_type<const A> | holder_type<const A> |
789 +----------------------+-----------------------+
791 +-----------------------------------------------------+
793 +=============================+=======================+
794 | holder_type<A> | holder_type<A> |
795 +-----------------------------+-----------------------+
796 | holder_type<const A> | holder_type<const A> |
797 +-----------------------------+-----------------------+
798 | const holder_type<A>& | holder_type<A> |
799 +-----------------------------+-----------------------+
800 | const holder_type<const A>& | holder_type<const A> |
801 +-----------------------------+-----------------------+
803 When using a holder type, it can be useful to know if the pointer is valid. For
804 example when using std::auto_ptr, the holder will be invalidated when passed as
805 a parameter to a function. For this purpose there is a member of all object
806 instances in luabind: ``__ok``. ::
809 void f(std::auto_ptr<test>);
814 .def(constructor<>()),
821 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
831 Since functions have to be able to take lua values (of variable type) we need a
832 wrapper around them. This wrapper is called ``luabind::object``. If the
833 function you register takes an object, it will match any lua value. To use it,
834 you need to include ``luabind/object.hpp``. The object class has the following
842 class array_iterator;
845 object(lua_State*, const T& value);
846 object(const object&);
852 iterator begin() const;
853 iterator end() const;
854 raw_iterator raw_begin() const;
855 raw_iterator raw_end() const;
856 array_iterator abegin() const;
857 array_iterator aend() const;
860 lua_State* lua_state() const;
861 void pushvalue() const;
862 bool is_valid() const;
863 operator safe_bool_type() const;
866 <implementation-defined> operator[](const Key&);
869 object at(const Key&) const;
872 object raw_at(const Key&) const;
875 object& operator=(const T&);
876 object& operator=(const object&);
879 bool operator==(const T&) const;
880 bool operator==(const object&) const;
881 bool operator<(const object&) const;
882 bool operator<=(const object&) const;
883 bool operator>(const object&) const;
884 bool operator>=(const object&) const;
885 bool operator!=(const object&) const;
890 <implementation-defined> operator()();
893 <implementation-defined> operator()(const A0& a0);
895 template<class A0, class A1>
896 <implementation-defined> operator()(const A0& a0, const A1& a1);
902 When you have a lua object, you can assign it a new value with the assignment
903 operator (=). When you do this, the ``default_policy`` will be used to make the
904 conversion from C++ value to lua. If your ``luabind::object`` is a table you
905 can access its members through the operator[] or the iterators. The value
906 returned from the operator[] is a proxy object that can be used both for
907 reading and writing values into the table (using operator=). Note that it is
908 impossible to know if a lua value is indexable or not (lua_gettable doesn't
909 fail, it succeeds or crashes). This means that if you're trying to index
910 something that cannot be indexed, you're on your own. Lua will call its
911 ``panic()`` function (you can define your own panic function using
912 lua_setpanicf). The ``at()`` and ``raw_at()`` functions returns the value at
913 the given table position (like operator[] but only for reading).
915 The ordinary ``object::iterator`` uses lua_gettable to extract the values from
916 the table, the standard way that will invoke metamethods if any. The
917 ``object::raw_iterator`` uses lua_rawget and ``object::array_iterator`` uses
918 lua_rawgeti. The latter will only iterate over numberical keys starting at 1
919 and continue until the first nil value.
921 The ``lua_state()`` function returns the lua state where this object is stored.
922 If you want to manipulate the object with lua functions directly you can push
923 it onto the lua stack by calling ``pushvalue()``. And set the object's value by
924 calling ``set()``, which will pop the top value from the lua stack and assign
927 The operator== will call lua_equal() on the operands and return its result.
929 The ``type()`` member function will return the lua type of the object. It will
930 return the same values as lua_type().
932 The ``is_valid()`` function tells you whether the object has been initialized
933 or not. When created with its default constructor, objects are invalid. To make
934 an object valid, you can assign it a value. If you want to invalidate an object
935 you can simply assign it an invalid object.
937 .. So what? implementation detail, leave out of docs
938 isn't really an implicit cast to bool, but an implicit cast
939 to a member pointer, since member pointers don't have any arithmetic operators
940 on them (which can cause hard to find errors). The functionality of the cast
943 The ``operator safe_bool_type()`` is equivalent to ``is_valid()``. This means
944 that these snippets are equivalent::
962 The application operator will call the value as if it was a function. You can
963 give it any number of parameters (currently the ``default_policy`` will be used
964 for the conversion). The returned object refers to the return value (currently
965 only one return value is supported). This operator may throw ``luabind::error``
966 if the function call fails. If you want to specify policies to your function
967 call, you can use index-operator (operator[]) on the function call, and give
968 the policies within the [ and ]. Like this::
973 , new my_complex_structure(6)
976 This tells luabind to make lua adopt the ownership and responsibility for the
977 pointer passed in to the lua-function.
979 It's important that all instances of object have been destructed by the time
980 the lua state is closed. The object will keep a pointer to the lua state and
981 release its lua object in its destructor.
983 Here's an example of how a function can use a table::
985 void my_function(const object& table)
987 if (table.type() == LUA_TTABLE)
989 table["time"] = std::clock();
990 table["name"] = std::rand() < 500 ? "unusual" : "usual";
992 std::cout << object_cast<std::string>(table[5]) << "\n";
996 If you take a ``luabind::object`` as a parameter to a function, any lua value
997 will match that parameter. That's why we have to make sure it's a table before
1004 The iterators, that are returned by ``begin() and ``end()`` (and their
1005 variants) are (almost) models of the ForwardIterator concept. The exception
1006 is that post increment doesn't exist on them.
1008 They look like this::
1010 class object::iterator
1013 iterator(const iterator&);
1015 iterator& operator++();
1016 bool operator!=(const iterator&) const;
1017 iterator& operator=(const iterator&);
1021 implementation-defined operator*();
1024 The implementation defined return value from the dereference operator is a
1025 proxy object that can be used as if it was an object, it can also be used to
1026 assign the specific table entry with a new value. If you want to assign a value
1027 to an entry pointed to by an iterator, just use the assignment operator on the
1028 dereferenced iterator::
1032 The ``key()`` member returns the key used by the iterator when indexing the
1033 associated lua table.
1039 There are a couple of functions related to objects and tables. ::
1041 T object_cast<T>(const object&);
1042 T object_cast<T>(const object&, const Policies&);
1045 object_cast_nothrow<T>(const object&);
1048 object_cast_nothrow<T>(const object&, const Policies&);
1054 The ``functor`` class is similar to object, with the exception that it can only
1055 be used to store functions. If you take it as a parameter, it will only match
1058 To use it you need to include its header::
1060 #include <luabind/functor.hpp>
1062 It takes one template parameter, the return value of the lua function it
1063 represents. Currently the functor can have at most one return value (unlike lua
1064 functions). It has the following synopsis::
1071 functor(lua_State*, const char* name);
1072 functor(const functor&);
1076 bool is_valid() const;
1077 operator safe_bool_type() const;
1079 lua_State* lua_state() const;
1080 void pushvalue() const;
1082 bool operator==(const functor<Ret>&);
1083 bool operator!=(const functor<Ret>&);
1085 <implementation-defined> operator()() const;
1088 <implementation-defined> operator()(const A0&) const;
1090 template<class A0, class A1>
1091 <implementation-defined> operator()(const A0&, const A1&) const;
1096 The application operator takes any parameters. The parameters are converted
1097 into lua and the function is called. The return value will act as if it was the
1098 type Ret, with the exception that you can use the return value to give policies
1099 to the call. You do this the same way as you do with objects, using the
1100 operator[], and giving the policies inside the brackets.
1102 The ``is_valid()`` function works just like the one on object, it tells you if
1103 the functor has been assigned with a valid lua function. The ``operator
1104 safe_bool_type()`` is an alias for this member function and also works just as
1105 the one found in object.
1107 For example, if you have the following lua function::
1113 You can expose it to C++ like this::
1115 functor<int> f(L, "f");
1117 std::cout << f(3, 5) << "\n";
1119 This will print out the sum of 3 and 5. Note that you can pass any parameters
1120 to the application operator of ``luabind::functor``, this is because lua
1121 doesn't have signatures for its functions. All lua functions take any number of
1122 parameters of any type.
1124 If we have a C++ function that takes a ``luabind::functor`` and registers it,
1125 it will accept lua functions passed to it. This enables us to expose APIs that
1126 requires you to register callbacks. For example, if your C++ API looks like
1129 void set_callback(void(*)(int, int));
1131 And you want to expose it to lua, you have to wrap the call to the lua
1132 function inside a real C++ function, like this::
1134 functor<void> lua_callback;
1136 void callback_wrapper(int a, int b)
1141 void set_callback_wrapper(const functor<void>& f)
1144 set_callback(&callback_wrapper);
1147 And then register ``set_callback_wrapper`` instead of registering
1148 ``set_callback``. This will have the effect that when one tries to register the
1149 callback from lua, your ``set_callback_wrapper`` will be called instead and
1150 first set the lua functor to the given function. It will then call the real
1151 ``set_callback`` with the ``callback_wrapper``. The ``callback_wrapper`` will
1152 be called whenever the callback should be called, and it will simply call the
1153 lua function that we registered.
1155 You can also use ``object_cast`` to cast an object to a functor.
1158 Defining classes in lua
1159 =======================
1161 In addition to binding C++ functions and classes with lua, luabind also provide
1162 an OO-system in lua. ::
1164 class 'lua_testclass'
1166 function lua_testclass:__init(name)
1170 function lua_testclass:print()
1174 a = lua_testclass('example')
1178 Inheritance can be used between lua-classes::
1180 class 'derived' (lua_testclass)
1182 function derived:__init() super('derived name')
1185 function derived:print()
1186 print('Derived:print() -> ')
1187 lua_testclass.print(self)
1190 Here the ``super`` keyword is used in the constructor to initialize the base
1191 class. The user is required to call ``super`` first in the constructor.
1193 As you can see in this example, you can call the base class member functions.
1194 You can find all member functions in the base class, but you will have to give
1195 the this-pointer (``self``) as first argument.
1201 It is also possible to derive lua classes from C++ classes, and override
1202 virtual functions with lua functions. To do this we have to create a wrapper
1203 class for our C++ base class. This is the class that will hold the lua object
1204 when we instantiate a lua class.
1206 The wrapper class has to provide the same constructors as the base class, with
1207 the addition of one extra parameter: ``luabind::object``. This is the reference
1208 to the lua object that should be held by the wrapper, and should be stored in a
1209 member variable as done in the sample below. ::
1215 { std::cout << s << "\n"; }
1217 virtual void f(int a)
1218 { std::cout << "f(" << a << ")\n"; }
1221 struct base_wrapper : base
1224 base_wrapper(object self_, const char* s)
1225 : base(s), self(self_)
1228 virtual void f(int a)
1230 call_member<void>(self, "f", a);
1233 static void f_static(base* ptr, int a)
1235 return ptr->base::f(a);
1243 class_<base, base_wrapper>("base")
1244 .def(constructor<const char*>())
1245 .def("f", &base_wrapper::f_static)
1248 Note that if you have both base classes and a base class wrapper, you must give
1249 both bases and the base class wrapper type as template parameter to
1250 ´´class_´´. The order in which you specify them is not important.
1252 If we didn't have a class wrapper, it would not be possible to pass a lua class
1253 back to C++. Since the entry points of the virtual functions would still point
1254 to the C++ base class, and not to the functions defined in lua. That's why we
1255 need one function that calls the base class' real function (used if the lua
1256 class doesn't redefine it) and one virtual function that dispatches the call
1257 into luabind, to allow it to select if a lua function should be called, or if
1258 the original function should be called. If you don't intend to derive from a
1259 C++ class, or if it doesn't have any virtual member functions, you can register
1260 it without a class wrapper.
1262 You don't need to have a class wrapper in order to derive from a class, but if
1263 it has virtual functions you may have silent errors.
1265 .. Unnecessary? The rule of thumb is:
1266 If your class has virtual functions, create a wrapper type, if it doesn't
1267 don't create a wrapper type.
1270 Overloading operators
1271 ---------------------
1273 You can overload most operators in lua for your classes. You do this by simply
1274 declaring a member function with the same name as an operator (the name of the
1275 metamethods in lua). The operators you can overload are:
1289 ``__tostring`` isn't really an operator, but it's the metamethod that is called
1290 by the standard library's ``tostring()`` function. There's one strange behavior
1291 regarding binary operators. You are not guaranteed that the self pointer you
1292 get actually refers to an instance of your class. This is because lua doesn't
1293 distinguish the two cases where you get the other operand as left hand value or
1294 right hand value. Consider the following examples::
1298 function my_class:__init(v)
1302 function my_class:__sub(v)
1303 return my_class(self.val - v.val)
1306 function my_class:__tostring()
1310 This will work well as long as you only subtracts instances of my_class with
1311 each other. But If you want to be able to subtract ordinary numbers from your
1312 class too, you have to manually check the type of both operands, including the
1315 function my_class:__sub(v)
1316 if (type(self) == 'number') then
1317 return my_class(self - v.val)
1319 elseif (type(v) == 'number') then
1320 return my_class(self.val - v)
1323 -- assume both operands are instances of my_class
1324 return my_class(self.val - v.val)
1329 The reason why ``__sub`` is used as an example is because subtraction is not
1330 commutative (the order of the operands matter). That's why luabind cannot
1331 change order of the operands to make the self reference always refer to the
1332 actual class instance.
1334 If you have two different lua classes with an overloaded operator, the operator
1335 of the right hand side type will be called. If the other operand is a C++ class
1336 with the same operator overloaded, it will be prioritized over the lua class'
1337 operator. If none of the C++ overloads matches, the lua class operator will be
1344 If an object needs to perform actions when it's collected we provide a
1345 ``__finalize`` function that can be overridden in lua-classes. The
1346 ``__finalize`` functions will be called on all classes in the inheritance
1347 chain, starting with the most derived type. ::
1351 function lua_testclass:__finalize()
1352 -- called when the an object is collected
1359 If any of the functions you register throws an exception when called, that
1360 exception will be caught by luabind and converted to an error string and
1361 ``lua_error()`` will be invoked. If the exception is a ``std::exception`` or a
1362 ``const char*`` the string that is pushed on the lua stack, as error message,
1363 will be the string returned by ``std::exception::what()`` or the string itself
1364 respectively. If the exception is unknown, a generic string saying that the
1365 function threw an exception will be pushed.
1367 Exceptions thrown from user defined functions have to be caught by luabind. If
1368 they weren't they would be thrown through lua itself, which is usually compiled
1369 as C code and doesn't support the stack-unwinding that exceptions imply.
1371 Any function that invokes lua code may throw ``luabind::error``. This exception
1372 means that a lua run-time error occurred. The error message is found on top of
1373 the lua stack. The reason why the exception doesn't contain the error string
1374 itself is because it would then require heap allocation which may fail. If an
1375 exception class throws an exception while it is being thrown itself, the
1376 application will be terminated.
1378 Error's synopsis is::
1380 class error : public std::exception
1384 lua_State* state() const throw();
1385 virtual const char* what() const throw();
1388 The state function returns a pointer to the lua state in which the error was
1389 thrown. This pointer may be invalid if you catch this exception after the lua
1390 state is destructed. If the lua state is valid you can use it to retrieve the
1391 error message from the top of the lua stack.
1393 An example of where the lua state pointer may point to an invalid state
1398 lua_state(lua_State* L): m_L(L) {}
1399 ~lua_state() { lua_close(m_L); }
1400 operator lua_State*() { return m_L; }
1408 lua_state L = lua_open();
1411 catch(luabind::error& e)
1413 lua_State* L = e.state();
1414 // L will now point to the destructed
1415 // lua state and be invalid
1420 There's another exception that luabind may throw: ``luabind::cast_failed``,
1421 this exception is thrown from ``call_function<>``, ``call_member<>`` or when
1422 ``functor<>`` is invoked. It means that the return value from the lua function
1423 couldn't be converted to a C++ value. It is also thrown from ``object_cast<>``
1424 if the cast cannot be made.
1426 The synopsis for ``luabind::cast_failed`` is::
1428 class cast_failed : public std::exception
1431 cast_failed(lua_State*);
1432 lua_State* state() const throw();
1433 LUABIND_TYPE_INFO info() const throw();
1434 virtual const char* what() const throw();
1437 Again, the state member function returns a pointer to the lua state where the
1438 error occurred. See the example above to see where this pointer may be invalid.
1440 The info member function returns the user defined ``LUABIND_TYPE_INFO``, which
1441 defaults to a ``const std::type_info*``. This type info describes the type that
1442 we tried to cast a lua value to.
1444 If you have defined ``LUABIND_NO_EXCEPTIONS`` none of these exceptions will be
1445 thrown, instead you can set two callback functions that are called instead.
1446 These two functions are only defined if ``LUABIND_NO_EXCEPTIONS`` are defined.
1450 luabind::set_error_callback(void(*)(lua_State*))
1452 The function you set will be called when a runtime-error occur in lua code. You
1453 can find an error message on top of the lua stack. This function is not
1454 expected to return, if it does luabind will call ``std::terminate()``.
1458 luabind::set_cast_failed_callback(void(*)(lua_State*, LUABIND_TYPE_INFO))
1460 The function you set is called instead of throwing ``cast_failed``. This function
1461 is not expected to return, if it does luabind will call ``std::terminate()``.
1467 Sometimes it is necessary to control how luabind passes arguments and return
1468 value, to do this we have policies. These are the policies that can be used:
1473 This will make a copy of the parameter. This is the default behavior when
1474 passing parameters by-value. Note that this can only be used when passing from
1475 C++ to lua. This policy requires that the parameter type has a copy
1478 To use this policy you need to include ``luabind/copy_policy.hpp``.
1484 This will transfer ownership of the parameter.
1486 Consider making a factory function in C++ and exposing it to lua::
1497 def("create_base", create_base)
1500 Here we need to make sure lua understands that it should adopt the pointer
1501 returned by the factory-function. This can be done using the adopt-policy.
1507 def(L, "create_base", adopt(return_value))
1510 To specify multiple policies we just separate them with '+'.
1514 base* set_and_get_new(base* ptr)
1516 base_ptrs.push_back(ptr);
1522 def("set_and_get_new", &set_and_get_new,
1523 adopt(return_value) + adopt(_1))
1526 When lua adopts a pointer, it will call delete on it. This means that it cannot
1527 adopt pointers allocated with another allocator than new (no malloc for
1530 To use this policy you need to include ``luabind/adopt_policy.hpp``.
1536 The dependency policy is used to create life-time dependencies between values.
1537 Consider the following example::
1543 const B& get_member()
1549 When wrapping this class, we would do something like::
1554 .def(constructor<>())
1555 .def("get_member", &A::get_member)
1559 However, since the return value of get_member is a reference to a member of A,
1560 this will create some life-time issues. For example::
1562 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1564 b = a:get_member() -- b points to a member of a
1566 collectgarbage(0) -- since there are no references left to a, it is
1568 -- at this point, b is pointing into a removed object
1570 When using the dependency-policy, it is possible to tell luabind to tie the
1571 lifetime of one object to another, like this::
1576 .def(constructor<>())
1577 .def("get_member", &A::get_member, dependency(result, self))
1580 This will create a dependency between the return-value of the function, and the
1581 self-object. This means that the self-object will be kept alive as long as the
1582 result is still alive. ::
1584 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1586 b = a:get_member() -- b points to a member of a
1588 collectgarbage(0) -- a is dependent on b, so it isn't removed
1590 collectgarbage(0) -- all dependencies to a gone, a is removed
1592 To use this policy you need to include ``luabind/dependency_policy.hpp``.
1598 It is very common to return references to arguments or the this-pointer to
1599 allow for chaining in C++.
1614 When luabind generates code for this, it will create a new object for the
1615 return-value, pointing to the self-object. This isn't a problem, but could be a
1616 bit inefficient. When using the return_reference_to-policy we have the ability
1617 to tell luabind that the return-value is already on the lua stack.
1624 .def(constructor<>())
1625 .def("set", &A::set, return_reference_to(self))
1628 Instead of creating a new object, luabind will just copy the object that is
1629 already on the stack.
1632 This policy ignores all type information and should be used only it
1633 situations where the parameter type is a perfect match to the
1634 return-type (such as in the example).
1636 To use this policy you need to include ``luabind/return_reference_to_policy.hpp``.
1642 This policy makes it possible to wrap functions that take non const references
1643 as its parameters with the intention to write return values to them.
1647 void f(float& val) { val = val + 10.f; }
1653 void f(float* val) { *val = *val + 10.f; }
1655 Can be wrapped by doing::
1659 def("f", &f, out_value(_1))
1662 When invoking this function from lua it will return the value assigned to its
1667 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1672 When this policy is used in conjunction with user define types we often need
1673 to do ownership transfers.
1679 void f1(A*& obj) { obj = new A(); }
1680 void f2(A** obj) { *obj = new A(); }
1682 Here we need to make sure luabind takes control over object returned, for
1683 this we use the adopt policy::
1688 def("f1", &f1, out_value(_1, adopt(_2)))
1689 def("f2", &f2, out_value(_1, adopt(_2)))
1692 Here we are using adopt as an internal policy to out_value. The index
1693 specified, _2, means adopt will be used to convert the value back to lua.
1694 Using _1 means the policy will be used when converting from lua to C++.
1696 To use this policy you need to include ``luabind/out_value_policy.hpp``.
1701 This policy works in exactly the same way as out_value, except that it
1702 replaces the parameters with default-constructed objects.
1706 void get(float& x, float& y)
1717 pure_out_value(_1) + pure_out_value(_2))
1722 Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
1727 Like out_value, it is possible to specify an internal policy used then
1728 converting the values back to lua.
1732 void get(test_class*& obj)
1734 obj = new test_class();
1741 def("get", &get, pure_out_value(_1, adopt(_1)))
1748 This is a very simple policy which makes it possible to throw away
1749 the value returned by a C++ function, instead of converting it to
1750 lua. This example makes sure the this reference never gets converted
1757 simple& set_name(const std::string& n)
1770 class_<simple>("simple")
1771 .def("set_name", &simple::set_name, discard_result)
1774 To use this policy you need to include ``luabind/discard_result_policy.hpp``.
1780 This policy converts an STL container to a generator function that can be used
1781 in lua to iterate over the container. It works on any container that defines
1782 ``begin()`` and ``end()`` member functions (they have to return iterators). It
1783 can be used like this::
1787 std::vector<std::string> names;
1794 .def_readwrite("names", &A::names, return_stl_iterator)
1797 The lua code to iterate over the container::
1801 for name in a.names do
1806 To use this policy you need to include ``luabind/iterator_policy.hpp``.
1812 This policy will cause the function to always yield the current thread when
1813 returning. See the lua manual for restrictions on yield.
1816 Splitting up the registration
1817 =============================
1821 luabind::scope register_a()
1831 luabind::scope register_b()
1841 luabind::scope register_a();
1842 luabind::scope register_b();
1844 void register_module(lua_State* L)
1857 There are a number of configuration options available when building luabind.
1858 It is very important that your project has the exact same conmfiguration
1859 options as the ones given when the library was build! The exceptions are the
1860 ``LUABIND_MAX_ARITY`` and ``LUABIND_MAX_BASES`` which are template-based
1861 options and only matters when you use the library (which means they can
1862 differ from the settings of the library).
1864 The default settings which will be used if no other settings are given
1865 can be found in ``luabind/config.hpp``.
1867 If you want to change the settings of the library, you can modify the
1868 config file. It is included and used by all makefiles. You can change paths
1869 to lua and boost in there as well.
1872 Controls the maximum arity of functions that are registered with luabind.
1873 You can't register functions that takes more parameters than the number
1874 this macro is set to. It defaults to 5, so, if your functions have greater
1875 arity you have to redefine it. A high limit will increase compilation time.
1878 Controls the maximum number of classes one class can derive from in
1879 luabind (the number of classes specified within ``bases<>``).
1880 ``LUABIND_MAX_BASES`` defaults to 4. A high limit will increase
1883 LUABIND_NO_ERROR_CHECKING
1884 If this macro is defined, all the lua code is expected only to make legal
1885 calls. If illegal function calls are made (e.g. giving parameters that
1886 doesn't match the function signature) they will not be detected by luabind
1887 and the application will probably crash. Error checking could be disabled
1888 when shipping a release build (given that no end-user has access to write
1889 custom lua code). Note that function parameter matching will be done if a
1890 function is overloaded, since otherwise it's impossible to know which one
1891 was called. Functions will still be able to throw exceptions when error
1892 checking is disabled.
1894 If a functions throws an exception it will be caught by luabind and
1895 propagated with ``lua_error()``.
1897 LUABIND_NO_EXCEPTIONS
1898 This define will disable all usage of try, catch and throw in luabind.
1899 This will in many cases disable run-time errors, when performing invalid
1900 casts or calling lua functions that fails or returns values that cannot
1901 be converted by the given policy. luabind requires that no function called
1902 directly or indirectly by luabind throws an exception (throwing exceptions
1903 through lua has undefined behavior).
1905 Where exceptions are the only way to get an error report from luabind,
1906 they will be replaced with calls to the callback functions set with
1907 ``set_error_callback()`` and ``set_cast_failed_callback()``.
1910 If you want to link dynamically against lua, you can set this define to
1911 the import-keyword on your compiler and platform. On windows in devstudio
1912 this should be ``__declspec(dllimport)`` if you want to link against lua
1915 LUABIND_EXPORT, LUABIND_IMPORT
1916 If you want to link against luabind as a dll (in devstudio), you can
1917 define ``LUABIND_EXPORT`` to ``__declspec(dllexport)`` and
1918 ``LUABIND_IMPORT`` to ``__declspec(dllimport)``.
1919 Note that you have to link against lua as a dll aswell, to make it work.
1921 LUABIND_TYPE_INFO, LUABIND_TYPE_INFO_EQUAL(i1,i2), LUABIND_TYPEID(t), LUABIND_INVALID_TYPE_INFO
1922 If you don't want to use the RTTI supplied by C++ you can supply your own
1923 type-info structure with the ``LUABIND_TYPE_INFO`` define. Your type-info
1924 structure must be copyable and must be able to compare itself against
1925 other type-info structures. You supply the compare function through the
1926 ``LUABIND_TYPE_INFO_EQUAL()`` define. It should compare the two type-info
1927 structures it is given and return true if they represent the same type and
1928 false otherwise. You also have to supply a function to generate your
1929 type-info structure. You do this through the ``LUABIND_TYPEID()`` define.
1930 It should return your type-info structure and it takes a type as its
1931 parameter. That is, a compile time parameter.
1932 ``LUABIND_INVALID_TYPE_INFO`` macro should be defined to an invalid type.
1933 No other type should be able to produce this type info. To use it you
1934 probably have to make a traits class with specializations for all classes
1935 that you have type-info for. Like this::
1941 template<class T> struct typeinfo_trait;
1943 template<> struct typeinfo_trait<A> { enum { type_id = 0 }; };
1944 template<> struct typeinfo_trait<B> { enum { type_id = 1 }; };
1945 template<> struct typeinfo_trait<C> { enum { type_id = 2 }; };
1947 If you have set up your own RTTI system like this (by using integers to
1948 identify types) you can have luabind use it with the following defines::
1950 #define LUABIND_TYPE_INFO const std::type_info*
1951 #define LUABIND_TYPEID(t) &typeid(t)
1952 #define LUABIND_TYPE_INFO_EQUAL(i1, i2) *i1 == *i2
1953 #define LUABIND_INVALID_TYPE_INFO &typeid(detail::null_type)
1955 Currently the type given through ``LUABIND_TYPE_INFO`` must be less-than
1959 This define will disable all asserts and should be defined in a release
1963 Implementation notes
1964 ====================
1966 The classes and objects are implemented as user data in lua. To make sure that
1967 the user data really is the internal structure it is supposed to be, we tag
1968 their metatables. A user data who's metatable contains a boolean member named
1969 ``__luabind_classrep`` is expected to be a class exported by luabind. A user
1970 data who's metatable contains a boolean member named ``__luabind_class`` is
1971 expected to be an instantiation of a luabind class.
1973 This means that if you make your own user data and tags its metatable with the
1974 exact same names, you can very easily fool luabind and crash the application.
1976 In the lua registry, luabind keeps an entry called ``__luabind_classes``. It
1977 should not be removed or overwritten.
1979 In the global table, a variable called ``super`` is used every time a
1980 constructor in a lua-class is called. This is to make it easy for that
1981 constructor to call its base class' constructor. So, if you have a global
1982 variable named super it may very well be overwritten. This is probably not the
1983 best solution, and this restriction may very well be removed in the future.
1985 Luabind uses two upvalues for functions that it registers. The first is a
1986 userdata containing a list of overloads for the function, the other is a light
1987 userdata with the value 0x1337, this last value is used to identify functions
1988 registered by luabind. It should be virtually impossible to have such a pointer
1989 as secondary upvalue by pure chance. This means, if you are trying to replace
1990 an existing function with a luabind function, luabind will see that the
1991 secondary upvalue isn't the magic id number and replace it. If it can identify
1992 the function to be a luabind function, it won't replace it, but rather add
1993 another overload to it.
1995 Inside the luabind namespace, there's another namespace called detail. This
1996 namespace contains non-public classes and are not supposed to be used directly.
2002 - `the attribute '<class-name>.<attribute-name>' is read only`
2004 There is no data member named <attribute-name> in the class <class-name>,
2005 or there's no setter-method registered on that property name. See the
2008 - `the attribute '<class-name>.<attribute-name>' is of type: (<class-name>)
2009 and does not match (<class_name>)`
2011 This error is generated if you try to assign an attribute with a value
2012 of a type that cannot be converted to the attribute's type.
2014 - `<class-name>() threw an exception, <class-name>:<function-name>() threw an
2017 The class' constructor or member function threw an unknown exception.
2018 Known exceptions are const char*, std::exception. See the
2019 `exceptions`_ section.
2021 - `no overload of '<class-name>:<function-name>' matched the arguments
2022 (<parameter-types>)`
2023 - `no match for function call '<function-name>' with the parameters
2024 (<parameter-types>)`
2025 - `no constructor of <class-name> matched the arguments (<parameter-types>)`
2026 - `no operator <operator-name> matched the arguments (<parameter-types>)`
2028 No function/operator with the given name takes the parameters you gave
2029 it. You have either misspelled the function name, or given it incorrect
2030 parameters. This error is followed by a list of possible candidate
2031 functions to help you figure out what parameter has the wrong type. If
2032 the candidate list is empty there's no function at all with that name.
2033 See the signature matching section.
2035 - `call of overloaded '<class-name>:<function-name>(<parameter-types>)' is
2037 - `ambiguous match for function call '<function-name>' with the parameters
2038 (<parameter-types>)`
2039 - `call of overloaded constructor '<class-name>(<parameter-types>)' is
2041 - `call of overloaded operator <operator-name> (<parameter-types>) is
2044 This means that the function/operator you are trying to call has at least
2045 one other overload that matches the arguments just as good as the first
2048 - `cannot derive from C++ class '<class-name>'. It does not have a wrapped
2051 You are trying to derive a lua class from a C++ class that doesn't have a
2052 wrapped type. You have to give your C++ class a wrapped type when you
2053 register it with lua. See the deriving in lua section.
2055 - `derived class must call super on base`
2056 - `cannot set property '<class-name>.<attribute_name>' because it's read only`
2058 The attribute you are trying to set is registered as read only. If you want
2059 it to be writeable you have to change your class registration and use
2060 def_readwrite() instead of def_readonly(). Alternatively (if your
2061 attribute is a property with getter and setter functions), you have to
2062 give a setter function when declaring your attribute. See the properties section.
2064 - `no static '<enum-name>' in class '<class-name>'`
2066 You will get this error message if you are trying to access an enum that
2067 doesn't exist. Read about how to declare enums.
2069 - `expected base class`
2071 You have written a malformed class definition in lua. The format is: class
2072 '<class-name>' [<base-class>]. If you don't want to derive from a base
2073 class, you have to break the line directly after the class declaration.
2075 - `invalid construct, expected class name`
2077 You have written a malformed class definition in lua. The class function
2078 expects a string as argument. That string is the name of the lua class to
2085 What's up with __cdecl and __stdcall?
2086 If you're having problem with functions
2087 that cannot be converted from 'void (__stdcall *)(int,int)' to 'void
2088 (__cdecl*)(int,int)'. You can change the project settings to make the
2089 compiler generate functions with __cdecl calling conventions. This is
2090 a problem in developer studio.
2092 What's wrong with functions taking variable number of arguments?
2093 You cannot register a function with ellipses in its signature. Since
2094 ellipses don't preserve type safety, those should be avoided anyway.
2096 Internal structure overflow in VC
2097 If you, in visual studio, get fatal error C1204: compiler limit :
2098 internal structure overflow. You should try to split that compilation
2099 unit up in smaller ones.
2101 What's wrong with precompiled headers in VC?
2102 Visual Studio doesn't like anonymous namespaces in its precompiled
2103 headers. If you encounter this problem you can disable precompiled
2104 headers for the compilation unit (cpp-file) that uses luabind.
2106 error C1076: compiler limit - internal heap limit reached in VC
2107 In visual studio you will probably hit this error. To fix it you have to
2108 increase the internal heap with a command-line option. We managed to
2109 compile the test suit with /Zm300, but you may need a larger heap then
2112 error C1055: compiler limit : out of keys in VC
2113 It seems that this error occurs when too many assert() are used in a
2114 program, or more specifically, the __LINE__ macro. It seems to be fixed by
2115 changing /ZI (Program database for edit and continue) to /Zi
2118 How come my executable is huge?
2119 If you're compiling in debug mode, you will probably have a lot of
2120 debug-info and symbols (luabind consists of a lot of functions). Also,
2121 if built in debug mode, no optimizations were applied, luabind relies on
2122 that the compiler is able to inline functions. If you built in release
2123 mode, try running strip on your executable to remove export-symbols,
2124 this will trim down the size.
2126 Our tests suggests that cygwin's gcc produces much bigger executables
2127 compared to gcc on other platforms and other compilers.
2129 .. HUH?! // check the magic number that identifies luabind's functions
2131 Can I register class templates with luabind?
2132 Yes you can, but you can only register explicit instantiations of the
2133 class. Because there's no lua counterpart to C++ templates. For example,
2134 you can register an explicit instantiation of std::vector<> like this::
2138 class_<std::vector<int> >("vector")
2139 .def(constructor<int>)
2140 .def("push_back", &std::vector<int>::push_back)
2143 .. Again, irrelevant to docs: Note that the space between the two > is required by C++.
2145 Do I have to register destructors for my classes?
2146 No, the destructor of a class is always called by luabind when an
2147 object is collected. Note that lua has to own the object to collect it.
2148 If you pass it to C++ and gives up ownership (with adopt policy) it will
2149 no longer be owned by lua, and not collected.
2151 If you have a class hierarchy, you should make the destructor virtual if
2152 you want to be sure that the correct destructor is called (this apply to C++
2155 .. And again, the above is irrelevant to docs. This isn't a general C++ FAQ.
2157 Fatal Error C1063 compiler limit : compiler stack overflow in VC
2158 VC6.5 chokes on warnings, if you are getting alot of warnings from your
2159 code try suppressing them with a pragma directive, this should solve the
2162 Crashes when linking against luabind as a dll in windows
2163 When you build luabind, lua and you project, make sure you link against
2164 the runtime dynamically (as a dll).
2166 I cannot register a function with a non-const parameter
2167 This is because there is no way to get a reference to a lua value. Have
2168 a look at out_value and pure_out_value policies.
2174 - You cannot use strings with extra nulls in them as member names that refers
2177 - If one class registers two functions with the same name and the same
2178 signature, there's currently no error. The last registered function will
2179 be the one that's used.
2181 - In vc7, classes can not be called test.
2183 .. remove? - Visual studio have problems selecting the correct overload of std::swap()
2184 for luabind::object.
2186 - If you register a function and later rename it, error messages will use the
2187 original function name.
2193 This library was written by Daniel Wallin and Arvid Norberg. © Copyright 2003.
2194 All rights reserved.
2196 This library was inspired by Dave Abrahams' Boost.Python library which can be
2197 found in the boost_ library.
2199 Evan Wies has contributed with thorough testing and countless bug reports and