]> gitweb.factorcode.org Git - factor.git/blob - core/alien/structs/structs-docs.factor
baf0b40707ff34fd7b30d937262a54d21cf2d85c
[factor.git] / core / alien / structs / structs-docs.factor
1 IN: alien.structs
2 USING: alien.c-types strings help.markup help.syntax
3 alien.syntax sequences io arrays slots.deprecated
4 kernel words slots assocs namespaces ;
5
6 ! Deprecated code
7 : ($spec-reader-values) ( slot-spec class -- element )
8     dup ?word-name swap 2array
9     over slot-spec-name
10     rot slot-spec-type 2array 2array
11     [ { $instance } swap suffix ] assoc-map ;
12
13 : $spec-reader-values ( slot-spec class -- )
14     ($spec-reader-values) $values ;
15
16 : $spec-reader-description ( slot-spec class -- )
17     [
18         "Outputs the value stored in the " ,
19         { $snippet } rot slot-spec-name suffix ,
20         " slot of " ,
21         { $instance } swap suffix ,
22         " instance." ,
23     ] { } make $description ;
24
25 : $spec-reader ( reader slot-specs class -- )
26     >r slot-of-reader r>
27     over [
28         2dup $spec-reader-values
29         2dup $spec-reader-description
30     ] when 2drop ;
31
32 GENERIC: slot-specs ( help-type -- specs )
33
34 M: word slot-specs "slots" word-prop ;
35
36 : $slot-reader ( reader -- )
37     first dup "reading" word-prop [ slot-specs ] keep
38     $spec-reader ;
39
40 : $spec-writer-values ( slot-spec class -- )
41     ($spec-reader-values) reverse $values ;
42
43 : $spec-writer-description ( slot-spec class -- )
44     [
45         "Stores a new value to the " ,
46         { $snippet } rot slot-spec-name suffix ,
47         " slot of " ,
48         { $instance } swap suffix ,
49         " instance." ,
50     ] { } make $description ;
51
52 : $spec-writer ( writer slot-specs class -- )
53     >r slot-of-writer r>
54     over [
55         2dup $spec-writer-values
56         2dup $spec-writer-description
57         dup ?word-name 1array $side-effects
58     ] when 2drop ;
59
60 : $slot-writer ( reader -- )
61     first dup "writing" word-prop [ slot-specs ] keep
62     $spec-writer ;
63
64 M: string slot-specs c-type struct-type-fields ;
65
66 M: array ($instance) first ($instance) " array" write ;
67
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."
72 $nl
73 "An example:"
74 { $code
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\" } ;"
86 }
87 "C structure objects can be allocated by calling " { $link <c-object> } " or " { $link malloc-object } "."
88 $nl
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: } "." ;
90
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 } "."
95 $nl
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: } "." ;