1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.prettyprint arrays
3 assocs classes classes.struct combinators combinators.short-circuit
4 continuations fry kernel libc make math math.parser mirrors
5 prettyprint.backend prettyprint.custom prettyprint.sections
6 see.private sequences slots strings summary words ;
7 IN: classes.struct.prettyprint
11 : struct-definer-word ( class -- word )
12 struct-slots dup length 2 >=
13 [ second offset>> 0 = \ UNION-STRUCT: \ STRUCT: ? ]
14 [ drop \ STRUCT: ] if ;
16 : struct>assoc ( struct -- assoc )
17 [ class struct-slots ] [ struct-slot-values ] bi zip ;
19 : pprint-struct-slot ( slot -- )
23 [ type>> pprint-c-type ]
24 [ read-only>> [ \ read-only pprint-word ] when ]
25 [ initial>> [ \ initial: pprint-word pprint* ] when* ]
27 \ } pprint-word block> ;
29 : pprint-struct ( struct -- )
33 [ struct>assoc [ [ name>> ] dip ] assoc-map ] bi
37 : pprint-struct-pointer ( struct -- )
38 \ S@ [ [ class pprint-word ] [ >c-ptr pprint* ] bi ] pprint-prefix ;
42 M: struct-class see-class*
43 <colon dup struct-definer-word pprint-word dup pprint-word
44 <block struct-slots [ pprint-struct-slot ] each
45 block> pprint-; block> ;
47 M: struct pprint-delims
50 M: struct >pprint-sequence
51 [ class ] [ struct-slot-values ] bi class-slot-sequence ;
55 [ pprint-struct-pointer ] pprint-c-object ;
65 TUPLE: struct-mirror { object read-only } ;
66 C: <struct-mirror> struct-mirror
68 : get-struct-slot ( struct slot -- value present? )
69 over class struct-slots slot-named
70 [ name>> reader-word execute( struct -- value ) t ]
72 : set-struct-slot ( value struct slot -- )
73 over class struct-slots slot-named
74 [ name>> writer-word execute( value struct -- ) ]
76 : reset-struct-slot ( struct slot -- )
77 over class struct-slots slot-named
78 [ [ initial>> swap ] [ name>> writer-word ] bi execute( value struct -- ) ]
80 : reset-struct-slots ( struct -- )
81 dup class struct-prototype
82 dup byte-length memcpy ;
86 { [ over "underlying" = ] [ nip >c-ptr t ] }
87 { [ over { [ array? ] [ length 1 >= ] } 1&& ] [ swap first get-struct-slot ] }
91 M: struct-mirror set-at
93 { [ over "underlying" = ] [ 3drop ] }
94 { [ over array? ] [ swap first set-struct-slot ] }
98 M: struct-mirror delete-at
100 { [ over "underlying" = ] [ 2drop ] }
101 { [ over array? ] [ swap first reset-struct-slot ] }
105 M: struct-mirror clear-assoc
106 object>> reset-struct-slots ;
108 M: struct-mirror >alist ( mirror -- alist )
110 [ drop "underlying" ] [ >c-ptr ] bi 2array 1array
114 [ [ [ name>> ] [ type>> ] bi 2array ] dip ] assoc-map
115 ] [ drop { } ] recover
118 M: struct make-mirror <struct-mirror> ;
120 INSTANCE: struct-mirror assoc