os/ossrv/glib/tsrc/BC/tests/gobject/ifaceinherit.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, 2003 Red Hat, Inc.
     3  * Portion Copyright © 2008 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 #undef	G_LOG_DOMAIN
    21 #define	G_LOG_DOMAIN "TestIfaceInherit"
    22 
    23 #undef G_DISABLE_ASSERT
    24 #undef G_DISABLE_CHECKS
    25 #undef G_DISABLE_CAST_CHECKS
    26 
    27 #include <glib-object.h>
    28 
    29 #include "testcommon.h"
    30 #include "testmodule.h"
    31 
    32 #ifdef SYMBIAN
    33 #include "mrt2_glib2_test.h"
    34 #endif // SYMBIAN
    35 
    36 
    37 /* This test tests inheritance of interface. We two object
    38  * class BaseObject and DerivedObject we add an interface
    39  * to BaseObject:
    40  *
    41  * I1) Before DerivedObject is registered
    42  * I2) After DerivedObject is registered, but before
    43  *     DerivedObject is class initialized
    44  * I3) During DerivedObject's class_init
    45  * I4) After DerivedObject's class init
    46  *
    47  * We also do some tests of overriding.
    48  * 
    49  * I5) We add an interface to BaseObject, then add the same
    50  *     interface to DerivedObject. (Note that this is only legal
    51  *     before DerivedObject's class_init; the results of
    52  *     g_type_interface_peek() are not allowed to change from one
    53  *     non-NULL vtable to another non-NULL vtable)
    54  */
    55    
    56 /*
    57  * BaseObject, a parent class for DerivedObject
    58  */
    59 #define BASE_TYPE_OBJECT          (base_object_get_type ())
    60 typedef struct _BaseObject        BaseObject;
    61 typedef struct _BaseObjectClass   BaseObjectClass;
    62 
    63 struct _BaseObject
    64 {
    65   GObject parent_instance;
    66 };
    67 struct _BaseObjectClass
    68 {
    69   GObjectClass parent_class;
    70 };
    71 
    72 static GType base_object_get_type ();
    73 static GType derived_object_get_type ();
    74 
    75 /*
    76  * DerivedObject, the child class of DerivedObject
    77  */
    78 #define DERIVED_TYPE_OBJECT          (derived_object_get_type ())
    79 typedef struct _DerivedObject        DerivedObject;
    80 typedef struct _DerivedObjectClass   DerivedObjectClass;
    81 
    82 struct _DerivedObject
    83 {
    84   BaseObject parent_instance;
    85 };
    86 struct _DerivedObjectClass
    87 {
    88   BaseObjectClass parent_class;
    89 };
    90 
    91 /*
    92  * The interfaces
    93  */
    94 typedef struct _TestIfaceClass TestIfaceClass;
    95 typedef struct _TestIfaceClass TestIface1Class;
    96 typedef struct _TestIfaceClass TestIface2Class;
    97 typedef struct _TestIfaceClass TestIface3Class;
    98 typedef struct _TestIfaceClass TestIface4Class;
    99 typedef struct _TestIfaceClass TestIface5Class;
   100 
   101 struct _TestIfaceClass
   102 {
   103   GTypeInterface base_iface;
   104   guint val;
   105 };
   106 
   107 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
   108 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
   109 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
   110 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
   111 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
   112 
   113 static DEFINE_IFACE (TestIface1, test_iface1,  NULL, NULL)
   114 static DEFINE_IFACE (TestIface2, test_iface2,  NULL, NULL)
   115 static DEFINE_IFACE (TestIface3, test_iface3,  NULL, NULL)
   116 static DEFINE_IFACE (TestIface4, test_iface4,  NULL, NULL)
   117 static DEFINE_IFACE (TestIface5, test_iface5,  NULL, NULL)
   118 
   119 static void
   120 add_interface (GType              object_type,
   121 	       GType              iface_type,
   122 	       GInterfaceInitFunc init_func)
   123 {
   124   GInterfaceInfo iface_info = {	NULL, NULL, NULL };
   125 
   126   iface_info.interface_init = init_func;
   127 								
   128   g_type_add_interface_static (object_type, iface_type, &iface_info);
   129 }
   130 
   131 static void
   132 init_base_interface (TestIfaceClass *iface)
   133 {
   134   iface->val = 21;
   135 }
   136 
   137 static void
   138 add_base_interface (GType object_type,
   139 		    GType iface_type)
   140 {
   141   add_interface (object_type, iface_type,
   142 		 (GInterfaceInitFunc)init_base_interface);
   143 }
   144 
   145 static gboolean
   146 interface_is_base (GType object_type,
   147 		   GType iface_type)
   148 {
   149   gpointer g_class = g_type_class_peek (object_type);
   150   TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
   151   return iface && iface->val == 21;
   152 }
   153 
   154 static void
   155 init_derived_interface (TestIfaceClass *iface)
   156 {
   157   iface->val = 42;
   158 }
   159 
   160 static void
   161 add_derived_interface (GType object_type,
   162 		       GType iface_type)
   163 {
   164   add_interface (object_type, iface_type,
   165 		 (GInterfaceInitFunc)init_derived_interface);
   166 }
   167 
   168 static gboolean
   169 interface_is_derived (GType object_type,
   170 		      GType iface_type)
   171 {
   172   gpointer g_class = g_type_class_peek (object_type);
   173   TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
   174   return iface && iface->val == 42;
   175 }
   176 
   177 static void
   178 derived_object_class_init (BaseObjectClass *class)
   179 {
   180   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3);
   181 }
   182 
   183 static DEFINE_TYPE(BaseObject, base_object,
   184 		   NULL, NULL, NULL,
   185 		   G_TYPE_OBJECT)
   186 static DEFINE_TYPE(DerivedObject, derived_object,
   187 		   derived_object_class_init, NULL, NULL,
   188 		   BASE_TYPE_OBJECT)
   189 
   190 int
   191 main (int   argc,
   192       char *argv[])
   193 {
   194 
   195 #ifdef SYMBIAN
   196   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);
   197   g_set_print_handler(mrtPrintHandler);
   198   #endif /*SYMBIAN*/
   199   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
   200 			  G_LOG_LEVEL_WARNING |
   201 			  G_LOG_LEVEL_CRITICAL);
   202   g_type_init ();
   203 
   204   /* Register BaseObject */
   205   BASE_TYPE_OBJECT;
   206   
   207   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5);
   208 
   209   /* Class init BaseObject */
   210   g_type_class_ref (BASE_TYPE_OBJECT);
   211   
   212   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1);
   213 
   214   /* Register DerivedObject */
   215   DERIVED_TYPE_OBJECT;
   216 
   217   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2);
   218   add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5);
   219 
   220   /* Class init DerivedObject */
   221   g_type_class_ref (DERIVED_TYPE_OBJECT);
   222   
   223   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4);
   224 
   225   /* Check that all the non-overridden interfaces were properly inherited
   226    */
   227   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE1));
   228   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE2));
   229   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE3));
   230   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE4));
   231 
   232   /* Check that all the overridden interfaces were properly overridden
   233    */
   234   g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5));
   235 
   236 #ifdef SYMBIAN
   237 testResultXml("ifaceinherit");
   238 #endif // SYMBIAN
   239   return 0;
   240 }