2 USING: alien.c-types strings help.markup help.syntax
3 alien.syntax sequences io arrays slots.deprecated
4 kernel words slots assocs namespaces ;
7 : ($spec-reader-values) ( slot-spec class -- element )
8 dup ?word-name swap 2array
10 rot slot-spec-type 2array 2array
11 [ { $instance } swap suffix ] assoc-map ;
13 : $spec-reader-values ( slot-spec class -- )
14 ($spec-reader-values) $values ;
16 : $spec-reader-description ( slot-spec class -- )
18 "Outputs the value stored in the " ,
19 { $snippet } rot slot-spec-name suffix ,
21 { $instance } swap suffix ,
23 ] { } make $description ;
25 : $spec-reader ( reader slot-specs class -- )
28 2dup $spec-reader-values
29 2dup $spec-reader-description
32 GENERIC: slot-specs ( help-type -- specs )
34 M: word slot-specs "slots" word-prop ;
36 : $slot-reader ( reader -- )
37 first dup "reading" word-prop [ slot-specs ] keep
40 : $spec-writer-values ( slot-spec class -- )
41 ($spec-reader-values) reverse $values ;
43 : $spec-writer-description ( slot-spec class -- )
45 "Stores a new value to the " ,
46 { $snippet } rot slot-spec-name suffix ,
48 { $instance } swap suffix ,
50 ] { } make $description ;
52 : $spec-writer ( writer slot-specs class -- )
55 2dup $spec-writer-values
56 2dup $spec-writer-description
57 dup ?word-name 1array $side-effects
60 : $slot-writer ( reader -- )
61 first dup "writing" word-prop [ slot-specs ] keep
64 M: string slot-specs c-type struct-type-fields ;
66 M: array ($instance) first ($instance) " array" write ;
68 ARTICLE: "c-structs" "C structure types"
69 "A " { $snippet "struct" } " in C is essentially a block of memory with the value of each structure field stored at a fixed offset from the start of the block. The C library interface provides some utilities to define words which read and write structure fields given a base address."
70 { $subsection POSTPONE: C-STRUCT: }
71 "Great care must be taken when working with C structures since no type or bounds checking is possible."
75 "C-STRUCT: XVisualInfo"
76 " { \"Visual*\" \"visual\" }"
77 " { \"VisualID\" \"visualid\" }"
78 " { \"int\" \"screen\" }"
79 " { \"uint\" \"depth\" }"
80 " { \"int\" \"class\" }"
81 " { \"ulong\" \"red_mask\" }"
82 " { \"ulong\" \"green_mask\" }"
83 " { \"ulong\" \"blue_mask\" }"
84 " { \"int\" \"colormap_size\" }"
85 " { \"int\" \"bits_per_rgb\" } ;"
87 "C structure objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
89 "Arrays of C structures can be created by calling " { $link <c-array> } " or " { $link malloc-array } ". Elements can be read and written using words named " { $snippet { $emphasis "type" } "-nth" } " and " { $snippet "set-" { $emphasis "type" } "-nth" } "; these words are automatically generated by " { $link POSTPONE: C-STRUCT: } "." ;
91 ARTICLE: "c-unions" "C unions"
92 "A " { $snippet "union" } " in C defines a type large enough to hold its largest member. This is usually used to allocate a block of memory which can hold one of several types of values."
93 { $subsection POSTPONE: C-UNION: }
94 "C union objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
96 "Arrays of C unions can be created by calling " { $link <c-array> } " or " { $link malloc-array } ". Elements can be read and written using words named " { $snippet { $emphasis "type" } "-nth" } " and " { $snippet "set-" { $emphasis "type" } "-nth" } "; these words are automatically generated by " { $link POSTPONE: C-UNION: } "." ;