Debian Codegraph @

3rdparty/toluapp/doc/tolua++.html

raw
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <HTML>
3 <HEAD>
4 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
5 <META NAME="GENERATOR" CONTENT="Textpad">
6 <META NAME="Author" CONTENT="Waldemar Celes, Ariel Manzur">
7 <TITLE>tolua++ reference manual</TITLE>
8 </HEAD>
9 <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
10
11 <H1>
12 <IMG SRC="toluapp.gif">tolua++ - Reference Manual</I></H1>
13 by Waldemar Celes, Ariel Manzur.
14
15 <H1>
16
17 </h1>
18
19 <HR ALIGN=LEFT SIZE=5 WIDTH="100%">
20
21 <b>tolua++</b> is an extended version of <a href="http://www.tecgraf.puc-rio.br/~celes/tolua/">tolua</a>, a tool to integrate
22 C/C++ code with <A HREF="http://www.tecgraf.puc-rio.br/lua">Lua</A>. <b>tolua++</b>
23 includes new features oriented to c++ such as:
24 <p>
25
26 <ul>
27 <li> Support for <b>std::string</b> as a <a href="#basics">basic type</a> (this can be turned off by a command line option).</li>
28 <li> Support for <a href="#templates">class templates</a></li>
29 </ul>
30 <p>
31
32 As well as other features and bugfixes.
33 <p>
34
35
36 <B>tolua</B> is a tool that greatly simplifies the integration of C/C++
37 code with <A HREF="http://www.tecgraf.puc-rio.br/lua">Lua</A>. Based on
38 a <I>cleaned</I> <I>header file</I> (or extracts from real header files),
39 <B>tolua</B> automatically generates
40 the binding code to access C/C++ features from Lua. Using Lua API and tag
41 method facilities, <B>tolua </B>maps C/C++ constants, external variables,
42 functions, classes, and methods to Lua.
43 <P>This manual is for <B>tolua++</B> version 1.0 and is implemented upon Lua
44 5.0 and based on <b>tolua 5.0</b>. See <A HREF="#changes-v30">Compatibility</A>
45 for details on switching from older versions.
46 <P>The sections below describe how to use <B>tolua</B>. Please <A HREF="mailto:[email protected]">contact us</A>
47 with bug reports, suggestions, and comments.
48 <UL>
49 <LI>
50 Shortcuts:</LI>
51
52 <UL>
53 <LI>
54 <A HREF="#introduction">How <B>tolua</B> works</A></LI>
55
56 <LI>
57 <A HREF="#using">How to use <B>tolua</B></A></LI>
58
59 <LI>
60 <A HREF="#basics">Basic Concepts</A></LI>
61
62 <LI>
63 <A HREF="#constants">Binding constants</A></LI>
64
65 <LI>
66 <A HREF="#variables">Binding external variables</A></LI>
67
68 <LI>
69 <A HREF="#functions">Binding functions</A></LI>
70
71 <LI>
72 <A HREF="#structs">Binding struct fields</A></LI>
73
74 <LI>
75 <A HREF="#classes">Binding classes and methods</A></LI>
76
77 <li>
78 <a href="#propeties">Binding properties</a></li>
79
80 <li>
81 <a href="#templates">Class Templates</a></li>
82
83 <LI>
84 <A HREF="#modules">Module definition</A></LI>
85
86 <LI>
87 <A HREF="#renaming">Renaming constants, variables and functions</A></LI>
88
89 <LI>
90 <A HREF="#additional">Storing additional fields</A></LI>
91
92 <li>
93 <a href="#additional_features">Additional features</a></li>
94
95 <LI>
96 <A HREF="#utilities">Exported utility functions</A></LI>
97
98 <LI>
99 <A HREF="#embedded">Embedded Lua code</A></LI>
100
101 <LI>
102 <a HREF="#customizing">Customizing tolua++</a></li>
103
104 <li>
105 <a href="#compatibility">Compatibility with older versions</a></li>
106
107 <li>
108 <A HREF="#changes-v30">Changes since v3 *</A></li>
109
110 <LI>
111 <A HREF="#changes-v2">Changes since v2.*</A></LI>
112
113 <LI>
114 <A HREF="#changes-v1">Changes since v1.*</A></LI>
115
116 <LI>
117 <A HREF="#credits">Credits</A></LI>
118
119 <LI>
120 <A HREF="#availability">Availability</A></LI>
121 </UL>
122 </UL>
123
124 <HR WIDTH="100%">
125 <H3>
126 <A NAME="introduction"></A>How tolua works</H3>
127 To use <B>tolua</B>, we create a <I>package file</I>, a C/C++ cleaned header
128 file<I>,</I> listing the constants, variables, functions, classes, and
129 methods we want to export to the Lua environment. Then <B>tolua</B> parses
130 this file and creates a C/C++ file that automatically binds the C/C++ code
131 to Lua. If we link the created file with our application, the specified
132 C/C++ code can be accessed from Lua.<br>
133 A package file can also include regular header files, other package files,
134 or lua files.<br>
135 <P>Let's start with some examples. If we specify as input the following
136 C-like header file to <B>tolua</B>:
137 <PRE>#define FALSE 0
138 #define TRUE 1
139
140 enum {&nbsp;
141 &nbsp;POINT = 100,&nbsp;
142 &nbsp;LINE,&nbsp;
143 &nbsp;POLYGON
144 }</PRE>
145
146 <PRE>Object* createObejct (int type);
147 void drawObject (Object* obj, double red, double green, double blue);
148 int isSelected (Object* obj);</PRE>
149 A C file that binds such a code to Lua is automatically generated. Therefore,
150 in Lua code, we can access the C code, writing, for instance:
151 <PRE>...
152 myLine = createObject(LINE)
153 ...
154 if isSelected(myLine) == TRUE then
155 &nbsp; drawObject(myLine, 1.0, 0.0, 0.0);
156 else
157 &nbsp; drawObject(myLine, 1.0, 1.0, 1.0);
158 end
159 ...</PRE>
160 Also, consider a C++-like header file:
161 <PRE>#define FALSE 0
162 #define TRUE 1</PRE>
163
164 <PRE>class Shape
165 {
166 &nbsp; void draw (void);
167 &nbsp; void draw (double red, double green, double blue);
168 &nbsp; int isSelected (void);
169 };</PRE>
170
171 <PRE>class Line : public Shape
172 {
173 &nbsp;Line (double x1, double y1, double x2, double y2);
174 &nbsp;~Line (void);
175 };</PRE>
176 If this file is used as input to <B>tolua</B>, a C++ file is automatically
177 generated proving access to such a code from Lua. Therefore, it would be
178 valid to write Lua statements like:
179 <PRE>...
180 myLine = Line:new (0,0,1,1)
181 ...
182 if myLine:isSelected() == TRUE then
183 &nbsp;myLine:draw(1.0,0.0,0.0)
184 else
185 &nbsp;myLine:draw()
186 end
187 ...
188 myLine:delete()
189 ...</PRE>
190 The package file (usually with extension <TT>.pkg</TT>) passed to <B>tolua</B>
191 is not the real C/C++ header file, but a <I>cleaned </I>version of it.
192 <B>tolua</B>
193 does not implement a complete parse to interpret C/C++ code, but it understands
194 a few declarations that are used to describe the features that are to be
195 exported to Lua. Regular header files can be included into packages files; <b>tolua</b>
196 will extract the code specified by the user to parse from the header (see <a href="#basics">Basic Concepts</a>).
197 <H3>
198 <A NAME="using"></A>How to use toLua</H3>
199 <B>tolua </B>is composed by two pieces of code: an executable and a library.
200 The executable represents the parser that reads a package file and output
201 a C/C++ code that implements the binding to access the C/C++ features from
202 Lua. If the package file is a C++ like code (i.e., includes class definitions),
203 a C++ code is generated. If the cleaned header file is a C like code (i.e.,
204 without classes), a C code is generated. <B>tolua</B> accepts a set of
205 options. Running <TT>"tolua -h"</TT> displays the current accepted options.
206 For instance, to parse a file called <TT>myfile.pkg
207 </TT>generating the
208 binding code in <TT>myfile.c</TT>, we do:
209 <P><TT>tolua -o myfile.c myfile.pkg</TT>
210 <P>The generated code must be compiled and linked with the application
211 to provide the desired access from Lua. Each parsed file represents a package
212 being exported to Lua. By default, the package name is the input file root
213 name (<TT>myfile </TT>in the example). The user can specify a different
214 name for the package:
215 <P><TT>tolua -n pkgname -o myfile.c myfile.pkg</TT>
216 <P>The package should also be explicitly initialized. To initialize the
217 package from our C/C++ code, we must declare and call the initialization
218 function. The initialization function is defined as
219 <P><TT>int tolua_<I>pkgname</I>_open (lua_State*);</TT>
220 <P>where <I><TT>pkgname </TT></I>represents the name of the package being
221 bound. If we are using C++, we can opt for automatic initialization:
222 <P><TT>tolua -a -n pkgname -o myfile.c myfile.pkg</TT>
223 <P>In that case, the initialization function is automatically called. However,
224 if we are planning to use multiple Lua states, automatic initialization
225 does not work, because the order static variables are initialized in C++
226 is not defined.
227 <!--
228 <P>The current <B>tolua</B> version also exports a closing function, which
229 can be called to unbind the package.
230 <P><TT>void tolua_<I>pkgname</I>_close (void);</TT>
231 -->
232 <P>Optionally, the prototype of the <TT>open</TT> function
233 can be outputted to a header file, which name is given by the <TT>-H</TT>
234 option.
235 <P>The binding code generated by <B>tolua </B>uses a set of functions defined
236 in the <B>tolua </B>library. Thus, this library also has to be linked with
237 the application. The file <TT>tolua.h</TT> is also necessary to compile
238 the generated code.
239 <P>An application can use tolua object oriented framework (see <A HREF="#utilities">exported
240 utility functions</A>) without binding any package. In that case, the application
241 must call <B>tolua </B>initialization function (this function is called
242 by any package file initialization function):
243 <P><TT>int tolua_open (void);</TT>
244 <!--
245 <P>If multiple Lua states are to be used, after setting a Lua state, we
246 need to call a function to restore <B>tolua </B>internal state:
247 <P><TT>void tolua_restorestate (void);</TT>
248 -->
249 <H3>
250 <A NAME="basics"></A>Basic Concepts</H3>
251 The first step in using <B>tolua</B> is to create the package file. Starting
252 with the real header files, we clean them by declaring the features we
253 want to access from Lua in a format that <B>tolua</B> can understand. The
254 format <B>tolua</B> understands is simple C/C++ declarations as described
255 below.
256
257 <H4>Including files</h4>
258
259 A package file may include other package file. The general format
260 to do that is:
261 <p>
262 <TT>$pfile "<I>include_file</I>"</TT>
263 <p>
264 A package file may also include regular C/C++ header files, using the <TT>hfile</TT>
265 or <TT>cfile</tt> directive:
266 <p>
267
268 <tt>$cfile "example.h"</tt>
269 <p>
270 In which case, <b>tolua</b> will extract the code enclosed between <tt>tolua_begin</tt>
271 and <tt>tolua_end</tt>, or or <tt>tolua_export</tt> for a single line. Consider this C++ header as example:
272
273 <PRE>
274
275 #ifndef EXAMPLE_H
276 #define EXAMPLE_H
277
278 class Example { // tolua_export
279
280 private:
281
282 string name;
283 int number;
284
285 public:
286
287 void set_number(int number);
288
289 //tolua_begin
290
291 string get_name();
292 int get_number();
293 };
294 // tolua_end
295
296 #endif
297 </pre>
298
299 <p>
300 In this case, the code that's not supported by <b>tolua</b> (the
301 private part of the class), along with the function <tt>set_number</tt>
302 is left outside of the package that includes this header.
303 <p>
304
305 Finally, lua files can be included on a package file, using <tt>$lfile</tt>:
306 <p>
307
308 <tt>$lfile "example.lua"</tt>
309 <p>
310
311 <b>New on tolua++</b>: an extra way to include source files is available since
312 version 1.0.4 of <b>tolua++</b>, using <tt>ifile</tt>:
313 <p>
314
315 <tt>$ifile "filename"</tt>
316 <p>
317
318 <tt>ifile</tt> also takes extra optional parameters after the filename, for example:
319 <p>
320
321 <tt>
322 $ifile "widget.h", GUI<br>
323 $ifile "vector.h", math, 3d
324 </tt>
325 <p>
326
327 <tt>ifile</tt>'s default behaviour is to include the whole file, untouched. However,
328 the contents of the file and the extra parameters are put through the <tt>include_file_hook</tt>
329 function before being included into the package (see <a href="#customizing">Customizing tolua++</a>
330 for more details).
331 <p>
332
333 <H4>
334 Basic types</H4>
335 <B>tolua </B>automatically maps C/C++ basic types to Lua basic types. Thus,
336 <TT>char</TT>,
337 <TT>int</TT>,
338 <TT>float</TT>, and <TT>double </TT>are mapped to the Lua type <TT>number</TT>;<TT>
339 char*</TT> is mapped to <TT>string</TT>; and<TT> void*</TT> is mapped to
340 <TT>userdata</TT>. Types may be preceded by modifiers (<TT>unsigned</TT>,
341 <TT>static</TT>, <TT>short</TT>, <TT>const</TT>, etc.); however, be aware
342 that <B>tolua </B>ignores the modifier <TT>const</TT> if applied to basic
343 types. Thus, if we pass a constant basic type to Lua and then pass it back
344 to C/C++ code where a non constant is expected, the constant to non constant
345 conversion will be silently done.
346 <P>Functions in C/C++ can also manipulate Lua objects explicitly. Thus
347 <TT>lua_Object</TT>
348 is also considered a basic type. In this case, any Lua value matches it.
349 <p>
350
351 <b>New on tolua++</b>: The C++ type <tt>string</tt> is also considered a basic type, and is passed as
352 a value to lua (using the <tt>c_str()</tt> method). This feature can be turned off
353 with the command line option <tt>-S</tt>.
354
355 <H4>
356 User defined types</H4>
357 All other types that appear in the package file being processed are considered
358 user defined types. These are mapped to tagged userdata type in Lua. Lua
359 can only store pointers to user defined types; although, <B>tolua
360 </B>automatically
361 makes the necessary arrangement to deal with references and values. For
362 instance, if a function or method returns a value of user defined type,
363 <B>tolua
364 </B>allocates
365 a clone object when returning it to Lua and sets the garbage collection
366 tag method to automatically free the allocated object when no longer in
367 use by Lua.
368 <P>For user defined types, <tt>const</tt>ness is preserved. Thus passing a non constant
369 user defined type to a function that expects constant type generates an
370 type mismatching error.
371 <H4>
372 <TT>NULL </TT>and <TT>nil</TT></H4>
373 C/C++ <TT>NULL</TT> or <TT>0 </TT>pointers are mapped to Lua
374 <TT>nil </TT>type;
375 conversely, <TT>nil </TT>may be specified wherever a C/C++ pointer is expected.
376 This is valid for any type: <TT>char*</TT>,
377 <TT>void*</TT>, and pointers
378 to user defined types.
379 <H4>
380 Typedefs</H4>
381 <B>tolua </B>also accepts simple typedef<I>'s </I>inside the package files.
382 Any occurrence of a type after its definition is mapped by <B>tolua
383 </B>to
384 the base type. They are useful because several packages redefine the basic
385 C/C++ types to their own types. For instance, one can define the type <TT>real
386 </TT>to
387 represent a <TT>double</TT>. In that case, <TT>real
388 </TT>can be used to
389 specify the variable types inside the package file interpreted by <B>tolua</B>,
390 but only if we include the following definition before any use of the type
391 <TT>real</TT>.
392 <P><TT>typedef double real;</TT>
393 <P>Otherwise, <TT>real </TT>would be interpreted as a user defined type
394 and would not be mapped to Lua numbers.
395 <H4>
396 Including real header files</H4>
397 In the package file, we must specify which are the real header files that
398 should be included so that the generated code can access the constants,
399 variables, functions, and classes we are binding. Any line in the package
400 file beginning with a <B>$ </B>(except $[hclp]file, $[ , and $] lines) is
401 inserted into the generated binding C/C++ code without any change, but the
402 elimination of the <B>$</B> itself. We use this feature to include the
403 real header files. So, our package files will usually start with a set
404 of <B>$</B> beginning lines specifying the files that must be included,
405 that is, the files the package file is based on.
406 <PRE>/* specify the files to be included */</PRE>
407
408 <PRE>$#include &quot;header1.h&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // include first header
409 $#include &quot;header2.h&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // include second header</PRE>
410 As illustrated, <B>tolua </B>also accepts comments, using C or C++ convention,
411 inside the package file. Nested C-like comments can also be used.
412 <p>
413 Also note that files included with <tt>$cfile</tt> or <tt>$hfile</tt> don't
414 need to be included using this method, this is done automatically by <b>tolua</b>.
415
416 <P>In the following sections, we describe how to specify the C/C++ code
417 we want to bind to Lua. The formats are simplified valid C/C++ statements.
418
419 <H3>
420 <A NAME="constants"></A>Binding constants</H3>
421 To bind constants, <B>tolua </B>accepts both define's and enum's. For define's
422 the general format is:
423 <PRE><B>#define</B> <I>NAME </I>[ <I>VALUE </I>]</PRE>
424 The value, as showed above, is optional. If such a code is inserted inside
425 the file being processed, <B>tolua </B>generates a code that allows the
426 use of <I><TT>NAME </TT></I>as a Lua global variable that has the corresponding
427 C/C++ constant value. Only numeric constants are accepted.
428 <p>
429 <b>New on tolua++</b>: All other preprocessor directives are ignored.
430 <P>For enum's, the general format is:
431 <PRE><B>enum {
432 </B>&nbsp; <I><TT>NAME1 </TT></I>[ <B>=</B> <I>VALUE1 </I>] <B>,
433 </B>&nbsp; <I>NAM</I>E2 [ <B>=</B> <I>VALUE2 </I>] <B>,
434 </B>&nbsp; ...
435 &nbsp; <I>NAMEn </I>[ <B>=</B> <I>VALUEn </I>]
436 <B>};</B></PRE>
437 Similarly, <B>tolua</B> creates a set of global variables, named <I><TT>NAMEi</TT></I>,
438 with their corresponding values.
439 <H3>
440 <A NAME="variables"></A>Binding external variables</H3>
441 Global extern variables can also be exported. In the cleaned header file
442 they are specified as:
443 <PRE><TT>[</TT><B>extern</B><TT>]</TT><B> </B><I>type var</I><B>;</B></PRE>
444 <B>tolua</B> binds such declarations to Lua global variables. Thus, in
445 Lua, we can access the C/C++ variable naturally. If the variable is non
446 constant, we can also assign the variable a new value from Lua. Global
447 variables that represent arrays of value can also be bound to Lua. Arrays
448 can be of any type. The corresponding Lua objects for arrays are Lua tables
449 indexed with numeric values; however, be aware that index 1 in Lua is mapped
450 to index 0 in an C/C++ array. Arrays must be pre dimensioned. For instance:
451 <P><TT>double v[10];</TT>
452
453 <p>
454 <b>New on tolua++</b>: External variables can use the <tt>tolua_readonly</tt> modifier (see <a href="#additional_features">Additional Features</a>)
455
456 <H3>
457 <A NAME="functions"></A>Binding functions</H3>
458 Functions are also specified as conventional C/C++ declarations:
459 <PRE><I>type funcname </I><B>(</B><I>type1 par1</I>[<B>,</B> <I>type2 <TT>par2</TT></I>[<B>,.</B>..<I>typeN parN</I>]]<B>);</B></PRE>
460 The returned type can be <TT>void</TT>, meaning no value is returned. A
461 function can also have no parameter. In that case, <TT>void </TT>may be
462 specified in the place of the list of parameters. The parameter types must
463 follow the rules already posted. <B>tolua </B>creates a Lua function binding
464 the C/C++ function. When calling a function from Lua, the parameter types
465 must match the corresponding C/C++ types, otherwise, <B>tolua
466 </B>generates
467 an error and reports which parameter is wrongly specified. If a parameter
468 name is omitted, <B>tolua </B>names it automatically, but its type should
469 be a basic type or user type previously used.
470 <H4>
471 Arrays</H4>
472 <B>tolua </B>also deals with function or method parameters that represent
473 arrays of values. The nice thing about arrays is that the corresponding
474 Lua tables have their values updated if the C/C++ function changes the
475 array contents.
476 <P>The arrays must be pre dimensioned. For instance:
477 <P><TT>void func (double a[3]);</TT>
478 <P>is a valid function declaration for <B>tolua</B> and calling this function
479 from Lua would be done by, for instance:
480 <P><TT>p = {1.0,1.5,8.6}</TT>
481 <BR><TT>func (p)</TT>
482 <P>The array dimension need not be a constant expression; the dimension
483 can also be specified by any expression that can be evaluated in run time.
484 For instance:
485 <P><TT>void func (int n, int m, double image[n*m]);</TT>
486 <P>is also valid since the expression <TT>n*m</TT> is valid in the binding
487 function scope. However, be aware that <B>tolua </B>uses dynamic allocation
488 for binding this function, what can degrade the performance.
489 <P>Despite the dimension specification, it is important to know that all
490 arrays passed to the actual C/C++ function are in the local scope of the
491 binding function. So, if the C/C++ function being called needs to hold
492 the array pointer for later use, the binding code will <I>not </I>work
493 properly.
494 <H4>
495 Overloaded functions</H4>
496 Overloaded functions are accepted. Remember that the distinction between
497 two functions with the same name is made based on the parameter types that
498 are mapped to Lua. So, although
499 <P><TT>void func (int a);</TT>
500 <BR><TT>void func (double a);</TT>
501 <P>represent two different functions in C++, they are the same function
502 for <B>tolua</B>, because both <TT>int </TT>and <TT>double </TT>are mapped
503 to the same Lua type: <TT>number</TT>.
504 <P>Another tricky situation occurs when expecting pointers. Suppose:
505 <PRE>void func (char* s);
506 void func (void* p);
507 void func (Object1* ptr);
508 void func (Object2* prt);</PRE>
509 Although these four functions represent different functions in C++, a Lua
510 statement like:
511 <PRE>func(nil)</PRE>
512 matches all of them.
513 <P>It is important to know that <B>tolua </B>decides which function will
514 be called in run-time, trying to match each provided function. <B>tolua
515 </B>first
516 tries to call the last specified function; if it fails, <B>tolua
517 </B>then
518 tries the previous one. This process is repeated until one function matches
519 the calling code or the first function is reached. For that reason, the
520 mismatching error message, when it occurs, is based on the first function
521 specification. When performance is important, we can specify the most used
522 function as the last one, because it will be tried first.
523 <P><B>tolua</B> allows the use of overloaded functions in C, see <A HREF="#renaming">Renaming
524 </A>for
525 details.
526 <H4>
527 Default parameter values</H4>
528 The last function parameters can have associated default values. In that
529 case, if the function is called with fewer parameters, the default values
530 are assumed. The format to specify the default values is the same as the
531 one used in C++ code:
532 <P><TT><I>type funcname </I><B>(</B><I>...</I><B>,</B><I> typeN-1 parN-1
533 [= valueN-1]<B>,</B> typeN parN [= valueN]</I><B>);</B></TT>
534 <P><B>toLua </B>implements this feature without using any C++ mechanism;
535 so, it can be used also to bind C functions.
536 <P>We can also specify default values for the elements of an array (there
537 is no way to specify a default value for the array itself, though). For
538 instance:
539 <P><TT>void func (int a[5]=0);</TT>
540 <P>sets the default element values to zero, thus the function can be called
541 from Lua with an uninitialized table.
542 <P>For Lua object types (<TT>lua_Object</TT>), <B>tolua </B>defines a constant
543 that can be used to specify <TT>nil </TT>as default value:
544 <P><TT>void func (lua_Object lo = TOLUA_NIL);</TT>
545 <p>
546
547 <b>New on tolua++</b>: C++ class constructors are valid
548 as default parameters. For example:
549 <p>
550 <tt>void set_color(const Color& color = Color(0,0,0));</tt>
551 <p>
552
553 <H4>
554 Multiple returned values</H4>
555 In Lua, a function may return any number of values. <B>tolua </B>uses this
556 feature to simulate values passed by reference. If a function parameter
557 is specified as a pointer to or reference of a basic type or a pointer
558 to or reference of a pointer to an user defined type, <B>tolua </B>accepts
559 the corresponding type as input and returns, besides the conventional function
560 returned value, if any, the updated parameter value.
561 <P>For instance, consider a C function that swaps two values:
562 <P><TT>void swap (double* x, double* y);</TT>
563 <P>or
564 <P><TT>void swap (double&amp; x, double&amp; y);</TT>
565 <P>If such a function is declared in the package file, <B>tolua </B>binds
566 it as a function receiving two numbers as input and returning two numbers.
567 So, a valid Lua code would be:
568 <P><TT>x,y = swap(x,y)</TT>
569 <P>If the input values are not used, the use of default parameter value
570 allows calling the function from Lua without specifying them:
571 <P><TT>void getBox (double* xmin=0, double* xmax=0, double* ymin=0, double*
572 ymax=0);</TT>
573 <P>In Lua:
574 <P><TT>xmin, xmax, ymin, ymax = getBox()</TT>
575 <P>With user defined types, we would have for instance:
576 <P><TT>void update (Point** p);</TT>
577 <P>or
578 <P><TT>void update (Point*&amp; p);</TT>
579 <H3>
580 <A NAME="structs"></A>Binding struct fields</H3>
581 User defined types are nicely bound by <B>tolua</B>. For each variable
582 or function type that does not correspond to a basic type, <B>tolua </B>automatically
583 creates a tagged userdata to represent the C/C++ type. If the type corresponds
584 to a struct, the struct fields can be directly accessed from Lua, indexing
585 a variable that holds an object of such a type. In C code, these types
586 are commonly defined using typedef's:
587 <PRE><B>typedef struct [name]</B><I> </I><B>{
588 </B>&nbsp;&nbsp; <I>type1 fieldname1</I><B>;
589 </B>&nbsp;&nbsp; <I>type2 fieldname2</I><B>;
590 </B>&nbsp;&nbsp; ...
591 &nbsp;&nbsp; <I>typeN fieldnameN</I><B>;
592 } </B><I>typename</I><B>;</B></PRE>
593 If such a code is inserted in the package file being processed, <B>tolua
594 </B>allows
595 any variable that holds an object of type <I><TT>typename </TT></I>to access
596 any listed field indexing the variable by the field name. For instance,
597 if <TT>var </TT>holds a such object, <I><TT>var.fieldnamei</TT></I> accesses
598 the field named <I><TT>fieldnamei</TT></I>.
599 <P>Fields that represent arrays of values can also be mapped:
600 <P><TT>typedef struct {</TT>
601 <BR><TT>&nbsp; int x[10];</TT>
602 <BR><TT>&nbsp; int y[10];</TT>
603 <BR><TT>} Example;</TT>
604 <BR>&nbsp;
605 <H3>
606 <A NAME="classes"></A>Binding classes and methods</H3>
607 C++ class definitions are also supported by <B>tolua</B>. Actually, the
608 <B>tolua
609 </B>deals
610 with single inheritance and polymorphism in a natural way. The subsections
611 below describe what can be exported by a class definition.
612 <H4>
613 Specifying inheritance</H4>
614 If <TT>var </TT>is a Lua variable that holds an object of a derived class,
615 <TT>var
616 </TT>can
617 be used wherever its base class type is expected and <TT>var
618 </TT>can access
619 any method of its base class. For this mechanism to take effect, we must
620 indicate that the derived class actually inherits the base class. This
621 is done in the conventional way:
622 <PRE><B>class </B><I>classname </I><B>: public</B> <I>basename
623 </I><B>{</B></PRE>
624
625 <PRE>&nbsp;/* class definition */</PRE>
626
627 <PRE><B>};</B></PRE>
628 <p>
629
630 In this case, the definition of <tt>basename</tt> needs to appear before <tt>classname</tt>
631 if the inheritance properties are to be taken advantage of from lua.
632
633 <h4>Multiple inheritance</h4>
634
635 <b>tolua++</b> (starting from version 1.0.4) supports multiple inheritance by allowing you
636 to access the extra parents 'manually'.
637 <p>
638 For example, consider the following class:
639 <p>
640
641 <PRE>
642 class Slider : public Widget, public Range {
643 ...
644 };
645 </PRE>
646 <p>
647
648 An object of type 'Slider' will fully retain its inheritance with Widget,
649 and will contain a 'member' of type Range, which will return the object
650 cast to the correct base type.
651 <p>
652
653 For example:
654 <p>
655
656 <PRE>
657 slider = Slider:new()
658 slider:show() -- a Widget method
659
660 slider:set_range(0, 100) -- this won't work, because
661 -- set_range is a method from Range
662
663 slider.__Range__:set_range(0, 100) -- this is the correct way
664 </PRE>
665
666 <p>
667
668 This is an experimental feature.
669 <p>
670
671 <H4>
672 Specifying exported members and methods</H4>
673 As for struct fields, class fields, static or not, can be exported. Class
674 methods and class static methods can also be exported. Of course, they
675 must be declared as public in the actual C++ code (the
676 <TT>public:</TT>keyword may appear in the package files, it will be ignored by <b>tolua</b>).
677 <P>For each bound class, <B>tolua </B>creates a Lua table and stores it
678 at a variable which name is the name of the C++ class. This tables may contain other
679 tables that represent other tables, the way C++ classes may contain other classes and structs.
680 Static exported
681 fields are accessed by indexing this table with the field names (similar
682 to struct fields). Static methods are also called using this table, with a colon.
683 Non static exported fields are accessed by indexing
684 the variable that holds the object. Class methods follow the format of
685 the function declaration showed above. They can be accessed from Lua code
686 using the conventional way Lua uses to call methods, applied of course
687 to a variable that holds the appropriate object or to the class table,
688 for static methods.
689 <P>There are a few special methods that are also supported by <B>tolua</B>.
690 Constructors are called as static methods, named <TT>new</TT>, <tt>new_local</tt> (on <b>tolua++</b>),
691 or calling the class name directly (also on <b>tolua++</b>, see below for the difference betwheen these methods). Destructors
692 are called as a conventional method called <TT>delete</TT>.
693 <P>Note that <B>tolua </B>does support overload. This applies even for
694 constructors. Also note that the <TT>virtual </TT>keyword has no effect
695 in the package file.
696 <P>The following code exemplifies class definitions that can be interpreted
697 by <B>tolua</B>.
698 <PRE>class Point {
699 &nbsp;&nbsp; static int n;&nbsp;&nbsp;&nbsp; // represents the total number of created Points
700 &nbsp;&nbsp; static int get_n();&nbsp;&nbsp;&nbsp; // static method
701
702 &nbsp;&nbsp; double x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // represents the x coordinate
703 &nbsp;&nbsp; double y;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // represents the y coordinate</PRE>
704
705 <PRE>&nbsp;&nbsp; static char* className (void);&nbsp;&nbsp; // returns the name of the class</PRE>
706
707 <PRE>&nbsp;&nbsp; Point (void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // constructor 1
708 &nbsp;&nbsp; Point (double px, double py);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // constructor 2
709 &nbsp;&nbsp; ~Point (void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // destructor</PRE>
710
711 <PRE>&nbsp;&nbsp; Point add (Point&amp; other);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add points, returning another one
712 };</PRE>
713
714 <PRE>class ColorPoint : public Color {
715 &nbsp;&nbsp; int red;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // red color component [0 - 255]
716 &nbsp;&nbsp; int green;&nbsp;&nbsp;&nbsp; // green color component [0 - 255]
717 &nbsp;&nbsp; int blue;&nbsp;&nbsp;&nbsp;&nbsp; // blue color component [0 - 255]</PRE>
718
719 <PRE>&nbsp;&nbsp; ColorPoint (double px, double py, int r, int g, int b);
720 };</PRE>
721 If this segment of code is processed by <B>tolua</B>, we would be able
722 to write the following Lua statements:
723 <PRE>p1 = Point:new(0.0,1.0)
724 p2 = ColorPoint:new(1.5,2.2,0,0,255)
725 print(Point.n)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- would print 2
726 print(Point:get_n())&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- would also print 2
727 p3 = p1:add(p2)
728 local p4 = ColorPoint()
729 print(p3.x,p3.y)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- would print 1.5 and 3.2
730 print(p2.red,p2.green,p2.blue)&nbsp;&nbsp;&nbsp;&nbsp; -- would print 0, 0, and 255
731 p1:delete()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- call destructor
732 p2:delete()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- call destructor</PRE>
733
734 Note that we can only explicitly delete objects that we explicitly create.
735 In the example above, the point <TT>p3</TT> will be garbage-collected by
736 <B>tolua
737 </B>automatically;
738 we cannot delete it.
739 <p>
740 <b>New on tolua++</b>: Also note that <tt>p4</tt> is created by calling the class name directly (<tt>ColorPoint()</tt>); this has the same effect as calling <tt>new_local</tt>, which
741 leaves the object to be deleted by the garbaje collector, and it should not be
742 deleted using <tt>delete</tt>. For each constructor on the pkg, one <tt>new</tt>,
743 <tt>new_local</tt> and <tt>.call</tt> callback for the class is created.
744 <P>Of course, we need to specify only the methods and members we want to
745 access from Lua. Sometimes, it will be necessary to declare a class with
746 no member or method just for the sake of not breaking a chain of inheritances.
747
748 <h4>Using Regular functions as class methods</h4>
749 <p>
750
751 <b>tolua++</b> (starting from version 1.0.5) uses the keyword <b>tolua_outside</b> to specify regular functions
752 as methods and static methods of a class or struct. For example:
753
754 <PRE><tt>
755 /////////////// position.h:
756
757 typedef struct {
758
759 int x;
760 int y;
761 } Position;
762
763 Position* position_create();
764 void position_set(Position* pos, int x, int y);
765
766 /////////////// position.pkg:
767
768 struct Position {
769
770 int x;
771 int y;
772
773 static tolua_outside Position* position_create @ create();
774 tolua_outside void position_set @ set(int x, int y);
775 };
776
777 --------------- position.lua
778
779 local pos = Position:create()
780
781 pos:set(10, 10)
782
783 </tt></pre>
784
785 Note that the <b>position_set</b> method takes a pointer to <b>Position</b> as its first parameter,
786 this is ommited on the <b>tolua_outside</b> declaration. Also note that we cannot name our methods
787 <b>new</b> or <b>new_local</b>, or as overloaded operators (see next section), this will result in
788 undefined behaviour.
789
790
791 <H4>
792 Overloaded operators</H4>
793 <B>tolua </B>automatically binds the following binary operators:
794 <UL>
795 <PRE>operator+&nbsp;&nbsp; operator-&nbsp;&nbsp; operator*&nbsp;&nbsp; operator/&nbsp;
796 operator&lt;&nbsp;&nbsp; operator&gt;=&nbsp;&nbsp;operator==&nbsp;&nbsp;operator[]</PRE>
797 </UL>
798 For the relational operators, <B>toLua </B>also automatically converts
799 a returned <TT>0</TT> value into <TT>nil</TT>, so <I>false </I>in C becomes
800 <I>false
801 </I>in
802 Lua.
803 <P>As an example, suppose that in the code above, instead of having:
804 <PRE>&nbsp;&nbsp; Point add (Point&amp; other);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add points, returning another one</PRE>
805 we had:
806 <PRE>&nbsp;&nbsp; Point operator+ (Point&amp; other);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add points, returning another one</PRE>
807 In that case, in Lua, we could simply write:
808 <PRE>p3 = p1 + p2</PRE>
809 The indexing operator (<TT>operator[]</TT>) when receiving a numeric parameter
810 can also be exported to Lua. In this case, <B>tolua </B>accepts reference
811 as returned value, even for basic types. Then if a reference is returned,
812 from Lua, the programmer can either get or set the value. If the returned
813 value is not a reference, the programmer can only get the value. An example
814 may clarify: suppose we have a vector class and bind the following operator:
815 <PRE>&nbsp;&nbsp; double&amp; operator[] (int index);</PRE>
816 In this case, in Lua, we would be able to write: <TT>value = myVector[i]</TT>
817 and also <TT>myVector[i] = value</TT>, which updates the C++ object. However,
818 if the bound operator was:
819 <PRE>&nbsp;&nbsp; double operator[] (int index);</PRE>
820 we would only be able to write: <TT>value = myVector[i]</TT>.
821 <P>Free functions (i.e., not class members) that overload operators are
822 not supported.
823
824 <p>
825
826 <H4>
827 Cast operators</H4>
828
829 <b>New on tolua++</b> (versions 1.0.90 and up): casting operators are also supported.
830 For example:
831
832 <pre>
833 /////////////// node.h
834
835 // a class that holds a value that can be of type int, double, string or Object*
836 class Node { // tolua_export
837
838 private:
839 union {
840 int int_value;
841 double double_value;
842 string string_value;
843 Object* object_value;
844 };
845
846 // tolua_begin
847 public:
848
849 enum Type {
850 T_INT,
851 T_DOUBLE,
852 T_STRING,
853 T_OBJECT,
854 T_MAX,
855 };
856
857 Type get_type();
858
859 operator int();
860 operator double();
861 operator string();
862 operator Object*();
863 };
864 // tolua_end
865 </pre>
866
867 <b>tolua++</b> will produce code that calls the operators by casting the object Node (using C++ <tt>static_cast</tt>),
868 and register them inside the class as ".typename". For example:
869
870 <pre>
871 -- node.lua
872
873 local node = list.get_node("some_node") -- returns a Node object
874
875 if node.get_type() == Node.T_STRING then
876
877 print("node is "..node[".string"]())
878
879 elseif node.get_type() == Node.T_OBJECT then
880
881 local object = node[".Object*"]()
882 object:method()
883 end
884
885 </pre>
886
887 <h3><a name="properties"></a>Binding Properties</h3>
888
889 <b>tolua++</b> (starting from version 1.0.6) supports declaration of class propeties,
890 using the <tt>tolua_property</tt> keyword. A
891 property will look like a 'field' of the class, but it's value will be retrieved
892 using class methods. For example:
893
894 <pre>
895 /////////////// label.h
896
897 class Label {
898
899 public:
900
901 string get_name();
902 void set_name(string p_name);
903
904 Widget* get_parent();
905 };
906
907 /////////////// label.pkg
908 class Label {
909
910 tolua_property string name;
911
912 tolua_readonly tolua_property Widget* parent;
913 };
914
915 --------------- label.lua
916
917 local label = Label()
918
919 label.name = "hello"
920 print(label.name)
921
922 label.parent:show()
923
924 </pre>
925
926 <h4>Property types</h4>
927 <p>
928
929 A property can have differt types, which determine how it's value will be set and retrieved.
930 <b>tolua++</b> comes with 3 different built-in types:
931 <p>
932
933 <li> <tt>default</tt> will use 'get_name' and 'set_name' methods to access a property called 'name'
934 <li> <tt>qt</tt> will use 'name' and 'setName'
935 <li> <tt>overload</tt> will use 'name' and 'name' (as in 'string name(void);' to get and 'void name(string);' to set)
936 <p>
937
938 The property type can be appended at the end of the 'tolua_property' keyword on the declaration:
939
940 <ul><tt>tolua_property__qt string name;</tt></ul>
941
942 When no type is specified, <tt>default</tt> will be used, but this can be changed (see below).
943 <p>
944
945 <h4>Changing the default property type</h4>
946 <p>
947
948 The default property type can be changed using the 'TOLUA_PROPERTY_TYPE' macro. This will change the
949 default type from the point of its invocation, until the end of the block that contains it. For example:
950 <p>
951
952 <pre>
953
954 TOLUA_PROPERTY_TYPE(default); // default type for the 'global' scope
955
956 namespace GUI {
957
958 class Point {
959
960 tolua_property int x; // will use get_x/set_x
961 tolua_property int y; // will use get_y/set_y
962 };
963
964 TOLUA_PROPERTY_TYPE(qt); // changes default type to 'qt' for the rest of the 'GUI' namespace
965
966 class Label {
967
968 tolua_property string name; // will use name/setName
969 };
970 };
971
972 class Sprite {
973
974 tolua_property GUI::Point position; // will use get_position/set_position
975
976 tolua_property__overload string name; // will use name/name
977 };
978
979 </pre>
980
981 <h4>Adding custom property types</h4>
982 <p>
983
984 Custom property types can be added by redefining the function "get_property_methods_hook"
985 (see <a href="#customizing">Customizing tolua++</a> for more details). The functions takes
986 the property type and the name, and returns the setter and getter function names. For example:
987 <p>
988
989 <pre>
990
991 /////////////// custom.lua
992
993 function get_property_methods_hook(ptype, name)
994
995 if ptype == "hungarian_string" then
996
997 return "sGet"..name, "Set"..name
998 end
999
1000 if ptype == "hungarian_int" then
1001
1002 return "iGet"..name, "Set"..name
1003 end
1004 -- etc
1005 end
1006
1007 /////////////// label.pkg
1008 class Label {
1009
1010 tolua_property__hungarian_string string Name; // uses 'sGetName' and 'SetName'
1011
1012 tolua_property__hungarian_int string Type; // uses 'iGetType' and 'SetType'
1013 };
1014
1015 </pre>
1016
1017 <h3><a name="templates"></a>Class Templates</h3>
1018
1019 One of the additional features of <b>tolua++</b> is the support for class templates,
1020 by using the <tt>TOLUA_TEMPLATE_BIND</tt> directive. For example:
1021
1022 <pre>
1023 class vector {
1024
1025 TOLUA_TEMPLATE_BIND(T, int, string, Vector3D, double)
1026
1027 void clear();
1028 int size() const;
1029
1030 const T& operator[](int index) const;
1031 T& operator[](int index);
1032 void push_back(T val);
1033
1034 vector();
1035 ~vector();
1036 };
1037 </pre>
1038
1039 The <tt>TOLUA_TEMPLATE_BIND</tt> directive has to be the first thing on the class declaration, otherwise it will be ignored.
1040 This code will create 4 versions of the class <tt>vector</tt>, one for each type
1041 specified on the <tt>TOLUA_TEMPLATE_BIND</tt> parameters, each replacing the macro <tt>T</tt>
1042 (specified as the first argument of <tt>TOLUA_TEMPLATE_BIND</tt>).
1043 Thus, the functions <tt>operator[]</tt>, <tt>&operator[]</tt> and <tt>push_back</tt>
1044 will have different signatures on each version of the object. The objects will be
1045 recognized as <tt>vector&lt;type&gt;</tt> on further declarations, and the name of
1046 the table on Lua will be <tt>vector_type_</tt>. Thus, the following Lua code could be used:
1047
1048 <pre>
1049 string_vector = vector_string_:new_local()
1050 string_vector:push_back("hello")
1051 string_vector:push_back("world")
1052 print(string_vector[0].." "..string_vector[1])
1053 </pre>
1054
1055 Similarily, a template with more than 1 macro could be bound, and it could also
1056 inherit from another template:
1057
1058 <pre>
1059 class hash_map : public map&lt;K,V&gt; {
1060
1061 TOLUA_TEMPLATE_BIND(K V, int string, string vector&lt;double&gt;)
1062
1063 V get_element(K key);
1064 void set_element(K key, V value);
1065
1066 hash_map();
1067 ~hash_map();
1068 };
1069 </tt></pre>
1070
1071 In this example, one of the objects has another template as one of its types, so
1072 it will be recognized as <tt>hash_map&lt;string,vector&lt;double&gt; &gt;</tt> while
1073 its constructor will be on the Lua table hash_map_string_vector_double___ (see
1074 <a href="#type_renaming">Type Renaming</a> for a better way to access these objects).
1075 <p>
1076
1077 Note that due to the complexity in the definition of some templates, you should be
1078 careful on how you declare them. For example, if you create an object with type
1079 <tt>hash_map&lt;string,vector&lt;double&gt; &gt;</tt> and then declare a variable
1080 with type <tt>hash_map&lt;string, vector&lt;double&gt; &gt;</tt> (note the space
1081 between string and vector), the type of the variable will not be recognized. The
1082 safest way is to declare a typedef, and use that to use each type (this is also a
1083 common practice on C++ programming). For example, using the previous declaration of
1084 <tt>vector</tt>:
1085 <p>
1086
1087 <pre>
1088 typedef vector<int> VectorInt;
1089
1090 VectorInt variable;
1091 </pre>
1092
1093 <tt>TOLUA_TEMPLATE_BIND</tt> can be used with more than one parenthesis to open and close,
1094 in order to be valid as a macro inside a regular .h file. The <tt>TOLUA_TEMPLATE_BIND</tt>
1095 macro is declared on <tt>tolua.h</tt> as:
1096 <p>
1097 <tt>#define TOLUA_TEMPLATE_BIND(x)</tt>
1098 <p>
1099
1100 Also, the parameters can have double quotes. Thus, the following uses are valid:
1101 <p>
1102
1103 <pre>
1104 TOLUA_TEMPLATE_BIND((T, int, float)) // to be used inside a real header file
1105 TOLUA_TEMPLATE_BIND("K V", "string string", int double)
1106 </pre>
1107
1108 Function templates are not supported on this version.
1109
1110 <H3>
1111 <A NAME="modules"></A>Module definition</H3>
1112 <B>tolua </B>allows us to group constants, variables, and functions in
1113 a module. The module itself is mapped to a table in Lua, and its constants,
1114 variables, and functions are mapped to fields in that table. The general
1115 format to specify a module is:
1116 <P><TT><B>module</B> name</TT>
1117 <BR><B><TT>{</TT></B>
1118 <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... // constant, variable, and function
1119 declarations</TT>
1120 <BR><B><TT>}</TT></B>
1121 <P>Thus, if we bound the following module declaration:
1122 <P><TT>module mod</TT>
1123 <BR><TT>{</TT>
1124 <BR><TT>&nbsp;#define N</TT>
1125 <BR><TT>&nbsp;extern int var;</TT>
1126 <BR><TT>&nbsp;int func (...):</TT>
1127 <BR><TT>}</TT>
1128 <P>In Lua we would be able to access such features by indexing the module:
1129 <TT>mod.N</TT>,
1130 <TT>mod.var</TT>,
1131 <TT>mod.func</TT>.
1132 <H3>
1133 <A NAME="renaming"></A>Renaming constants, variables and functions</H3>
1134 When exporting constants, variable, and functions (members of a class or
1135 not), we can rename them, such that they will be bound with a different
1136 name from their C/C++ counterparts. To do that, we write the name they
1137 will be referenced in Lua after the character <TT>@</TT>. For instance:
1138 <P><TT>extern int cvar @ lvar;</TT>
1139 <P><TT>#define CNAME @ LNAME</TT>
1140 <P><TT>enum {</TT>
1141 <BR><TT>&nbsp; CITEM1 @ LITEM1,</TT>
1142 <BR><TT>&nbsp; CITEM2 @ LITEM2,</TT>
1143 <BR><TT>&nbsp; ...</TT>
1144 <BR><TT>};</TT>
1145 <P><TT>void cfunc @ lfunc (...);</TT>
1146 <P><TT>class T</TT>
1147 <BR><TT>{</TT>
1148 <BR><TT>&nbsp;&nbsp; double cfield @ lfield;</TT>
1149 <BR><TT>&nbsp;&nbsp; void cmeth @ lmeth (...);</TT>
1150 <BR><TT>&nbsp;&nbsp; ...</TT>
1151 <BR><TT>};</TT>
1152 <P>In such a case, the global variable <TT>cvar </TT>would be identified
1153 in Lua by <TT>lvar</TT>, the constant <TT>CNAME </TT>by <TT>LNAME</TT>,
1154 and so on. Note that class cannot be renamed, because they represent types
1155 in C.
1156 <P>This renaming feature allows function overload in C, because we can
1157 choose to export two different C functions with a same Lua name:
1158 <P><TT>void glVertex3d @ glVertex (double x, double y, double z=0.0);</TT>
1159 <BR><TT>void glVertexdv @ glVertex (double v[3]=0.0);</TT>
1160
1161 <a name="type_renaming"></a><h3>Renaming Types</h3>
1162
1163 Types can be renamed using the <tt>$renaming</tt> directive on pkg files, using the
1164 format:
1165 <p>
1166
1167 <tt>$renaming real_name @ new_name</tt>
1168 <p>
1169
1170 The parameters to renaming can be Lua <i>patterns</i>. For example:
1171 <p>
1172
1173 <pre>
1174 $renaming ^_+ @
1175 $renaming hash_map&lt;string,vector&lt;double&gt; &gt; @ StringHash
1176 </pre>
1177
1178 The first example will remove all underscores at the beginning of all types,
1179 the second will rename the template type hash_map&lt;string,vector&lt;double&gt; &gt;
1180 to StringHash. Once renamed, the Lua table for each type can be accessed only by their
1181 new name, for example: <tt>StringHash:new()</tt>
1182
1183 <A NAME="additional"></A><h3>Storing additional fields</H3>
1184 Finally, it is important to know that even though the variables that hold
1185 C/C++ objects are actually tagged userdata for Lua,<B> tolua</B> creates
1186 a mechanism that allows us to store any additional field attached to these
1187 objects. That is, these objects can be seen as conventional Lua tables.
1188 <PRE>obj = <I>ClassName</I>:new()</PRE>
1189
1190 <PRE>obj.myfield = 1&nbsp; -- even though &quot;myfield&quot; does not represent a field of ClassName</PRE>
1191 Such a construction is possible because, if needed, <B>tolua </B>automatically
1192 creates a Lua table and associates it with the object. So that, the object
1193 can store additional fields not mapped to C/C++, but actually stored in
1194 the conjugate table. The Lua programmer accesses the C/C++ features and
1195 these additional fields in an uniform way. Note that, in fact, these additional
1196 fields overwrite C/C++ fields or methods when the names are the same.
1197
1198
1199
1200 <a name="additional_features"></a><h3>Additional features on tolua++</h3>
1201
1202 <h4>Multiple variable declarations</h4>
1203
1204 Multiple variables of the same type can be declared at the same time, for example:
1205 <p>
1206 <tt>float x,y,z;</tt>
1207 <p>
1208 will create 3 different variables of type float. Make sure you don't leave any
1209 spaces between the commas, as that will raise a parse error.
1210 <p>
1211
1212 <h4>tolua_readonly</h4>
1213
1214 Any variable declaration can use the <tt>tolua_readonly</tt> modifier, to ensure
1215 that the variable is read-only, even when its type is not <tt>const</tt>. Example:
1216
1217 <pre>
1218 class Widget {
1219
1220 tolua_readonly string name;
1221 };
1222 </pre>
1223
1224 This feature could be used to 'hack' the support for other unsupported things like
1225 <tt>operator-&gt;</tt>. Consider this example <tt>pkg</tt> file:
1226
1227 <pre>
1228 $hfile "node.h"
1229 $#define __operator_arrow operator->()
1230 $#define __get_name get_name()
1231 </pre>
1232
1233 And on the file <tt>node.h</tt>:
1234
1235 <pre>
1236 template class&lt;T&gt;
1237 class Node { // tolua_export
1238
1239 private:
1240 string name;
1241 T* value;
1242
1243 public:
1244
1245 T* operator->() {return value;};
1246 string get_name() {return name;};
1247
1248 // tolua_begin
1249
1250 #if 0
1251 TOLUA_TEMPLATE_BIND(T, Vector3D)
1252
1253 tolua_readonly __operator_arrow @ p;
1254 tolua_readonly __get_name @ name;
1255 #endif
1256
1257
1258 Node* next;
1259 Node* prev;
1260
1261 void set_name(string p_name) {name = p_name;};
1262
1263 Node();
1264 };
1265 // tolua_end
1266 </pre>
1267
1268 While not a pretty thing to do to a header file, this accomplishes a number of
1269 things:
1270 <p>
1271
1272 <li> The method <tt>operator->()</tt> can be used from Lua by calling the variable
1273 <tt>p</tt> on the object.</li>
1274 <li> The method <tt>get_name()</tt> can be using from Lua by calling the variable
1275 <tt>name</tt> on the boject.</li>
1276
1277 Example lua usage:
1278
1279 <pre>
1280 node = Node_Vector3D_:new_local()
1281 -- do something with the node here --
1282 print("node name is "..node.name)
1283 print("node value is ".. node.p.x ..", ".. node.p.y ..", ".. node.p.z)
1284 </pre>
1285
1286 Since <b>tolua++</b> ignores all preprocessor directives (except for #define), <tt>node.h</tt>
1287 remains a valid C++ header file, and also a valid source for <b>tolua++</b>,
1288 eliminating the need to maintain 2 different files, even for objects with
1289 unusual features such as these ones.
1290
1291 <p>
1292
1293 The ability to rename functions as variables might be expanded on future versions.
1294
1295 <h4>Defining values on command line</h4>
1296
1297 Starting from version 1.0.92, the command line option <tt>-E</tt> allows
1298 you to introduce values into to the luastate where <b>tolua++</b> runs,
1299 similar to GCC's <tt>-D</tt>. For example:
1300 <p>
1301
1302 <pre>$ tolua++ -E VERSION=5.1 -E HAVE_ZLIB package.pkg > package_bind.cpp</pre>
1303
1304 <p>
1305 This will add 2 fields to the global table <tt>_extra_parameters</tt>:
1306 "VERSION", with the string value "5.1", and "HAVE_ZLIB" with the boolean value
1307 <tt>true</tt>. For the moment, there is no way to 'use' these values, except
1308 in custom scripts defined by the user (see
1309 <a href="#customizing">customizing tolua++</a> for details).
1310
1311 <h4>Using C++ typeid</h4>
1312
1313 Starting from version 1.0.92, the command line option <tt>-t</tt> is available,
1314 which generates a list of calls to the empty macro <tt>Mtolua_typeid</tt>, with its
1315 C++ <tt>type_info object</tt>, and the name used by tolua++ to identify the type. For example,
1316 if you have a package that binds 2 classes, <tt>Foo</tt> and <tt>Bar</tt>, using <tt>-t</tt>
1317 will produce the following output:
1318
1319 <pre>
1320 #ifndef Mtolua_typeid
1321 #define Mtolua_typeid(L,TI,T)
1322 #endif
1323 Mtolua_typeid(tolua_S,typeid(Foo), "Foo");
1324 Mtolua_typeid(tolua_S,typeid(Bar), "Bar");
1325 </pre>
1326
1327 The implementation of Mtolua_typename is left as an exercise to the user.
1328
1329 <H3>
1330 <A NAME="utilities"></A>Exported utility functions</H3>
1331 <B>tolua </B>uses itself to export some utility functions to Lua, including
1332 its object-oriented framework. The package file used by <B>tolua </B>is
1333
1334 shown below:
1335 <P><TT>module tolua</TT>
1336 <BR><TT>{</TT>
1337 <BR><TT>&nbsp;char* tolua_bnd_type @ type (lua_Object lo);</TT>
1338
1339 <BR><TT>&nbsp;void tolua_bnd_takeownership @ takeownership (lua_Object lo);</TT>
1340 <BR><TT>&nbsp;void tolua_bnd_releaseownership @ releaseownership (lua_Object lo);</TT>
1341
1342 <BR><TT>&nbsp;lua_Object tolua_bnd_cast @ cast (lua_Object lo, char* type);</TT>
1343
1344 <BR><TT>&nbsp;void tolua_bnd_inherit @ inherit (lua_Object table, lua_Object instance);</TT>
1345 <p>
1346 <TT>/* for lua 5.1 */</tt>
1347 <BR><TT>&nbsp;void tolua_bnd_setpeer @ setpeer (lua_Object object, lua_Object peer_table);</TT>
1348 <BR><TT>&nbsp;void tolua_bnd_getpeer @ getpeer (lua_Object object);</TT>
1349
1350 <BR><TT>}</TT>
1351
1352 <H4>
1353 tolua.type (<I>var</I>)</H4>
1354 Returns a string representing the object type. For instance, <TT>tolua.type(tolua)</TT>
1355 returns the string <TT>table</TT> and <TT>tolua.type(tolua.type)</TT>
1356 returns <TT>cfunction</TT>. Similarly, if <TT>var </TT>is a variable holding
1357 a user defined type <TT>T</TT>, <TT>tolua.type(var)</TT> would return
1358 <TT>const
1359 T</TT> or <TT>T</TT>, depending whether it is a constant reference.
1360 <p>
1361
1362 <h4>tolua.takeownership (<I>var</i>)</h4>
1363
1364 Takes ownership of the object referenced <i>var</i>. This means that when all references
1365 to that object are lost, the objects itself will be deleted by lua.
1366 <p>
1367
1368 <h4>tolua.releaseownership (<i>var</i>)</h4>
1369
1370 Releases ownership of the object referenced by <i>var</i>.
1371 <p>
1372
1373 <h4>tolua.cast (<i>var</i>, <i>type</i>)</h4>
1374
1375 Changes the metatable of <i>var</i> in order to make it of type <i>type</i>. <i>type</i> needs
1376 to be a string with the complete C type of the object (including namespaces, etc).
1377
1378 <h4>tolua.inherit (<i>table</i>, <i>var</i>)</h4> (new on <b>tolua++</b>)
1379
1380 Causes <b>tolua++</b> to recognise <i>table</i> as an object with the same type as <i>var</i>,
1381 and to use <i>var</i> when necesary. For example, consider this method:
1382 <p>
1383
1384 <ul><tt>void set_parent(Widget* p_parent);</tt></ul>
1385 <p>
1386
1387 A lua object could be used like this:
1388 <p>
1389
1390 <pre>
1391 local w = Widget()
1392 local lua_widget = {}
1393 tolua.inherit(lua_widget, w)
1394
1395 set_parent(lua_widget);
1396
1397 </pre>
1398
1399 Remember that this will only cause the table to be recognised as type 'Widget' when
1400 necesary. To be able to access Widget's methods, you'll have to implement your own
1401 object system. A simple example:
1402 <p>
1403
1404 <pre>
1405
1406 lua_widget.show = Widget.show
1407
1408 lua_widget:show() -- this will call the 'show' method from 'Widget', using the lua
1409 -- table as 'self'. Since lua_widget inherits from a widget instance,
1410 -- tolua++ will recognise 'self' as a 'Widget', and call the method
1411
1412 </pre>
1413 <p>
1414
1415 Of course a better way would be to add a __index metamethod for the lua object.
1416 <p>
1417
1418 Similarily, to implement virtual functions, you'll need to create a c++ object that inherits
1419 from the desired type, implement its virtual functions, and use that to inherit from lua. The
1420 object would have a reference to the lua table, and call its methods from the c++ virtual
1421 methods.
1422 <p>
1423
1424 <b>Note:</b> the current implementation (as of version 1.0.6) stores the C instance
1425 inside the lua table on the field ".c_instance", and looks that up when necesary. This
1426 might change in the future, so it is recommended to use an alternative way to store the
1427 C instance to use with your own object system.
1428
1429 <h4>tolua.setpeer (<i>object</i>, <i>peer_table</i>) (lua 5.1 only)</h4>
1430
1431 Sets the table as the object's <i>peer</i> table (can be <tt>nil</tt>). The peer table is where all the custom
1432 lua fields for the object are stored. When compiled with lua 5.1, <b>tolua++</b> stores the
1433 peer as the object's <i>envirnment table</i>, and uses uses <tT>lua_gettable/settable</tT> (instead of
1434 <tt>lua_rawget/set</tt> for lua 5.0) to retrieve and store fields on it. This allows us to implement our own
1435 object system on our table (using metatables), and use it as a way to inherit from the userdata object.
1436 Consider an alternative to the previous example:
1437
1438 <pre>
1439 -- a 'LuaWidget' class
1440 LuaWidget = {}
1441 LuaWidget.__index = LuaWidget
1442
1443 function LuaWidget:add_button(caption)
1444
1445 -- add a button to our widget here. 'self' will be the userdata Widget
1446 end
1447
1448
1449 local w = Widget()
1450 local t = {}
1451 setmetatable(t, LuaWidget) -- make 't' an instance of LuaWidget
1452
1453 tolua.setpeer(w, t) -- make 't' the peer table of 'w'
1454
1455 set_parent(w) -- we use 'w' as the object now
1456
1457 w:show() -- a method from 'Widget'
1458 w:add_button("Quit") -- a method from LuaWidget (but we still use 'w' to call it)
1459 </pre>
1460
1461 When indexing our object, the peer table (if present) will be consulted first, so we
1462 don't need to implement our own __index metamethod to call the C++ functions.
1463
1464 <h4>tolua.getpeer (<i>object</i>) (lua 5.1 only)</h4>
1465
1466 Retrieves the peer table from the object (can be <tt>nil</tt>).
1467
1468 <!--
1469 <H3>
1470 <A NAME="utilities"></A>Exported utility functions</H3>
1471 <B>tolua </B>uses itself to export some utility functions to Lua, including
1472 its object-oriented framework. The package file used by <B>tolua </B>is
1473 shown below:
1474 <P><TT>module tolua</TT>
1475 <BR><TT>{</TT>
1476 <BR><TT>&nbsp;void tolua_using @ using (lua_Table module);</TT>
1477 <BR><TT>&nbsp;char* tolua_type @ type (lua_Object lo);</TT>
1478 <BR><TT>&nbsp;void tolua_foreach @ foreach (lua_Object lo, lua_Function
1479 f);</TT>
1480 <BR><TT>&nbsp;void tolua_class @ class (lua_Table derived, lua_Table base=TOLUA_NIL);</TT>
1481 <BR><TT>&nbsp;void tolua_instance @ instance (lua_Table instance, lua_Table
1482 classobj);</TT>
1483 <BR><TT>&nbsp;lua_Object tolua_base @ base (lua_Object lo);</TT>
1484 <BR><TT>}</TT>
1485 <H4>
1486 tolua.using (<I>table</I>)</H4>
1487 This functions receives a table and maps all its fields to the global environment.
1488 Thus we can map an entire module and access its features without the module
1489 prefix. For instance, if in our Lua code we do:
1490 <P><TT>tolua.using(tolua)</TT>
1491 <P>all <B>tolua </B>utility functions are mapped to the global environment.
1492 <H4>
1493 tolua.type (<I>var</I>)</H4>
1494 Returns a string representing the object type. For instance, <TT>tolua.type(tolua)</TT>
1495 returns the string <TT>generic module</TT> and <TT>tolua.type(tolua.type)</TT>
1496 returns <TT>cfunction</TT>. Similarly, if <TT>var </TT>is a variable holding
1497 a user defined type <TT>T</TT>, <TT>tolua.type(var)</TT> would return
1498 <TT>const
1499 T</TT> or <TT>T</TT>, depending whether it is a constant reference.
1500 <H4>
1501 tolua.tag (<I>"type"</I>)</H4>
1502 Returns type corresponding tag number.
1503 <H4>
1504 tolua.foreach (<I>object</I>)</H4>
1505 Allows us to traverse the conjugate table of an user defined instance.
1506 If applied to conventional table, it has a similar behavior as the Lua
1507 built-in <TT>foreach </TT>function. The difference is that this function
1508 filters all fields starting with a dot, not passing them to the provided
1509 callback function. This filter is need because <B>tolua </B>adds "hidden"
1510 fields to the tables it manipulates, and all its "hidden" fields start
1511 with a dot.
1512 <H4>
1513 tolua.cast (<I>object, "typename"</I>)</H4>
1514 Returns the object "casted" to the given type. The object must represent
1515 an user type, otherwise the function returns <B><TT>nil</TT></B>.
1516 <H4>
1517 tolua.takeownership (<I>object</I>)</H4>
1518 Asks <B>tolua</B> to take the ownership of the given object. This means
1519 the C/C++ object will be freed/ destructed when garbage-collected by Lua.&nbsp;
1520 The object must represent an user type, otherwise an execution error is
1521 generated.
1522 <H4>
1523 tolua.class (<I>table</I>, <I>base=nil</I>)</H4>
1524 Creates a class by setting the appropriate tag methods to the given table.
1525 The created class can inherit from a base class, previously created.
1526 <H4>
1527 tolua.instance (<I>table</I>, <I>class</I>)</H4>
1528 Sets the given table to be an instance of the given class. This and the
1529 previous utility functions allow object-oriented programming in Lua. As
1530 an example consider:
1531 <P><TT>-- define a Point class</TT>
1532 <BR><TT>classPoint = { x=0, y=0 }</TT>
1533 <BR><TT>tolua.class(classPoint) -- set as a class</TT>
1534 <P><TT>-- define print method</TT>
1535 <BR><TT>function classPoint:print ()</TT>
1536 <BR><TT>&nbsp;&nbsp; print(self.x,self.y)</TT>
1537 <BR><TT>end</TT>
1538 <P><TT>-- define add method</TT>
1539 <BR><TT>function classPoint:add (p2)</TT>
1540 <BR><TT>&nbsp;&nbsp; return Point{x=self.x+p2.x,y=self.y+p2.y}</TT>
1541 <BR><TT>end</TT>
1542 <P><TT>-- define a Point constructor</TT>
1543 <BR><TT>function Point (p)</TT>
1544 <BR><TT>&nbsp;&nbsp; tolua.instance(p,classPoint) -- set as an instance
1545 of classPoint</TT>
1546 <BR><TT>return p end</TT>
1547 <P><TT>-- define a Color Point class</TT>
1548 <BR><TT>classColorPoint = { color = 'black' }</TT>
1549 <BR><TT>tolua.class(classColorPoint,classPoint) -- set as class inheriting
1550 from classPoint</TT>
1551 <P><TT>-- define class methods</TT>
1552 <BR><TT>function classColorPoint:print ()</TT>
1553 <BR><TT>&nbsp;&nbsp; print(self.x,self.y,self.color)</TT>
1554 <BR><TT>end</TT>
1555 <P><TT>-- define Color Point constructor</TT>
1556 <BR><TT>function ColorPoint (p)</TT>
1557 <BR><TT>&nbsp;&nbsp; tolua.instance(p,classColorPoint) -- set as an instance
1558 of classColorPoint</TT>
1559 <BR><TT>&nbsp;&nbsp; return p</TT>
1560 <BR><TT>end</TT>
1561 <P><TT>-- Some valid codes would then be</TT>
1562 <BR><TT>p = Point{x=1}</TT>
1563 <BR><TT>q = ColorPoint{x=2,y=3,color=2}</TT>
1564 <BR><TT>r = p:add(q)</TT>
1565 <BR><TT>r:print() --&gt; would print "3 3"</TT>
1566 <BR>&nbsp;
1567 -->
1568
1569 <H3>
1570 <A NAME="embedded"></A>Embedded Lua code</H3>
1571 <B>tolua</B> allows us to embed Lua code in the C/C++ generated code. To
1572 do that, it compiles the specified Lua code and creates a C constant string,
1573 storing the corresponding bytecodes, in the generated code.&nbsp; When
1574 the package is opened, such a string is executed. The format to embed Lua
1575 code is:
1576 <P><B><TT>$[</TT></B>
1577 <P><I><TT>embedded Lua code</TT></I>
1578 <BR><I><TT>...</TT></I>
1579 <P><B><TT>$]</TT></B>
1580 <P>As an example consider the following .pkg excerpt:
1581 <P><TT>/* Bind a Point class */</TT>
1582 <BR><TT>class Point</TT>
1583 <BR><TT>{</TT>
1584 <BR><TT>&nbsp;Point (int x, int y);</TT>
1585 <BR><TT>&nbsp;~Point ();</TT>
1586 <BR><TT>&nbsp;void print ();</TT>
1587 <BR><TT>&nbsp;...</TT>
1588 <BR><TT>} CPoint;</TT>
1589 <P><TT>$[</TT>
1590 <P><TT>-- Create a Point constructor</TT>
1591 <BR><TT>function Point (self)</TT>
1592 <BR><TT>&nbsp;local cobj = CPoint:new(self.x or 0, self.y or 0)</TT>
1593 <BR><TT>&nbsp;tolua.takeownership(cobj)</TT>
1594 <BR><TT>&nbsp;return cobj</TT>
1595 <BR><TT>end</TT>
1596 <P><TT>$]</TT>
1597 <P>Binding such a code would allow us to write the following Lua code:
1598 <P><TT>p = Point{ x=2, y=3 }</TT>
1599 <BR><TT>p:print()</TT>
1600 <BR><TT>...</TT>
1601 <BR>&nbsp;
1602
1603 <h3><a name="customizing"></a>Customizing tolua++</h3>
1604 <p>
1605
1606 <b>tolua++</b> calls empty functions at specific points of its execution. This functions
1607 can be redefined on a separate lua file (and included using the -L command line option)
1608 and be used to control the way <b>tolua++</b> behaves. This is the list of functions
1609 (taken from basic.lua on the <b>tolua++</b> source):
1610 <p>
1611
1612 <PRE>
1613
1614 -- called right after processing the $[ichl]file directives,
1615 -- right before processing anything else
1616 -- takes the package object as the parameter
1617 function preprocess_hook(p)
1618 -- p.code has all the input code from the pkg
1619 end
1620
1621
1622 -- called for every $ifile directive
1623 -- takes a table with a string called 'code' inside, the filename, and any extra arguments
1624 -- passed to $ifile. no return value
1625 function include_file_hook(t, filename, ...)
1626
1627 end
1628
1629 -- called after processing anything that's not code (like '$renaming', comments, etc)
1630 -- and right before parsing the actual code.
1631 -- takes the Package object with all the code on the 'code' key. no return value
1632 function preparse_hook(package)
1633
1634 end
1635
1636
1637 -- called after writing all the output.
1638 -- takes the Package object
1639 function post_output_hook(package)
1640
1641 end
1642
1643 -- called at the beginning of the main parser function, with the code being parsed as a parameter
1644 -- it can return nil if nothing was foind, or the contents of 'code', modified by the function
1645 -- Usually a parser hook will search the beginning of the string for a token, and if it finds
1646 -- anything, return the string without that token (after doing whatever it has to do with the token).
1647 function parser_hook(code)
1648
1649 end
1650
1651
1652 -- called from classFunction:supcode, before the call to the function is output
1653 -- the classFunction object is passed.
1654 function pre_call_hook(f)
1655
1656 end
1657
1658 -- called from classFunction:supcode, after the call to the function is output
1659 -- the classFunction object is passed.
1660 function post_call_hook(f)
1661
1662 end
1663
1664 -- called before the register code is output
1665 function pre_register_hook(package)
1666
1667 end
1668
1669 -- called to output an error message
1670 function output_error_hook(...)
1671 return string.format(...)
1672 end
1673
1674
1675 </PRE>
1676 <p>
1677
1678 <h4>Handling custom types</h4>
1679
1680 Starting from version 1.0.93, it is possible to specify custom functions to handle certain types. There are 3
1681 types of functions: a <tt>'push function'</tt>, used to push the C value onto the Lua stack, a <tt>'to function'</tt>,
1682 used to retrieve the value from the Lua stack, and return it as a C value, and an <tt>'is function'</tt>, used to
1683 check if the value on the stack is valid (or convertible to a C value by the <tt>to function</tt>). These functions are modelled upon
1684 <tt>tolua_pushusertype</tt>, <tt>tolua_tousertype</tt> and <tt>tolua_isusertype</tt>, declared in <tt>tolua++.h</tt>.
1685 <p>
1686 A number of arrays found in <tt>basic.lua</tt> are used to specify these functions:
1687 <p>
1688
1689 <PRE>
1690 -- for specific types
1691 _push_functions = {}
1692 _is_functions = {}
1693 _to_functions = {}
1694
1695 -- for base types
1696 _base_push_functions = {}
1697 _base_is_functions = {}
1698 _base_to_functions = {}
1699 </PRE>
1700
1701 <b>Example (using the -L command line option):</b>
1702 <p>
1703
1704 <PRE>
1705 _is_functions['Vector3'] = 'custom_is_vector3' -- checks for a 3d vector
1706 -- (either userdata, or a table with 3 values)
1707 _to_functions['Vector3'] = 'custom_to_vector3' -- convertes the eventual table to a Vector3
1708
1709 _base_push_functions['Widget'] = 'custom_push_widget' -- pushes anything that inherits from Widget
1710 </PRE>
1711
1712 The <tt>_base</tt> tables are used to lookup functions for types that are up in the inheritance chain.
1713
1714 <h4>Access</h4>
1715
1716 Starting from version 1.0.7, all objects have a an <tt>access</tt> flag, which determines the object's access
1717 inside its container. Container objects also have a member called <tt>curr_member_access</tt>, which determines
1718 the access of each child object at the moment of its addition to the container. If the <tt>access</tt> flag has
1719 the value <tt>nil</tt> (default), <tt>false</tt> or <tt>0</tt>, the object is public. Otherwise, the object
1720 is not public, and <b>tolua++</b> will not export any code for that object (and any objects it may contain).
1721 <p>
1722
1723 Another 'interesting' function is <tt>extract_code</tt>, defined on basic.lua, which
1724 extracs the code from files included with $cfile and $hfile (by looking for tolua_begin/end and tolua_export).
1725 <p>
1726
1727 <h3><a name="compatibility"></a>Compatibility with older versions.</h3>
1728
1729 <h4>tolua++ &lt;1.0.90</h4>
1730 <p>
1731 Version 1.0.90 of <b>tolua++</b> introduces 2 small API changes the might be incompatible
1732 with older versions on some cases:
1733 <p>
1734 <b>TEMPLATE_BIND</b>
1735 <p>
1736 <tt>TEMPLATE_BIND</tt> is deprecated. Use <tt>TOLUA_TEMPLATE_BIND</tt> instead. Also, when
1737 declaring a template, the <tt>TOLUA_TEMPLATE_BIND</tt> statement has to be the first thing
1738 inside the class declaration, otherwise it will be ignored. This fixes a possible problem
1739 with nested template declarations.
1740 <p>
1741
1742 <b>Retrieving Objects</b>
1743 <p>
1744
1745 When passing a full userdata to a function that accepts light userdata parameters (void*),
1746 the <b>tolua++</b> library function <tt>tolua_touserdata</tt> will detect the full userdata and dereference
1747 the void** pointer if necesary. This is a change on the function's behaviour (which used to return the pointer as-is). This allows us to pass pointers to objects to a function that accepts
1748 void* pointers. Note that this was a problem when switching from <b>toLua</b> version 4 to version 5 (and <b>tolua++</b> versions &lt; 1.0.90).
1749
1750 <h4>toLua 4</h4>
1751 <p>
1752
1753 <b>Retrieving Objects</b>
1754 <p>
1755
1756 Users switching from <b>tolua</b> v4 should know that <b>tolua++</b> stores the objects as
1757 <tt>void**</tt> on Lua, so when retrieving an object from the luastate using <tt>lua_touserdata</tt>,
1758 the pointer should be dereferenced. The library function <tt>tolua_tousertype</tt>
1759 should work as expected. Example:
1760 <p>
1761
1762 <pre>
1763 lua_pushglobal(lua_state, "get_Object");
1764 lua_call(lua_state, 0, 1); // calling a function that returns an Object
1765
1766 Object *new_object = (Object*)(*lua_touserdata(lua_state, -1));
1767 <i>or</i>
1768 Object *new_object = (Object*)tolua_tousertype(lua_state, -1, NULL);
1769 </pre>
1770
1771 <b>C++ Strings</b>
1772 <p>
1773
1774 <b>tolua++</b> binds the c++ type <tt>std::string</tt> as a basic type, passing it
1775 to Lua as a regular string (using the method <tt>c_str()</tt>). This feature can be
1776 turned off with the command line option <tt>-S</tt>.
1777 <p>
1778
1779 <b>Operators</b>
1780 <p>
1781
1782 The list of supported operators has changed, see <a href="#classes">Binding classes and methods</a>
1783 for more information.
1784 <p>
1785
1786 <h4>toLua 5</h4>
1787 <p>
1788
1789 <b>Class destructors.</b>
1790 <p>
1791
1792 With every class constructor, <b>tolua++</b> exports a 'local' constructor, to create an instance
1793 of the class that is owned by the lua state. To implement this, <b>tolua++</b> will also export
1794 a function that will <tt>delete</tt> the class. There are 2 options to prevent this:
1795 <p>
1796
1797 Using the <b>-D</b> command line option will turn this off completely. Destructor functions will
1798 only be exported when a destructor for the class is explicitly declared, or when there is a function
1799 or method that returns an object of the type by value (this is compatible with <b>tolua</b>'s behaviour).
1800 <p>
1801
1802 Using the <tt>TOLUA_PROTECTED_DESTRUCTOR</tt> directive inside the class declaration, to specify that the
1803 class has a private or protected destructor. In this case, no destructor will be exported for that class.
1804 <p>
1805
1806 <b><tt>operator[]</tt> index</b>
1807 <p>
1808
1809 Users switching from <b>tolua</b> v5 should know that <b>tolua</b> 5 substracts 1
1810 from the index on operator[] functions, for compatibility with lua's method for indexing arrays
1811 (1 is the first element). This feature is turned off by default on <b>tolua++</b>
1812 (making it compatible with <b>tolua</b> 4). It can be turned back on with the
1813 command line option <tt>-1</tt>
1814 <p>
1815
1816 <b>C++ Strings</b>
1817 <p>
1818
1819 (see c++ strings on <b>tolua</b> 4 below)
1820
1821
1822 <H3>
1823 <A NAME="changes-v30"></A>Changes since v. 3.0</H3>
1824
1825 <ul>
1826
1827 <LI>
1828 Support for binding arrays as variables and struct/class fields;</LI>
1829
1830 <LI>
1831 Support for embedding Lua code into the generated binding code;</LI>
1832
1833 <LI>
1834 New utility functions: <I>cast</I> and <I>takeownership</I>;</LI>
1835
1836 <LI>
1837 Option to create the corresponding header file of the binding code;</LI>
1838
1839 <LI>
1840 New "close" package function;</LI>
1841
1842 <LI>
1843 Fixed bug on cloning objects in C++;</LI>
1844
1845 <LI>
1846 Fixed bug on enum and struct parsing;</LI>
1847 </ul>
1848 <H3>
1849 <A NAME="changes-v2"></A>Changes since v. 2.0</H3>
1850 <ul>
1851
1852 <LI>
1853 There is a new executable parser;</LI>
1854
1855 <LI>
1856 Support for multiple Lua states is provided;</LI>
1857
1858 <LI>
1859 Support for module definition is provided;</LI>
1860
1861 <LI>
1862 Global variables is now directly bound to Lua global variables;</LI>
1863
1864 <LI>
1865 Constness of user defined types is preserved in Lua;</LI>
1866
1867 <LI>
1868 Support for multiple returned values from C/C++ is provided (simulating
1869 parameters passed by reference);</LI>
1870
1871 <LI>
1872 Constants, variables, and functions bound to Lua can have different names
1873 from their C/C++ counterparts;</LI>
1874
1875 <LI>
1876 Object-oriented framework (and other utility functions) used in <B>tolua
1877 </B>is
1878 now exported for Lua programmers;</LI>
1879
1880 <H4>
1881 Incompatibilities</H4>
1882 Lua code based on <B>tolua</B> v2.* should run with no change on <B>tolua
1883 </B>v3.0.
1884 Although, it may be necessary to change the .pkg file in order to get the
1885 same behavior. The following incompatibilities exist:
1886 <UL>
1887 <LI>
1888 Parameters defined as pointer to basic types are no longer converted to
1889 arrays of dimension one; they are now considered parameters passed by reference.</LI>
1890
1891 <LI>
1892 Automatic initialization for C++ code must be explicitly requested when
1893 using the new parser;</LI>
1894
1895 <LI>
1896 Global variables are no longer mapped to a table; the definition of a module
1897 including the global variables may be used to simulate the old behavior;</LI>
1898
1899 <LI>
1900 The initialization function is no longer toLua_<I>package</I>_open but
1901 tolua_<I>package</I>_open, without the capital letter (sorry!).</LI>
1902 </UL>
1903 </ul>
1904 <H3>
1905 <A NAME="changes-v1"></A>Changes since v. 1.*</H3>
1906
1907 <ul>
1908 <LI>
1909 The binding code should run much faster;</LI>
1910
1911 <LI>
1912 The <I>cleaned header file </I>extension should now be <TT>.pkg</TT> instead
1913 of <TT>.L</TT>;</LI>
1914
1915 <LI>
1916 Type modifiers is now accepted (though the current version ignores
1917 <TT>const</TT>'s);</LI>
1918
1919 <LI>
1920 Returning object by value is accepted and memory allocation is controlled
1921 by Lua garbage collection;</LI>
1922
1923 <LI>
1924 Overloaded functions/methods are accepted;</LI>
1925
1926 <LI>
1927 Parameters with default values are accepted;</LI>
1928
1929 <LI>
1930 Some overloaded operators are automatically bound.</LI>
1931 </ul>
1932
1933 <H3>
1934 <A NAME="credits"></A>Credits</H3>
1935 Luiz Henrique de Figueiredo had the idea of creating a tool to automatically
1936 bind C code to Lua. L.H.F. wrote the very first version of such a tool
1937 (that bound C functions, variables, and constants) in <I>awk</I>. At that
1938 time, Waldemar Celes (now the main author) was only responsible for the C code that supported the generated
1939 binding code.
1940 <p>
1941 While working at NGD Studios, Ariel Manzur made some changes to tolua4 for their
1942 game engine. After the release of tolua5, having left NGD, enough changes were
1943 made to tolua to justify a separate release (with Waldemar's blessing :-)
1944 <BR>&nbsp;
1945 <H3>
1946 <A NAME="availability"></A><B>Availability</B></H3>
1947 <B>tolua++ </B>is freely available by <A HREF="http://www.codenix.com/~tolua/tolua++-current.tar.bz2">http</A>.
1948 The software provided hereunder is on an "as is" basis, and the author
1949 has no obligation to provide maintenance, support, updates, enhancements,
1950 or modifications.
1951 <P>
1952 <HR WIDTH="100%">This document was created by <A HREF="http://www.tecgraf.puc-rio.br/~celes/">Waldemar Celes</A>
1953 With modifications by <a href="http://www.codenix.com/index.php?section=contact">Ariel Manzur</a>/
1954 <BR>Last update: Sept 2003<BODT>
1955 </BODY>
1956 </HTML>