Example #1
0
File: value.go Project: ziutek/glib
func (v *Value) Get() interface{} {
	t := Type(v.g().g_type)
	switch t {
	case TYPE_INVALID:
		return nil

	case TYPE_STRING:
		return C.GoString((*C.char)(C.g_value_get_string(v.g())))

	case TYPE_GO_INT:
		return int(C.g_value_get_long(v.g()))

	case TYPE_GO_UINT:
		return uint(C.g_value_get_ulong(v.g()))

	case TYPE_CHAR:
		return int8(C.g_value_get_schar(v.g()))

	case TYPE_UCHAR:
		return uint8(C.g_value_get_uchar(v.g()))

	case TYPE_GO_INT32:
		return int32(C.g_value_get_int(v.g()))

	case TYPE_GO_UINT32:
		return uint32(C.g_value_get_uint(v.g()))

	case TYPE_INT64:
		return int64(C.g_value_get_int64(v.g()))

	case TYPE_UINT64:
		return uint64(C.g_value_get_uint64(v.g()))

	case TYPE_BOOLEAN:
		return (C.g_value_get_boolean(v.g()) != C.gboolean(0))

	case TYPE_FLOAT:
		return float32(C.g_value_get_float(v.g()))

	case TYPE_DOUBLE:
		return float64(C.g_value_get_double(v.g()))

	case TYPE_POINTER:
		return Pointer(C.g_value_get_pointer(v.g()))

	case TYPE_GTYPE:
		return Type(C.g_value_get_gtype(v.g()))
	}
	if t.IsA(TYPE_OBJECT) {
		o := new(Object)
		o.SetPtr(Pointer(C.g_value_get_object(v.g())))
		return o
	}
	if C.g_value_fits_pointer(v.g()) != 0 {
		return Pointer(C.g_value_peek_pointer(v.g()))
	}
	// Value of unknown type is returned as is
	return v
}
Example #2
0
File: glib.go Project: jrick/gotk3
// GoValue() converts a Value to comparable Go type.  GoValue()
// returns a non-nil error if the conversion was unsuccessful.  The
// returned interface{} must be type asserted as the actual Go
// representation of the Value.
//
// This function is a wrapper around the many g_value_get_*()
// functions, depending on the type of the Value.
func (v *Value) GoValue() (interface{}, error) {
	switch v.GetType() {
	case TYPE_INVALID:
		return nil, errors.New("Invalid type")
	case TYPE_NONE:
		return nil, nil
	case TYPE_BOOLEAN:
		c := C.g_value_get_boolean(v.Native())
		return gobool(c), nil
	case TYPE_CHAR:
		c := C.g_value_get_schar(v.Native())
		return int8(c), nil
	case TYPE_UCHAR:
		c := C.g_value_get_uchar(v.Native())
		return uint8(c), nil
	case TYPE_INT64:
		c := C.g_value_get_int64(v.Native())
		return int64(c), nil
	case TYPE_INT:
		c := C.g_value_get_int(v.Native())
		return int(c), nil
	case TYPE_UINT64:
		c := C.g_value_get_uint64(v.Native())
		return uint64(c), nil
	case TYPE_UINT:
		c := C.g_value_get_uint(v.Native())
		return uint(c), nil
	case TYPE_FLOAT:
		c := C.g_value_get_float(v.Native())
		return float32(c), nil
	case TYPE_DOUBLE:
		c := C.g_value_get_double(v.Native())
		return float64(c), nil
	case TYPE_STRING:
		c := C.g_value_get_string(v.Native())
		return C.GoString((*C.char)(c)), nil
	default:
		return nil, errors.New("Type conversion not supported")
	}
}
Example #3
0
File: glib.go Project: vvanpo/gotk3
func marshalChar(p uintptr) (interface{}, error) {
	c := C.g_value_get_schar((*C.GValue)(unsafe.Pointer(p)))
	return int8(c), nil
}
Example #4
0
// GoValue() converts a Value to comparable Go type.  GoValue()
// returns a non-nil error if the conversion was unsuccessful.  The
// returned interface{} must be type asserted as the actual Go
// representation of the Value.
//
// This function is a wrapper around the many g_value_get_*()
// functions, depending on the type of the Value.
func (v *Value) GoValue() (interface{}, error) {
	_, fundamental, err := v.Type()
	if err != nil {
		return nil, err
	}

	// TODO: verify that all of these cases are indeed fundamental types
	switch fundamental {
	case TYPE_INVALID:
		return nil, errors.New("invalid type")

	case TYPE_NONE:
		return nil, nil

	case TYPE_INTERFACE:
		return nil, errors.New("interface conversion not yet implemented")

	case TYPE_CHAR:
		c := C.g_value_get_schar(v.Native())
		return int8(c), nil

	case TYPE_UCHAR:
		c := C.g_value_get_uchar(v.Native())
		return uint8(c), nil

	case TYPE_BOOLEAN:
		c := C.g_value_get_boolean(v.Native())
		return gobool(c), nil

	// TODO: TYPE_INT should probably be a Go int32.
	case TYPE_INT, TYPE_LONG, TYPE_ENUM:
		c := C.g_value_get_int(v.Native())
		return int(c), nil

	case TYPE_INT64:
		c := C.g_value_get_int64(v.Native())
		return int64(c), nil

	// TODO: TYPE_UINT should probably be a Go uint32.
	case TYPE_UINT, TYPE_ULONG, TYPE_FLAGS:
		c := C.g_value_get_uint(v.Native())
		return uint(c), nil

	case TYPE_UINT64:
		c := C.g_value_get_uint64(v.Native())
		return uint64(c), nil

	case TYPE_FLOAT:
		c := C.g_value_get_float(v.Native())
		return float32(c), nil

	case TYPE_DOUBLE:
		c := C.g_value_get_double(v.Native())
		return float64(c), nil

	case TYPE_STRING:
		c := C.g_value_get_string(v.Native())
		return C.GoString((*C.char)(c)), nil

	case TYPE_POINTER:
		c := C.g_value_get_pointer(v.Native())
		return unsafe.Pointer(c), nil

	case TYPE_OBJECT:
		c := C.g_value_get_object(v.Native())
		// TODO: need to try and return an actual pointer to the correct object type
		// this may require an additional cast()-like method for each module
		return ObjectNew(unsafe.Pointer(c)), nil

	case TYPE_VARIANT:
		return nil, errors.New("variant conversion not yet implemented")

	default:
		return nil, errors.New("type conversion not supported")
	}
}