static void gimp_text_undo_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { GimpTextUndo *text_undo = GIMP_TEXT_UNDO (object); switch (property_id) { case PROP_PARAM: text_undo->pspec = g_value_get_param (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } }
static HaskellObj #ifdef GHC_RTS_USES_CAPABILITY gtk2hs_value_as_haskellobj(Capability *cap, const GValue *value) { #else gtk2hs_value_as_haskellobj(const GValue *value) { #endif switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) { case G_TYPE_INTERFACE: if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT)) return rts_mkPtr(CAP g_value_get_object(value)); else break; case G_TYPE_CHAR: return rts_mkChar(CAP g_value_get_schar(value)); case G_TYPE_UCHAR: return rts_mkChar(CAP g_value_get_uchar(value)); case G_TYPE_BOOLEAN: return rts_mkBool(CAP g_value_get_boolean(value)); case G_TYPE_INT: return rts_mkInt(CAP g_value_get_int(value)); case G_TYPE_UINT: return rts_mkWord(CAP g_value_get_uint(value)); case G_TYPE_LONG: return rts_mkInt(CAP g_value_get_long(value)); case G_TYPE_ULONG: return rts_mkWord(CAP g_value_get_ulong(value)); /* case G_TYPE_INT64: return rts_mkInt64(CAP g_value_get_int64(value)); case G_TYPE_UINT64: return rts_mkWord64(CAP g_value_get_uint64(value)); */ case G_TYPE_ENUM: return rts_mkInt(CAP g_value_get_enum(value)); case G_TYPE_FLAGS: return rts_mkWord(CAP g_value_get_enum(value)); case G_TYPE_FLOAT: return rts_mkFloat(CAP g_value_get_float(value)); case G_TYPE_DOUBLE: return rts_mkDouble(CAP g_value_get_double(value)); case G_TYPE_STRING: return rts_mkPtr(CAP (char *)g_value_get_string(value)); /* CHECKME: is the string freed? */ case G_TYPE_POINTER: return rts_mkPtr(CAP g_value_get_pointer(value)); case G_TYPE_BOXED: return rts_mkPtr(CAP g_value_get_boxed(value)); case G_TYPE_PARAM: return rts_mkPtr(CAP g_value_get_param(value)); case G_TYPE_OBJECT: return rts_mkPtr(CAP g_value_get_object(value)); } g_error("gtk2hs_value_as_haskellobj: unable to handle GValue with type %s\n" "please report this as a bug to [email protected]", g_type_name(G_VALUE_TYPE(value))); } void gtk2hs_value_from_haskellobj(GValue *value, HaskellObj obj) { switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) { case G_TYPE_INVALID: case G_TYPE_NONE: return; case G_TYPE_INTERFACE: /* we only handle interface types that have a GObject prereq */ if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT)) { g_value_set_object(value, rts_getPtr(obj)); } else { break; } return; case G_TYPE_CHAR: g_value_set_schar(value, rts_getChar(obj)); return; case G_TYPE_UCHAR: g_value_set_schar(value, rts_getChar(obj)); return; case G_TYPE_BOOLEAN: g_value_set_boolean(value, rts_getBool(obj)); return; case G_TYPE_INT: g_value_set_int(value, rts_getInt(obj)); return; case G_TYPE_UINT: g_value_set_uint(value, rts_getWord(obj)); return; case G_TYPE_LONG: g_value_set_long(value, rts_getInt(obj)); return; case G_TYPE_ULONG: g_value_set_ulong(value, rts_getWord(obj)); return; /* case G_TYPE_INT64: g_value_set_int64(value, rts_getInt64(obj)); return; case G_TYPE_UINT64: g_value_set_uint64(value, rts_getWord64(obj)); return; */ case G_TYPE_ENUM: g_value_set_enum(value, rts_getInt(obj)); return; case G_TYPE_FLAGS: g_value_set_flags(value, rts_getInt(obj)); return; case G_TYPE_FLOAT: g_value_set_float(value, rts_getFloat(obj)); return; case G_TYPE_DOUBLE: g_value_set_double(value, rts_getDouble(obj)); return; case G_TYPE_STRING: g_value_set_string(value, rts_getPtr(obj)); return; case G_TYPE_POINTER: g_value_set_pointer(value, rts_getPtr(obj)); return; /* case G_TYPE_BOXED: { g_value_set_boxed(value, obj); break; } case G_TYPE_PARAM: g_value_set_param(value, (obj)); break; */ case G_TYPE_OBJECT: g_value_set_object(value, rts_getPtr(obj)); return; } g_error("gtk2hs_value_from_haskellobj: unable to handle GValue with type %s\n" "please report this as a bug to [email protected]", g_type_name(G_VALUE_TYPE(value))); }
VALUE rbgobj_gvalue_to_rvalue(const GValue* value) { GType type, fundamental_type; VALUE rvalue; if (!value) return Qnil; type = G_VALUE_TYPE(value); if (rbgobj_convert_gvalue2rvalue(type, value, &rvalue)) return rvalue; fundamental_type = G_TYPE_FUNDAMENTAL(type); switch (fundamental_type) { case G_TYPE_NONE: return Qnil; case G_TYPE_CHAR: return CHR2FIX(g_value_get_char(value)); case G_TYPE_UCHAR: return INT2FIX(g_value_get_uchar(value)); case G_TYPE_BOOLEAN: return CBOOL2RVAL(g_value_get_boolean(value)); case G_TYPE_INT: return INT2NUM(g_value_get_int(value)); case G_TYPE_UINT: return UINT2NUM(g_value_get_uint(value)); case G_TYPE_LONG: return LONG2NUM(g_value_get_long(value)); case G_TYPE_ULONG: return ULONG2NUM(g_value_get_ulong(value)); case G_TYPE_INT64: return rbglib_int64_to_num(g_value_get_int64(value)); case G_TYPE_UINT64: return rbglib_uint64_to_num(g_value_get_uint64(value)); case G_TYPE_FLOAT: return rb_float_new(g_value_get_float(value)); case G_TYPE_DOUBLE: return rb_float_new(g_value_get_double(value)); case G_TYPE_STRING: return CSTR2RVAL(g_value_get_string(value)); case G_TYPE_ENUM: return rbgobj_make_enum(g_value_get_enum(value), type); case G_TYPE_FLAGS: return rbgobj_make_flags(g_value_get_flags(value), type); case G_TYPE_OBJECT: case G_TYPE_INTERFACE: { GObject* gobj = g_value_get_object(value); return gobj ? GOBJ2RVAL(gobj) : Qnil; } case G_TYPE_PARAM: { GParamSpec* pspec = g_value_get_param(value); return pspec ? rbgobj_ruby_object_from_instance(pspec) : Qnil; } case G_TYPE_POINTER: { gpointer ptr = g_value_get_pointer(value); if (!ptr) return Qnil; else return rbgobj_ptr_new(type, ptr); } case G_TYPE_BOXED: { GType gtype; for (gtype = type; gtype != G_TYPE_INVALID; gtype = g_type_parent(gtype)) { GValueToRValueFunc func = g_type_get_qdata(gtype, qGValueToRValueFunc); if (!func) continue; return func(value); } } default: if (!rbgobj_convert_gvalue2rvalue(fundamental_type, value, &rvalue)) { GValueToRValueFunc func; func = g_type_get_qdata(type, qGValueToRValueFunc); if (!func) { g_warning("rbgobj_gvalue_to_rvalue: unsupported type: %s\n", g_type_name(type)); } else { rvalue = func(value); } } return rvalue; } }
static JSBool gjs_value_from_g_value_internal(JSContext *context, jsval *value_p, const GValue *gvalue, gboolean no_copy, GSignalQuery *signal_query, gint arg_n) { GType gtype; gtype = G_VALUE_TYPE(gvalue); gjs_debug_marshal(GJS_DEBUG_GCLOSURE, "Converting gtype %s to jsval", g_type_name(gtype)); if (gtype == G_TYPE_STRING) { const char *v; v = g_value_get_string(gvalue); if (v == NULL) { gjs_debug_marshal(GJS_DEBUG_GCLOSURE, "Converting NULL string to JSVAL_NULL"); *value_p = JSVAL_NULL; } else { if (!gjs_string_from_utf8(context, v, -1, value_p)) return JS_FALSE; } } else if (gtype == G_TYPE_CHAR) { char v; v = g_value_get_schar(gvalue); *value_p = INT_TO_JSVAL(v); } else if (gtype == G_TYPE_UCHAR) { unsigned char v; v = g_value_get_uchar(gvalue); *value_p = INT_TO_JSVAL(v); } else if (gtype == G_TYPE_INT) { int v; v = g_value_get_int(gvalue); return JS_NewNumberValue(context, v, value_p); } else if (gtype == G_TYPE_UINT) { uint v; v = g_value_get_uint(gvalue); return JS_NewNumberValue(context, v, value_p); } else if (gtype == G_TYPE_DOUBLE) { double d; d = g_value_get_double(gvalue); return JS_NewNumberValue(context, d, value_p); } else if (gtype == G_TYPE_FLOAT) { double d; d = g_value_get_float(gvalue); return JS_NewNumberValue(context, d, value_p); } else if (gtype == G_TYPE_BOOLEAN) { gboolean v; v = g_value_get_boolean(gvalue); *value_p = BOOLEAN_TO_JSVAL(v); } else if (g_type_is_a(gtype, G_TYPE_OBJECT) || g_type_is_a(gtype, G_TYPE_INTERFACE)) { GObject *gobj; JSObject *obj; gobj = g_value_get_object(gvalue); obj = gjs_object_from_g_object(context, gobj); *value_p = OBJECT_TO_JSVAL(obj); } else if (gtype == G_TYPE_STRV) { if (!gjs_array_from_strv (context, value_p, g_value_get_boxed (gvalue))) { gjs_throw(context, "Failed to convert strv to array"); return JS_FALSE; } } else if (g_type_is_a(gtype, G_TYPE_HASH_TABLE) || g_type_is_a(gtype, G_TYPE_ARRAY) || g_type_is_a(gtype, G_TYPE_BYTE_ARRAY) || g_type_is_a(gtype, G_TYPE_PTR_ARRAY)) { gjs_throw(context, "Unable to introspect element-type of container in GValue"); return JS_FALSE; } else if (g_type_is_a(gtype, G_TYPE_BOXED) || g_type_is_a(gtype, G_TYPE_VARIANT)) { GjsBoxedCreationFlags boxed_flags; GIBaseInfo *info; void *gboxed; JSObject *obj; if (g_type_is_a(gtype, G_TYPE_BOXED)) gboxed = g_value_get_boxed(gvalue); else gboxed = g_value_get_variant(gvalue); boxed_flags = GJS_BOXED_CREATION_NONE; /* special case GError */ if (g_type_is_a(gtype, G_TYPE_ERROR)) { obj = gjs_error_from_gerror(context, gboxed, FALSE); *value_p = OBJECT_TO_JSVAL(obj); return TRUE; } /* The only way to differentiate unions and structs is from * their g-i info as both GBoxed */ info = g_irepository_find_by_gtype(g_irepository_get_default(), gtype); if (info == NULL) { gjs_throw(context, "No introspection information found for %s", g_type_name(gtype)); return JS_FALSE; } if (g_base_info_get_type(info) == GI_INFO_TYPE_STRUCT && g_struct_info_is_foreign((GIStructInfo*)info)) { JSBool ret; GIArgument arg; arg.v_pointer = gboxed; ret = gjs_struct_foreign_convert_from_g_argument(context, value_p, info, &arg); g_base_info_unref(info); return ret; } switch (g_base_info_get_type(info)) { case GI_INFO_TYPE_BOXED: case GI_INFO_TYPE_STRUCT: if (no_copy) boxed_flags |= GJS_BOXED_CREATION_NO_COPY; obj = gjs_boxed_from_c_struct(context, (GIStructInfo *)info, gboxed, boxed_flags); break; case GI_INFO_TYPE_UNION: obj = gjs_union_from_c_union(context, (GIUnionInfo *)info, gboxed); break; default: gjs_throw(context, "Unexpected introspection type %d for %s", g_base_info_get_type(info), g_type_name(gtype)); g_base_info_unref(info); return JS_FALSE; } *value_p = OBJECT_TO_JSVAL(obj); g_base_info_unref(info); } else if (g_type_is_a(gtype, G_TYPE_ENUM)) { return convert_int_to_enum(context, value_p, gtype, g_value_get_enum(gvalue)); } else if (g_type_is_a(gtype, G_TYPE_PARAM)) { GParamSpec *gparam; JSObject *obj; gparam = g_value_get_param(gvalue); obj = gjs_param_from_g_param(context, gparam); *value_p = OBJECT_TO_JSVAL(obj); } else if (signal_query && g_type_is_a(gtype, G_TYPE_POINTER)) { JSBool res; GArgument arg; GIArgInfo *arg_info; GIBaseInfo *obj; GISignalInfo *signal_info; GITypeInfo type_info; obj = g_irepository_find_by_gtype(NULL, signal_query->itype); if (!obj) { gjs_throw(context, "Signal argument with GType %s isn't introspectable", g_type_name(signal_query->itype)); return JS_FALSE; } signal_info = g_object_info_find_signal((GIObjectInfo*)obj, signal_query->signal_name); if (!signal_info) { gjs_throw(context, "Unknown signal."); g_base_info_unref((GIBaseInfo*)obj); return JS_FALSE; } arg_info = g_callable_info_get_arg(signal_info, arg_n - 1); g_arg_info_load_type(arg_info, &type_info); arg.v_pointer = g_value_get_pointer(gvalue); res = gjs_value_from_g_argument(context, value_p, &type_info, &arg, TRUE); g_base_info_unref((GIBaseInfo*)arg_info); g_base_info_unref((GIBaseInfo*)signal_info); g_base_info_unref((GIBaseInfo*)obj); return res; } else if (g_type_is_a(gtype, G_TYPE_POINTER)) { gpointer pointer; pointer = g_value_get_pointer(gvalue); if (pointer == NULL) { *value_p = JSVAL_NULL; } else { gjs_throw(context, "Can't convert non-null pointer to JS value"); return JS_FALSE; } } else if (g_value_type_transformable(gtype, G_TYPE_DOUBLE)) { GValue double_value = { 0, }; double v; g_value_init(&double_value, G_TYPE_DOUBLE); g_value_transform(gvalue, &double_value); v = g_value_get_double(&double_value); return JS_NewNumberValue(context, v, value_p); } else if (g_value_type_transformable(gtype, G_TYPE_INT)) { GValue int_value = { 0, }; int v; g_value_init(&int_value, G_TYPE_INT); g_value_transform(gvalue, &int_value); v = g_value_get_int(&int_value); return JS_NewNumberValue(context, v, value_p); } else { gjs_throw(context, "Don't know how to convert GType %s to JavaScript object", g_type_name(gtype)); return JS_FALSE; } return JS_TRUE; }
/** * pyg_value_as_pyobject: * @value: the GValue object. * @copy_boxed: true if boxed values should be copied. * * This function creates/returns a Python wrapper object that * represents the GValue passed as an argument. * * Returns: a PyObject representing the value. */ PyObject * pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed) { gchar buf[128]; switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) { case G_TYPE_INTERFACE: if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT)) return pygobject_new(g_value_get_object(value)); else break; case G_TYPE_CHAR: { gint8 val = g_value_get_char(value); return PYGLIB_PyUnicode_FromStringAndSize((char *)&val, 1); } case G_TYPE_UCHAR: { guint8 val = g_value_get_uchar(value); return PYGLIB_PyBytes_FromStringAndSize((char *)&val, 1); } case G_TYPE_BOOLEAN: { return PyBool_FromLong(g_value_get_boolean(value)); } case G_TYPE_INT: return PYGLIB_PyLong_FromLong(g_value_get_int(value)); case G_TYPE_UINT: { /* in Python, the Int object is backed by a long. If a long can hold the whole value of an unsigned int, use an Int. Otherwise, use a Long object to avoid overflow. This matches the ULongArg behavior in codegen/argtypes.h */ #if (G_MAXUINT <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) g_value_get_uint(value)); #else return PyLong_FromUnsignedLong((gulong) g_value_get_uint(value)); #endif } case G_TYPE_LONG: return PYGLIB_PyLong_FromLong(g_value_get_long(value)); case G_TYPE_ULONG: { gulong val = g_value_get_ulong(value); if (val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromUnsignedLong(val); } case G_TYPE_INT64: { gint64 val = g_value_get_int64(value); if (G_MINLONG <= val && val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromLongLong(val); } case G_TYPE_UINT64: { guint64 val = g_value_get_uint64(value); if (val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromUnsignedLongLong(val); } case G_TYPE_ENUM: return pyg_enum_from_gtype(G_VALUE_TYPE(value), g_value_get_enum(value)); case G_TYPE_FLAGS: return pyg_flags_from_gtype(G_VALUE_TYPE(value), g_value_get_flags(value)); case G_TYPE_FLOAT: return PyFloat_FromDouble(g_value_get_float(value)); case G_TYPE_DOUBLE: return PyFloat_FromDouble(g_value_get_double(value)); case G_TYPE_STRING: { const gchar *str = g_value_get_string(value); if (str) return PYGLIB_PyUnicode_FromString(str); Py_INCREF(Py_None); return Py_None; } case G_TYPE_POINTER: return pyg_pointer_new(G_VALUE_TYPE(value), g_value_get_pointer(value)); case G_TYPE_BOXED: { PyGTypeMarshal *bm; if (G_VALUE_HOLDS(value, PY_TYPE_OBJECT)) { PyObject *ret = (PyObject *)g_value_dup_boxed(value); if (ret == NULL) { Py_INCREF(Py_None); return Py_None; } return ret; } else if (G_VALUE_HOLDS(value, G_TYPE_VALUE)) { GValue *n_value = g_value_get_boxed (value); return pyg_value_as_pyobject(n_value, copy_boxed); } else if (G_VALUE_HOLDS(value, G_TYPE_VALUE_ARRAY)) { GValueArray *array = (GValueArray *) g_value_get_boxed(value); PyObject *ret = PyList_New(array->n_values); int i; for (i = 0; i < array->n_values; ++i) PyList_SET_ITEM(ret, i, pyg_value_as_pyobject (array->values + i, copy_boxed)); return ret; } else if (G_VALUE_HOLDS(value, G_TYPE_GSTRING)) { GString *string = (GString *) g_value_get_boxed(value); PyObject *ret = PYGLIB_PyUnicode_FromStringAndSize(string->str, string->len); return ret; } bm = pyg_type_lookup(G_VALUE_TYPE(value)); if (bm) { return bm->fromvalue(value); } else { if (copy_boxed) return pyg_boxed_new(G_VALUE_TYPE(value), g_value_get_boxed(value), TRUE, TRUE); else return pyg_boxed_new(G_VALUE_TYPE(value), g_value_get_boxed(value),FALSE,FALSE); } } case G_TYPE_PARAM: return pyg_param_spec_new(g_value_get_param(value)); case G_TYPE_OBJECT: return pygobject_new(g_value_get_object(value)); default: { PyGTypeMarshal *bm; if ((bm = pyg_type_lookup(G_VALUE_TYPE(value)))) return bm->fromvalue(value); break; } } g_snprintf(buf, sizeof(buf), "unknown type %s", g_type_name(G_VALUE_TYPE(value))); PyErr_SetString(PyExc_TypeError, buf); return NULL; }