0: void hello_world()
1: {
2: printf ("Hello World\n");
3: }
First we note that this function has 0 arguments and 0 return values, so we can use the PDB_PROC_0_0 macro to set it up. I'll write the code and then explain what I am doing:
0: PDB_PROC_0_0
1: ( hello_world,
2: "hello-world",
3: "Prints \"Hello World\"." )
4: {
5: printf ("Hello World\n");
6: }
Line 0 uses the PDB_PROC_X_Y macro to set the function up (X is the number of arguments, and Y is the number of return values.)
Line 1 is the name of the c function.
Line 2 is the name of the procedural database function. Dashes and underscores can be used pretty much interchangeably. When the database is exported to a language binding, that binding will change the hyphens or dashes to something it can grok.
Line 3 is the "docstring". This should be a short sentence that describes the function.
Since the above example doesn't take any arguments or return any values, there's not much more to it, so lets move on to something a little more complex:
0: PDB_PROC_2_1
1: ( advanced_hello_world,
2: "advanced-hello-world",
3: "Prints hello to the specified world, N times. Return N+1.",
4: world, N,
5: PDBstr, PDBint,
6: N_1,
7: PDBint )
8: {
9: PDB_ASSERT_2 (advanced_hello_world, world, N);
10: char * world_c = gh_scm2newstr (world);
11: int N_c = gh_scm2int (N);
12: for (int i=0; i<N_c; i++)
13: printf("%s\n", world_c);
14: free (world_c);
15: return gh_int2scm(N_c + 1);
16: }
Line 0 specifies the PDB_PROC_2_1 macro (2 arguments, 1 return value).
Lines 1-3 serve the same purpose as before.
Line 4 is the list of arguments. In this case "world" and "N". All PDB function arguments are SCM objects. The PDB_PROC_X_Y macro generates a function that looks like this for the compiler:
SCM advanced_hello_world (SCM world, SCM N) {
Line 5 is a list of types for each SCM argument. In this example, "world" is of type PDBstr (a string), and "N" is of type PDBint (an integer).
Line 6 is a list of the return values, in this case "N_1".
Line 7 is a list of the return value types. N_1 is of the PDBint.
So in summary, within the c/c++ environment all procedural database arguments and return values are SCM objects. SCM objects can be of different types, and it up to the programmer to do the type checking. But this is done easily enough with the PDB_ASSERT_X (function, arg0, .. argX) macro as seen in line 9. This macros assures that world really is a string (PDBstr) and N really is an integer (PDBint).
Lines 10 - 11 demonstrate how we convert scheme objects to c / c++ types. These are the most common conversion functions provided by Guile:
int gh_scm2int(SCM); SCM gh_int2scm(int); int gh_scm2long(SCM); SCM gh_long2scm(int); double gh_scm2double(SCM); SCM gh_double2scm(double);
Lines 12-13 do the actual work of this function.
Line 14 frees the space allocated for the string, world_c.
Line 15 adds one to N_c and returns the value as N_1.
Modified on Tue Jun 29 14:17:51 1999 by James Dean Palmer.