os/ossrv/glib/tests/gobject/ifaceinherit.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* GObject - GLib Type, Object, Parameter and Signal Library
sl@0
     2
 * Copyright (C) 2001, 2003 Red Hat, Inc.
sl@0
     3
 *
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General
sl@0
    15
 * Public License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
#undef	G_LOG_DOMAIN
sl@0
    21
#define	G_LOG_DOMAIN "TestIfaceInherit"
sl@0
    22
sl@0
    23
#undef G_DISABLE_ASSERT
sl@0
    24
#undef G_DISABLE_CHECKS
sl@0
    25
#undef G_DISABLE_CAST_CHECKS
sl@0
    26
sl@0
    27
#include <glib-object.h>
sl@0
    28
sl@0
    29
#include "testcommon.h"
sl@0
    30
#include "testmodule.h"
sl@0
    31
sl@0
    32
#ifdef __SYMBIAN32__
sl@0
    33
#include "mrt2_glib2_test.h"
sl@0
    34
#endif // __SYMBIAN32__
sl@0
    35
/* This test tests inheritance of interface. We two object
sl@0
    36
 * class BaseObject and DerivedObject we add an interface
sl@0
    37
 * to BaseObject:
sl@0
    38
 *
sl@0
    39
 * I1) Before DerivedObject is registered
sl@0
    40
 * I2) After DerivedObject is registered, but before
sl@0
    41
 *     DerivedObject is class initialized
sl@0
    42
 * I3) During DerivedObject's class_init
sl@0
    43
 * I4) After DerivedObject's class init
sl@0
    44
 *
sl@0
    45
 * We also do some tests of overriding.
sl@0
    46
 * 
sl@0
    47
 * I5) We add an interface to BaseObject, then add the same
sl@0
    48
 *     interface to DerivedObject. (Note that this is only legal
sl@0
    49
 *     before DerivedObject's class_init; the results of
sl@0
    50
 *     g_type_interface_peek() are not allowed to change from one
sl@0
    51
 *     non-NULL vtable to another non-NULL vtable)
sl@0
    52
 */
sl@0
    53
   
sl@0
    54
/*
sl@0
    55
 * BaseObject, a parent class for DerivedObject
sl@0
    56
 */
sl@0
    57
#define BASE_TYPE_OBJECT          (base_object_get_type ())
sl@0
    58
typedef struct _BaseObject        BaseObject;
sl@0
    59
typedef struct _BaseObjectClass   BaseObjectClass;
sl@0
    60
sl@0
    61
struct _BaseObject
sl@0
    62
{
sl@0
    63
  GObject parent_instance;
sl@0
    64
};
sl@0
    65
struct _BaseObjectClass
sl@0
    66
{
sl@0
    67
  GObjectClass parent_class;
sl@0
    68
};
sl@0
    69
sl@0
    70
static GType base_object_get_type (void);
sl@0
    71
static GType derived_object_get_type (void);
sl@0
    72
sl@0
    73
/*
sl@0
    74
 * DerivedObject, the child class of DerivedObject
sl@0
    75
 */
sl@0
    76
#define DERIVED_TYPE_OBJECT          (derived_object_get_type ())
sl@0
    77
typedef struct _DerivedObject        DerivedObject;
sl@0
    78
typedef struct _DerivedObjectClass   DerivedObjectClass;
sl@0
    79
sl@0
    80
struct _DerivedObject
sl@0
    81
{
sl@0
    82
  BaseObject parent_instance;
sl@0
    83
};
sl@0
    84
struct _DerivedObjectClass
sl@0
    85
{
sl@0
    86
  BaseObjectClass parent_class;
sl@0
    87
};
sl@0
    88
sl@0
    89
/*
sl@0
    90
 * The interfaces
sl@0
    91
 */
sl@0
    92
typedef struct _TestIfaceClass TestIfaceClass;
sl@0
    93
typedef struct _TestIfaceClass TestIface1Class;
sl@0
    94
typedef struct _TestIfaceClass TestIface2Class;
sl@0
    95
typedef struct _TestIfaceClass TestIface3Class;
sl@0
    96
typedef struct _TestIfaceClass TestIface4Class;
sl@0
    97
typedef struct _TestIfaceClass TestIface5Class;
sl@0
    98
sl@0
    99
struct _TestIfaceClass
sl@0
   100
{
sl@0
   101
  GTypeInterface base_iface;
sl@0
   102
  guint val;
sl@0
   103
};
sl@0
   104
sl@0
   105
#define TEST_TYPE_IFACE1 (test_iface1_get_type ())
sl@0
   106
#define TEST_TYPE_IFACE2 (test_iface2_get_type ())
sl@0
   107
#define TEST_TYPE_IFACE3 (test_iface3_get_type ())
sl@0
   108
#define TEST_TYPE_IFACE4 (test_iface4_get_type ())
sl@0
   109
#define TEST_TYPE_IFACE5 (test_iface5_get_type ())
sl@0
   110
sl@0
   111
static DEFINE_IFACE (TestIface1, test_iface1,  NULL, NULL)
sl@0
   112
static DEFINE_IFACE (TestIface2, test_iface2,  NULL, NULL)
sl@0
   113
static DEFINE_IFACE (TestIface3, test_iface3,  NULL, NULL)
sl@0
   114
static DEFINE_IFACE (TestIface4, test_iface4,  NULL, NULL)
sl@0
   115
static DEFINE_IFACE (TestIface5, test_iface5,  NULL, NULL)
sl@0
   116
sl@0
   117
static void
sl@0
   118
add_interface (GType              object_type,
sl@0
   119
	       GType              iface_type,
sl@0
   120
	       GInterfaceInitFunc init_func)
sl@0
   121
{
sl@0
   122
  GInterfaceInfo iface_info = {	NULL, NULL, NULL };
sl@0
   123
sl@0
   124
  iface_info.interface_init = init_func;
sl@0
   125
								
sl@0
   126
  g_type_add_interface_static (object_type, iface_type, &iface_info);
sl@0
   127
}
sl@0
   128
sl@0
   129
static void
sl@0
   130
init_base_interface (TestIfaceClass *iface)
sl@0
   131
{
sl@0
   132
  iface->val = 21;
sl@0
   133
}
sl@0
   134
sl@0
   135
static void
sl@0
   136
add_base_interface (GType object_type,
sl@0
   137
		    GType iface_type)
sl@0
   138
{
sl@0
   139
  add_interface (object_type, iface_type,
sl@0
   140
		 (GInterfaceInitFunc)init_base_interface);
sl@0
   141
}
sl@0
   142
sl@0
   143
static gboolean
sl@0
   144
interface_is_base (GType object_type,
sl@0
   145
		   GType iface_type)
sl@0
   146
{
sl@0
   147
  gpointer g_class = g_type_class_peek (object_type);
sl@0
   148
  TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
sl@0
   149
  return iface && iface->val == 21;
sl@0
   150
}
sl@0
   151
sl@0
   152
static void
sl@0
   153
init_derived_interface (TestIfaceClass *iface)
sl@0
   154
{
sl@0
   155
  iface->val = 42;
sl@0
   156
}
sl@0
   157
sl@0
   158
static void
sl@0
   159
add_derived_interface (GType object_type,
sl@0
   160
		       GType iface_type)
sl@0
   161
{
sl@0
   162
  add_interface (object_type, iface_type,
sl@0
   163
		 (GInterfaceInitFunc)init_derived_interface);
sl@0
   164
}
sl@0
   165
sl@0
   166
static gboolean
sl@0
   167
interface_is_derived (GType object_type,
sl@0
   168
		      GType iface_type)
sl@0
   169
{
sl@0
   170
  gpointer g_class = g_type_class_peek (object_type);
sl@0
   171
  TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
sl@0
   172
  return iface && iface->val == 42;
sl@0
   173
}
sl@0
   174
sl@0
   175
static void
sl@0
   176
derived_object_class_init (BaseObjectClass *class)
sl@0
   177
{
sl@0
   178
  add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3);
sl@0
   179
}
sl@0
   180
sl@0
   181
static DEFINE_TYPE(BaseObject, base_object,
sl@0
   182
		   NULL, NULL, NULL,
sl@0
   183
		   G_TYPE_OBJECT)
sl@0
   184
static DEFINE_TYPE(DerivedObject, derived_object,
sl@0
   185
		   derived_object_class_init, NULL, NULL,
sl@0
   186
		   BASE_TYPE_OBJECT)
sl@0
   187
sl@0
   188
int
sl@0
   189
main (int   argc,
sl@0
   190
      char *argv[])
sl@0
   191
{
sl@0
   192
#ifdef __SYMBIAN32__
sl@0
   193
  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);
sl@0
   194
  g_set_print_handler(mrtPrintHandler);
sl@0
   195
  #endif /*__SYMBIAN32__*/
sl@0
   196
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
sl@0
   197
			  G_LOG_LEVEL_WARNING |
sl@0
   198
			  G_LOG_LEVEL_CRITICAL);
sl@0
   199
  g_type_init ();
sl@0
   200
sl@0
   201
  /* Register BaseObject */
sl@0
   202
  BASE_TYPE_OBJECT;
sl@0
   203
  
sl@0
   204
  add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5);
sl@0
   205
sl@0
   206
  /* Class init BaseObject */
sl@0
   207
  g_type_class_ref (BASE_TYPE_OBJECT);
sl@0
   208
  
sl@0
   209
  add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1);
sl@0
   210
sl@0
   211
  /* Register DerivedObject */
sl@0
   212
  DERIVED_TYPE_OBJECT;
sl@0
   213
sl@0
   214
  add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2);
sl@0
   215
  add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5);
sl@0
   216
sl@0
   217
  /* Class init DerivedObject */
sl@0
   218
  g_type_class_ref (DERIVED_TYPE_OBJECT);
sl@0
   219
  
sl@0
   220
  add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4);
sl@0
   221
sl@0
   222
  /* Check that all the non-overridden interfaces were properly inherited
sl@0
   223
   */
sl@0
   224
  g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE1));
sl@0
   225
  g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE2));
sl@0
   226
  g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE3));
sl@0
   227
  g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE4));
sl@0
   228
sl@0
   229
  /* Check that all the overridden interfaces were properly overridden
sl@0
   230
   */
sl@0
   231
  g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5));
sl@0
   232
sl@0
   233
#ifdef __SYMBIAN32__
sl@0
   234
testResultXml("ifaceinherit");
sl@0
   235
#endif // __SYMBIAN32__
sl@0
   236
  return 0;
sl@0
   237
}