]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/generic.txt
Merge branch 'dev-lib' of ssh://git.ucw.cz/projects/sherlock/GIT/sherlock into dev-lib
[libucw.git] / ucw / doc / generic.txt
1 Generic data structures and algorithms
2 ======================================
3
4 The C preprocessor is a very powerful tool. One handy way to use it
5 can be generating generic data structures and algorithms. Here you can
6 find some conventions that are used in all such generic structures in
7 libUCW, and also hints for use of these structures.
8
9 - <<idea,General idea>>
10 - <<use,How to use them>>
11 - <<implement,How it is implemented>>
12 - Modules with generics
13   * <<growbuf:gbuf,`gbuf.h`>>
14
15 // TODO The module list
16
17 [[idea]]
18 General idea
19 ------------
20
21 The idea is simple. If you have some code, you can customize it a
22 little by preprocessor macros. You can change constants, data types it
23 operates on, whole expressions, or you can compile parts of the code
24 conditionally. You can generate new function names using macros.
25
26 So if you provide few macros for data types, function names and
27 parameters and include some code using them, it gets modified by it
28 and a code for a specific data type is created. Then you can provide
29 new macros and include it again, to get another version of the code,
30 with different function names and types.
31
32 [[use]]
33 How to use them
34 ---------------
35
36 The use is best explained with an example, so we will suppose there
37 is a header file `array.h`, which contains a generic array data type
38 and an indexing function, which returns a pointer to n'th element.
39
40 To get an array of integers, we need to provide macro for used data
41 type and macro that will provide prefixes for identifier names. Then
42 we include the file. Then we could get another array with unsigned
43 integers, so we will do the same:
44
45   #define ARRAY_TYPE int
46   #define ARRAY_PREFIX(name) intarray_##name
47   #include <array.h>
48
49   #define ARRAY_TYPE uns
50   #define ARRAY_PREFIX(name) unsarray_##name
51   #include <array.h>
52
53 This will generate the data types (presumably `intarray_t` and
54 `unsarray_t`) and the index functions (`intarray_index` and
55 `unsarray_index`). We can use them like anything else.
56
57 Maybe the `ARRAY_PREFIX` deserves some attention. When the header file
58 wants to generate an identifier, it uses this macro with
59 some name. Then the macro takes the name, adds a prefix to it and
60 returns the new identifier, so `ARRAY_PREFIX(t)` will generate
61 `intarray_t` in the first case and `unsarray_t` in the second. This
62 allows having more than one instance of the same data structure or
63 algorithm, because it generates different identifiers for them.
64
65 A similar macro is needed for every generic header in libUCW.
66
67 [[implement]]
68 How it is implemented
69 ---------------------
70
71 For those who want to write their own or are just interested, how it
72 works, here is the `array.h` header and some description to it.
73
74   #define ARRAY_A_TYPE ARRAY_PREFIX(t)
75   typedef ARRAY_TYPE *ARRAY_A_TYPE
76
77   static ARRAY_TYPE *ARRAY_PREFIX(index)(ARRAY_A_TYPE array, uns index)
78   {
79     return array + index;
80   }
81
82   #undef ARRAY_A_TYPE
83   #undef ARRAY_TYPE
84   #undef ARRAY_PREFIX
85
86 There are few things that are worth noticing. The first two lines
87 define the data type. The macro (`ARRAY_A_TYPE`) is only for
88 convenience inside the header, since such type names can be used quite
89 often inside the header (if it is large).
90
91 Then there is the function with its name generated (do not get scared
92 by the double parenthesis, ones will be eaten by the macro, the second
93 ones are real function parameters). The function is static, since more
94 than one `.c` file might want to use the same header with the same
95 prefix -- each one generates it's own instance.
96
97 And the end just undefines all the macros, so user may define them
98 again and get another instance of the data structure.
99
100 Also note it is not protected against multiple inclusion in the usual
101 way (eg. `#ifndef ARRAY_H` ...), since multiple inclusion is desired
102 -- it generates multiple versions of the data structure.