comparison src/animate.c @ 192:54fdc2a65242

Remove factory from observer APIs. - Save factory that used to new a subject as subject's attribute. - Get factory from subject himself instead of passing as an argument. - It make API of observer more simple and clean.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 18 Nov 2008 21:42:30 +0800
parents 530bb7728546
children 45d9a1e2764d
comparison
equal deleted inserted replaced
191:18f8c3126cdb 192:54fdc2a65242
138 138
139 void mb_progm_free(mb_progm_t *progm) { 139 void mb_progm_free(mb_progm_t *progm) {
140 int n_words; 140 int n_words;
141 mb_word_t *word; 141 mb_word_t *word;
142 mb_action_t *cur_act; 142 mb_action_t *cur_act;
143 #ifndef UNITTEST
144 ob_factory_t *factory;
145 #endif /* UNITTEST */
146 int i; 143 int i;
147 144
148 n_words = progm->n_words; 145 n_words = progm->n_words;
149 for(i = 0; i < n_words; i++) { 146 for(i = 0; i < n_words; i++) {
150 word = progm->words + i; 147 word = progm->words + i;
155 cur_act->free(cur_act); 152 cur_act->free(cur_act);
156 } 153 }
157 } 154 }
158 155
159 #ifndef UNITTEST 156 #ifndef UNITTEST
160 factory = rdman_get_ob_factory(progm->rdman); 157 subject_free(progm->complete);
161 subject_free(factory, progm->complete);
162 #endif /* UNITTEST */ 158 #endif /* UNITTEST */
163 159
164 free(progm); 160 free(progm);
165 } 161 }
166 162
236 void *arg) { 232 void *arg) {
237 mb_progm_t *progm = (mb_progm_t *)arg; 233 mb_progm_t *progm = (mb_progm_t *)arg;
238 #ifndef UNITTEST 234 #ifndef UNITTEST
239 /*! \todo Leverage aspective programming to prevent problem of unittest. 235 /*! \todo Leverage aspective programming to prevent problem of unittest.
240 */ 236 */
241 ob_factory_t *factory;
242 mb_progm_complete_t comp_evt; 237 mb_progm_complete_t comp_evt;
243 #endif /* UNITTEST */ 238 #endif /* UNITTEST */
244 mb_timeval_t next_tmo; 239 mb_timeval_t next_tmo;
245 mb_word_t *word; 240 mb_word_t *word;
246 mb_timer_t *timer; 241 mb_timer_t *timer;
292 mb_progm_step, progm); 287 mb_progm_step, progm);
293 progm->cur_timer = timer; 288 progm->cur_timer = timer;
294 } else { 289 } else {
295 /* Make program to complete. */ 290 /* Make program to complete. */
296 #ifndef UNITTEST 291 #ifndef UNITTEST
297 factory = rdman_get_ob_factory(progm->rdman);
298 comp_evt.event.type = EVT_PROGM_COMPLETE; 292 comp_evt.event.type = EVT_PROGM_COMPLETE;
299 comp_evt.event.tgt = comp_evt.event.cur_tgt = progm->complete; 293 comp_evt.event.tgt = comp_evt.event.cur_tgt = progm->complete;
300 comp_evt.progm = progm; 294 comp_evt.progm = progm;
301 subject_notify(factory, progm->complete, &comp_evt.event); 295 subject_notify(progm->complete, &comp_evt.event);
302 #endif /* UNITTEST */ 296 #endif /* UNITTEST */
303 progm->cur_timer = NULL; 297 progm->cur_timer = NULL;
304 } 298 }
305 } 299 }
306 300
348 342
349 /*! \brief Return event subject for completion of a program. 343 /*! \brief Return event subject for completion of a program.
350 */ 344 */
351 subject_t *mb_progm_get_complete(mb_progm_t *progm) { 345 subject_t *mb_progm_get_complete(mb_progm_t *progm) {
352 return progm->complete; 346 return progm->complete;
347 }
348
349 static void _free_completed_hdlr(event_t *event, void *arg) {
350 mb_progm_t *progm = (mb_progm_t *)arg;
351
352 mb_progm_free(progm);
353 }
354
355 /*! \brief The program should be freed after completed. */
356 void mb_progm_free_completed(mb_progm_t *progm) {
357 subject_t *complete;
358
359 complete = mb_progm_get_complete(progm);
360 subject_add_observer(complete, _free_completed_hdlr, progm);
353 } 361 }
354 362
355 #ifdef UNITTEST 363 #ifdef UNITTEST
356 364
357 #include <CUnit/Basic.h> 365 #include <CUnit/Basic.h>