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