]> git.draconx.ca Git - gob-dx.git/blob - doc/gob2.1.in
Release 2.0.10
[gob-dx.git] / doc / gob2.1.in
1 .\"
2 .\" gob manual page
3 .\" (C) 1999,2000,2001,2002 George Lebl <jirka@5z.com>
4 .\" 
5 .\" This manual page is covered by the terms of the GNU General
6 .\" Public License.  
7 .\"
8 .TH GOB2 1 "GOB2 @VERSION@" 
9 .SH NAME
10 GOB2 \- The GObject Builder
11 .SH SYNOPSIS
12 .PP
13 .B gob2
14 [ option ] ...
15 file
16
17 .SH DESCRIPTION
18 .PP
19 GObject Builder is a simple preprocessor for easily creating
20 GObject objects.  It does not parse any C code and ignores any C errors.  It
21 is in spirit similar to things like lex or yacc.  In some ways it
22 also resembles java.  But it is really just a simple preprocessor
23 for creating GObjects for use in C or C++ and it is not a programming
24 language.
25
26 .SH OPTIONS
27 .PP
28 .TP
29 .B -? -h --help
30 Display a simple help screen.
31 .TP
32 .B --version
33 Display version information
34 .TP
35 .B -w --exit-on-warn
36 Exit with an error code even when you encounter a warning.
37 .TP
38 .B --no-exit-on-warn
39 Exit with an error only on errors, not on warnings, this is the default.
40 .TP
41 .B --for-cpp
42 Generate C++ code.
43 .TP
44 .B --no-extern-c
45 Never add the extern "C" to the header.
46 .TP
47 .B --no-gnu
48 Never generate any code with GNU C extensions.  However all the GNU C
49 extensions are always wrapped in #ifdef __GNUC__, so code using them compiles
50 correctly even on non-GNU compilers.  This option is for purists only.
51 (using GNU extensions some warnings are eliminated, some ugly hacks and there
52 is better argument type safety, so it\'s good to use them)
53 .TP
54 .B --no-touch-headers
55 Don\'t touch the generated header file unless it really changed, this avoids
56 spurious rebuilds, but can confuse some make systems (automake in particular),
57 so it is not enabled by default.  Private header is still touched even if
58 unchanged however.
59 .TP
60 .B --always-private-header
61 Always create a \fB<basename>-private.h\fR file, even if it would be empty.
62 .TP
63 .B --ondemand-private-header
64 Create the private header only if it would have something in it, that is,
65 if there are some private data members or protected methods.
66 This is the default.
67 .TP
68 .B --no-private-header
69 Never create a private header file.  If we use any private data members,
70 define the private data structure at the point in the .c source where
71 the class definition begins.
72 .TP
73 .B --m4
74 Preprocess source with m4. Following args will be passed to m4.
75 .TP
76 .B --m4-dir
77 Print directory that will be searched for m4 files.
78 .TP
79 .B -n --no-write
80 Do not write any output files, just check syntax of the input file.
81 .TP
82 .B --no-lines
83 Do not print out the \'#line\' statements into the output.  Useful for debugging
84 the auto-generated generated code.
85 .TP
86 .B --no-self-alias
87 Do not create the Self and SelfClass type aliases and the SELF, IS_SELF
88 and SELF_CLASS macros.
89 .TP
90 .B --no-kill-underscores
91 Do not remove the initial underscore from method names.
92 .TP
93 .B --always-private-struct
94 Always include the private pointer in the public header file.  This is useful
95 for files which are part of a library and you want to reserve the right to add
96 some private data members without breaking binary compatibility.
97 .TP
98 .B -o --output-dir
99 The directory into which output should be placed.
100 .TP
101 .B --file-sep[=c]
102 Replace default \`-\' file name separator.  If no separator character
103 is given then none is used.  Only one character can be used.
104
105 .SH TYPENAMES
106 .PP
107 Because we need to parse out different parts of the typename, sometimes you
108 need to specify the typename with some special syntax.  Types are specified in
109 capitalized form and words are separated by \`:\'.  The first word of the type
110 (which can be empty) is the "namespace".  This fact is for example used for the
111 type checking macro and the type macro.  For "Gtk:New:Button", the macros will
112 be GTK_IS_NEW_BUTTON and GTK_TYPE_NEW_BUTTON.  This colon separated format of
113 typenames is used in the class declaration header and for method argument
114 types.
115
116 .SH OUTPUT FILES
117 .PP
118 The filenames are created from the typename.  The words are
119 separated by \`-\' (this can be changed with
120 \fB--file-sep\fR option) and all in lower case.  For example for an object named
121 "Gtk:New:Button", the files are \fBgtk-new-button.c\fR and
122 \fBgtk-new-button.h\fR.
123 If you are using C++ mode, the output .c file will in fact be a .cc file.
124 If you have any private data members, a private header file will also
125 be created, called \fB<basename>-private.h\fR (for the example above it
126 would be gtk-new-button-private.h).
127 The public header file is created to be human readable and to be used as a
128 reference to the object.  The .c source file is not created as a human
129 readable source and is littered with #line statements, which make the
130 compiler attempt to point you to the right line in your .gob file in
131 case of parsing errors.  The output should not be edited by hand, and
132 you should only edit the .gob file.
133
134 .SH INCLUDING NORMAL C CODE IN THE OUTPUT FILES
135 .PP
136 To include some code directly in the output C file begin with \'%{\'
137 on an empty line and end the code with a \'%}\' on an empty line.  These
138 sections will appear in the output files in the order they are given.
139 There are several other \fIsections\fR to which you can put code.  You can
140 put it in the \'header\' section (which can be abbreviated \'h\') and it will
141 go into the public header file.  You can also put it in the \'privateheader\'
142 section (abbreviated \'ph\') which will make the code go into the private
143 header file.  Sometimes you want some code (other includes) to appear before
144 the extern "C" and the protecting define.  To do this you can put them
145 into the \'headertop\' (or \'ht\') section.  You may wish to include code or
146 comments in all the files, which you can do by putting them into the \'all\'
147 (or \'a\') section.  Similarly, code you wish to appear at the top of all
148 files go in the \'alltop\' (or \'at\') section.  For example:
149 .nf
150
151   %alltop{
152   /* this will be on top of all output files */
153   %}
154
155   %headertop{
156   /* this will be on top of the public header */
157   %}
158
159   %privateheader{
160   /* this will go into the private header file */
161   %}
162
163   %h{
164   /* will be included in the header */
165   void somefunc(int i);
166   %}
167
168   %a{
169   /* will be included in all files */
170   %}
171
172   %{
173   /* will be included in the C file */
174   void somefunc(int i)
175   {
176         /* some code */
177   }
178   %}
179
180 .fi
181
182 .SH INCLUDE FILES
183 .PP
184 Gob will automatically include the class header file at the top of the .c 
185 source file.  If you wish to include it somewhere else, put the include
186 into some %{ %} section above the class definition, and gob will not include
187 it automatically.  This way you can avoid circular includes and control
188 where in the file do you want to include the header.
189 .PP
190 If you made any data members private, gob will also create a source file
191 that will be called \fB<basename>-private.h\fR.  Same rule as above applies
192 for this just as it does for the regular header file.  If you do explicitly
193 include the regular header file, you should always include this private
194 header file below it.  That is, if you use any private data members.  If you
195 don\'t, the private header file automatically includes the public header file,
196 and thus the public header file will be indirectly included at the very top
197 of the file.
198
199 .SH THE CLASS HEADER
200 .PP
201 There can be only one class per input file.  Defining a class
202 is sort of like in Java, you define the class and write inline code
203 directly into the class definition.  To define a class you need to specify
204 the new object name and the name of the object from which it is derived
205 from, such as this "class <new type> from <parent type> { <class code> }".
206 For example:
207 .nf
208
209   class Gtk:New:Button from Gtk:Button {
210           <class code>
211   }
212
213 .fi
214
215 .SH DATA MEMBERS
216 .PP
217 There are five types of data members.  Three of them are normal data members,
218 one is class wide (global) in scope and one is a virtual one, usually linked to
219 a normal data member or a class wide data member.  The three normal data
220 members are public, protected and private.  Public and protected are basically
221 just entries in the object structure, while private has it\'s own dynamically
222 allocated private structure.  Protected members are always put after the public
223 one in the structure and are marked protected in the header file.  There is
224 only one identifier allowed per typename unlike in normal C.  Example:
225 .nf
226
227   public int i;
228   private GtkWidget *h;
229   protected long k;
230
231 .fi
232 .PP
233 Public and protected data members are accessed normally as members of
234 the object struct.  Example where \'i\' is as above a public data member:
235 .nf
236
237   object->i = 1;
238
239 .fi
240 .PP
241 The private data members are defined in a structure which is only available
242 inside the .c file, or by including a private header file.  You must access
243 them using the structure _priv.  Example
244 where \'h\' is the private data member (as in the above example):
245 .nf
246
247   object->_priv->h = NULL;
248
249 .fi
250 The _priv structure is defined in the \fB<basename>-private.h\fR.
251 This file is automatically included if you don\'t include it yourself.  You
252 should always explicitly include it in your .gob file if you explicitly also
253 include the main header file.  The reason it is a separate header file is
254 that you can also include it in other places that need to access this objects
255 private data, such as if you have the majority of functionality of an object
256 in a separate .c file.  Or if a derived object needs to access the protected
257 methods.
258 .PP
259 In case you use the \fB--no-private-header\fR option, no
260 private header file is created and you can only access the _priv pointer
261 below the class definition in the .gob file.
262 .PP
263 Also note that this structure is dynamically allocated, and is freed in the
264 finalize handler.  If you override the finalized handler, your code will be
265 run first and only then will the _priv structure be freed.
266 .PP
267 .B "Classwide data members:"
268 .PP
269 Sometimes you want a datamember to be shared by all objects.  You then need
270 the "classwide" scope keyword.  So for example the following adds a global
271 member foo:
272 .nf
273
274   classwide int foo;
275
276 .fi
277 To access the member you do the standard voodoo of getting the class from the
278 object and casting it to your class pointer.  Thus the following would work:
279 .nf
280
281   SELF_CLASS(GTK_OBJECT(object)->klass)->foo = 20;
282
283 .fi
284 .PP
285 .B "Automatic Initialization:"
286 .PP
287 You can automatically initialize the public private and protected data members
288 without having to add an init method.  The advantage here is that
289 initialization is kept close to the definition of the data member and thus
290 it\'s easier to check.  To do this, just add a \'=\' followed by a number or
291 a token.  It is also possible to include arbitrary C code for more elaborate
292 initializations by putting it all in curly braces.  Note that the curly braces
293 will not be printed into the output, but since gob does not C parsing it needs
294 them to figure out where the C code ends.  The code will be inserted into the
295 init method, above the user defined body.  So for example the following
296 will initialize an integer to -1 and a string with a newly allocated string
297 of "hello".
298 .nf
299
300   public int foo = -1;
301   private char *bar = {g_strdup("hello")};
302
303 .fi
304 .PP
305 .B "Automatic Destruction:"
306 .PP
307 Most data stored as pointers needs to have a function called when the object
308 is finalized to either free the data.  Gob will let you
309 define a function to be called on the data the object is finalized.  This is
310 achieved by putting \'destroywith\' followed by a function name after the
311 variable definition.  It is only called if the data you defined this on
312 is not NULL, so you cans specify functions which do not handle NULL.  It
313 is very much like the GDestroyNotify function used in GTK+ and glib in many
314 places.  Unlike many other places, gob will not enforce any kind of type
315 safety here so be a little bit more careful.  Any function you give it will
316 be called as a "void function(void *)".  It will in fact be cast into such
317 a form before called.  This is to avoid spurious warnings for gtk calls to
318 subclass methods.  The function needs not be of that form exactly, it just has
319 to take one argument which is the pointer to the data.  You should also not
320 define this on any non-pointer data as the results may be undefined.
321 Example:
322 .nf
323
324   public char *foo = {g_strdup("bar")}
325           destroywith g_free;
326
327 .fi
328 Note that the function name you give must be a real function and not macro.
329 Also note that this is always called in the "finalize" method of GObject.
330 It is always called after any user defined body of the finalize handler.
331 .PP
332 Sometimes you may want to run arbitrary code on destruction.  While this can
333 be perfectly well done in the finalize handler.  Depending on the style you
334 may want to include all destruction/initialization code together with the
335 definition of the data member.  Thus you may want to put arbitrary code which
336 will then be inserted into the "finalize" method of GObject.  This can be
337 done with the "destroy" keyword followed by arbitrary code in curly braces.  
338 Inside this code a macro called VAR will be define which refers to your
339 variable.  So for example destroying a GString can be either done with
340 a helper routine or the following code:
341 .nf
342
343   public GString *string = {g_string_new(NULL)}
344           destroy {
345                   if(VAR) g_string_free(VAR, TRUE);
346           };
347
348 .fi
349 The thing to remember with these is that there are many ways to do this
350 and you\'d better be consistent in your code in how you use the above things.
351 Also defining a helper routine that will do the destruction will be a nicer
352 thing to do if that\'s a possibility.  The "destroy" keyword with code does
353 take up more space in the file and it may become more cluttered.
354 .PP
355 The data is zeroed out after being destroyed.  This is to make debugging easier
356 in case your code might try to access an already finalized object.  In case
357 you have overridden the finalize method, your code will be run first and
358 only then will the destructors be called.  You should not however make any
359 assumptions about the order at which the destructors are called.  If you have
360 interdependencies between destructors for different data members, you will
361 have to do this in your own finalize override function.
362 .PP
363 .B "Automatic Unreffing:"
364 .PP
365 This is very much like the automatic destruction, but is instead run in the
366 dispose method (it is among other places called from the "destroy" method of
367 GtkObject).  All data and other objects that you need to unref should be
368 done here, and not at finalize time.  The semantics are otherwise the same
369 as for the "destroywith" and "destroy" keywords, except that you use
370 "unrefwith" and "unref".
371 .nf
372
373   public G:Object *foo = NULL
374           unrefwith g_object_unref;
375   public G:Object *bar = NULL
376           unref {
377                 g_object_unref (VAR);
378           };
379
380 .fi
381
382 .SH GOBJECT PROPERTIES
383 .PP
384 The fourth type of a data member a property type.  It is a named data member
385 which is one of the features of the GObject system.  It just defines a way to
386 get and set some data, but you have to take care of storing that data
387 somewhere.  So it is normal to also have a normal private (or public)
388 data member where you store the real data.
389 You normally need to define a
390 get and a set handler.  They are fragments of C code that will be used to get
391 the value or set the value of the argument.  Inside them you can use the define
392 VAL to which you assign the data or get the data.  You should treat this VAL
393 as a GValue which stores the data of the correct type.
394 You can also use the
395 identifier "self" as pointer to the object instance.  The type is defined as
396 one of the GObject type enums, but without the G_TYPE_ prefix.  There are
397 also some attributes of a property which you can set.  For example the
398 following is a definition of an integer property \'height\' which will
399 be synchronized with a private integer data member also of the name \'height\'.
400 .nf
401
402   private int height;
403   property INT height
404          (nick = _("Short nickname"),
405           blurb = _("Long description"),
406           minimum = 10,
407           maximum = 200,
408           default_value = 100)
409         set { self->_priv->height = g_value_get_int (VAL); }
410         get { g_value_set_int (VAL, self->_priv->height); };
411
412 .fi
413 .PP
414 The attributes are really optional though you should at least set some
415 of them.
416 All property types have a \'nick\' and a \'blurb\' attribute and you should
417 set those accordingly.  This will make runtime querying the object
418 nicer as things such as gui editors and class browsers can be more
419 verbose about the class itself.  You can use the \'_("string")\' notation
420 instead of just "string", and that will mark the string for translation.
421 .PP
422 Almost all types also have a \'default_value\' attribute which sets the initial
423 value of this property (on object initialization, the set handler will be run
424 automatically with this value).  This value will be overriden if the user 
425 sets a value of this property on the call to g_object_new.
426 .PP
427 All the numeric types (including CHAR) have \'minimum\' and \'maximum\'
428 attributes which can restrict the range.  If you do not specify these
429 the range will be the full range that the data type can handle.
430 .PP
431 Types such as UNICHAR and BOOLEAN only have the \'nick\', \'blurb\' and
432 \'default_value\' attributes.
433 .PP
434 The ENUM type has an \'enum_type\' attribute which is the exact
435 type of the enum.  This is so that the property knows which exact
436 type you can set, rather then just knowing it is an enum.  You should
437 always create an enum type specific for the enum itself (see section
438 on the enum types)
439 .PP
440 Similarly FLAGS type has a \'flags_type\' which again you should set to
441 the specific type of this flags data member.
442 .PP
443 There is a STRING type which has only the extra \'default_value\' attribute.
444 .PP
445 The OBJECT type is one of the types that doesn\'t have a \'default_value\' and it
446 only has an \'object_type\' attribute (in addition to nick and blurb of course)
447 that is the exact object type that this property accepts.
448 .PP
449 There is a BOXED type which is a pointer which has a boxed type defined
450 (such that GObject knows how to copy and destroy this pointer).  Here
451 you will need to specify the \'boxed_type\' attribute with the specific
452 type of the boxed pointer.
453 .PP
454 There is also a POINTER type, which has only the \'nick\' and \'blurb\'
455 attributes.  This is for storing arbitrary pointers.  You should be
456 careful with this one, as GObject knows nothing about the data
457 stored at this pointer.  It is somewhat like a \'void *\' type.
458 .PP
459 There is also the PARAM type for storing parameters with a \'param_type\'
460 attribute.
461 .PP
462 You should notice that this list is pretty much like the list of g_param_spec_*
463 functions from gobject/gparamspecs.h, and the attributes are like the
464 arguments of those functions.  Note however that value array is NOT supported
465 yet.
466 .PP
467 You can also specify extra flags, such as CONSTRUCT or CONSTRUCT_ONLY using the
468 \'flags\' attribute.  You can specify multiple flags by oring them together with
469 \'|\'.  These flags correspond to the GParamFlags enumeration except do not
470 include the G_PARAM_ prefix.  So for example to define an enumeration property,
471 which is a CONSTRUCT_ONLY property, we could do the following:
472 .nf
473
474   private SomeEnumerationType foo;
475   property ENUM foo
476          (nick = _("Short nickname"),
477           blurb = _("Long description"),
478           enum_type = Some:Enumeration:Type
479           default_value = SOME_ENUMERATION_VALUE,
480           flags = CONSTRUCT_ONLY,
481           link);
482
483 .fi
484 .PP
485 The above example also gives an example of automatic linking to a standard data
486 memember.  By including the attribute \'link\' a get and set handlers will be
487 automatically added without having to type them by hand.  This is useful for a
488 vast majority data types that are just linked to some standard data member and
489 do not need to do anything extra on get or set.
490 .PP
491 Another extra feature of properties is the possibility of automatically
492 exporing methods to get and set the property.  That is without having to
493 use g_object_set and g_object_get.  This is achieved by adding an
494 \'export\' attribute to the list of property attributes.
495 .PP
496 If you do not define a set or get handler, the property will automatically
497 be only readable or writable as appropriate.
498 .PP
499 Gob2 also creates macros which can be used for type safe access to
500 properties through g_object_set and g_object_get.
501 The macros are called <type>_PROP_<argument name>(x) and
502 <type>_GET_PROP_<argument name>(x).  They define both the string and the
503 value part of the argument.  So for setting an argument of height, one would
504 use (for object type My:Object):
505 .nf
506
507   g_object_set (G_OBJECT (object),
508                 MY_OBJECT_PROP_HEIGHT (7),
509                 NULL);
510
511 .fi
512 And for getting, you would use:
513 .nf
514
515   int height;
516   g_object_get (G_OBJECT (object),
517                 MY_OBJECT_GET_PROP_HEIGHT (&height),
518                 NULL);
519
520 .fi
521 Note however that the type safety only works completely on GNU C compilers.
522 The code will compile on other compilers but with minimal type safety.
523 For complete type safety it is useful to use the get/set methods that
524 are defined by using the \'export\' attribute.
525 .PP
526 To get bettery type safety on some of the property types, you can specify
527 the \'type\' attribute which will add casts where appropriate in code dealing
528 with this property.  This is especially useful for POINTER and OBJECT types.
529 But even for others.
530 .PP
531 You can also override properties from parent objects (that is override their
532 implementation, not their attributes).  Do this by adding the
533 special \'override\' attribute.  For example if the parent object
534 had a \'height\' property then you could override it by
535 .nf
536
537   private int height;
538   property INT height
539          (override)
540         set { self->_priv->height = g_value_get_int (VAL); }
541         get { g_value_set_int (VAL, self->_priv->height); };
542
543 .fi
544 Overriding is supported since gob 2.0.10.
545
546 .SH METHODS
547 .PP
548 There is a whole array of possible methods.  The three normal,
549 "familiar" method types are private, protected and public.  Public are
550 defined as normal functions with a prototype in the header file.  
551 Protected methods are defined as normal methods (which you can call from other
552 files), but their prototype is placed in the private header file.  Private
553 methods
554 are defined as static functions with prototypes at the top of the .c
555 file.  Then there are signal, virtual and override methods.  More on those
556 later.  You can also
557 define init and class_init methods with a special definition if you want
558 to add code to the constructors or you can just leave them out.
559 You can also not define a body for a method, by just using \';\' instead of a
560 body.  This will define an empty function.  You can\'t do this for non-void
561 regular public, private or protected methods, however it is acceptable for
562 non-void virtual, signal and override methods.
563 .PP
564 .B "Function argument lists:"
565 .PP
566 For all but the init and class_init methods, you use the
567 following syntax for arguments.  The first argument can be just "self",
568 which gob will translate into a pointer to the object instance.  The rest
569 of the arguments are very similar to normal C arguments.  If the
570 typename is an object pointer you should use the syntax defined above
571 with the words separated by \':\'
572 .nf
573 <type> <argument id>
574 or
575 <type> <argument id> (check <list of checks>)
576 .fi
577 .PP
578 The checks are glib type preconditions, and can be the following:
579 "null", which tests pointers for being NULL, "type" which checks GTK+
580 object pointers for being the right type, "<test> <number>" which tests
581 numeric arguments for being a certain value.  The test can be a <,>,<=,>=
582 != or ==.  Example:
583 .nf
584   
585   public int
586   foo (self,
587        int h (check > 0 < 11),
588        Gtk:Widget *w (check null type))
589
590 .fi
591 .PP
592 This will be the prototype of a function which has a self pointer
593 as the first argument, an integer argument which will be checked and has
594 to be more then 0 and less then 11, and a pointer to a GtkWidget object
595 instance and it is checked for being null and the type will also be
596 checked.
597 .PP
598 .B "Error return:"
599 .PP
600 Methods which have a return value, there also has to be something
601 returned if there is an error, such as if a precondition is not met.  The
602 default is 0, casted to the type of the method.  If you need to return
603 something else then you can specify an "onerror" keyword after the
604 prototype and after that a number, a token (an identifier) or a bit of C
605 code enclosed in braces {}.  The braces will not be printed into the
606 output, they just delimit the string.  For example:
607 .nf
608
609   public void * get_something (self, int i (check >= 0)) onerror NULL {
610           ...
611   }
612
613 .fi
614 The onerror value is also used in overrides that have a return value, in
615 case there isn\'t a parent method, PARENT_HANDLER will return it.  More about
616 this later.
617 .PP
618 .B "Default return:"
619 .PP
620 Some signal and virtual methods have a return type.  But what happens if
621 there is no default handler and no one connects to a signal.  GOB2 will
622 normally have the wrappers return whatever you specify with onerror or \'0\'
623 if you haven\'t specified anything.  You can also specify a default
624 return value with the keyword \'defreturn\'.  It\'s use is identical to the
625 use of onerror, and you can in fact use both at the same time.  Example
626 .nf
627
628   virtual int get_some_int (self) onerror -1 defreturn 10 ;
629
630 .fi
631 That is an empty virtual method (in C++ terms a pure virtual).  If you never
632 specify any handler for it in the derived children it will just return 10.
633 .PP
634 .B "Constructor methods:"
635 .PP
636 There are two methods that handle the construction of an object, init and
637 class_init.  You define them by just using the init or class_init keyword 
638 with an untyped argument in the argument list.  The argument will be
639 usable in your function as a pointer to your object or class depending if
640 it\'s init or class_init.
641 For example:
642 .nf
643
644   init (self) {
645           /* initialize the object here */
646           self->a = 9;
647           self->b = 9;
648   }
649
650   class_init (class) {
651           /* initialize the class, this is rarely needed */
652           class->blah = NULL;
653   }
654
655 .fi
656 The class_init function is very rarely needed as all standard class
657 initialization is taken care of for you by gob itself.  The init function
658 should on the other hand be used whenever you need to construct or initialize
659 anything in the object to put it into a sane state.
660 .PP
661 .B "Virtual methods:"
662 .PP
663 Virtual methods are basically pointers in the class structure,
664 so that one can override the method in derived methods.  That is to implement
665 the method in a derived class, you must then use an override method (more
666 on those later).
667 They can be empty
668 (if you put \';\' instead of the C code).  A wrapper will also be defined
669 which makes calling the methods he same as public methods.  This type of
670 method is just a little bit "slower" then normal functions, but not as
671 slow as signals.  You define them by using "virtual" keyword before the
672 prototype.  If you put the keyword "private" right after the "virtual"
673 keyword, the wrapper will not be a public method, but a private one.
674 You can do the same with "protected" to make a protected wrapper.
675 .PP
676 .B "Signals:"
677 .PP
678 Signals are methods to which the user can bind other handlers
679 and override the default handler.  The default handler is basically the
680 method body.  This is the most versatile and flexible type of a method
681 and also the slowest.  You need to specify a whole bunch of things when
682 you define a signal.  One thing is when the default handler will be run,
683 first or last.  You specify that by "first" or "last" right after the
684 "signal" keyword.  Then you need to define the GObject enum types (again
685 without the G_TYPE_ prefix).  For that you define the return types
686 and the types of arguments after the "self" pointer (not including the
687 "self" pointer).  You put it in the following syntax "<return type> (<list
688 of arguments>)".  If the return type is void, the type should be "NONE",
689 the same should be for the argument list.  The rest of the prototype is
690 the same as for other method types.  The body can also be empty, and
691 also there is a public method wrapper which you can use for calling the
692 signal just like a public method.  Example:
693 .nf
694
695   signal first INT (POINTER, INT)
696   int do_something (self, Gtk:Widget *w (check null type), int length)
697   {
698           ...
699   }
700   
701 .fi
702 or
703 .nf
704
705   signal last NONE (NONE) void foo (self);
706
707 .fi
708 .PP
709 If you don\'t want the wrapper that emits the signal to be public, you can
710 include the keyword "private" after the "signal" keyword.  This will make
711 the wrapper a normal private method.  You can also make a protected wrapper
712 by using "protected" instead of "private".
713 .PP
714 If you don\'t define a "first" or a "last", the default will be taken as
715 "last".
716 .PP
717 You can also add additional flags.  You do this just like with the argument
718 flags, although this is probably very rare.  These are the G_SIGNAL_* flags,
719 and you can add them without the G_SIGNAL_ prefix into a parenthesis, just
720 after the "signal" keyword.  By default all public signals are G_SIGNAL_ACTION.
721 .PP
722 Also gob2 creates a wrapper macros for typesafe signal connection.  That is
723 you will be warned by the compiler if you pass a callback that is not the
724 correct prototype.  This will again only warn you on gcc, but it will
725 compile without warning on another compiler.  So as with all the typesafety
726 hacks in gob, it is better to test your objects under gcc to get any warnings
727 even if you are using a different compiler in the end.
728 .PP
729 The methods that are created for you are:
730 .nf
731
732   <class_name>_connect__<signal_name> (<object>, <callback>, <data>)
733   <class_name>_connect_after__<signal_name> (<object>, <callback>, <data>)
734   <class_name>_connect_data__<signal_name> (<object>, <callback>, <data>,
735                                             <destroy_notify>, <flags>)
736
737 .fi
738 .PP
739 These three functions correspond to the g_signal_connect,
740 g_signal_connect_after and g_signal_connect_data functions that you would
741 normally use, except they are for a specific signal.  Also do note
742 the two underscores between the method name and the signal name.  For
743 example to connect the signal "foo" on the object "Test:Object" you
744 would do:
745 .nf
746
747   test_object_connect__foo (object, callback, data);
748
749 .fi
750 .PP
751 .B "Override methods:"
752 .PP
753 If you need to override some method (a signal or a virtual method
754 of some class in the parent tree of the new object), you can define and
755 override method.  After the "override" keyword, you should put the
756 typename of the class you are overriding a method from.  Other then that
757 it is the same as for other methods.  The "self" pointer in this case
758 should be the type of the method you are overriding so that you don\'t
759 get warnings during compilation.  Also to call the method of the parent
760 class, you can use the PARENT_HANDLER macro with your arguments.  Example:
761 .nf
762
763   override (Gtk:Container) void
764   add (Gtk:Container *self (check null type), Gtk:Widget *wid (check null type))
765   {
766           /* some code here */
767           PARENT_HANDLER(self, wid);
768   }
769
770 .fi
771 If the function has a return value, then PARENT_HANDLER is an expression that
772 you can use.  It will return whatever the parent handler returned, or the
773 "onerror" expression if there was no parent handler.
774 .PP
775 .B "Method names:"
776 .PP
777 Inside the code, aliases are set for the methods, so that you don\'t
778 have to type the class name before each call, just type \fBself_\fR instead
779 of the name of the class.  So to call a method called \fBblah\fR, you
780 would use the name \fBself_blah\fR.
781 Example:
782 .nf
783
784   private int
785   foo (self)
786   {
787           return self->len;
788   }
789   
790   private int
791   bar (self, int i)
792   {
793           return self_foo (self) + i;
794   }
795
796 .fi
797
798 .SH MAKING NEW OBJECTS
799 .PP
800 You should define a new method which should be a normal public method.  Inside
801 this method, you can use the GET_NEW macro that is defined for you and that
802 will fetch a new object, so a fairly standard new method would look like:
803 .nf
804
805   public GObject *
806   new (void) {
807           GObject *ret = GET_NEW;
808           return G_OBJECT (ret);
809   }
810
811 .fi
812 .PP
813 You should not a subtle peculiarity of the GObject system here.  If there is
814 any code inside the G_OBJECT macro argument, it will get executed multiple
815 times.  This means that things such as G_OBJECT(GET_NEW) would actually create
816 4 objects, leaking 3 of them.  A good rule (as with anywhere in C) is to be
817 careful with all macros.
818
819 .SH SELF REFERENCES
820 .PP
821 .B "Self alias casts:"
822 .PP
823 There are some standard casts defined for you.  Instead of using the full
824 macros inside the .c file, you can use SELF, IS_SELF and SELF_CLASS.  Using
825 these makes it easier to for example change class names around.
826 .PP
827 .B "Self alias types:"
828 .PP
829 There are also the Self and SelfClass types inside
830 your .c file.  These serve the same function as the above, they make it easier
831 to type and easier to change typenames around which can help a lot during
832 prototyping stage.  However you should note that the Self type should not be
833 used in function prototypes as one of the arguments or as a return value type.
834 This is because this is a simple C typedef which is only available inside your
835 \&.c file and not in the header files.  You can disable both the self casting
836 macros and the self type aliases by passing --no-self-alias to gob.
837
838 .SH DEALING WITH DIFFERENT GOB VERSIONS
839 .PP
840 .B "Defines:"
841 .PP
842 In your generated C file, you can use the defines GOB_VERSION_MAJOR
843 GOB_VERSION_MINOR and GOB_VERSION_PATCHLEVEL if you wish to for example
844 use a feature that is only available in some newer gob version.  Note however
845 that you can only use these defines in the C code portions of your .gob file,
846 and #ifdef\'s cannot span multiple functions.  Check the BUGS section
847 for more on using the C preprocessor and gob.
848 .PP
849 .B "Minimum version requires:"
850 .PP
851 You can also make your .gob file require at least certain version of gob.  You
852 do this by putting \'requires x.y.z\' (where x.y.z is the version number)
853 outside of any C block, comment or class, usually you should make this the
854 first line in the file or close to the top.  If gob finds this and the version
855 of gob used to compile the code is lower then that listed in the require, gob
856 will generate an error and exit.  For example to require that gob2 version
857 2.0.0 or higher be used to compile a file, put this at the top of that file:
858 .nf
859
860   requires 2.0.0
861
862 .fi
863
864 .SH CREATING NEW ENUM, FLAGS and ERROR TYPES
865 .PP
866 You can create new GObject ENUM, FLAGS and GError types for use in your
867 classes easily.  Glib includes some utilities for handling these, however
868 it may be cleaner to use the below specified way in your classes.  It also
869 then doesn\'t require any Makefile setup.  Make sure this is defined in the
870 same section as the class, that is not in any of the \'%?{\' \'%}\' sections.
871 .PP
872 You use the keywords \'enum\' \'flags\' and \'error\' as you would use the
873 \'class\' keyword.  Then you give a prefix for the values in the enumeration.
874 Then you define a list of values just like in C.  For \'enum\' types you can
875 also specify the values assigned to each string.  Then you specify the type in
876 the standard gob style of specifying types.  Here are a few examples
877 of all of these:
878 .nf
879
880   enum LAME_CLIENT {
881         IS_CONNECTED,
882         NONE = 9,
883         LAST
884   } Test:Enum;
885
886   flags BUGA_BUGA {
887         ONE,
888         TWO,
889         MANY,
890   } Some:Flags;
891
892   error TEST_OBJECT_ERROR {
893         BAD_THIS,
894         BAD_THAT
895   } Test:Object:Error;
896
897 .fi
898 .PP
899 This will for example define an enum that is equivalent to the following
900 C code:
901 .nf
902
903   typedef enum {
904         LAME_CLIENT_IS_CONNECTED,
905         LAME_CLIENT_NONE = 9,
906         LAME_CLIENT_LAST
907   } TestEnum;
908
909 .fi
910
911 .SH C++ MODE
912 .PP
913 There is a C++ mode so that gob creates C++ compiler friendly files.  You need
914 to use the --for-cpp argument to gob.  This will make the generated file have
915 a .cc instead of a .c extension, and several things will be adjusted to
916 make it all work for a C++ compiler.  One thing that will be missing is an
917 alias to the new method, as that clashes with C++, so instead you\'ll have to
918 use the full name of the method inside your code.  Also note that gob does
919 not use any C++ features, this option will just make the generated code
920 compile with a C++ compiler.
921
922 .SH OVERRIDING THE GET_TYPE METHOD
923 .PP
924 The get_type is not really a method, but a function which initializes your
925 object.  Recently objects appeared which require you to make a custom
926 get_type function.  So it is possible
927 to override this function.  To do so, just define a new public method called
928 get_type, with no arguments.  Example:
929 .nf
930
931   public GType
932   get_type (void)
933   {
934         /* code goes here */
935         return some_type;
936   }
937
938 .fi
939
940 .SH INTERFACES
941 .PP
942 Currently gob will only allow you to implement interfaces (that is, define new
943 classes which implement an interface) and doesn\'t yet have support for making
944 new interfaces, but this will be coming at some point in the future.
945 .PP
946 To define a class that implements an interface add a class flag \'interface\'
947 with the type name of the interface as an argument.  Then to implement
948 a specific method of the interface, just add \'interface <typename>\'
949 before the method definition.  The method can, and probably should be,
950 private.
951 .PP
952 The following example implements a new object, that implements the
953 Gtk:Tree:Model interface and implements the get_flags method of that
954 interface.  Do note that except for standard (GTK+ and glib) specific
955 interfaces which seem
956 to have a non-standard name for the interface structure, the structure
957 should end with and Iface, if you are implementing an interface.  That
958 is for example for the Gtk:Tree:Model, the structure containing the
959 table of methods should be named GtkTreeModelIface.
960 .nf
961   class Some:Object from G:Object
962           (interface Gtk:Tree:Model)
963   {
964           /* function implemented for the Gtk:Tree:Model interface */
965           interface Gtk:Tree:Model
966           private GtkTreeModelFlags
967           get_flags (Gtk:Tree:Model *self (check null type))
968           {
969                 /* Here would be the implementation */
970                 return (GtkTreeModelFlags)0;
971           }
972   }
973
974 .fi
975 .PP
976 If you want to implement multiple interfaces just list more class flag lines
977 as follows:
978 .nf
979
980   class Some:Object from G:Object
981           (interface Gtk:Tree:Model)
982           (interface Gtk:Editable)
983   {
984           /* ... */
985   }
986
987 .fi
988
989 .SH DIRECT BonoboObject SUPPORT
990 .PP
991 If you want to build a BonoboObject class gob2 has direct support for these.
992 Just create a new object that derives from
993 Bonobo:Object.
994 Then use a "BonoboObject" class flag with the interface name as an
995 argument.  The interface name should be as you would type it in C, that is with
996 underscores as namespace separators.  Then you add the methods (using exact
997 same names as in the idl file) and prepend those methods with a BonoboObject
998 keyword.  For example imagine you have an interface GNOME/Foo/SomeInterface,
999 with a method fooBar that takes a single string:
1000 .nf
1001
1002   class Foo:Some:Interface from Bonobo:Object
1003     (BonoboObject GNOME_Foo_SomeInterface) {
1004
1005           BonoboObject
1006           private void
1007           fooBar (PortableServer_Servant servant,
1008                   const CORBA_char *string,
1009                   CORBA_Environment *ev)
1010           {
1011                   Self *self = SELF (bonobo_object_from_servant (servant));
1012
1013                   /* your code here */
1014           }
1015
1016           /* rest of class */
1017   }
1018
1019 .fi
1020 Note that the implementation method can be private, in fact that\'s probably
1021 a good idea to do.  It won\'t work to make this a signal, it can however
1022 be a virtual.  Note that the method prototype must match the one from the
1023 interface header file, or you will get a bad assignment warning.  You should
1024 check the header file generated by orbit-idl and see the epv structure
1025 for the correct prototypes if you can\'t figure them out from the idl itself.
1026 Also note that the first argument is not "self", but the servant and you must
1027 use bonobo_object_from_servant function to get the actual object pointer.
1028
1029 .SH IDENTIFIER CONFLICTS
1030 .PP
1031 Gob will need to define some local variables and functions in the generated
1032 files, so you need to take some precaution not to conflict with these.  The
1033 general rule of thumb is that all of these start with three underscores.  There
1034 is one, "parent_class" which doesn\'t because it\'s intended for use in your
1035 code.  For virtuals or signals, you cannot use the identifier __parent__
1036 which is used for the parent of the object.  You should actually never access
1037 __parent__ either as it not guaranteed that it will stay named this way.
1038 Data members cannot be named __parent__ nor _priv.  For methods, you cannot
1039 use the identifiers "init" or "class_init" unless you mean the constructor
1040 methods.  You shouldn\'t generally use 3 underscores even in override method
1041 argument lists and virtual and signal method names as it might confuse the
1042 PARENT_HANDLER macro.  In fact avoiding all names with three underscores is
1043 the best policy when working with gob.
1044 .PP
1045 There are a couple of defines which you shouldn\'t be redefining in the code
1046 or other headers.  These are SELF, IS_SELF, SELF_CLASS, SELF_TYPE, ARG, VAR,
1047 PARENT_HANDLER, GET_NEW, GOB_VERSION_MAJOR, GOB_VERSION_MINOR and
1048 GOB_VERSION_PATCHLEVEL.
1049 .PP
1050 As for types, there are Self and SelfClass types which are only defined in your
1051 source files.  Their generation (just like the generation of the SELF macros)
1052 can be turned off, see command line options.
1053
1054 .SH USING GTK-DOC STYLE INLINE DOCUMENTATION
1055 .PP
1056 If you want to use gtk-doc style inline documentation for your objects, you
1057 can do one of two things.  First, you could include the inline documentation
1058 comments in your %{ %} section which will then be put verbatim into the
1059 output source file.  This is the way you should use for functions you define
1060 outside of the class.
1061 .PP
1062 For class methods, you should use a gtk+ style comment, however it can be
1063 indented any number of tabs or spaces and you can use the short method name
1064 without the type prefix.  Gob will automatically try to extract these and
1065 translate to full names and put them in the output source file.  An example
1066 would be:
1067 .nf
1068
1069   class Gtk:Button:Example from Gtk:Button {
1070           /**
1071            * new:
1072            *
1073            * Makes a new #GtkButtonExample widget
1074            *
1075            * Returns: a new widget
1076            **/
1077           public
1078           GtkWidget *
1079           new(void)
1080           {
1081                   return (GtkWidget *)GET_NEW;
1082           }
1083   } 
1084
1085 .fi
1086 If the function you are documenting is a signal or a virtual then it will
1087 be documenting the wrapper that starts that virtual function or emits
1088 that signal.
1089
1090 .SH DEALING WITH CIRCULAR HEADERS
1091 .PP
1092 Sometimes you may need to use an object of type MyObjectA in the MyObjectB
1093 class and vice versa.  Obviously you can\'t include headers for both.  So you
1094 need to just declare the typedef in the header of A for B, and the other way
1095 around as well.  The headers generated include a protecting
1096 define before it declares the typedef.  This define is the
1097 __TYPEDEF_<upper case object name>__.  So inside my-object-a.h there will be
1098 this:
1099 .nf
1100
1101   #ifndef __TYPEDEF_MY_OBJECT_A__
1102   #define __TYPEDEF_MY_OBJECT_A__
1103   typedef struct _MyObjectA MyObjectA;
1104   #endif
1105
1106 .fi
1107 Now instead of including my-object-a.h in the header section of
1108 my-object-b.gob, just copy the above code there and you\'re set for using
1109 MyObjectA as a type in the method parameters and public types.
1110 .PP
1111 Another way to get out of this problem is if you can use those types only
1112 in the private members, in which case they won\'t be in the generated public
1113 header.
1114
1115 .SH BUILDING WITH MAKE
1116 .PP
1117 If you are using normal makefiles, what you need to do is to add a generic
1118 rule for .gob files.  So you would include the following in the Makefile
1119 and then just use the .c and .h files as usual (make sure the space
1120 before the \'gob2\' is a tab, not spaces):
1121 .nf
1122
1123   %.c %.h %-private.h: %.gob
1124           gob2 $<
1125
1126 .fi
1127
1128 .SH BUILDING WITH AUTOCONF and AUTOMAKE
1129 .PP
1130 This is a little bit more involved.  Basically the first thing to do is to
1131 check for GOB2 in your configure.in file.  You can use the supplied m4 macro
1132 which will also check the version of gob.  Basically you include this:
1133 .nf
1134
1135   GOB2_CHECK([2.0.0])
1136
1137 .fi
1138 This will replace @GOB2@ in your makefiles with the full path of gob2.  Thus
1139 when adding the generic rule to your Makefile.am file, it should look like:
1140 .nf
1141
1142   %.c %.h %-private.h: %.gob
1143           @GOB2@ $<
1144
1145 .fi
1146 .PP
1147 For Makefile.am you have to set up a couple more things.  First you have to
1148 include the generated .c and .h files into BUILT_SOURCES variable.  You
1149 have to include both the .gob and the .c and .h files in the SOURCES for your
1150 program.
1151
1152 .SH DEBUGGING
1153 .PP
1154 GOB does several things to make debugging the code easier.  First it adds
1155 preprocessor commands into the output c file that point to the correct places
1156 in your .gob input file.  However sometimes there might be some bigger
1157 confusion and this is just not helpful.  In this case you will probably want
1158 to have gcc point you directly at the generated files.  For this use
1159 the --no-lines command line option.  You should also note that these commands
1160 are not generated for the public header file at all.  If there is an error which
1161 points you to the public header file, make sure you fix this error in the .gob
1162 file, otherwise your changes will not have any effect after gob recompiles the
1163 sources again.
1164 .PP
1165 Sometimes you might want to know which method you are in for some debugging
1166 output.  GOB will define __GOB_FUNCTION__ macro, which is just a string constant
1167 with a pretty name of the method.
1168
1169 .SH M4 SUPPORT
1170 .PP
1171 It is possible to have your .gob file also preprocessed by m4.  This is useful
1172 if you have a lot of files and you\'d like to have some preprocessor put in
1173 some common features.  All you have to do is add --m4 to the command line
1174 of gob2 and gob2 will first run your file through m4.  You can print the
1175 directory that is searched for m4 files by running "gob2 --m4-dir"
1176 .PP
1177 All the arguments after --m4 will be passed to m4 itself, so it has to be the
1178 last gob2 argument on the command line.  This way you can specify arbitrary
1179 options to pass to m4.
1180
1181 .SH BUGS
1182 .PP
1183 The lexer does not actually parse the C code, so I\'m sure that some corner
1184 cases or maybe even some not so corner cases of C syntax might confuse gob
1185 completely.  If you find any, send me the source that makes it go gaga and
1186 I\'ll try to make the lexer try to handle it properly, but no promises.
1187 .PP
1188 Another thing is that gob ignores preprocessor macros.  Since gob counts
1189 braces, the following code won\'t work:
1190 .nf
1191
1192   #ifdef SOME_DEFINE
1193   if(foo) {
1194   #else
1195   if(bar) {
1196   #endif
1197           blah();
1198   }
1199
1200 .fi
1201 To make this work, you\'d have to do this:
1202 .nf
1203
1204   #ifdef SOME_DEFINE
1205   if(foo)
1206   #else
1207   if(bar)
1208   #endif
1209   {
1210           blah();
1211   }
1212
1213 .fi
1214 There is no real good way we can handle this without parsing C code, so we
1215 probably never will.  In the future, I might add #if 0 as a comment but
1216 that\'s about as far as I can really take it and even that is problematic.
1217 Basically, if you use gob, just don\'t use the C preprocessor too extensively.
1218 And if you use it make sure that you do not cross the boundaries of the C
1219 code segments.
1220 .PP
1221 Comments will not get through to the generated files unless inside C code.
1222 This is not the case for gtk-doc style comments which are supported.
1223 .PP
1224 The short name aliases are actually implemented as pointers to functions.  Thus
1225 if you want to get the pointer of a function using the short name alias you
1226 can\'t use the \'&\'.  Thus:
1227 .nf
1228
1229   void (*foo)(Self *);
1230
1231   /* this will NOT work */
1232   foo = &self_short_name;
1233
1234   /* this will work */
1235   foo = self_short_name;
1236
1237   /* Both of these will work */
1238   foo = &my_class_long_name;
1239   foo = my_class_long_name;
1240
1241 .fi
1242
1243 .SH AUTHOR
1244 .PP
1245 George Lebl <jirka@5z.com>
1246 .PP
1247 GOB2 Homepage: http://www.jirka.org/gob.html