1 | /* We are testing some deprecated APIs here */ |
2 | #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS |
3 | #define GLIB_DISABLE_DEPRECATION_WARNINGS |
4 | #endif |
5 | |
6 | #include <glib-object.h> |
7 | |
8 | typedef struct { |
9 | GObject parent_instance; |
10 | } TestObject; |
11 | |
12 | typedef struct { |
13 | int dummy_0; |
14 | float dummy_1; |
15 | } TestObjectPrivate; |
16 | |
17 | typedef struct { |
18 | GObjectClass parent_class; |
19 | } TestObjectClass; |
20 | |
21 | GType test_object_get_type (void); |
22 | |
23 | G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT, |
24 | G_ADD_PRIVATE (TestObject)) |
25 | |
26 | static void |
27 | test_object_class_init (TestObjectClass *klass) |
28 | { |
29 | } |
30 | |
31 | static void |
32 | test_object_init (TestObject *self) |
33 | { |
34 | TestObjectPrivate *priv = test_object_get_instance_private (self); |
35 | |
36 | if (g_test_verbose ()) |
37 | g_printerr (format: "Offset of %sPrivate for type '%s': %d\n" , |
38 | G_OBJECT_TYPE_NAME (self), |
39 | G_OBJECT_TYPE_NAME (self), |
40 | TestObject_private_offset); |
41 | |
42 | priv->dummy_0 = 42; |
43 | priv->dummy_1 = 3.14159f; |
44 | } |
45 | |
46 | static int |
47 | test_object_get_dummy_0 (TestObject *self) |
48 | { |
49 | TestObjectPrivate *priv = test_object_get_instance_private (self); |
50 | |
51 | return priv->dummy_0; |
52 | } |
53 | |
54 | static float |
55 | test_object_get_dummy_1 (TestObject *self) |
56 | { |
57 | TestObjectPrivate *priv = test_object_get_instance_private (self); |
58 | |
59 | return priv->dummy_1; |
60 | } |
61 | |
62 | typedef struct { |
63 | TestObject parent_instance; |
64 | } TestDerived; |
65 | |
66 | typedef struct { |
67 | char *dummy_2; |
68 | } TestDerivedPrivate; |
69 | |
70 | typedef struct { |
71 | TestObjectClass parent_class; |
72 | } TestDerivedClass; |
73 | |
74 | GType test_derived_get_type (void); |
75 | |
76 | G_DEFINE_TYPE_WITH_CODE (TestDerived, test_derived, test_object_get_type (), |
77 | G_ADD_PRIVATE (TestDerived)) |
78 | |
79 | static void |
80 | test_derived_finalize (GObject *obj) |
81 | { |
82 | TestDerivedPrivate *priv = test_derived_get_instance_private (self: (TestDerived *) obj); |
83 | |
84 | g_free (mem: priv->dummy_2); |
85 | |
86 | G_OBJECT_CLASS (test_derived_parent_class)->finalize (obj); |
87 | } |
88 | |
89 | static void |
90 | test_derived_class_init (TestDerivedClass *klass) |
91 | { |
92 | G_OBJECT_CLASS (klass)->finalize = test_derived_finalize; |
93 | } |
94 | |
95 | static void |
96 | test_derived_init (TestDerived *self) |
97 | { |
98 | TestDerivedPrivate *priv = test_derived_get_instance_private (self); |
99 | |
100 | if (g_test_verbose ()) |
101 | g_printerr (format: "Offset of %sPrivate for type '%s': %d\n" , |
102 | G_OBJECT_TYPE_NAME (self), |
103 | G_OBJECT_TYPE_NAME (self), |
104 | TestDerived_private_offset); |
105 | |
106 | priv->dummy_2 = g_strdup (str: "Hello" ); |
107 | } |
108 | |
109 | static const char * |
110 | test_derived_get_dummy_2 (TestDerived *self) |
111 | { |
112 | TestDerivedPrivate *priv = test_derived_get_instance_private (self); |
113 | |
114 | return priv->dummy_2; |
115 | } |
116 | |
117 | typedef struct { |
118 | TestObject parent_instance; |
119 | } TestMixed; |
120 | |
121 | typedef struct { |
122 | gint dummy_3; |
123 | } TestMixedPrivate; |
124 | |
125 | typedef struct { |
126 | TestObjectClass parent_class; |
127 | } TestMixedClass; |
128 | |
129 | GType test_mixed_get_type (void); |
130 | |
131 | G_DEFINE_TYPE (TestMixed, test_mixed, test_object_get_type ()) |
132 | |
133 | static void |
134 | test_mixed_class_init (TestMixedClass *klass) |
135 | { |
136 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
137 | g_type_class_add_private (g_class: klass, private_size: sizeof (TestMixedPrivate)); |
138 | G_GNUC_END_IGNORE_DEPRECATIONS |
139 | } |
140 | |
141 | static void |
142 | test_mixed_init (TestMixed *self) |
143 | { |
144 | TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate); |
145 | |
146 | if (g_test_verbose ()) |
147 | g_printerr (format: "Offset of %sPrivate for type '%s': %d\n" , |
148 | G_OBJECT_TYPE_NAME (self), |
149 | G_OBJECT_TYPE_NAME (self), |
150 | TestMixed_private_offset); |
151 | |
152 | priv->dummy_3 = 47; |
153 | } |
154 | |
155 | static gint |
156 | test_mixed_get_dummy_3 (TestMixed *self) |
157 | { |
158 | TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate); |
159 | |
160 | return priv->dummy_3; |
161 | } |
162 | |
163 | typedef struct { |
164 | TestMixed parent_instance; |
165 | } TestMixedDerived; |
166 | |
167 | typedef struct { |
168 | gint64 dummy_4; |
169 | } TestMixedDerivedPrivate; |
170 | |
171 | typedef struct { |
172 | TestMixedClass parent_class; |
173 | } TestMixedDerivedClass; |
174 | |
175 | GType test_mixed_derived_get_type (void); |
176 | |
177 | G_DEFINE_TYPE_WITH_CODE (TestMixedDerived, test_mixed_derived, test_mixed_get_type (), |
178 | G_ADD_PRIVATE (TestMixedDerived)) |
179 | |
180 | static void |
181 | test_mixed_derived_class_init (TestMixedDerivedClass *klass) |
182 | { |
183 | } |
184 | |
185 | static void |
186 | test_mixed_derived_init (TestMixedDerived *self) |
187 | { |
188 | TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self); |
189 | |
190 | if (g_test_verbose ()) |
191 | g_printerr (format: "Offset of %sPrivate for type '%s': %d\n" , |
192 | G_OBJECT_TYPE_NAME (self), |
193 | G_OBJECT_TYPE_NAME (self), |
194 | TestMixedDerived_private_offset); |
195 | |
196 | priv->dummy_4 = g_get_monotonic_time (); |
197 | } |
198 | |
199 | static gint64 |
200 | test_mixed_derived_get_dummy_4 (TestMixedDerived *self) |
201 | { |
202 | TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self); |
203 | |
204 | return priv->dummy_4; |
205 | } |
206 | |
207 | static void |
208 | private_instance (void) |
209 | { |
210 | TestObject *obj = g_object_new (object_type: test_object_get_type (), NULL); |
211 | gpointer class; |
212 | gint offset; |
213 | |
214 | g_assert_cmpint (test_object_get_dummy_0 (obj), ==, 42); |
215 | g_assert_cmpfloat (test_object_get_dummy_1 (obj), ==, 3.14159f); |
216 | |
217 | class = g_type_class_ref (type: test_object_get_type ()); |
218 | offset = g_type_class_get_instance_private_offset (g_class: class); |
219 | g_type_class_unref (g_class: class); |
220 | |
221 | g_assert (offset == TestObject_private_offset); |
222 | |
223 | g_object_unref (object: obj); |
224 | } |
225 | |
226 | static void |
227 | private_derived_instance (void) |
228 | { |
229 | TestDerived *obj = g_object_new (object_type: test_derived_get_type (), NULL); |
230 | |
231 | g_assert_cmpstr (test_derived_get_dummy_2 (obj), ==, "Hello" ); |
232 | g_assert_cmpint (test_object_get_dummy_0 ((TestObject *) obj), ==, 42); |
233 | |
234 | g_object_unref (object: obj); |
235 | } |
236 | |
237 | static void |
238 | private_mixed_derived_instance (void) |
239 | { |
240 | TestMixedDerived *derived = g_object_new (object_type: test_mixed_derived_get_type (), NULL); |
241 | TestMixed *mixed = g_object_new (object_type: test_mixed_get_type (), NULL); |
242 | |
243 | g_assert_cmpint (test_mixed_get_dummy_3 (mixed), ==, 47); |
244 | g_assert (test_mixed_derived_get_dummy_4 (derived) <= g_get_monotonic_time ()); |
245 | |
246 | g_object_unref (object: derived); |
247 | g_object_unref (object: mixed); |
248 | } |
249 | |
250 | int |
251 | main (int argc, |
252 | char *argv[]) |
253 | { |
254 | g_test_init (argc: &argc, argv: &argv, NULL); |
255 | |
256 | g_test_add_func (testpath: "/private/instance" , test_func: private_instance); |
257 | g_test_add_func (testpath: "/private/derived-instance" , test_func: private_derived_instance); |
258 | g_test_add_func (testpath: "/private/mixed-derived-instance" , test_func: private_mixed_derived_instance); |
259 | |
260 | return g_test_run (); |
261 | } |
262 | |