1 #include <glib-object.h>
4 GObject parent_instance
;
13 GObjectClass parent_class
;
16 GType
test_object_get_type (void);
18 G_DEFINE_TYPE_WITH_CODE (TestObject
, test_object
, G_TYPE_OBJECT
,
19 G_ADD_PRIVATE (TestObject
))
22 test_object_class_init (TestObjectClass
*klass
)
27 test_object_init (TestObject
*self
)
29 TestObjectPrivate
*priv
= test_object_get_instance_private (self
);
31 if (g_test_verbose ())
32 g_printerr ("Offset of %sPrivate for type '%s': %d\n",
33 G_OBJECT_TYPE_NAME (self
),
34 G_OBJECT_TYPE_NAME (self
),
35 TestObject_private_offset
);
38 priv
->dummy_1
= 3.14159f
;
42 test_object_get_dummy_0 (TestObject
*self
)
44 TestObjectPrivate
*priv
= test_object_get_instance_private (self
);
50 test_object_get_dummy_1 (TestObject
*self
)
52 TestObjectPrivate
*priv
= test_object_get_instance_private (self
);
58 TestObject parent_instance
;
66 TestObjectClass parent_class
;
69 GType
test_derived_get_type (void);
71 G_DEFINE_TYPE_WITH_CODE (TestDerived
, test_derived
, test_object_get_type (),
72 G_ADD_PRIVATE (TestDerived
))
75 test_derived_finalize (GObject
*obj
)
77 TestDerivedPrivate
*priv
= test_derived_get_instance_private ((TestDerived
*) obj
);
79 g_free (priv
->dummy_2
);
81 G_OBJECT_CLASS (test_derived_parent_class
)->finalize (obj
);
85 test_derived_class_init (TestDerivedClass
*klass
)
87 G_OBJECT_CLASS (klass
)->finalize
= test_derived_finalize
;
91 test_derived_init (TestDerived
*self
)
93 TestDerivedPrivate
*priv
= test_derived_get_instance_private (self
);
95 if (g_test_verbose ())
96 g_printerr ("Offset of %sPrivate for type '%s': %d\n",
97 G_OBJECT_TYPE_NAME (self
),
98 G_OBJECT_TYPE_NAME (self
),
99 TestDerived_private_offset
);
101 priv
->dummy_2
= g_strdup ("Hello");
105 test_derived_get_dummy_2 (TestDerived
*self
)
107 TestDerivedPrivate
*priv
= test_derived_get_instance_private (self
);
109 return priv
->dummy_2
;
113 TestObject parent_instance
;
121 TestObjectClass parent_class
;
124 GType
test_mixed_get_type (void);
126 G_DEFINE_TYPE (TestMixed
, test_mixed
, test_object_get_type ())
129 test_mixed_class_init (TestMixedClass
*klass
)
131 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
132 g_type_class_add_private (klass
, sizeof (TestMixedPrivate
));
133 G_GNUC_END_IGNORE_DEPRECATIONS
137 test_mixed_init (TestMixed
*self
)
139 TestMixedPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
, test_mixed_get_type (), TestMixedPrivate
);
141 if (g_test_verbose ())
142 g_printerr ("Offset of %sPrivate for type '%s': %d\n",
143 G_OBJECT_TYPE_NAME (self
),
144 G_OBJECT_TYPE_NAME (self
),
145 TestMixed_private_offset
);
151 test_mixed_get_dummy_3 (TestMixed
*self
)
153 TestMixedPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
, test_mixed_get_type (), TestMixedPrivate
);
155 return priv
->dummy_3
;
159 TestMixed parent_instance
;
164 } TestMixedDerivedPrivate
;
167 TestMixedClass parent_class
;
168 } TestMixedDerivedClass
;
170 GType
test_mixed_derived_get_type (void);
172 G_DEFINE_TYPE_WITH_CODE (TestMixedDerived
, test_mixed_derived
, test_mixed_get_type (),
173 G_ADD_PRIVATE (TestMixedDerived
))
176 test_mixed_derived_class_init (TestMixedDerivedClass
*klass
)
181 test_mixed_derived_init (TestMixedDerived
*self
)
183 TestMixedDerivedPrivate
*priv
= test_mixed_derived_get_instance_private (self
);
185 if (g_test_verbose ())
186 g_printerr ("Offset of %sPrivate for type '%s': %d\n",
187 G_OBJECT_TYPE_NAME (self
),
188 G_OBJECT_TYPE_NAME (self
),
189 TestMixedDerived_private_offset
);
191 priv
->dummy_4
= g_get_monotonic_time ();
195 test_mixed_derived_get_dummy_4 (TestMixedDerived
*self
)
197 TestMixedDerivedPrivate
*priv
= test_mixed_derived_get_instance_private (self
);
199 return priv
->dummy_4
;
203 private_instance (void)
205 TestObject
*obj
= g_object_new (test_object_get_type (), NULL
);
209 g_assert_cmpint (test_object_get_dummy_0 (obj
), ==, 42);
210 g_assert_cmpfloat (test_object_get_dummy_1 (obj
), ==, 3.14159f
);
212 class = g_type_class_ref (test_object_get_type ());
213 offset
= g_type_class_get_instance_private_offset (class);
214 g_type_class_unref (class);
216 g_assert (offset
== TestObject_private_offset
);
218 g_object_unref (obj
);
222 private_derived_instance (void)
224 TestDerived
*obj
= g_object_new (test_derived_get_type (), NULL
);
226 g_assert_cmpstr (test_derived_get_dummy_2 (obj
), ==, "Hello");
227 g_assert_cmpint (test_object_get_dummy_0 ((TestObject
*) obj
), ==, 42);
229 g_object_unref (obj
);
233 private_mixed_derived_instance (void)
235 TestMixedDerived
*derived
= g_object_new (test_mixed_derived_get_type (), NULL
);
236 TestMixed
*mixed
= g_object_new (test_mixed_get_type (), NULL
);
238 g_assert_cmpint (test_mixed_get_dummy_3 (mixed
), ==, 47);
239 g_assert (test_mixed_derived_get_dummy_4 (derived
) <= g_get_monotonic_time ());
241 g_object_unref (derived
);
242 g_object_unref (mixed
);
249 g_test_init (&argc
, &argv
, NULL
);
251 g_test_add_func ("/private/instance", private_instance
);
252 g_test_add_func ("/private/derived-instance", private_derived_instance
);
253 g_test_add_func ("/private/mixed-derived-instance", private_mixed_derived_instance
);
255 return g_test_run ();