Update Friulian translation
[glib.git] / tests / refcount / properties2.c
blob1684bd45bc59ddcb7a93ab2c10256e60895d6ce0
1 #include <glib.h>
2 #include <glib-object.h>
4 #ifdef G_OS_UNIX
5 #include <unistd.h>
6 #endif
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))
15 enum {
16 PROP_0,
17 PROP_DUMMY
20 typedef struct _GTest GTest;
21 typedef struct _GTestClass GTestClass;
23 struct _GTest
25 GObject object;
27 gint dummy;
30 struct _GTestClass
32 GObjectClass parent_class;
35 static GType my_test_get_type (void);
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);
49 static GObjectClass *parent_class = NULL;
51 static GType
52 my_test_get_type (void)
54 static GType test_type = 0;
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),
66 (GInstanceInitFunc) my_test_init,
67 NULL
70 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
71 &test_info, 0);
73 return test_type;
76 static void
77 my_test_class_init (GTestClass * klass)
79 GObjectClass *gobject_class;
81 gobject_class = (GObjectClass *) klass;
83 parent_class = g_type_class_ref (G_TYPE_OBJECT);
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;
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));
98 static void
99 my_test_init (GTest * test)
101 g_print ("init %p\n", test);
104 static void
105 my_test_dispose (GObject * object)
107 GTest *test;
109 test = MY_TEST (object);
111 g_print ("dispose %p!\n", test);
113 G_OBJECT_CLASS (parent_class)->dispose (object);
116 static void
117 my_test_get_property (GObject *object,
118 guint prop_id,
119 GValue *value,
120 GParamSpec *pspec)
122 GTest *test;
124 test = MY_TEST (object);
126 switch (prop_id)
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;
137 static void
138 my_test_set_property (GObject *object,
139 guint prop_id,
140 const GValue *value,
141 GParamSpec *pspec)
143 GTest *test;
145 test = MY_TEST (object);
147 switch (prop_id)
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;
158 static gint count = 0;
160 static void
161 dummy_notify (GObject *object,
162 GParamSpec *pspec)
164 count++;
165 if (count % 10000 == 0)
166 g_print (".");
169 static void
170 my_test_do_property (GTest * test)
172 gint dummy;
174 g_object_get (test, "dummy", &dummy, NULL);
175 g_object_set (test, "dummy", dummy + 1, NULL);
179 main (int argc, char **argv)
181 gint i;
182 GTest *test;
184 g_print ("START: %s\n", argv[0]);
185 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
187 test = g_object_new (G_TYPE_TEST, NULL);
189 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
191 g_assert (count == test->dummy);
193 for (i=0; i<1000000; i++) {
194 my_test_do_property (test);
197 g_assert (count == test->dummy);
199 g_object_unref (test);
201 return 0;