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-class 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 : slot-of-reader ( reader specs -- spec/f )
26 [ slot-spec-reader eq? ] with find nip ;
28 : $spec-reader ( reader slot-specs class -- )
31 2dup $spec-reader-values
32 2dup $spec-reader-description
35 GENERIC: slot-specs ( help-type -- specs )
37 M: word slot-specs "slots" word-prop ;
39 : $slot-reader ( reader -- )
40 first dup "reading" word-prop [ slot-specs ] keep
43 : $spec-writer-values ( slot-spec class -- )
44 ($spec-reader-values) reverse $values ;
46 : $spec-writer-description ( slot-spec class -- )
48 "Stores a new value to the " ,
49 { $snippet } rot slot-spec-name suffix ,
51 { $instance } swap suffix ,
53 ] { } make $description ;
55 : slot-of-writer ( writer specs -- spec/f )
56 [ slot-spec-writer eq? ] with find nip ;
58 : $spec-writer ( writer slot-specs class -- )
61 2dup $spec-writer-values
62 2dup $spec-writer-description
63 dup ?word-name 1array $side-effects
66 : $slot-writer ( reader -- )
67 first dup "writing" word-prop [ slot-specs ] keep
70 M: string slot-specs c-type struct-type-fields ;
72 M: array ($instance) first ($instance) " array" write ;
74 ARTICLE: "c-structs" "C structure types"
75 "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."
76 { $subsection POSTPONE: C-STRUCT: }
77 "Great care must be taken when working with C structures since no type or bounds checking is possible."
81 "C-STRUCT: XVisualInfo"
82 " { \"Visual*\" \"visual\" }"
83 " { \"VisualID\" \"visualid\" }"
84 " { \"int\" \"screen\" }"
85 " { \"uint\" \"depth\" }"
86 " { \"int\" \"class\" }"
87 " { \"ulong\" \"red_mask\" }"
88 " { \"ulong\" \"green_mask\" }"
89 " { \"ulong\" \"blue_mask\" }"
90 " { \"int\" \"colormap_size\" }"
91 " { \"int\" \"bits_per_rgb\" } ;"
93 "C structure objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
95 "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: } "." ;
97 ARTICLE: "c-unions" "C unions"
98 "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."
99 { $subsection POSTPONE: C-UNION: }
100 "C union objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
102 "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: } "." ;