1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors generic kernel math sequences arrays assocs
4 alarms calendar math.order ;
7 TUPLE: model < identity-tuple
8 value connections dependencies ref locked? ;
10 : new-model ( value class -- model )
13 V{ } clone >>connections
14 V{ } clone >>dependencies
17 : <model> ( value -- model )
20 M: model hashcode* drop model hashcode* ;
22 : add-dependency ( dep model -- )
25 : remove-dependency ( dep model -- )
26 dependencies>> delete ;
30 GENERIC: model-activated ( model -- )
32 M: model model-activated drop ;
34 : ref-model ( model -- n )
35 [ 1+ ] change-ref ref>> ;
37 : unref-model ( model -- n )
38 [ 1- ] change-ref ref>> ;
40 : activate-model ( model -- )
43 [ dup activate-model dupd add-connection ] each
49 DEFER: remove-connection
51 : deactivate-model ( model -- )
52 dup unref-model zero? [
54 [ dup deactivate-model remove-connection ] with each
59 GENERIC: model-changed ( model observer -- )
61 : add-connection ( observer model -- )
62 dup connections>> empty? [ dup activate-model ] when
65 : remove-connection ( observer model -- )
66 [ connections>> delete ] keep
67 dup connections>> empty? [ dup deactivate-model ] when
70 : with-locked-model ( model quot -- )
74 f >>locked? drop ; inline
76 GENERIC: update-model ( model -- )
78 M: model update-model drop ;
80 : notify-connections ( model -- )
81 dup connections>> [ model-changed ] with each ;
83 : set-model ( value model -- )
89 [ update-model ] [ notify-connections ] bi
93 : ((change-model)) ( model quot -- newvalue model )
94 over [ [ value>> ] dip call ] dip ; inline
96 : change-model ( model quot -- )
97 ((change-model)) set-model ; inline
99 : (change-model) ( model quot -- )
100 ((change-model)) (>>value) ; inline
102 GENERIC: range-value ( model -- value )
103 GENERIC: range-page-value ( model -- value )
104 GENERIC: range-min-value ( model -- value )
105 GENERIC: range-max-value ( model -- value )
106 GENERIC: range-max-value* ( model -- value )
107 GENERIC: set-range-value ( value model -- )
108 GENERIC: set-range-page-value ( value model -- )
109 GENERIC: set-range-min-value ( value model -- )
110 GENERIC: set-range-max-value ( value model -- )
112 : clamp-value ( value range -- newvalue )
113 [ range-min-value max ] keep
114 range-max-value* min ;