| 
									
										
										
										
											2008-02-21 20:14:50 -05:00
										 |  |  | ! Copyright (C) 2006, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-04 18:58:37 -04:00
										 |  |  | USING: accessors generic kernel math sequences arrays assocs | 
					
						
							| 
									
										
										
										
											2010-05-21 21:48:33 -04:00
										 |  |  | calendar math.order continuations fry ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: models | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-02 22:27:49 -04:00
										 |  |  | TUPLE: model < identity-tuple | 
					
						
							|  |  |  | value connections dependencies ref locked? ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-04 18:58:37 -04:00
										 |  |  | : new-model ( value class -- model )
 | 
					
						
							|  |  |  |     new
 | 
					
						
							|  |  |  |         swap >>value | 
					
						
							|  |  |  |         V{ } clone >>connections | 
					
						
							|  |  |  |         V{ } clone >>dependencies | 
					
						
							|  |  |  |         0 >>ref ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : <model> ( value -- model )
 | 
					
						
							| 
									
										
										
										
											2008-07-04 18:58:37 -04:00
										 |  |  |     model new-model ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-dependency ( dep model -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |     dependencies>> push ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : remove-dependency ( dep model -- )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 00:25:35 -04:00
										 |  |  |     dependencies>> remove! drop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFER: add-connection | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: model-activated ( model -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: model model-activated drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ref-model ( model -- n )
 | 
					
						
							| 
									
										
										
										
											2014-11-30 01:37:59 -05:00
										 |  |  |     [ 1 + dup ] change-ref drop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : unref-model ( model -- n )
 | 
					
						
							| 
									
										
										
										
											2014-11-30 01:37:59 -05:00
										 |  |  |     [ 1 - dup ] change-ref drop ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : activate-model ( model -- )
 | 
					
						
							|  |  |  |     dup ref-model 1 = [ | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |         dup dependencies>> | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         [ dup activate-model dupd add-connection ] each
 | 
					
						
							|  |  |  |         model-activated | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: remove-connection | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deactivate-model ( model -- )
 | 
					
						
							|  |  |  |     dup unref-model zero? [ | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |         dup dependencies>> | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |         [ dup deactivate-model remove-connection ] with each
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  | GENERIC: model-changed ( model observer -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-connection ( observer model -- )
 | 
					
						
							| 
									
										
										
										
											2014-11-30 01:37:59 -05:00
										 |  |  |     dup connections>> | 
					
						
							|  |  |  |     [ empty? [ activate-model ] [ drop ] if ] | 
					
						
							|  |  |  |     [ push ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : remove-connection ( observer model -- )
 | 
					
						
							| 
									
										
										
										
											2014-11-30 01:37:59 -05:00
										 |  |  |     [ connections>> remove! ] keep swap
 | 
					
						
							|  |  |  |     empty? [ deactivate-model ] [ drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  | : with-locked-model ( model quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-26 03:59:29 -05:00
										 |  |  |     [ '[ _ t >>locked? @ ] ] | 
					
						
							| 
									
										
										
										
											2014-11-30 01:37:59 -05:00
										 |  |  |     [ drop '[ f _ locked?<< ] ] | 
					
						
							| 
									
										
										
										
											2009-02-26 03:59:29 -05:00
										 |  |  |     2bi [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  | GENERIC: update-model ( model -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: model update-model drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | : notify-connections ( model -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |     dup connections>> [ model-changed ] with each ;
 | 
					
						
							| 
									
										
										
										
											2008-01-09 04:52:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  | : set-model ( value model -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |     dup locked?>> [ | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  |         2drop
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2009-02-26 03:59:29 -05:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-08-29 17:03:37 -04:00
										 |  |  |             swap >>value | 
					
						
							|  |  |  |             [ update-model ] [ notify-connections ] bi
 | 
					
						
							| 
									
										
										
										
											2007-11-16 03:01:45 -05:00
										 |  |  |         ] with-locked-model | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-14 18:40:55 -04:00
										 |  |  | : ?set-model ( value model -- )
 | 
					
						
							|  |  |  |     2dup value>> = [ 2drop ] [ set-model ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 06:10:47 -04:00
										 |  |  | : call-change-model ( model quot -- newvalue model )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |     over [ [ value>> ] dip call ] dip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-23 23:07:19 -04:00
										 |  |  | : change-model ( ..a model quot: ( ..a obj -- ..b newobj ) -- ..b )
 | 
					
						
							| 
									
										
										
										
											2016-03-25 06:10:47 -04:00
										 |  |  |     call-change-model set-model ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-23 23:07:19 -04:00
										 |  |  | : (change-model) ( ..a model quot: ( ..a obj -- ..b newobj ) -- ..b )
 | 
					
						
							| 
									
										
										
										
											2016-03-25 06:10:47 -04:00
										 |  |  |     call-change-model value<< ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: range-value ( model -- value )
 | 
					
						
							|  |  |  | GENERIC: range-page-value ( model -- value )
 | 
					
						
							|  |  |  | GENERIC: range-min-value ( model -- value )
 | 
					
						
							|  |  |  | GENERIC: range-max-value ( model -- value )
 | 
					
						
							|  |  |  | GENERIC: range-max-value* ( model -- value )
 | 
					
						
							|  |  |  | GENERIC: set-range-value ( value model -- )
 | 
					
						
							|  |  |  | GENERIC: set-range-page-value ( value model -- )
 | 
					
						
							|  |  |  | GENERIC: set-range-min-value ( value model -- )
 | 
					
						
							|  |  |  | GENERIC: set-range-max-value ( value model -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : clamp-value ( value range -- newvalue )
 | 
					
						
							| 
									
										
										
										
											2009-05-24 22:35:50 -04:00
										 |  |  |     [ range-min-value ] [ range-max-value* ] bi clamp ;
 | 
					
						
							| 
									
										
										
										
											2010-06-23 23:07:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : change-model* ( ..a model quot: ( ..a obj -- ..b ) -- ..b )
 | 
					
						
							|  |  |  |     '[ _ keep ] change-model ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : push-model ( value model -- )
 | 
					
						
							|  |  |  |     [ push ] change-model* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : pop-model ( model -- value )
 | 
					
						
							|  |  |  |     [ pop ] change-model* ;
 |