1 #include <glib.h>
2 #include <glib-object.h>
3
4 #ifdef G_OS_UNIX
5 #include <unistd.h>
6 #endif
7
8 #define G_TYPE_TEST (my_test_get_type ())
9 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
10 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
11 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
12 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
13 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
14
15 enum {
16 PROP_0,
17 PROP_DUMMY
18 };
19
20 typedef struct _GTest GTest;
21 typedef struct _GTestClass GTestClass;
22
23 struct _GTest
24 {
25 GObject object;
26
27 gint dummy;
28 };
29
30 struct _GTestClass
31 {
32 GObjectClass parent_class;
33 };
34
35 static GType my_test_get_type (void);
36
37 static void my_test_class_init (GTestClass * klass);
38 static void my_test_init (GTest * test);
39 static void my_test_dispose (GObject * object);
40 static void my_test_get_property (GObject *object,
41 guint prop_id,
42 GValue *value,
43 GParamSpec *pspec);
44 static void my_test_set_property (GObject *object,
45 guint prop_id,
46 const GValue *value,
47 GParamSpec *pspec);
48
49 static GObjectClass *parent_class = NULL;
50
51 static GType
52 my_test_get_type (void)
53 {
54 static GType test_type = 0;
55
56 if (!test_type) {
57 const GTypeInfo test_info = {
58 sizeof (GTestClass),
59 NULL,
60 NULL,
61 (GClassInitFunc) my_test_class_init,
62 NULL,
63 NULL,
64 sizeof (GTest),
65 0,
66 (GInstanceInitFunc) my_test_init,
67 NULL
68 };
69
70 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
71 &test_info, 0);
72 }
73 return test_type;
74 }
75
76 static void
77 my_test_class_init (GTestClass * klass)
78 {
79 GObjectClass *gobject_class;
80
81 gobject_class = (GObjectClass *) klass;
82
83 parent_class = g_type_class_ref (G_TYPE_OBJECT);
84
85 gobject_class->dispose = my_test_dispose;
86 gobject_class->get_property = my_test_get_property;
87 gobject_class->set_property = my_test_set_property;
88
89 g_object_class_install_property (gobject_class,
90 PROP_DUMMY,
91 g_param_spec_int ("dummy",
92 NULL,
93 NULL,
94 0, G_MAXINT, 0,
95 G_PARAM_READWRITE));
96 }
97
98 static void
99 my_test_init (GTest * test)
100 {
101 g_test_message ("init %p\n", test);
102 }
103
104 static void
105 my_test_dispose (GObject * object)
106 {
107 GTest *test;
108
109 test = MY_TEST (object);
110
111 g_test_message ("dispose %p!\n", test);
112
113 G_OBJECT_CLASS (parent_class)->dispose (object);
114 }
115
116 static void
117 my_test_get_property (GObject *object,
118 guint prop_id,
119 GValue *value,
120 GParamSpec *pspec)
121 {
122 GTest *test;
123
124 test = MY_TEST (object);
125
126 switch (prop_id)
127 {
128 case PROP_DUMMY:
129 g_value_set_int (value, test->dummy);
130 break;
131 default:
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 break;
134 }
135 }
136
137 static void
138 my_test_set_property (GObject *object,
139 guint prop_id,
140 const GValue *value,
141 GParamSpec *pspec)
142 {
143 GTest *test;
144
145 test = MY_TEST (object);
146
147 switch (prop_id)
148 {
149 case PROP_DUMMY:
150 test->dummy = g_value_get_int (value);
151 break;
152 default:
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 break;
155 }
156 }
157
158 static gint count = 0;
159
160 static void
161 dummy_notify (GObject *object,
162 GParamSpec *pspec)
163 {
164 count++;
165 if (count % 10000 == 0)
166 g_test_message (".");
167 }
168
169 static void
170 my_test_do_property (GTest * test)
171 {
172 gint dummy;
173
174 g_object_get (test, "dummy", &dummy, NULL);
175 g_object_set (test, "dummy", dummy + 1, NULL);
176 }
177
178 static void
179 test_refcount_properties_2 (void)
180 {
181 gint i;
182 GTest *test;
183
184 test = g_object_new (G_TYPE_TEST, NULL);
185
186 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
187 g_assert_cmpint (count, ==, test->dummy);
188
189 for (i = 0; i < 1000000; i++)
190 {
191 my_test_do_property (test);
192 }
193 g_assert_cmpint (count, ==, test->dummy);
194
195 g_object_unref (test);
196 }
197
198 int
199 main (int argc, gchar *argv[])
200 {
201 g_log_set_always_fatal (G_LOG_LEVEL_WARNING |
202 G_LOG_LEVEL_CRITICAL |
203 g_log_set_always_fatal (G_LOG_FATAL_MASK));
204
205 g_test_init (&argc, &argv, NULL);
206
207 g_test_add_func ("/gobject/refcount/properties-2", test_refcount_properties_2);
208
209 return g_test_run ();
210 }