/* -*-c-*- ---------------- mvc.h : * This header defines basic types supporting the MVC pattern * ------------------------------------------------------------------ * Last change: Time-stamp: <01/02/13 02:16:08 jose> * ------------------------------------------------------------------ * Copyright (C) 2001 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #ifndef MVC_H #define MVC_H #include /* the subject which is observed (the model) */ typedef struct subject_t subject_t; /* subject's observer (the view) */ typedef struct observer_t observer_t; /* list of observers (maintained by the subject) */ typedef GSList observer_list_t; /* data used in callbacks */ typedef void data_t; /* (virtual) function used by subject on each observer to notify an update */ typedef void (*update_callback_t)(observer_t *obs, data_t *data); /* (virtual) function used by observers to notify subject of a change */ typedef void (*notify_callback_t)(subject_t *subj, data_t *data); /* base subject type, containing a notify callback to be invoked by observers */ struct subject_t { notify_callback_t notify; /* notify callback used by observers */ observer_list_t *observers; /* the list of current observers */ }; /* base observer type, containing an update callback (to be invoked by the subject) and a pointer to the observed subject */ struct observer_t { update_callback_t update; /* update callback used by subject */ subject_t *subject; /* observed subject */ }; /* subject interface */ /* init a subject */ #define subject_init(sub) \ do { \ (sub)->notify = NULL; \ (sub)->observers = NULL; \ } while (FALSE) /* clean up a subject */ extern void subject_clean (subject_t *sub); /* add a new observer */ extern void subject_add_observer (subject_t *sub, observer_t *obs); /* remove an observer */ extern void subject_remove_observer (subject_t *sub, observer_t *obs); /* notify all observers of a change */ extern void subject_update_observers (subject_t *sub, data_t *data); /* observer interface */ /* init an observer */ #define observer_init(obs) \ do { \ (obs)->update = NULL; \ (obs)->subject = NULL; \ } while (FALSE) /* macro used by observers to notify their subject of a change */ #define observer_notify_subject(obs, data) \ (*((observer_t *)obs->subject->notify)((observer_t *)obs->subject, \ (data_t *)data)) #endif /* MVC_H */