Bonobo Unique Application v5.1 => it's in!



A Seg, 2003-11-17 ās 20:14, Michael Meeks escreveu:
> Hi Gustavo,
> 
> On Mon, 2003-11-17 at 14:56, Gustavo J. A. M. Carneiro wrote:
> > 	Oops! :-)
> > 	Here it is, then.
> 
> 	:-)
> 
> 	Is there any reason we can't do:
> 
> 	GValue    *bonobo_arg_to_gvalue_copy (const BonoboArg *in_arg);
> 	BonoboArg *bonobo_arg_from_gvalue_copy (const GValue *in_arg);
> 
> 	And use NULL to signal the (rare) error conditions ?

	I changed my mind and decided to keep the original signature.  The
reason why it's better that way is that sometimes you have a vector of
CORBA_any and you want initialize each element of such vector in-place. 
Other similar situations may arise.  Anyway, here's a diff of the
bonobo-arg changes.

> 
> > 	OK.  In fact I didn't touch the old functions, so I expect
> > this to have zero impact on existing applications.
> 
> 	Sure; of course I think it's prolly more ideal to have a fast in-lined
> 'case statement' mapping for the base types instead of always going to
> the hash-table; would be a smaller chunk of code too I think, and
> fall-back to the hash if we can't do it ourselves. [ also saves us from
> strange people re-registering bogus types and having problems ;-].
> 
> 	Apart from that looks great, please do get it in,

	Bonobo Unique Application just hit CVS! ;-)

	I also improved the test program a bit.  It's more like an example than
a test program, actually.  Maybe it should be moved elsewhere...

	Now, I'll continue coding to fix remaining issues next weekend.

-- 
Gustavo J. A. M. Carneiro
<gjc inescporto pt> <gustavo users sourceforge net>
Index: bonobo-arg.c
===================================================================
RCS file: /cvs/gnome/libbonobo/bonobo/bonobo-arg.c,v
retrieving revision 1.30
diff -u -p -r1.30 bonobo-arg.c
--- bonobo-arg.c	23 Sep 2003 10:24:16 -0000	1.30
+++ bonobo-arg.c	17 Nov 2003 21:51:37 -0000
@@ -1,3 +1,4 @@
+/* -*- mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * bonobo-arg.c Bonobo argument support:
  *
@@ -13,6 +14,14 @@
 #include <bonobo/bonobo-main.h>
 #include <bonobo/bonobo-exception.h>
 #include <bonobo/bonobo-arg.h>
+#include "bonobo-types.h"
+
+
+/* Key: CORBA_TypeCode; Value: BonoboArgToGValueFn. */
+static GHashTable *bonobo_arg_to_gvalue_mapping   = NULL;
+/* Key: GType; Value: BonoboArgFromGValueFn */
+static GHashTable *bonobo_arg_from_gvalue_mapping = NULL;
+
 
 /**
  * bonobo_arg_new:
@@ -388,3 +397,159 @@ bonobo_arg_is_equal (const BonoboArg *a,
 
 	return retval;
 }
+
+
+/**
+ * bonobo_arg_to_gvalue_copy:
+ * @arg: source value
+ * @value: destination value
+ * 
+ * Converts a #BonoboArg @arg into a #GValue.  Unlike
+ * bonobo_arg_to_gvalue(), the destination #GValue does not need to --
+ * and should not -- be initialized.
+ * 
+ * Return value: Returns %TRUE if conversion succeeds, %FALSE otherwise.
+ **/
+gboolean
+bonobo_arg_to_gvalue_copy (BonoboArg const *arg, GValue *value)
+{
+	BonoboArgToGValueFn converter;
+
+	g_assert (bonobo_arg_from_gvalue_mapping);
+
+#define TO_GVALUE_CASE(gtypename, bonoboargname, typename, typecode)			\
+	if (CORBA_TypeCode_equal(arg->_type, typecode, NULL)) {				\
+		g_value_init (value, G_TYPE_##gtypename);				\
+		g_value_set_##typename (value, BONOBO_ARG_GET_##bonoboargname(arg));	\
+		return TRUE;								\
+	}
+
+	TO_GVALUE_CASE (STRING,  STRING,  string,  TC_CORBA_string);
+	TO_GVALUE_CASE (CHAR,    CHAR,    char,    TC_CORBA_char);
+	TO_GVALUE_CASE (BOOLEAN, BOOLEAN, boolean, TC_CORBA_boolean);
+	TO_GVALUE_CASE (LONG,    LONG,    long,    TC_CORBA_long);
+	TO_GVALUE_CASE (ULONG,   ULONG,   ulong,   TC_CORBA_unsigned_long);
+	TO_GVALUE_CASE (FLOAT,   FLOAT,   float,   TC_CORBA_float);
+	TO_GVALUE_CASE (DOUBLE,  DOUBLE,  double,  TC_CORBA_double);
+	
+	converter = g_hash_table_lookup (bonobo_arg_to_gvalue_mapping,
+					 arg->_type);
+	if (converter)
+	    converter (arg, value);
+	else
+	    return FALSE;
+	return TRUE;
+}
+
+
+/**
+ * bonobo_arg_to_gvalue_copy:
+ * @arg: destination value
+ * @value: source value
+ * 
+ * Converts a #GValue into a #BonoboArg.  Unlike
+ * bonobo_arg_from_gvalue(), the destination #BonoboArg does not need
+ * to -- and should not -- be initialized.
+ * 
+ * Return value: Returns %TRUE if conversion succeeds, %FALSE otherwise.
+ **/
+gboolean
+bonobo_arg_from_gvalue_copy (BonoboArg *arg, GValue const *value)
+{
+	BonoboArgFromGValueFn converter;
+
+	g_return_val_if_fail (arg, FALSE);
+	g_return_val_if_fail (value, FALSE);
+	g_assert (bonobo_arg_from_gvalue_mapping);
+
+#define FROM_GVALUE_CASE(gtype, gtypename, tcid, corbatype)				\
+case G_TYPE_##gtype:									\
+	arg->_type = tcid;								\
+	arg->_value = ORBit_alloc_tcval (tcid, 1);					\
+	*((corbatype *)arg->_value) = (corbatype) g_value_get_##gtypename (value);	\
+	arg->_release = CORBA_TRUE;							\
+	return TRUE;
+	
+	switch (G_VALUE_TYPE (value))
+	{
+		FROM_GVALUE_CASE (CHAR,    char,    TC_CORBA_char,          CORBA_char);
+		FROM_GVALUE_CASE (UCHAR,   uchar,   TC_CORBA_char,          CORBA_char);
+		FROM_GVALUE_CASE (BOOLEAN, boolean, TC_CORBA_boolean,       CORBA_boolean);
+		FROM_GVALUE_CASE (INT,     int,     TC_CORBA_long,          CORBA_long);
+		FROM_GVALUE_CASE (UINT,    uint,    TC_CORBA_unsigned_long, CORBA_unsigned_long);
+		FROM_GVALUE_CASE (LONG,    long,    TC_CORBA_long,          CORBA_long);
+		FROM_GVALUE_CASE (ULONG,   ulong,   TC_CORBA_unsigned_long, CORBA_unsigned_long);
+		FROM_GVALUE_CASE (FLOAT,   float,   TC_CORBA_float,         CORBA_float);
+		FROM_GVALUE_CASE (DOUBLE,  double,  TC_CORBA_double,        CORBA_double);
+#undef FROM_GVALUE_FN
+
+	case G_TYPE_STRING:
+		*((CORBA_char **)arg->_value) =
+			CORBA_string_dup (g_value_get_string (value));
+		arg->_release = CORBA_TRUE;
+		return TRUE;
+	}
+	  /* default: try to lookup a converter function */
+	converter = g_hash_table_lookup (bonobo_arg_from_gvalue_mapping,
+					 GUINT_TO_POINTER (G_VALUE_TYPE (value)));
+	if (converter)
+		converter (arg, value);
+	else
+		return FALSE;
+	return TRUE;
+}
+
+
+/* GValue => BonoboArg converters */
+
+static void
+__bonobo_arg_from_CORBA_ANY (BonoboArg    *out_arg,
+			     GValue const *value)
+{
+	out_arg->_type    = TC_CORBA_any;
+	out_arg->_value   = bonobo_value_get_corba_any (value);
+	out_arg->_release = CORBA_TRUE;
+}
+
+static void
+__TC_CORBA_any_to_gvalue (BonoboArg const *arg,
+			  GValue          *out_value)
+{
+	g_value_init (out_value, BONOBO_TYPE_CORBA_ANY);
+	bonobo_value_set_corba_any (out_value, arg->_value);
+}
+
+
+void
+bonobo_arg_register_from_gvalue_converter (GType                 gtype,
+					   BonoboArgFromGValueFn converter)
+{
+	g_return_if_fail (bonobo_arg_from_gvalue_mapping != NULL);
+	g_hash_table_insert (bonobo_arg_from_gvalue_mapping,
+			     GUINT_TO_POINTER (gtype),
+			     converter);
+}
+
+void
+bonobo_arg_register_to_gvalue_converter (BonoboArgType       arg_type,
+					 BonoboArgToGValueFn converter)
+{
+	g_return_if_fail (bonobo_arg_to_gvalue_mapping != NULL);
+	g_hash_table_insert (bonobo_arg_to_gvalue_mapping,
+			     arg_type, converter);
+}
+
+void bonobo_arg_init (void)
+{
+	g_assert (bonobo_arg_to_gvalue_mapping == NULL);
+	g_assert (bonobo_arg_from_gvalue_mapping == NULL);
+
+	bonobo_arg_to_gvalue_mapping   = g_hash_table_new (g_direct_hash, g_direct_equal);
+	bonobo_arg_from_gvalue_mapping = g_hash_table_new (g_direct_hash, g_direct_equal);
+
+	bonobo_arg_register_from_gvalue_converter 
+		(BONOBO_TYPE_CORBA_ANY, __bonobo_arg_from_CORBA_ANY);
+	bonobo_arg_register_to_gvalue_converter
+		(TC_CORBA_any, __TC_CORBA_any_to_gvalue);
+}
+
Index: bonobo-arg.h
===================================================================
RCS file: /cvs/gnome/libbonobo/bonobo/bonobo-arg.h,v
retrieving revision 1.29
diff -u -p -r1.29 bonobo-arg.h
--- bonobo-arg.h	11 Mar 2003 17:55:59 -0000	1.29
+++ bonobo-arg.h	17 Nov 2003 21:51:37 -0000
@@ -21,6 +21,13 @@ G_BEGIN_DECLS
 typedef CORBA_any      BonoboArg;
 typedef CORBA_TypeCode BonoboArgType;
 
+typedef void (*BonoboArgToGValueFn)   (BonoboArg const *arg,
+				       GValue          *out_value);
+
+typedef void (*BonoboArgFromGValueFn) (BonoboArg       *out_arg,
+				       GValue const    *value);
+
+
 #define BONOBO_ARG_NULL     TC_null
 #define BONOBO_ARG_BOOLEAN  TC_CORBA_boolean
 #define BONOBO_ARG_SHORT    TC_CORBA_short
@@ -51,6 +58,8 @@ typedef CORBA_TypeCode BonoboArgType;
 #define BONOBO_ARG_SET_INT(a,v)     (BONOBO_ARG_SET_GENERAL (a, v, TC_CORBA_long, CORBA_long, NULL))
 #define BONOBO_ARG_GET_LONG(a)      (BONOBO_ARG_GET_GENERAL (a, TC_CORBA_long, CORBA_long, NULL))
 #define BONOBO_ARG_SET_LONG(a,v)    (BONOBO_ARG_SET_GENERAL (a, v, TC_CORBA_long, CORBA_long, NULL))
+#define BONOBO_ARG_GET_ULONG(a)     (BONOBO_ARG_GET_GENERAL (a, TC_CORBA_unsigned_long, CORBA_unsigned_long, NULL))
+#define BONOBO_ARG_SET_ULONG(a,v)   (BONOBO_ARG_SET_GENERAL (a, v, TC_CORBA_unsigned_long, CORBA_unsigned_long, NULL))
 #define BONOBO_ARG_GET_LONGLONG(a)  (BONOBO_ARG_GET_GENERAL (a, TC_CORBA_long_long, CORBA_long_long, NULL))
 #define BONOBO_ARG_SET_LONGLONG(a,v) (BONOBO_ARG_SET_GENERAL (a, v, TC_CORBA_long_long, CORBA_long_long, NULL))
 
@@ -99,6 +108,18 @@ gboolean      bonobo_arg_is_equal       
 gboolean      bonobo_arg_type_is_equal   (BonoboArgType      a, 
 					  BonoboArgType      b,
 					  CORBA_Environment *opt_ev);
+
+gboolean bonobo_arg_to_gvalue_copy                 (BonoboArg const       *arg,
+						    GValue                *value);
+gboolean bonobo_arg_from_gvalue_copy               (BonoboArg             *arg,
+						    GValue const          *value);
+void     bonobo_arg_register_to_gvalue_converter   (BonoboArgType          arg_type,
+						    BonoboArgToGValueFn    converter);
+void     bonobo_arg_register_from_gvalue_converter (GType                  gtype,
+						    BonoboArgFromGValueFn  converter);
+  /* private */
+void     bonobo_arg_init                           (void);
+
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]