Mercurial > MadButterfly
annotate src/observer.c @ 126:55f2c6402c81
-
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 16 Sep 2008 15:44:01 +0800 |
parents | 1c1f28c124c9 |
children | d2cc7400c971 |
rev | line source |
---|---|
73 | 1 #include <stdio.h> |
2 #include "redraw_man.h" | |
3 #include "observer.h" | |
4 #include "tools.h" | |
5 | |
126 | 6 #ifndef ASSERT |
7 #define ASSERT(x) | |
8 #endif | |
9 | |
73 | 10 subject_t *subject_new(ob_factory_t *factory, void *obj, int obj_type) { |
11 subject_t *subject; | |
12 | |
13 subject = factory->subject_alloc(factory); | |
14 if(subject == NULL) | |
15 return NULL; | |
16 | |
17 subject->obj = obj; | |
18 subject->obj_type = obj_type; | |
19 subject->flags = 0; | |
20 STAILQ_INIT(subject->observers); | |
21 | |
22 return subject; | |
23 } | |
24 | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
25 /*! |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
26 * \todo Keep ob_factory following subject objects. |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
27 */ |
73 | 28 void subject_free(ob_factory_t *factory, subject_t *subject) { |
29 observer_t *observer; | |
30 | |
126 | 31 ASSERT(!(subject->flags & SUBF_FREE)); |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
32 if(subject->flags & SUBF_BUSY) { |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
33 /* Postpond the request until busy status been stoped. |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
34 * SUBF_BUSY means in subject_notify(). |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
35 */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
36 subject->flags |= SUBF_FREE; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
37 return; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
38 } |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
39 |
73 | 40 while((observer = STAILQ_HEAD(subject->observers))) { |
41 STAILQ_REMOVE(subject->observers, observer_t, next, observer); | |
42 factory->observer_free(factory, observer); | |
43 } | |
44 factory->subject_free(factory, subject); | |
45 } | |
46 | |
47 | |
48 void subject_notify(ob_factory_t *factory, subject_t *subject, event_t *evt) { | |
49 observer_t *observer; | |
50 | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
51 /*! |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
52 * \note What is happend when the subject is freed by observer? |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
53 * Postponding the request of free until notification |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
54 * been finished. (\ref SUBF_BUSY / \ref SUBF_FREE) |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
55 */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
56 subject->flags |= SUBF_BUSY; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
57 |
77 | 58 evt->tgt = subject; |
73 | 59 while(subject) { |
77 | 60 evt->cur_tgt = subject->obj; |
73 | 61 for(observer = STAILQ_HEAD(subject->observers); |
62 observer != NULL; | |
63 observer = STAILQ_NEXT(observer_t, next, observer)) { | |
64 observer->hdr(evt, observer->arg); | |
65 } | |
66 | |
67 if(subject->flags & SUBF_STOP_PROPAGATE) | |
68 break; | |
69 | |
70 subject = factory->get_parent_subject(factory, subject); | |
71 } | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
72 |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
73 subject->flags &= ~SUBF_BUSY; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
74 if(subject->flags & SUBF_FREE) |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
75 subject_free(factory, subject); |
73 | 76 } |
77 | |
78 observer_t *subject_add_observer(ob_factory_t *factory, | |
79 subject_t *subject, | |
80 evt_handler hdr, void *arg) { | |
81 observer_t *observer; | |
82 | |
83 observer = factory->observer_alloc(factory); | |
84 if(observer == NULL) | |
85 return NULL; | |
86 observer->hdr = hdr; | |
87 observer->arg = arg; | |
88 | |
89 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer); | |
90 | |
91 return observer; | |
92 } | |
93 | |
94 void subject_remove_observer(ob_factory_t *factory, | |
95 subject_t *subject, | |
96 observer_t *observer) { | |
97 STAILQ_REMOVE(subject->observers, observer_t, next, observer); | |
98 factory->observer_free(factory, observer); | |
99 } | |
100 | |
101 #ifdef UNITTEST | |
102 | |
103 #include <CUnit/Basic.h> | |
104 #include <stdlib.h> | |
105 | |
106 static subject_t *test_subject_alloc(ob_factory_t *factory) { | |
107 subject_t *subject; | |
108 | |
109 subject = (subject_t *)malloc(sizeof(subject_t)); | |
110 return subject; | |
111 } | |
112 | |
113 static void test_subject_free(ob_factory_t *factory, subject_t *subject) { | |
114 free(subject); | |
115 } | |
116 | |
117 static observer_t *test_observer_alloc(ob_factory_t *factory) { | |
118 observer_t *observer; | |
119 | |
120 observer = (observer_t *)malloc(sizeof(observer_t)); | |
121 return observer; | |
122 } | |
123 | |
124 static void test_observer_free(ob_factory_t *factory, observer_t *observer) { | |
125 free(observer); | |
126 } | |
127 | |
128 static subject_t *test_get_parent_subject(ob_factory_t *factory, | |
129 subject_t *subject) { | |
130 return NULL; | |
131 } | |
132 | |
133 static ob_factory_t test_factory = { | |
134 test_subject_alloc, | |
135 test_subject_free, | |
136 test_observer_alloc, | |
137 test_observer_free, | |
138 test_get_parent_subject | |
139 }; | |
140 | |
141 static void handler(event_t *evt, void *arg) { | |
142 int *cnt = (int *)arg; | |
143 | |
74 | 144 CU_ASSERT(evt->type == EVT_MOUSE_OUT); |
73 | 145 (*cnt)++; |
146 } | |
147 | |
148 void test_observer(void) { | |
149 subject_t *subject; | |
74 | 150 observer_t *observer[2]; |
73 | 151 event_t evt; |
152 int cnt = 0; | |
153 | |
154 subject = subject_new(&test_factory, NULL, 0); | |
155 subject->flags |= SUBF_STOP_PROPAGATE; | |
74 | 156 observer[0] = subject_add_observer(&test_factory, subject, |
157 handler, &cnt); | |
158 observer[1] = subject_add_observer(&test_factory, subject, | |
159 handler, &cnt); | |
73 | 160 |
74 | 161 evt.type = EVT_MOUSE_OUT; |
73 | 162 evt.tgt = NULL; |
163 evt.cur_tgt = NULL; | |
164 subject_notify(&test_factory, subject, &evt); | |
74 | 165 CU_ASSERT(cnt == 2); |
73 | 166 |
74 | 167 subject_remove_observer(&test_factory, subject, observer[0]); |
168 subject_remove_observer(&test_factory, subject, observer[1]); | |
73 | 169 subject_free(&test_factory, subject); |
170 } | |
171 | |
172 CU_pSuite get_observer_suite(void) { | |
173 CU_pSuite suite; | |
174 | |
175 suite = CU_add_suite("Suite_observer", NULL, NULL); | |
176 CU_ADD_TEST(suite, test_observer); | |
177 | |
178 return suite; | |
179 } | |
180 | |
181 #endif /* UNITTEST */ |