os/ossrv/glib/tsrc/BC/tests/testgobject.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* GObject - GLib Type, Object, Parameter and Signal Library
     2  * Copyright (C) 2001 Red Hat, Inc.
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     4  * This library is free software; you can redistribute it and/or
     5  * modify it under the terms of the GNU Lesser General Public
     6  * License as published by the Free Software Foundation; either
     7  * version 2 of the License, or (at your option) any later version.
     8  *
     9  * This library is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12  * Lesser General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU Lesser General
    15  * Public License along with this library; if not, write to the
    16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    17  * Boston, MA 02111-1307, USA.
    18  */
    19 
    20 #include <string.h>
    21 
    22 #undef	G_LOG_DOMAIN
    23 #define	G_LOG_DOMAIN "TestObject"
    24 #include	<glib-object.h>
    25 
    26 #ifdef SYMBIAN
    27 #include "mrt2_glib2_test.h"
    28 #endif /*SYMBIAN*/
    29 
    30 
    31 /* --- TestIface --- */
    32 #define TEST_TYPE_IFACE           (test_iface_get_type ())
    33 #define TEST_IFACE(obj)		  (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface))
    34 #define TEST_IS_IFACE(obj)	  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE))
    35 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
    36 typedef struct _TestIface      TestIface;
    37 typedef struct _TestIfaceClass TestIfaceClass;
    38 struct _TestIfaceClass
    39 {
    40   GTypeInterface base_iface;
    41   void	(*print_string)	(TestIface	*tiobj,
    42 			 const gchar	*string);
    43 };
    44 static void	iface_base_init		(TestIfaceClass	*iface);
    45 static void	iface_base_finalize	(TestIfaceClass	*iface);
    46 static void	print_foo		(TestIface	*tiobj,
    47 					 const gchar	*string);
    48 GType
    49 test_iface_get_type (void)
    50 {
    51   static GType test_iface_type = 0;
    52 
    53   if (!test_iface_type)
    54     {
    55       static const GTypeInfo test_iface_info =
    56       {
    57 	sizeof (TestIfaceClass),
    58 	(GBaseInitFunc)	iface_base_init,		/* base_init */
    59 	(GBaseFinalizeFunc) iface_base_finalize,	/* base_finalize */
    60       };
    61 
    62       test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0);
    63       g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT);
    64     }
    65 
    66   return test_iface_type;
    67 }
    68 static guint iface_base_init_count = 0;
    69 static void
    70 iface_base_init (TestIfaceClass *iface)
    71 {
    72   iface_base_init_count++;
    73   if (iface_base_init_count == 1)
    74     {
    75       /* add signals here */
    76     }
    77 }
    78 static void
    79 iface_base_finalize (TestIfaceClass *iface)
    80 {
    81   iface_base_init_count--;
    82   if (iface_base_init_count == 0)
    83     {
    84       /* destroy signals here */
    85     }
    86 }
    87 static void
    88 print_foo (TestIface   *tiobj,
    89 	   const gchar *string)
    90 {
    91   if (!string)
    92     string = "<NULL>";
    93 }
    94 
    95 static void
    96 test_object_test_iface_init (gpointer giface,
    97 			     gpointer iface_data)
    98 {
    99   TestIfaceClass *iface = giface;
   100 
   101   g_assert (iface_data == GUINT_TO_POINTER (42));
   102 
   103   g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
   104 
   105   /* assert iface_base_init() was already called */
   106   g_assert (iface_base_init_count > 0);
   107 
   108   /* initialize stuff */
   109   iface->print_string = print_foo;
   110 }
   111 void
   112 iface_print_string (TestIface   *tiobj,
   113 		    const gchar *string)
   114 {
   115   TestIfaceClass *iface;
   116 
   117   g_return_if_fail (TEST_IS_IFACE (tiobj));
   118   g_return_if_fail (G_IS_OBJECT (tiobj)); /* ensured through prerequisite */
   119 
   120   iface = TEST_IFACE_GET_CLASS (tiobj);
   121   g_object_ref (tiobj);
   122   iface->print_string (tiobj, string);
   123   g_object_unref (tiobj);
   124 }
   125 
   126 
   127 /* --- TestObject --- */
   128 #define TEST_TYPE_OBJECT            (test_object_get_type ())
   129 #define TEST_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject))
   130 #define TEST_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
   131 #define TEST_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT))
   132 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
   133 #define TEST_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
   134 #define TEST_OBJECT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_TYPE_OBJECT, TestObjectPrivate))
   135 typedef struct _TestObject        TestObject;
   136 typedef struct _TestObjectClass   TestObjectClass;
   137 typedef struct _TestObjectPrivate TestObjectPrivate;
   138 struct _TestObject
   139 {
   140   GObject parent_instance;
   141 };
   142 struct _TestObjectClass
   143 {
   144   GObjectClass parent_class;
   145 
   146   gchar* (*test_signal) (TestObject *tobject,
   147 			 TestIface  *iface_object,
   148 			 gpointer    tdata);
   149 };
   150 struct _TestObjectPrivate
   151 {
   152   int     dummy1;
   153   gdouble dummy2;
   154 };
   155 static void	test_object_class_init	(TestObjectClass	*class);
   156 static void	test_object_init	(TestObject		*tobject);
   157 static gboolean	test_signal_accumulator	(GSignalInvocationHint	*ihint,
   158 					 GValue            	*return_accu,
   159 					 const GValue       	*handler_return,
   160 					 gpointer                data);
   161 static gchar*	test_object_test_signal	(TestObject		*tobject,
   162 					 TestIface		*iface_object,
   163 					 gpointer		 tdata);
   164 GType
   165 test_object_get_type (void)
   166 {
   167   static GType test_object_type = 0;
   168 
   169   if (!test_object_type)
   170     {
   171       static const GTypeInfo test_object_info =
   172       {
   173 	sizeof (TestObjectClass),
   174 	NULL,           /* base_init */
   175 	NULL,           /* base_finalize */
   176 	(GClassInitFunc) test_object_class_init,
   177 	NULL,           /* class_finalize */
   178 	NULL,           /* class_data */
   179 	sizeof (TestObject),
   180 	5,              /* n_preallocs */
   181 	(GInstanceInitFunc) test_object_init,
   182       };
   183       GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) };
   184 
   185       test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
   186       g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
   187     }
   188 
   189   return test_object_type;
   190 }
   191 static void
   192 test_object_class_init (TestObjectClass *class)
   193 {
   194   /*  GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
   195 
   196   class->test_signal = test_object_test_signal;
   197 
   198   g_signal_new ("test-signal",
   199 		G_OBJECT_CLASS_TYPE (class),
   200 		G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
   201 		G_STRUCT_OFFSET (TestObjectClass, test_signal),
   202 		test_signal_accumulator, NULL,
   203 		g_cclosure_marshal_STRING__OBJECT_POINTER,
   204 		G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
   205 
   206   g_type_class_add_private (class, sizeof (TestObjectPrivate));
   207 }
   208 static void
   209 test_object_init (TestObject *tobject)
   210 {
   211   TestObjectPrivate *priv;
   212 
   213   priv = TEST_OBJECT_GET_PRIVATE (tobject);
   214 
   215   g_assert (priv);
   216   g_assert ((gchar *)priv >= (gchar *)tobject + sizeof (TestObject));
   217 
   218   priv->dummy1 = 54321;
   219 }
   220 /* Check to see if private data initialization in the
   221  * instance init function works.
   222  */
   223 static void
   224 test_object_check_private_init (TestObject *tobject)
   225 {
   226   TestObjectPrivate *priv;
   227 
   228   priv = TEST_OBJECT_GET_PRIVATE (tobject);
   229 
   230   g_assert (priv->dummy1 == 54321);
   231 }
   232 static gboolean
   233 test_signal_accumulator (GSignalInvocationHint *ihint,
   234 			 GValue                *return_accu,
   235 			 const GValue          *handler_return,
   236 			 gpointer               data)
   237 {
   238   gchar *accu_string = (char *)g_value_get_string (return_accu);
   239   gchar *new_string = (char *)g_value_get_string (handler_return);
   240   gchar *result_string;
   241 
   242   if (accu_string)
   243     result_string = g_strconcat (accu_string, new_string, NULL);
   244   else if (new_string)
   245     result_string = g_strdup (new_string);
   246   else
   247     result_string = NULL;
   248 
   249   g_value_take_string (return_accu, result_string);
   250 
   251   return TRUE;
   252 }
   253 static gchar*
   254 test_object_test_signal (TestObject *tobject,
   255 			 TestIface  *iface_object,
   256 			 gpointer    tdata)
   257 {
   258   g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
   259   
   260   return g_strdup ("<default_handler>");
   261 }
   262 
   263 
   264 /* --- TestIface for DerivedObject --- */
   265 static void
   266 print_bar (TestIface   *tiobj,
   267 	   const gchar *string)
   268 {
   269   TestIfaceClass *parent_iface;
   270 
   271   g_return_if_fail (TEST_IS_IFACE (tiobj));
   272 
   273   if (!string)
   274     string = "<NULL>";
   275 
   276   parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj));
   277   parent_iface->print_string (tiobj, string);
   278 
   279   g_assert (g_type_interface_peek_parent (parent_iface) == NULL);
   280 }
   281 
   282 static void
   283 derived_object_test_iface_init (gpointer giface,
   284 				gpointer iface_data)
   285 {
   286   TestIfaceClass *iface = giface;
   287 
   288   g_assert (iface_data == GUINT_TO_POINTER (87));
   289 
   290   g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
   291 
   292   /* assert test_object_test_iface_init() was already called */
   293   g_assert (iface->print_string == print_foo);
   294 
   295   /* override stuff */
   296   iface->print_string = print_bar;
   297 }
   298 
   299 
   300 /* --- DerivedObject --- */
   301 #define DERIVED_TYPE_OBJECT            (derived_object_get_type ())
   302 #define DERIVED_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject))
   303 #define DERIVED_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass))
   304 #define DERIVED_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT))
   305 #define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT))
   306 #define DERIVED_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass))
   307 #define DERIVED_OBJECT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), DERIVED_TYPE_OBJECT, DerivedObjectPrivate))
   308 typedef struct _DerivedObject        DerivedObject;
   309 typedef struct _TestObjectClass      DerivedObjectClass;
   310 typedef struct _DerivedObjectPrivate DerivedObjectPrivate;
   311 struct _DerivedObject
   312 {
   313   TestObject parent_instance;
   314   int  dummy1;
   315   int  dummy2;
   316 };
   317 struct _DerivedObjectPrivate
   318 {
   319   char dummy;
   320 };
   321 static void derived_object_class_init (DerivedObjectClass *class);
   322 static void derived_object_init       (DerivedObject      *dobject);
   323 GType
   324 derived_object_get_type (void)
   325 {
   326   static GType derived_object_type = 0;
   327 
   328   if (!derived_object_type)
   329     {
   330       static const GTypeInfo derived_object_info =
   331       {
   332 	sizeof (DerivedObjectClass),
   333 	NULL,           /* base_init */
   334 	NULL,           /* base_finalize */
   335 	(GClassInitFunc) derived_object_class_init,
   336 	NULL,           /* class_finalize */
   337 	NULL,           /* class_data */
   338 	sizeof (DerivedObject),
   339 	5,              /* n_preallocs */
   340 	(GInstanceInitFunc) derived_object_init,
   341       };
   342       GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) };
   343 
   344       derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
   345       g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
   346     }
   347 
   348   return derived_object_type;
   349 }
   350 static void
   351 derived_object_class_init (DerivedObjectClass *class)
   352 {
   353   g_type_class_add_private (class, sizeof (DerivedObjectPrivate));
   354 }
   355 static void
   356 derived_object_init (DerivedObject *dobject)
   357 {
   358   TestObjectPrivate *test_priv;
   359   DerivedObjectPrivate *derived_priv;
   360 
   361   derived_priv = DERIVED_OBJECT_GET_PRIVATE (dobject);
   362 
   363   g_assert (derived_priv);
   364   g_assert ((gchar *)derived_priv >= (gchar *)TEST_OBJECT_GET_PRIVATE (dobject) + sizeof (TestObjectPrivate));
   365 
   366   test_priv = TEST_OBJECT_GET_PRIVATE (dobject);
   367   
   368   g_assert (test_priv);
   369   g_assert ((gchar *)test_priv >= (gchar *)dobject + sizeof (TestObject));
   370 
   371 }
   372 
   373 /* --- main --- */
   374 int
   375 main (int   argc,
   376       char *argv[])
   377 {
   378   GTypeInfo info = { 0, };
   379   GTypeFundamentalInfo finfo = { 0, };
   380   GType type;
   381   TestObject *sigarg;
   382   DerivedObject *dobject;
   383   TestObjectPrivate *priv;
   384   gchar *string = NULL;
   385   
   386   #ifdef SYMBIAN
   387   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
   388   g_set_print_handler(mrtPrintHandler);
   389   #endif /*SYMBIAN*/
   390 
   391 
   392   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   393 			  G_LOG_LEVEL_WARNING |
   394 			  G_LOG_LEVEL_CRITICAL);
   395   g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
   396 
   397   /* test new fundamentals */
   398   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST) == g_type_fundamental_next ());
   399   type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow1", &info, &finfo, 0);
   400   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ());
   401   type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0);
   402   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ());
   403   g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
   404   g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
   405 
   406   /* to test past class initialization interface setups, create the class here */
   407   g_type_class_ref (TEST_TYPE_OBJECT);
   408 
   409   dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL);
   410   test_object_check_private_init (TEST_OBJECT (dobject));
   411 
   412   sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
   413 
   414   g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string);
   415   g_assert (strcmp (string, "<default_handler><default_handler>") == 0);
   416   g_free (string);
   417 
   418   iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
   419   iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
   420 
   421   priv = TEST_OBJECT_GET_PRIVATE (dobject);
   422   g_assert (priv->dummy1 == 54321);
   423   
   424   g_object_unref (sigarg);
   425   g_object_unref (dobject);
   426 
   427 #ifdef SYMBIAN
   428   testResultXml("testgobject");
   429 #endif /* EMULATOR */
   430   return 0;
   431 }