Beispiel #1
0
// Set value to i
func (v *Value) Set(i interface{}) {
	if vg, ok := i.(ValueGetter); ok {
		vg.Value().Copy(v)
		return
	}
	// Other types
	r := reflect.ValueOf(i)
	switch r.Kind() {
	case reflect.Invalid:
		C.g_value_reset(v.g())

	case reflect.Bool:
		C.g_value_set_boolean(v.g(), gBoolean(r.Bool()))

	case reflect.Int:
		C.g_value_set_long(v.g(), C.glong(i.(int)))

	case reflect.Int8:
		C.g_value_set_schar(v.g(), C.gint8(i.(int8)))

	case reflect.Int32:
		C.g_value_set_int(v.g(), C.gint(i.(int32)))

	case reflect.Int64:
		C.g_value_set_int64(v.g(), C.gint64(i.(int64)))

	case reflect.Uint:
		C.g_value_set_ulong(v.g(), C.gulong(i.(uint)))

	case reflect.Uint8:
		C.g_value_set_uchar(v.g(), C.guchar(i.(uint8)))

	case reflect.Uint32:
		C.g_value_set_uint(v.g(), C.guint(i.(uint32)))

	case reflect.Uint64:
		C.g_value_set_uint64(v.g(), C.guint64(i.(uint64)))

	case reflect.Float32:
		C.g_value_set_float(v.g(), C.gfloat(i.(float32)))

	case reflect.Float64:
		C.g_value_set_double(v.g(), C.gdouble(i.(float64)))

	case reflect.Ptr:
		C.g_value_set_pointer(v.g(), C.gpointer(r.Pointer()))

	case reflect.String:
		C.g_value_set_static_string(v.g(), (*C.gchar)(C.CString(r.String())))

	default:
		panic("Can't represent Go value in Glib type system.")
	}
}
Beispiel #2
0
func CreateCGValue(tn GType, object ...unsafe.Pointer) *GValue {
	var cv C.GValue
	C.g_value_init(&cv, C.GType(tn))

	// If no data, then return Gvalue initialized with default
	if len(object) == 0 {
		gv := GValue{tn, &cv}
		return &gv
	}
	obj := object[0]

	// Foundamental types are special
	// TODO: Handle more cases, like creating GValue from GdkEvents
	switch tn {
	case G_TYPE_STRING:
		C.g_value_take_string(&cv, (*C.gchar)(obj))
	case G_TYPE_BOOLEAN:
		C.g_value_set_boolean(&cv, *((*C.gboolean)(obj)))
	case G_TYPE_CHAR:
		C.g_value_set_char(&cv, *((*C.gchar)(obj)))
	case G_TYPE_INT:
		C.g_value_set_int(&cv, *((*C.gint)(obj)))
	case G_TYPE_LONG:
		C.g_value_set_long(&cv, *((*C.glong)(obj)))
	case G_TYPE_INT64:
		C.g_value_set_int64(&cv, *((*C.gint64)(obj)))
	case G_TYPE_UCHAR:
		C.g_value_set_uchar(&cv, *((*C.guchar)(obj)))
	case G_TYPE_UINT:
		C.g_value_set_uint(&cv, *((*C.guint)(obj)))
	case G_TYPE_ULONG:
		C.g_value_set_ulong(&cv, *((*C.gulong)(obj)))
	case G_TYPE_UINT64:
		C.g_value_set_uint64(&cv, *((*C.guint64)(obj)))
	case G_TYPE_FLOAT:
		C.g_value_set_float(&cv, *((*C.gfloat)(obj)))
	case G_TYPE_DOUBLE:
		C.g_value_set_double(&cv, *((*C.gdouble)(obj)))
	default:
		C.g_value_set_object(&cv, C.gpointer(obj))
	}

	gv := GValue{tn, &cv}
	return &gv
}