Improve the RcBox and ArcBox documentation
[glib.git] / tests / refcount / properties4.c
blobd4bca9486523958c6bba76c44459eb3023f589b6
1 #include <glib.h>
2 #include <glib-object.h>
4 #define MY_TYPE_BADGER (my_badger_get_type ())
5 #define MY_BADGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MY_TYPE_BADGER, MyBadger))
6 #define MY_IS_BADGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MY_TYPE_BADGER))
7 #define MY_BADGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MY_TYPE_BADGER, MyBadgerClass))
8 #define MY_IS_BADGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MY_TYPE_BADGER))
9 #define MY_BADGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MY_TYPE_BADGER, MyBadgerClass))
11 enum {
12 PROP_0,
13 PROP_MAMA
16 typedef struct _MyBadger MyBadger;
17 typedef struct _MyBadgerClass MyBadgerClass;
19 struct _MyBadger
21 GObject parent_instance;
23 MyBadger * mama;
24 guint mama_notify_count;
27 struct _MyBadgerClass
29 GObjectClass parent_class;
32 static GType my_badger_get_type (void);
33 G_DEFINE_TYPE (MyBadger, my_badger, G_TYPE_OBJECT)
35 static void my_badger_dispose (GObject * object);
37 static void my_badger_get_property (GObject *object,
38 guint prop_id,
39 GValue *value,
40 GParamSpec *pspec);
41 static void my_badger_set_property (GObject *object,
42 guint prop_id,
43 const GValue *value,
44 GParamSpec *pspec);
46 static void my_badger_mama_notify (GObject *object,
47 GParamSpec *pspec);
49 static void
50 my_badger_class_init (MyBadgerClass * klass)
52 GObjectClass *gobject_class;
54 gobject_class = (GObjectClass *) klass;
56 gobject_class->dispose = my_badger_dispose;
58 gobject_class->get_property = my_badger_get_property;
59 gobject_class->set_property = my_badger_set_property;
61 g_object_class_install_property (gobject_class,
62 PROP_MAMA,
63 g_param_spec_object ("mama",
64 NULL,
65 NULL,
66 MY_TYPE_BADGER,
67 G_PARAM_READWRITE));
70 static void
71 my_badger_init (MyBadger * self)
73 g_signal_connect (self, "notify::mama", G_CALLBACK (my_badger_mama_notify),
74 NULL);
77 static void
78 my_badger_dispose (GObject * object)
80 MyBadger * self;
82 self = MY_BADGER (object);
84 if (self->mama != NULL)
86 g_object_unref (self->mama);
87 self->mama = NULL;
90 G_OBJECT_CLASS (my_badger_parent_class)->dispose (object);
93 static void
94 my_badger_get_property (GObject *object,
95 guint prop_id,
96 GValue *value,
97 GParamSpec *pspec)
99 MyBadger *self;
101 self = MY_BADGER (object);
103 switch (prop_id)
105 case PROP_MAMA:
106 g_value_set_object (value, self->mama);
107 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 break;
114 static void
115 my_badger_set_property (GObject *object,
116 guint prop_id,
117 const GValue *value,
118 GParamSpec *pspec)
120 MyBadger *self;
122 self = MY_BADGER (object);
124 switch (prop_id)
126 case PROP_MAMA:
127 if (self->mama)
128 g_object_unref (self->mama);
129 self->mama = g_value_dup_object (value);
130 if (self->mama)
131 g_object_set (self->mama, "mama", NULL, NULL); /* another notify */
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 break;
139 static void
140 my_badger_mama_notify (GObject *object,
141 GParamSpec *pspec)
143 MyBadger *self;
145 self = MY_BADGER (object);
147 self->mama_notify_count++;
151 main (int argc, char **argv)
153 MyBadger * badger1, * badger2;
154 gpointer test;
156 g_print ("START: %s\n", argv[0]);
157 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
159 badger1 = g_object_new (MY_TYPE_BADGER, NULL);
160 badger2 = g_object_new (MY_TYPE_BADGER, NULL);
162 g_object_set (badger1, "mama", badger2, NULL);
163 g_assert_cmpuint (badger1->mama_notify_count, ==, 1);
164 g_assert_cmpuint (badger2->mama_notify_count, ==, 1);
165 g_object_get (badger1, "mama", &test, NULL);
166 g_assert (test == badger2);
167 g_object_unref (test);
169 g_object_unref (badger1);
170 g_object_unref (badger2);
172 return 0;