00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "dbus-gtest.h"
00026 #include "dbus-gsignature.h"
00027 #include "dbus-gvalue-utils.h"
00028 #include <string.h>
00029 #include <glib.h>
00030
00031 #define MAP_BASIC(d_t, g_t) \
00032 case DBUS_TYPE_##d_t: \
00033 return G_TYPE_##g_t;
00034 static GType
00035 typecode_to_gtype (int type)
00036 {
00037 switch (type)
00038 {
00039 MAP_BASIC (BOOLEAN, BOOLEAN);
00040 MAP_BASIC (BYTE, UCHAR);
00041 MAP_BASIC (INT16, INT);
00042 MAP_BASIC (INT32, INT);
00043 MAP_BASIC (UINT16, UINT);
00044 MAP_BASIC (UINT32, UINT);
00045 MAP_BASIC (INT64, INT64);
00046 MAP_BASIC (UINT64, UINT64);
00047 MAP_BASIC (DOUBLE, DOUBLE);
00048 MAP_BASIC (STRING, STRING);
00049 default:
00050 return G_TYPE_INVALID;
00051 }
00052 }
00053 #undef MAP_BASIC
00054
00055 static gboolean
00056 dbus_typecode_maps_to_basic (int typecode)
00057 {
00058 return typecode_to_gtype (typecode) != G_TYPE_INVALID;
00059 }
00060
00061 GType
00062 _dbus_gtype_from_basic_typecode (int typecode)
00063 {
00064 g_assert (dbus_type_is_basic (typecode));
00065 g_assert (dbus_typecode_maps_to_basic (typecode));
00066 return typecode_to_gtype (typecode);
00067 }
00068
00069 static GType
00070 signature_iter_to_g_type_dict (const DBusSignatureIter *subiter, gboolean is_client)
00071 {
00072 DBusSignatureIter iter;
00073 GType key_gtype;
00074 GType value_gtype;
00075
00076 g_assert (dbus_signature_iter_get_current_type (subiter) == DBUS_TYPE_DICT_ENTRY);
00077
00078 dbus_signature_iter_recurse (subiter, &iter);
00079
00080 key_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
00081 if (key_gtype == G_TYPE_INVALID)
00082 return G_TYPE_INVALID;
00083
00084 dbus_signature_iter_next (&iter);
00085 value_gtype = _dbus_gtype_from_signature_iter (&iter, is_client);
00086 if (value_gtype == G_TYPE_INVALID)
00087 return G_TYPE_INVALID;
00088
00089 if (!_dbus_gtype_is_valid_hash_key (key_gtype)
00090 || !_dbus_gtype_is_valid_hash_value (value_gtype))
00091
00092 return G_TYPE_INVALID;
00093
00094 return dbus_g_type_get_map ("GHashTable", key_gtype, value_gtype);
00095 }
00096
00097 static GType
00098 signature_iter_to_g_type_array (DBusSignatureIter *iter, gboolean is_client)
00099 {
00100 GType elt_gtype;
00101
00102 elt_gtype = _dbus_gtype_from_signature_iter (iter, is_client);
00103 if (elt_gtype == G_TYPE_INVALID)
00104 return G_TYPE_INVALID;
00105
00106 if (elt_gtype == G_TYPE_OBJECT)
00107 return DBUS_TYPE_G_OBJECT_ARRAY;
00108 if (elt_gtype == G_TYPE_STRING)
00109 return G_TYPE_STRV;
00110 if (_dbus_g_type_is_fixed (elt_gtype))
00111 return dbus_g_type_get_collection ("GArray", elt_gtype);
00112 else if (g_type_is_a (elt_gtype, G_TYPE_OBJECT)
00113 || g_type_is_a (elt_gtype, G_TYPE_BOXED))
00114 return dbus_g_type_get_collection ("GPtrArray", elt_gtype);
00115
00116
00117 return G_TYPE_INVALID;
00118 }
00119
00120 static GType
00121 signature_iter_to_g_type_struct (DBusSignatureIter *iter, gboolean is_client)
00122 {
00123 GArray *types;
00124 GType ret;
00125 types = g_array_new (FALSE, FALSE, sizeof (GType));
00126 do
00127 {
00128 GType curtype;
00129 curtype = _dbus_gtype_from_signature_iter (iter, is_client);
00130 g_array_append_val (types, curtype);
00131 }
00132 while (dbus_signature_iter_next (iter));
00133
00134 ret = dbus_g_type_get_structv ("GValueArray", types->len, (GType*) types->data);
00135 g_array_free (types, TRUE);
00136 return ret;
00137 }
00138
00139 GType
00140 _dbus_gtype_from_signature_iter (DBusSignatureIter *iter, gboolean is_client)
00141 {
00142 int current_type;
00143
00144 current_type = dbus_signature_iter_get_current_type (iter);
00145
00146 if (dbus_typecode_maps_to_basic (current_type))
00147 return _dbus_gtype_from_basic_typecode (current_type);
00148 else if (current_type == DBUS_TYPE_OBJECT_PATH)
00149 return DBUS_TYPE_G_OBJECT_PATH;
00150 else if (current_type == DBUS_TYPE_SIGNATURE)
00151 return DBUS_TYPE_G_SIGNATURE;
00152 else
00153 {
00154 DBusSignatureIter subiter;
00155
00156 g_assert (dbus_type_is_container (current_type));
00157
00158 if (current_type == DBUS_TYPE_VARIANT)
00159 return G_TYPE_VALUE;
00160
00161 dbus_signature_iter_recurse (iter, &subiter);
00162
00163 if (current_type == DBUS_TYPE_ARRAY)
00164 {
00165 int elt_type = dbus_signature_iter_get_current_type (&subiter);
00166 if (elt_type == DBUS_TYPE_DICT_ENTRY)
00167 return signature_iter_to_g_type_dict (&subiter, is_client);
00168 else
00169 return signature_iter_to_g_type_array (&subiter, is_client);
00170 }
00171 else if (current_type == DBUS_TYPE_STRUCT)
00172 {
00173 return signature_iter_to_g_type_struct (&subiter, is_client);
00174 }
00175 else
00176 {
00177 g_assert_not_reached ();
00178 return G_TYPE_INVALID;
00179 }
00180 }
00181 }
00182
00183 GType
00184 _dbus_gtype_from_signature (const char *signature, gboolean is_client)
00185 {
00186 DBusSignatureIter iter;
00187
00188 dbus_signature_iter_init (&iter, signature);
00189
00190 return _dbus_gtype_from_signature_iter (&iter, is_client);
00191 }
00192
00193 GArray *
00194 _dbus_gtypes_from_arg_signature (const char *argsig, gboolean is_client)
00195 {
00196 GArray *ret;
00197 int current_type;
00198 DBusSignatureIter sigiter;
00199
00200 ret = g_array_new (FALSE, FALSE, sizeof (GType));
00201
00202 dbus_signature_iter_init (&sigiter, argsig);
00203 while ((current_type = dbus_signature_iter_get_current_type (&sigiter)) != DBUS_TYPE_INVALID)
00204 {
00205 GType curtype;
00206
00207 curtype = _dbus_gtype_from_signature_iter (&sigiter, is_client);
00208 g_array_append_val (ret, curtype);
00209 dbus_signature_iter_next (&sigiter);
00210 }
00211 return ret;
00212 }