summaryrefslogtreecommitdiffhomepage
path: root/mixmvc/mvc.h
blob: 6371a27c8e0c0900dbca06776f3a6fc52b7416eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*-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 <glib.h>

/* 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 */