]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct-docs.factor
move classes.struct from extra to basis
[factor.git] / basis / classes / struct / struct-docs.factor
1 ! (c)Joe Groff bsd license
2 USING: alien classes help.markup help.syntax kernel libc
3 quotations slots ;
4 IN: classes.struct
5
6 HELP: <struct-boa>
7 { $values
8     { "class" class }
9 }
10 { $description "This macro implements " { $link boa } " for " { $link struct } " classes. A struct of the given class is constructed, and its slots are initialized using values off the top of the datastack." } ;
11
12 HELP: <struct>
13 { $values
14     { "class" class }
15     { "struct" struct }
16 }
17 { $description "Allocates garbage-collected heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are initialized with the initial values specified in the struct definition." } ;
18
19 { <struct> <struct-boa> malloc-struct memory>struct } related-words
20
21 HELP: STRUCT:
22 { $syntax "STRUCT: class { slot type } { slot type } ... ;" }
23 { $values { "class" "a new " { $link struct } " class to define" } { "slots" "a list of slot specifiers" } }
24 { $description "Defines a new " { $link struct } " type. The syntax is nearly identical to " { $link POSTPONE: TUPLE: } "; however, there are some additional restrictions on struct types:"
25 { $list
26 { "Struct classes cannot have a superclass defined." }
27 { "The slots of a struct must all have a type declared. The type must be a C type." } 
28 { { $link read-only } " slots on structs are not enforced, though they may be declared." }
29 } } ;
30
31 HELP: S{
32 { $syntax "S{ class slots... }" }
33 { $values { "class" "a " { $link struct } " class word" } { "slots" "slot values" } }
34 { $description "Marks the beginning of a literal struct. The syntax is identical to tuple literal syntax with " { $link POSTPONE: T{ } { $snippet " }" } "; either the assoc syntax (that is, " { $snippet "S{ class { slot value } { slot value } ... }" } ") or the simple syntax (" { $snippet "S{ class f value value ... }" } ") can be used." } ;
35
36 HELP: UNION-STRUCT:
37 { $syntax "UNION-STRUCT: class { slot type } { slot type } ... ;" }
38 { $values { "class" "a new " { $link struct } " class to define" } { "slots" "a list of slot specifiers" } }
39 { $description "Defines a new " { $link struct } " type where all of the slots share the same storage. See " { $link POSTPONE: STRUCT: } " for details on the syntax." } ;
40
41 HELP: define-struct-class
42 { $values
43     { "class" class } { "slots" "a sequence of " { $link slot-spec } "s" }
44 }
45 { $description "Defines a new " { $link struct } " class. This is the runtime equivalent of the " { $link POSTPONE: STRUCT: } " syntax." } ;
46
47 HELP: define-union-struct-class
48 { $values
49     { "class" class } { "slots" "a sequence of " { $link slot-spec } "s" }
50 }
51 { $description "Defines a new " { $link struct } " class where all of the slots share the same storage. This is the runtime equivalent of the " { $link POSTPONE: UNION-STRUCT: } " syntax." } ;
52
53 HELP: malloc-struct
54 { $values
55     { "class" class }
56     { "struct" struct }
57 }
58 { $description "Allocates unmanaged C heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are left uninitialized. The struct should be " { $link free } "d when it is no longer needed." } ;
59
60 HELP: memory>struct
61 { $values
62     { "ptr" c-ptr } { "class" class }
63     { "struct" struct }
64 }
65 { $description "Constructs a new " { $link struct } " of the specified " { $snippet "class" } " at the memory location referenced by " { $snippet "ptr" } ". The referenced memory is unchanged." } ;
66
67 HELP: struct
68 { $class-description "The parent class of all struct types." } ;
69
70 { struct POSTPONE: STRUCT: POSTPONE: UNION-STRUCT: } related-words
71
72 HELP: struct-class
73 { $class-description "The metaclass of all " { $link struct } " classes." } ;
74
75 ARTICLE: "classes.struct" "Struct classes"
76 { $link struct } " classes are similar to " { $link tuple } "s, but their slots exhibit value semantics, and they are backed by a contiguous structured block of memory. Structs can be used for structured access to C memory or Factor byte arrays and for passing struct values in and out of the FFI. Struct types are defined using a syntax similar to tuple syntax:"
77 { $subsection POSTPONE: STRUCT: }
78 "Structs can be allocated with " { $link new } "- and " { $link boa } "-like constructor words. Additional words are provided for building structs from C memory and from existing buffers:"
79 { $subsection <struct> }
80 { $subsection <struct-boa> }
81 { $subsection malloc-struct }
82 { $subsection memory>struct }
83 "Structs have literal syntax like tuples:"
84 { $subsection POSTPONE: S{ }
85 "Union structs are also supported, which behave like structs but share the same memory for all the type's slots."
86 { $subsection POSTPONE: UNION-STRUCT: }
87 ;
88
89 ABOUT: "classes.struct"