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