Mercurial > MadButterfly
annotate src/observer.c @ 194:45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
- Free members of a coord in rdman_coord_subtree_free().
- Fix core dump issue of subject_notify() that can not free subjects well.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 19 Nov 2008 20:53:40 +0800 |
parents | 54fdc2a65242 |
children | 75ec0124202a |
rev | line source |
---|---|
73 | 1 #include <stdio.h> |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
2 #include "mb_redraw_man.h" |
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
3 #include "mb_observer.h" |
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
4 #include "mb_tools.h" |
73 | 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 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
22 subject->factory = factory; |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
23 |
73 | 24 return subject; |
25 } | |
26 | |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
27 /*! |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
28 * \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
|
29 */ |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
30 void subject_free(subject_t *subject) { |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
31 ob_factory_t *factory = subject->factory; |
73 | 32 observer_t *observer; |
33 | |
126 | 34 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
|
35 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
|
36 /* 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
|
37 * 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
|
38 */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
39 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
|
40 return; |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
41 } |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
77
diff
changeset
|
42 |
73 | 43 while((observer = STAILQ_HEAD(subject->observers))) { |
44 STAILQ_REMOVE(subject->observers, observer_t, next, observer); | |
45 factory->observer_free(factory, observer); | |
46 } | |
47 factory->subject_free(factory, subject); | |
48 } | |
49 | |
50 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
51 void subject_notify(subject_t *subject, event_t *evt) { |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
52 ob_factory_t *factory = subject->factory; |
73 | 53 observer_t *observer; |
194
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
54 subject_t *old_subject; |
73 | 55 |
77 | 56 evt->tgt = subject; |
73 | 57 while(subject) { |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
58 /*! |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
59 * \note What is happend when the subject is freed by observer? |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
60 * Postponding the request of free until notification |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
61 * been finished. (\ref SUBF_BUSY / \ref SUBF_FREE) |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
62 */ |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
63 subject->flags |= SUBF_BUSY; |
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
64 |
77 | 65 evt->cur_tgt = subject->obj; |
73 | 66 for(observer = STAILQ_HEAD(subject->observers); |
67 observer != NULL; | |
68 observer = STAILQ_NEXT(observer_t, next, observer)) { | |
69 observer->hdr(evt, observer->arg); | |
70 } | |
71 | |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
72 subject->flags &= ~SUBF_BUSY; |
194
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
73 |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
74 old_subject = subject; |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
75 subject = factory->get_parent_subject(factory, subject); |
127
d2cc7400c971
Bug of subject_notify() when free subjects.
Thinker K.F. Li <thinker@branda.to>
parents:
126
diff
changeset
|
76 |
194
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
77 if(old_subject->flags & SUBF_FREE) |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
78 subject_free(old_subject); |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
79 |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
80 if(old_subject->flags & SUBF_STOP_PROPAGATE) |
73 | 81 break; |
82 } | |
83 } | |
84 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
85 observer_t *subject_add_observer(subject_t *subject, |
73 | 86 evt_handler hdr, void *arg) { |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
87 ob_factory_t *factory = subject->factory; |
73 | 88 observer_t *observer; |
89 | |
90 observer = factory->observer_alloc(factory); | |
91 if(observer == NULL) | |
92 return NULL; | |
93 observer->hdr = hdr; | |
94 observer->arg = arg; | |
95 | |
96 STAILQ_INS_TAIL(subject->observers, observer_t, next, observer); | |
97 | |
98 return observer; | |
99 } | |
100 | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
101 void subject_remove_observer(subject_t *subject, |
73 | 102 observer_t *observer) { |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
103 ob_factory_t *factory = subject->factory; |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
104 |
73 | 105 STAILQ_REMOVE(subject->observers, observer_t, next, observer); |
106 factory->observer_free(factory, observer); | |
107 } | |
108 | |
109 #ifdef UNITTEST | |
110 | |
111 #include <CUnit/Basic.h> | |
112 #include <stdlib.h> | |
113 | |
114 static subject_t *test_subject_alloc(ob_factory_t *factory) { | |
115 subject_t *subject; | |
116 | |
117 subject = (subject_t *)malloc(sizeof(subject_t)); | |
118 return subject; | |
119 } | |
120 | |
121 static void test_subject_free(ob_factory_t *factory, subject_t *subject) { | |
122 free(subject); | |
123 } | |
124 | |
125 static observer_t *test_observer_alloc(ob_factory_t *factory) { | |
126 observer_t *observer; | |
127 | |
128 observer = (observer_t *)malloc(sizeof(observer_t)); | |
129 return observer; | |
130 } | |
131 | |
132 static void test_observer_free(ob_factory_t *factory, observer_t *observer) { | |
133 free(observer); | |
134 } | |
135 | |
136 static subject_t *test_get_parent_subject(ob_factory_t *factory, | |
137 subject_t *subject) { | |
138 return NULL; | |
139 } | |
140 | |
141 static ob_factory_t test_factory = { | |
142 test_subject_alloc, | |
143 test_subject_free, | |
144 test_observer_alloc, | |
145 test_observer_free, | |
146 test_get_parent_subject | |
147 }; | |
148 | |
149 static void handler(event_t *evt, void *arg) { | |
150 int *cnt = (int *)arg; | |
151 | |
74 | 152 CU_ASSERT(evt->type == EVT_MOUSE_OUT); |
73 | 153 (*cnt)++; |
154 } | |
155 | |
156 void test_observer(void) { | |
157 subject_t *subject; | |
74 | 158 observer_t *observer[2]; |
73 | 159 event_t evt; |
160 int cnt = 0; | |
161 | |
162 subject = subject_new(&test_factory, NULL, 0); | |
163 subject->flags |= SUBF_STOP_PROPAGATE; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
164 observer[0] = subject_add_observer(subject, handler, &cnt); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
165 observer[1] = subject_add_observer(subject, handler, &cnt); |
73 | 166 |
74 | 167 evt.type = EVT_MOUSE_OUT; |
73 | 168 evt.tgt = NULL; |
169 evt.cur_tgt = NULL; | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
170 subject_notify(subject, &evt); |
74 | 171 CU_ASSERT(cnt == 2); |
73 | 172 |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
173 subject_remove_observer(subject, observer[0]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
174 subject_remove_observer(subject, observer[1]); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
175 subject_free(subject); |
73 | 176 } |
177 | |
178 CU_pSuite get_observer_suite(void) { | |
179 CU_pSuite suite; | |
180 | |
181 suite = CU_add_suite("Suite_observer", NULL, NULL); | |
182 CU_ADD_TEST(suite, test_observer); | |
183 | |
184 return suite; | |
185 } | |
186 | |
187 #endif /* UNITTEST */ |