dbus-gsignature.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-gsignature.c Mapping from dbus type signatures to GType
00003  *
00004  * Copyright (C) 2005 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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     /* Later we need to return DBUS_TYPE_G_VALUE */
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   /* Later we need to return DBUS_TYPE_G_VALUE */
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   /* TODO: handle type 0? */
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
00151     {
00152       DBusSignatureIter subiter;
00153 
00154       g_assert (dbus_type_is_container (current_type));
00155 
00156       if (current_type == DBUS_TYPE_VARIANT)
00157         return G_TYPE_VALUE;
00158       
00159       dbus_signature_iter_recurse (iter, &subiter);
00160 
00161       if (current_type == DBUS_TYPE_ARRAY)
00162         {
00163           int elt_type = dbus_signature_iter_get_current_type (&subiter);
00164           if (elt_type == DBUS_TYPE_DICT_ENTRY)
00165             return signature_iter_to_g_type_dict (&subiter, is_client);
00166           else 
00167             return signature_iter_to_g_type_array (&subiter, is_client);
00168         }
00169       else if (current_type == DBUS_TYPE_STRUCT)
00170         {
00171           return signature_iter_to_g_type_struct (&subiter, is_client);
00172         }
00173       else
00174         {
00175           g_assert_not_reached ();
00176           return G_TYPE_INVALID;
00177         }
00178     }
00179 }
00180 
00181 GType
00182 _dbus_gtype_from_signature (const char *signature, gboolean is_client)
00183 {
00184   DBusSignatureIter iter;
00185 
00186   dbus_signature_iter_init (&iter, signature);
00187 
00188   return _dbus_gtype_from_signature_iter (&iter, is_client);
00189 }
00190 
00191 GArray *
00192 _dbus_gtypes_from_arg_signature (const char *argsig, gboolean is_client)
00193 {
00194   GArray *ret;
00195   int current_type;
00196   DBusSignatureIter sigiter;
00197 
00198   ret = g_array_new (FALSE, FALSE, sizeof (GType));
00199 
00200   dbus_signature_iter_init (&sigiter, argsig);
00201   while ((current_type = dbus_signature_iter_get_current_type (&sigiter)) != DBUS_TYPE_INVALID)
00202     {
00203       GType curtype;
00204 
00205       curtype = _dbus_gtype_from_signature_iter (&sigiter, is_client);
00206       g_array_append_val (ret, curtype);
00207       dbus_signature_iter_next (&sigiter);
00208     }
00209   return ret;
00210 }

Generated on Wed Oct 3 10:04:23 2007 for D-BUSGLibBindings by  doxygen 1.5.1