Update contrib.
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gsignature.c Mapping from dbus type signatures to GType
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-gtest.h"
26 #include "dbus-gsignature.h"
27 #include "dbus-gvalue-utils.h"
31 #define MAP_BASIC(d_t, g_t) \
32 case DBUS_TYPE_##d_t: \
35 typecode_to_gtype (int type)
39 MAP_BASIC (BOOLEAN, BOOLEAN);
40 MAP_BASIC (BYTE, UCHAR);
41 MAP_BASIC (INT16, INT);
42 MAP_BASIC (INT32, INT);
43 MAP_BASIC (UINT16, UINT);
44 MAP_BASIC (UINT32, UINT);
45 MAP_BASIC (INT64, INT64);
46 MAP_BASIC (UINT64, UINT64);
47 MAP_BASIC (DOUBLE, DOUBLE);
48 MAP_BASIC (STRING, STRING);
50 return G_TYPE_INVALID;
56 dbus_typecode_maps_to_basic (int typecode)
58 return typecode_to_gtype (typecode) != G_TYPE_INVALID;
62 _dbus_gtype_from_basic_typecode (int typecode)
64 g_assert (dbus_type_is_basic (typecode));
65 g_assert (dbus_typecode_maps_to_basic (typecode));
66 return typecode_to_gtype (typecode);
70 signature_iter_to_g_type_dict (const DBusSignatureIter *subiter, gboolean is_client)
72 DBusSignatureIter iter;
76 g_assert (dbus_signature_iter_get_current_type (subiter) == DBUS_TYPE_DICT_ENTRY);
78 dbus_signature_iter_recurse (subiter, &iter);
80 key_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
81 if (key_gtype == G_TYPE_INVALID)
82 return G_TYPE_INVALID;
84 dbus_signature_iter_next (&iter);
85 value_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
86 if (value_gtype == G_TYPE_INVALID)
87 return G_TYPE_INVALID;
89 if (!_dbus_gtype_is_valid_hash_key (key_gtype)
90 || !_dbus_gtype_is_valid_hash_value (value_gtype))
91 /* Later we need to return DBUS_TYPE_G_VALUE */
92 return G_TYPE_INVALID;
94 return dbus_g_type_get_map ("GHashTable", key_gtype, value_gtype);
98 signature_iter_to_g_type_array (DBusSignatureIter *iter, gboolean is_client)
102 elt_gtype = _dbus_gtype_from_signature_iter (iter, is_client);
103 if (elt_gtype == G_TYPE_INVALID)
104 return G_TYPE_INVALID;
106 if (elt_gtype == G_TYPE_OBJECT)
107 return DBUS_TYPE_G_OBJECT_ARRAY;
108 if (elt_gtype == G_TYPE_STRING)
110 if (_dbus_g_type_is_fixed (elt_gtype))
111 return dbus_g_type_get_collection ("GArray", elt_gtype);
112 else if (g_type_is_a (elt_gtype, G_TYPE_OBJECT)
113 || g_type_is_a (elt_gtype, G_TYPE_BOXED))
114 return dbus_g_type_get_collection ("GPtrArray", elt_gtype);
116 /* Later we need to return DBUS_TYPE_G_VALUE */
117 return G_TYPE_INVALID;
121 signature_iter_to_g_type_struct (DBusSignatureIter *iter, gboolean is_client)
125 types = g_array_new (FALSE, FALSE, sizeof (GType));
129 curtype = _dbus_gtype_from_signature_iter (iter, is_client);
130 g_array_append_val (types, curtype);
132 while (dbus_signature_iter_next (iter));
134 ret = dbus_g_type_get_structv ("GValueArray", types->len, (GType*) types->data);
135 g_array_free (types, TRUE);
140 _dbus_gtype_from_signature_iter (DBusSignatureIter *iter, gboolean is_client)
144 current_type = dbus_signature_iter_get_current_type (iter);
145 /* TODO: handle type 0? */
146 if (dbus_typecode_maps_to_basic (current_type))
147 return _dbus_gtype_from_basic_typecode (current_type);
148 else if (current_type == DBUS_TYPE_OBJECT_PATH)
149 return DBUS_TYPE_G_OBJECT_PATH;
152 DBusSignatureIter subiter;
154 g_assert (dbus_type_is_container (current_type));
156 if (current_type == DBUS_TYPE_VARIANT)
159 dbus_signature_iter_recurse (iter, &subiter);
161 if (current_type == DBUS_TYPE_ARRAY)
163 int elt_type = dbus_signature_iter_get_current_type (&subiter);
164 if (elt_type == DBUS_TYPE_DICT_ENTRY)
165 return signature_iter_to_g_type_dict (&subiter, is_client);
167 return signature_iter_to_g_type_array (&subiter, is_client);
169 else if (current_type == DBUS_TYPE_STRUCT)
171 return signature_iter_to_g_type_struct (&subiter, is_client);
175 g_assert_not_reached ();
176 return G_TYPE_INVALID;
182 _dbus_gtype_from_signature (const char *signature, gboolean is_client)
184 DBusSignatureIter iter;
186 dbus_signature_iter_init (&iter, signature);
188 return _dbus_gtype_from_signature_iter (&iter, is_client);
192 _dbus_gtypes_from_arg_signature (const char *argsig, gboolean is_client)
196 DBusSignatureIter sigiter;
198 ret = g_array_new (FALSE, FALSE, sizeof (GType));
200 dbus_signature_iter_init (&sigiter, argsig);
201 while ((current_type = dbus_signature_iter_get_current_type (&sigiter)) != DBUS_TYPE_INVALID)
205 curtype = _dbus_gtype_from_signature_iter (&sigiter, is_client);
206 g_array_append_val (ret, curtype);
207 dbus_signature_iter_next (&sigiter);