static void test_defaults (GType type, const char *name) { GParamSpec **property_specs; guint n_property_specs; GObject *setting; int i; setting = g_object_new (type, NULL); property_specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (setting), &n_property_specs); ASSERT (property_specs != NULL, name, "couldn't find property specs for object of type '%s'", g_type_name (G_OBJECT_TYPE (setting))); for (i = 0; i < n_property_specs; i++) { GParamSpec *prop_spec = property_specs[i]; GValue value = G_VALUE_INIT; GValue defvalue = G_VALUE_INIT; char *actual, *expected; gboolean ok = FALSE; /* Ignore non-fundamental types since they won't really have * defaults. */ if (!G_TYPE_IS_FUNDAMENTAL (prop_spec->value_type)) continue; g_value_init (&value, prop_spec->value_type); g_object_get_property (G_OBJECT (setting), prop_spec->name, &value); g_value_init (&defvalue, prop_spec->value_type); g_param_value_set_default (prop_spec, &defvalue); actual = g_strdup_value_contents (&value); expected = g_strdup_value_contents (&defvalue); if (!strcmp (prop_spec->name, NM_SETTING_NAME)) { /* 'name' is always the setting name, not the default value */ ok = !strcmp (nm_setting_get_name (NM_SETTING (setting)), name); g_free (expected); expected = g_strdup (name); } else ok = g_param_value_defaults (prop_spec, &value); ASSERT (ok, name, "property '%s' value '%s' not the expected default value '%s'", prop_spec->name, actual, expected); g_free (actual); g_free (expected); g_value_unset (&value); g_value_unset (&defvalue); } g_free (property_specs); g_object_unref (setting); }
static GTokenType gimp_config_deserialize_value (GValue *value, GimpConfig *config, GParamSpec *prop_spec, GScanner *scanner) { if (G_TYPE_FUNDAMENTAL (prop_spec->value_type) == G_TYPE_ENUM) { return gimp_config_deserialize_enum (value, prop_spec, scanner); } else if (G_TYPE_IS_FUNDAMENTAL (prop_spec->value_type)) { return gimp_config_deserialize_fundamental (value, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_MEMSIZE) { return gimp_config_deserialize_memsize (value, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_CONFIG_PATH) { return gimp_config_deserialize_path (value, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_RGB) { return gimp_config_deserialize_rgb (value, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_MATRIX2) { return gimp_config_deserialize_matrix2 (value, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_VALUE_ARRAY) { return gimp_config_deserialize_value_array (value, config, prop_spec, scanner); } else if (prop_spec->value_type == GIMP_TYPE_UNIT) { return gimp_config_deserialize_unit (value, prop_spec, scanner); } else if (prop_spec->value_type == G_TYPE_FILE) { return gimp_config_deserialize_file_value (value, prop_spec, scanner); } /* This fallback will only work for value_types that * can be transformed from a string value. */ return gimp_config_deserialize_any (value, prop_spec, scanner); }
/* Help function to dump a GType */ void my_dump_type(GType type_id) { printf("Type id: %d\n", type_id); printf("Type name: %s\n", g_type_name(type_id)); printf("Is fundamental? %s\n", G_TYPE_IS_FUNDAMENTAL(type_id) ? "yes" : "no"); printf("Is derived? %s\n", G_TYPE_IS_DERIVED(type_id) ? "yes" : "no"); printf("Is interface? %s\n", G_TYPE_IS_INTERFACE(type_id) ? "yes" : "no"); printf("Is classed? %s\n", G_TYPE_IS_CLASSED(type_id) ? "yes" : "no"); printf("Is instantiatable? %s\n", G_TYPE_IS_INSTANTIATABLE(type_id) ? "yes" : "no"); printf("Is derivable? %s\n", G_TYPE_IS_DERIVABLE(type_id) ? "yes" : "no"); printf("Is deep derivable? %s\n", G_TYPE_IS_DEEP_DERIVABLE(type_id) ? "yes" : "no"); printf("Is abstract? %s\n", G_TYPE_IS_ABSTRACT(type_id) ? "yes" : "no"); printf("Is value abstract? %s\n", G_TYPE_IS_VALUE_ABSTRACT(type_id) ? "yes" : "no"); printf("Is value type: %s\n", G_TYPE_IS_VALUE_TYPE(type_id) ? "yes" : "no"); printf("Has value table: %s\n", G_TYPE_HAS_VALUE_TABLE(type_id) ? "yes" : "no"); }
static GVariant * serialize_pspec(GvsSerializer *self, GParamSpec *pspec, const GValue *value) { GvsPropertySerializeFunc func = NULL; GVariant *variant = NULL; GType type = pspec->value_type; /* Try to find the right serialization function */ func = g_param_spec_get_qdata(pspec, gvs_property_serialize_func_quark()); if (func == NULL) { if (G_TYPE_IS_FUNDAMENTAL(type)) { func = serialize_fundamental; } else if (G_TYPE_IS_ENUM(type)) { func = serialize_enum; } else if (G_TYPE_IS_FLAGS(type)) { func = serialize_flags; } else if (G_TYPE_IS_OBJECT(type) || G_TYPE_IS_INTERFACE (type)) { func = serialize_object_property; } else if (g_type_is_a(type, G_TYPE_BOXED)) { func = serialize_boxed_property; } } if (func) { variant = func(self, value, NULL); } else { g_warning("Could not serialize property %s of type %s\n" "Use gvs_register_property_serialize_func() in your class_init function\n", pspec->name, g_type_name(pspec->value_type)); } return variant; }
/** * shp_complextype_has value: * @self: a #ShpComplextype * @name: data's field name * @type: self type * * Check whether there is a field with name @name and of type @type in the * data. This function is normally called before calling any of the * shp_complextype_get_*() set of functions. * * Returns: TRUE if such a field exists and FALSE otherwise */ gboolean shp_complextype_has_value (ShpComplextype * self, const gchar * name, GType type) { ShpComplextypePrivate *priv; _ShpValue *value; g_return_val_if_fail (IS_SHP_COMPLEXTYPE (self), FALSE); g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (G_TYPE_IS_FUNDAMENTAL (type), FALSE); priv = self->priv; value = g_hash_table_lookup (priv->values, name); if (value == NULL) return FALSE; if (G_VALUE_TYPE (value) == type) return TRUE; return FALSE; }
static VALUE rg_fundamental_p(VALUE self) { return CBOOL2RVAL(G_TYPE_IS_FUNDAMENTAL(rbgobj_gtype_get(self))); }
static void print_signal_info (GstElement * element) { /* Signals/Actions Block */ guint *signals; guint nsignals; gint i = 0, j, k; GSignalQuery *query = NULL; GType type; GSList *found_signals, *l; for (k = 0; k < 2; k++) { found_signals = NULL; /* For elements that have sometimes pads, also list a few useful GstElement * signals. Put these first, so element-specific ones come later. */ if (k == 0 && has_sometimes_template (element)) { query = g_new0 (GSignalQuery, 1); g_signal_query (g_signal_lookup ("pad-added", GST_TYPE_ELEMENT), query); found_signals = g_slist_append (found_signals, query); query = g_new0 (GSignalQuery, 1); g_signal_query (g_signal_lookup ("pad-removed", GST_TYPE_ELEMENT), query); found_signals = g_slist_append (found_signals, query); query = g_new0 (GSignalQuery, 1); g_signal_query (g_signal_lookup ("no-more-pads", GST_TYPE_ELEMENT), query); found_signals = g_slist_append (found_signals, query); } for (type = G_OBJECT_TYPE (element); type; type = g_type_parent (type)) { if (type == GST_TYPE_ELEMENT || type == GST_TYPE_OBJECT) break; if (type == GST_TYPE_BIN && G_OBJECT_TYPE (element) != GST_TYPE_BIN) continue; signals = g_signal_list_ids (type, &nsignals); for (i = 0; i < nsignals; i++) { query = g_new0 (GSignalQuery, 1); g_signal_query (signals[i], query); if ((k == 0 && !(query->signal_flags & G_SIGNAL_ACTION)) || (k == 1 && (query->signal_flags & G_SIGNAL_ACTION))) found_signals = g_slist_append (found_signals, query); else g_free (query); } g_free (signals); signals = NULL; } if (found_signals) { n_print ("\n"); if (k == 0) n_print ("Element Signals:\n"); else n_print ("Element Actions:\n"); } else { continue; } for (l = found_signals; l; l = l->next) { gchar *indent; const gchar *pmark; int indent_len; query = (GSignalQuery *) l->data; indent_len = strlen (query->signal_name) + strlen (g_type_name (query->return_type)) + 24; if (query->return_type == G_TYPE_POINTER) { pmark = ""; } else if (G_TYPE_FUNDAMENTAL (query->return_type) == G_TYPE_POINTER || G_TYPE_IS_BOXED (query->return_type) || G_TYPE_IS_OBJECT (query->return_type)) { pmark = "* "; indent_len += 2; } else { pmark = ""; } indent = g_new0 (gchar, indent_len + 1); memset (indent, ' ', indent_len); n_print (" \"%s\" : %s %suser_function (%s* object", query->signal_name, g_type_name (query->return_type), pmark, g_type_name (type)); for (j = 0; j < query->n_params; j++) { g_print (",\n"); if (G_TYPE_IS_FUNDAMENTAL (query->param_types[j])) { n_print ("%s%s arg%d", indent, g_type_name (query->param_types[j]), j); } else if (G_TYPE_IS_ENUM (query->param_types[j])) { n_print ("%s%s arg%d", indent, g_type_name (query->param_types[j]), j); } else { n_print ("%s%s* arg%d", indent, g_type_name (query->param_types[j]), j); } } if (k == 0) { g_print (",\n"); n_print ("%sgpointer user_data);\n", indent); } else g_print (");\n"); g_free (indent); } if (found_signals) { g_slist_foreach (found_signals, (GFunc) g_free, NULL); g_slist_free (found_signals); } } }
static JSBool gjs_value_to_g_value_internal(JSContext *context, jsval value, GValue *gvalue, gboolean no_copy) { GType gtype; gtype = G_VALUE_TYPE(gvalue); if (gtype == 0) { gtype = gjs_value_guess_g_type(context, value); if (gtype == G_TYPE_INVALID) { gjs_throw(context, "Could not guess unspecified GValue type"); return JS_FALSE; } gjs_debug_marshal(GJS_DEBUG_GCLOSURE, "Guessed GValue type %s from JS Value", g_type_name(gtype)); g_value_init(gvalue, gtype); } gjs_debug_marshal(GJS_DEBUG_GCLOSURE, "Converting jsval to gtype %s", g_type_name(gtype)); if (gtype == G_TYPE_STRING) { /* Don't use ValueToString since we don't want to just toString() * everything automatically */ if (JSVAL_IS_NULL(value)) { g_value_set_string(gvalue, NULL); } else if (JSVAL_IS_STRING(value)) { gchar *utf8_string; if (!gjs_string_to_utf8(context, value, &utf8_string)) return JS_FALSE; g_value_take_string(gvalue, utf8_string); } else { gjs_throw(context, "Wrong type %s; string expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_CHAR) { gint32 i; if (JS_ValueToInt32(context, value, &i) && i >= SCHAR_MIN && i <= SCHAR_MAX) { g_value_set_schar(gvalue, (signed char)i); } else { gjs_throw(context, "Wrong type %s; char expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_UCHAR) { guint16 i; if (JS_ValueToUint16(context, value, &i) && i <= UCHAR_MAX) { g_value_set_uchar(gvalue, (unsigned char)i); } else { gjs_throw(context, "Wrong type %s; unsigned char expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_INT) { gint32 i; if (JS_ValueToInt32(context, value, &i)) { g_value_set_int(gvalue, i); } else { gjs_throw(context, "Wrong type %s; integer expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_DOUBLE) { gdouble d; if (JS_ValueToNumber(context, value, &d)) { g_value_set_double(gvalue, d); } else { gjs_throw(context, "Wrong type %s; double expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_FLOAT) { gdouble d; if (JS_ValueToNumber(context, value, &d)) { g_value_set_float(gvalue, d); } else { gjs_throw(context, "Wrong type %s; float expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_UINT) { guint32 i; if (JS_ValueToECMAUint32(context, value, &i)) { g_value_set_uint(gvalue, i); } else { gjs_throw(context, "Wrong type %s; unsigned integer expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (gtype == G_TYPE_BOOLEAN) { JSBool b; /* JS_ValueToBoolean() pretty much always succeeds, * which is maybe surprising sometimes, but could * be handy also... */ if (JS_ValueToBoolean(context, value, &b)) { g_value_set_boolean(gvalue, b); } else { gjs_throw(context, "Wrong type %s; boolean expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (g_type_is_a(gtype, G_TYPE_OBJECT) || g_type_is_a(gtype, G_TYPE_INTERFACE)) { GObject *gobj; gobj = NULL; if (JSVAL_IS_NULL(value)) { /* nothing to do */ } else if (JSVAL_IS_OBJECT(value)) { JSObject *obj; obj = JSVAL_TO_OBJECT(value); if (!gjs_typecheck_object(context, obj, gtype, JS_TRUE)) return JS_FALSE; gobj = gjs_g_object_from_object(context, obj); } else { gjs_throw(context, "Wrong type %s; object %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } g_value_set_object(gvalue, gobj); } else if (gtype == G_TYPE_STRV) { if (JSVAL_IS_NULL(value)) { /* do nothing */ } else if (gjs_object_has_property(context, JSVAL_TO_OBJECT(value), "length")) { jsval length_value; guint32 length; if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(value), NULL, "length", &length_value) || !JS_ValueToECMAUint32(context, length_value, &length)) { gjs_throw(context, "Wrong type %s; strv expected", gjs_get_type_name(value)); return JS_FALSE; } else { void *result; char **strv; if (!gjs_array_to_strv (context, value, length, &result)) return JS_FALSE; /* cast to strv in a separate step to avoid type-punning */ strv = result; g_value_take_boxed (gvalue, strv); } } else { gjs_throw(context, "Wrong type %s; strv expected", gjs_get_type_name(value)); return JS_FALSE; } } else if (g_type_is_a(gtype, G_TYPE_BOXED)) { void *gboxed; gboxed = NULL; if (JSVAL_IS_NULL(value)) { /* nothing to do */ } else if (JSVAL_IS_OBJECT(value)) { JSObject *obj; obj = JSVAL_TO_OBJECT(value); if (g_type_is_a(gtype, G_TYPE_ERROR)) { /* special case GError */ if (!gjs_typecheck_gerror(context, obj, JS_TRUE)) return JS_FALSE; gboxed = gjs_gerror_from_error(context, obj); } else { /* First try a union, if that fails, assume a boxed struct. Distinguishing which one is expected would require checking the associated GIBaseInfo, which is not necessary possible, if e.g. we see the GType without loading the typelib. */ if (gjs_typecheck_union(context, obj, NULL, gtype, JS_FALSE)) { gboxed = gjs_c_union_from_union(context, obj); } else { if (!gjs_typecheck_boxed(context, obj, NULL, gtype, JS_TRUE)) return JS_FALSE; gboxed = gjs_c_struct_from_boxed(context, obj); } } } else { gjs_throw(context, "Wrong type %s; boxed type %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } if (no_copy) g_value_set_static_boxed(gvalue, gboxed); else g_value_set_boxed(gvalue, gboxed); } else if (g_type_is_a(gtype, G_TYPE_VARIANT)) { GVariant *variant = NULL; if (JSVAL_IS_NULL(value)) { /* nothing to do */ } else if (JSVAL_IS_OBJECT(value)) { JSObject *obj = JSVAL_TO_OBJECT(value); if (!gjs_typecheck_boxed(context, obj, NULL, G_TYPE_VARIANT, JS_TRUE)) return JS_FALSE; variant = gjs_c_struct_from_boxed(context, obj); } else { gjs_throw(context, "Wrong type %s; boxed type %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } g_value_set_variant (gvalue, variant); } else if (g_type_is_a(gtype, G_TYPE_ENUM)) { gint64 value_int64; if (gjs_value_to_int64 (context, value, &value_int64)) { GEnumValue *v; /* See arg.c:_gjs_enum_to_int() */ v = g_enum_get_value(G_ENUM_CLASS(g_type_class_peek(gtype)), (int)value_int64); if (v == NULL) { gjs_throw(context, "%d is not a valid value for enumeration %s", JSVAL_TO_INT(value), g_type_name(gtype)); return JS_FALSE; } g_value_set_enum(gvalue, v->value); } else { gjs_throw(context, "Wrong type %s; enum %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } } else if (g_type_is_a(gtype, G_TYPE_FLAGS)) { gint64 value_int64; if (gjs_value_to_int64 (context, value, &value_int64)) { if (!_gjs_flags_value_is_valid(context, gtype, value_int64)) return JS_FALSE; /* See arg.c:_gjs_enum_to_int() */ g_value_set_flags(gvalue, (int)value_int64); } else { gjs_throw(context, "Wrong type %s; flags %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } } else if (g_type_is_a(gtype, G_TYPE_PARAM)) { void *gparam; gparam = NULL; if (JSVAL_IS_NULL(value)) { /* nothing to do */ } else if (JSVAL_IS_OBJECT(value)) { JSObject *obj; obj = JSVAL_TO_OBJECT(value); if (!gjs_typecheck_param(context, obj, gtype, JS_TRUE)) return JS_FALSE; gparam = gjs_g_param_from_param(context, obj); } else { gjs_throw(context, "Wrong type %s; param type %s expected", gjs_get_type_name(value), g_type_name(gtype)); return JS_FALSE; } g_value_set_param(gvalue, gparam); } else if (g_type_is_a(gtype, G_TYPE_GTYPE)) { GType type; if (!JSVAL_IS_OBJECT(value)) { gjs_throw(context, "Wrong type %s; expect a GType object", gjs_get_type_name(value)); return JS_FALSE; } type = gjs_gtype_get_actual_gtype(context, JSVAL_TO_OBJECT(value)); g_value_set_gtype(gvalue, type); } else if (g_type_is_a(gtype, G_TYPE_POINTER)) { if (JSVAL_IS_NULL(value)) { /* Nothing to do */ } else { gjs_throw(context, "Cannot convert non-null JS value to G_POINTER"); return JS_FALSE; } } else if (JSVAL_IS_NUMBER(value) && g_value_type_transformable(G_TYPE_INT, gtype)) { /* Only do this crazy gvalue transform stuff after we've * exhausted everything else. Adding this for * e.g. ClutterUnit. */ gint32 i; if (JS_ValueToInt32(context, value, &i)) { GValue int_value = { 0, }; g_value_init(&int_value, G_TYPE_INT); g_value_set_int(&int_value, i); g_value_transform(&int_value, gvalue); } else { gjs_throw(context, "Wrong type %s; integer expected", gjs_get_type_name(value)); return JS_FALSE; } } else { gjs_debug(GJS_DEBUG_GCLOSURE, "jsval is number %d gtype fundamental %d transformable to int %d from int %d", JSVAL_IS_NUMBER(value), G_TYPE_IS_FUNDAMENTAL(gtype), g_value_type_transformable(gtype, G_TYPE_INT), g_value_type_transformable(G_TYPE_INT, gtype)); gjs_throw(context, "Don't know how to convert JavaScript object to GType %s", g_type_name(gtype)); return JS_FALSE; } return JS_TRUE; }
static void print_signal_info (GstElement * element) { /* Signals/Actions Block */ guint *signals; guint nsignals; gint i = 0, j, k; GSignalQuery *query = NULL; GType type; GSList *found_signals, *l; for (k = 0; k < 2; k++) { found_signals = NULL; for (type = G_OBJECT_TYPE (element); type; type = g_type_parent (type)) { if (type == GST_TYPE_ELEMENT || type == GST_TYPE_OBJECT) break; if (type == GST_TYPE_BIN && G_OBJECT_TYPE (element) != GST_TYPE_BIN) continue; signals = g_signal_list_ids (type, &nsignals); for (i = 0; i < nsignals; i++) { query = g_new0 (GSignalQuery, 1); g_signal_query (signals[i], query); if ((k == 0 && !(query->signal_flags & G_SIGNAL_ACTION)) || (k == 1 && (query->signal_flags & G_SIGNAL_ACTION))) found_signals = g_slist_append (found_signals, query); else g_free (query); } g_free (signals); signals = NULL; } if (found_signals) { n_print ("\n"); if (k == 0) n_print ("Element Signals:\n"); else n_print ("Element Actions:\n"); } else { continue; } for (l = found_signals; l; l = l->next) { gchar *indent; int indent_len; query = (GSignalQuery *) l->data; indent_len = strlen (query->signal_name) + strlen (g_type_name (query->return_type)) + 24; indent = g_new0 (gchar, indent_len + 1); memset (indent, ' ', indent_len); n_print (" \"%s\" : %s user_function (%s* object", query->signal_name, g_type_name (query->return_type), g_type_name (type)); for (j = 0; j < query->n_params; j++) { if (_name) g_print ("%s", _name); if (G_TYPE_IS_FUNDAMENTAL (query->param_types[j])) { g_print (",\n%s%s arg%d", indent, g_type_name (query->param_types[j]), j); } else if (G_TYPE_IS_ENUM (query->param_types[j])) { g_print (",\n%s%s arg%d", indent, g_type_name (query->param_types[j]), j); } else { g_print (",\n%s%s* arg%d", indent, g_type_name (query->param_types[j]), j); } } if (k == 0) { if (_name) g_print ("%s", _name); g_print (",\n%sgpointer user_data);\n", indent); } else g_print (");\n"); g_free (indent); } if (found_signals) { g_slist_foreach (found_signals, (GFunc) g_free, NULL); g_slist_free (found_signals); } } }