]> gitweb.factorcode.org Git - factor.git/blob - basis/delegate/delegate.factor
Merge remote-tracking branch 'Blei/gtk-image-loader'
[factor.git] / basis / delegate / delegate.factor
1 ! Copyright (C) 2007, 2008 Daniel Ehrenberg
2 ! Portions copyright (C) 2009, 2010 Slava Pestov, Joe Groff
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs classes.tuple definitions effects generic
5 generic.standard hashtables kernel lexer math parser
6 generic.parser sequences sets slots words words.symbol fry
7 compiler.units make ;
8 IN: delegate
9
10 ERROR: broadcast-words-must-have-no-outputs group ;
11
12 <PRIVATE
13
14 : protocol-words ( protocol -- words )
15     \ protocol-words word-prop ;
16
17 : protocol-consult ( protocol -- consulters )
18     \ protocol-consult word-prop ;
19
20 GENERIC: group-words ( group -- words )
21
22 M: standard-generic group-words
23     dup "combination" word-prop #>> 2array 1array ;
24
25 : slot-words, ( slot-spec -- )
26     [ name>> reader-word 0 2array , ]
27     [
28         dup read-only>> [ drop ] [
29             name>> writer-word 0 2array ,
30         ] if
31     ] bi ;
32
33 : slot-group-words ( slots -- words )
34     [ [ slot-words, ] each ] { } make ;
35
36 M: tuple-class group-words
37     all-slots slot-group-words ;
38
39 : check-broadcast-group ( group -- group )
40     dup group-words [ first stack-effect out>> empty? ] all?
41     [ broadcast-words-must-have-no-outputs ] unless ;
42
43 ! Consultation
44
45 TUPLE: consultation group class quot loc ;
46 TUPLE: broadcast < consultation ;
47
48 : <consultation> ( group class quot -- consultation )
49     f consultation boa ; 
50 : <broadcast> ( group class quot -- consultation )
51     [ check-broadcast-group ] 2dip f broadcast boa ; 
52
53 : create-consult-method ( word consultation -- method )
54     [ class>> swap first create-method dup fake-definition ] keep
55     [ drop ] [ "consultation" set-word-prop ] 2bi ;
56
57 PREDICATE: consult-method < method "consultation" word-prop ;
58
59 M: consult-method reset-word
60     [ call-next-method ] [ f "consultation" set-word-prop ] bi ;
61
62 GENERIC# (consult-method-quot) 2 ( consultation quot word -- object )
63
64 M: consultation (consult-method-quot)
65     '[ _ call _ execute ] nip ;
66 M: broadcast (consult-method-quot)
67     '[ _ call [ _ execute ] each ] nip ;
68
69 : consult-method-quot ( consultation word -- object )
70     [ dup quot>> ] dip
71     [ second [ [ dip ] curry ] times ] [ first ] bi
72     (consult-method-quot) ;
73
74 : consult-method ( word consultation -- )
75     [ create-consult-method ]
76     [ swap consult-method-quot ] 2bi
77     define ;
78
79 : change-word-prop ( word prop quot -- )
80     [ swap props>> ] dip change-at ; inline
81
82 : each-generic ( consultation quot -- )
83     [ [ group>> group-words ] keep ] dip curry each ; inline
84
85 : register-consult ( consultation -- )
86     [ group>> \ protocol-consult ] [ ] [ class>> ] tri
87     '[ [ _ _ ] dip ?set-at ] change-word-prop ;
88
89 : consult-methods ( consultation -- )
90     [ consult-method ] each-generic ;
91
92 : unregister-consult ( consultation -- )
93     [ class>> ] [ group>> ] bi
94     \ protocol-consult word-prop delete-at ;
95
96 : unconsult-method ( word consultation -- )
97     [ class>> swap first method ] keep
98     over [
99         over "consultation" word-prop eq?
100         [ forget ] [ drop ] if
101     ] [ 2drop ] if ;
102
103 : unconsult-methods ( consultation -- )
104     [ unconsult-method ] each-generic ;
105
106 PRIVATE>
107
108 : define-consult ( consultation -- )
109     [ register-consult ] [ consult-methods ] bi ;
110
111 SYNTAX: CONSULT:
112     scan-word scan-word parse-definition <consultation>
113     [ save-location ] [ define-consult ] bi ;
114
115 SYNTAX: BROADCAST:
116     scan-word scan-word parse-definition <broadcast>
117     [ save-location ] [ define-consult ] bi ;
118
119 M: consultation where loc>> ;
120
121 M: consultation set-where loc<< ;
122
123 M: consultation forget*
124     [ unconsult-methods ] [ unregister-consult ] bi ;
125
126 ! Protocols
127 <PRIVATE
128
129 : forget-all-methods ( classes words -- )
130     [ first method forget ] cartesian-each ;
131
132 : protocol-users ( protocol -- users )
133     protocol-consult keys ;
134
135 : lost-words ( protocol wordlist -- lost-words )
136     [ protocol-words ] dip diff ;
137
138 : forget-old-definitions ( protocol new-wordlist -- )
139     [ drop protocol-users ] [ lost-words ] 2bi
140     forget-all-methods ;
141
142 : added-words ( protocol wordlist -- added-words )
143     swap protocol-words diff ;
144
145 : add-new-definitions ( protocol wordlist -- )
146     [ drop protocol-consult values ] [ added-words ] 2bi
147     [ swap consult-method ] cartesian-each ;
148
149 : initialize-protocol-props ( protocol wordlist -- )
150     [
151         drop \ protocol-consult
152         [ H{ } assoc-like ] change-word-prop
153     ] [ { } like \ protocol-words set-word-prop ] 2bi ;
154
155 : fill-in-depth ( wordlist -- wordlist' )
156     [ dup word? [ 0 2array ] when ] map ;
157
158 : show-words ( wordlist' -- wordlist )
159     [ dup second zero? [ first ] when ] map ;
160
161 PRIVATE>
162
163 : define-protocol ( protocol wordlist -- )
164     [ drop define-symbol ] [
165         fill-in-depth
166         [ forget-old-definitions ]
167         [ add-new-definitions ]
168         [ initialize-protocol-props ] 2tri
169     ] 2bi ;
170
171 SYNTAX: PROTOCOL:
172     CREATE-WORD parse-definition define-protocol ;
173
174 PREDICATE: protocol < word protocol-words ; ! Subclass of symbol?
175
176 M: protocol forget*
177     [ f forget-old-definitions ] [ call-next-method ] bi ;
178
179
180 M: protocol definition protocol-words show-words ;
181
182 M: protocol definer drop \ PROTOCOL: \ ; ;
183
184 M: protocol group-words protocol-words ;
185
186 SYNTAX: SLOT-PROTOCOL:
187     CREATE-WORD ";"
188     [ [ reader-word ] [ writer-word ] bi 2array ]
189     map-tokens concat define-protocol ;