From a43f7a959840d59c29cce9a39174415f19d61096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Derkacz?= Date: Wed, 5 Nov 2014 18:19:02 +0100 Subject: [PATCH] Fix (u)int(32) type maping. --- glib_test.go | 18 +++++++-------- type.go | 53 +++++++++++++----------------------------- value.go | 65 ++++++++++++++++++---------------------------------- 3 files changed, 47 insertions(+), 89 deletions(-) diff --git a/glib_test.go b/glib_test.go index 86ca416..1d7d84d 100644 --- a/glib_test.go +++ b/glib_test.go @@ -1,8 +1,8 @@ package glib import ( - "testing" "fmt" + "testing" ) // GLib values @@ -16,12 +16,12 @@ func TestValue(t *testing.T) { t.Error("TYPE_UINT64") } v2 := -1 - a = ValueOf(v2) - b = NewValue(TYPE_INT) - a.Copy(b) + a = ValueOf(v2) // TYPE_GO_INT == TYPE_LONG + b = NewValue(TYPE_GO_INT32) // TYPE_GO_INT32 == TYPE_INT + a.Transform(b) t.Logf("a = %s(%s), b = %s(%s)", a.Type(), a, b.Type(), b) - if b.Get() != v2 { - t.Error("TYPE_INT") + if b.Get().(int32) != int32(v2) { + t.Error("TYPE_GO_INT32 (TYPE_INT)") } } @@ -89,11 +89,11 @@ func TestSignalName(t *testing.T) { type A string func (a *A) handler(o *Object, i int) { - fmt.Printf("handler: %s, %v, %d\n", a, o, i) + fmt.Printf("handler: %s, %v, %d\n", *a, o, i) } func (a *A) noiHandler(i int) { - fmt.Printf("noiHandler: %s, %d\n", a, i) + fmt.Printf("noiHandler: %s, %d\n", *a, i) } func funcHandler(o *Object, i int) { @@ -105,5 +105,5 @@ func funcNoiHandler(i int) { } func funcHandlerParam0(a *A, o *Object, i int) { - fmt.Printf("funcHandlerParam0: %s %v %d\n", a, o, i) + fmt.Printf("funcHandlerParam0: %s %v %d\n", *a, o, i) } diff --git a/type.go b/type.go index 4932acb..0c91fa5 100644 --- a/type.go +++ b/type.go @@ -5,9 +5,6 @@ package glib #include #include -#define _GINT_SIZE sizeof(gint) -#define _GLONG_SIZE sizeof(glong) - #cgo CFLAGS: -Wno-deprecated-declarations #cgo pkg-config: glib-2.0 gobject-2.0 gthread-2.0 */ @@ -15,7 +12,6 @@ import "C" import ( "reflect" - "strconv" "unsafe" ) @@ -38,9 +34,13 @@ const ( TYPE_UCHAR = Type(C.G_TYPE_UCHAR) TYPE_BOOLEAN = Type(C.G_TYPE_BOOLEAN) TYPE_INT = Type(C.G_TYPE_INT) + TYPE_GO_INT32 = TYPE_INT TYPE_UINT = Type(C.G_TYPE_UINT) + TYPE_GO_UINT32 = TYPE_UINT TYPE_LONG = Type(C.G_TYPE_LONG) + TYPE_GO_INT = TYPE_LONG TYPE_ULONG = Type(C.G_TYPE_ULONG) + TYPE_GO_UINT = TYPE_ULONG TYPE_INT64 = Type(C.G_TYPE_INT64) TYPE_UINT64 = Type(C.G_TYPE_UINT64) TYPE_ENUM = Type(C.G_TYPE_ENUM) @@ -55,13 +55,7 @@ const ( TYPE_VARIANT = Type(C.G_TYPE_VARIANT) ) -var ( - TYPE_GTYPE Type - TYPE_GO_INT Type - TYPE_GO_UINT Type - TYPE_GO_INT32 Type - TYPE_GO_UINT32 Type -) +var TYPE_GTYPE Type func (t Type) g() C.GType { return C.GType(t) @@ -89,8 +83,8 @@ func (t Type) Parent() Type { return Type(C.g_type_parent(t.g())) } -func (t Type) Depth() uint { - return uint(C.g_type_depth(t.g())) +func (t Type) Depth() int { + return int(C.g_type_depth(t.g())) } // Returns the type that is derived directly from root type which is also @@ -166,7 +160,15 @@ func (t Type) Match(rt reflect.Type) bool { return false } -// Returns the Type of the value in the interface{}. +func (t Type) Compatible(dst Type) bool { + return C.g_value_type_compatible(t.g(), dst.g()) != C.gboolean(0) +} + +func (t Type) Transformable(dst Type) bool { + return C.g_value_type_transformable(t.g(), dst.g()) != C.gboolean(0) +} + +// TypeOf returns the Type of the value in the i}. func TypeOf(i interface{}) Type { // Types ov values that implements TypeGetter if o, ok := i.(TypeGetter); ok { @@ -228,29 +230,6 @@ func TypeFromName(name string) Type { func init() { C.g_type_init() TYPE_GTYPE = Type(C.g_gtype_get_type()) - int_bytes := strconv.IntSize / 8 - if int_bytes == int(C._GINT_SIZE) { - TYPE_GO_INT = TYPE_INT - TYPE_GO_UINT = TYPE_UINT - } else if int_bytes == C._GLONG_SIZE { - TYPE_GO_INT = TYPE_LONG - TYPE_GO_UINT = TYPE_ULONG - } else if int_bytes == 64 { - TYPE_GO_INT = TYPE_INT64 - TYPE_GO_UINT = TYPE_UINT64 - } else { - panic("Unexpectd size of 'int'") - } - int32_bytes := C.uint(4) - if int32_bytes == C._GINT_SIZE { - TYPE_GO_INT32 = TYPE_INT - TYPE_GO_UINT32 = TYPE_UINT - } else if int32_bytes == C._GLONG_SIZE { - TYPE_GO_INT32 = TYPE_LONG - TYPE_GO_UINT32 = TYPE_ULONG - } else { - panic("Neither gint nor glong are 32 bit numbers") - } } type Pointer C.gpointer diff --git a/value.go b/value.go index bd2428d..d0bfa27 100644 --- a/value.go +++ b/value.go @@ -6,8 +6,8 @@ package glib import "C" import ( - "reflect" "fmt" + "reflect" ) type ValueGetter interface { @@ -41,41 +41,25 @@ func (v *Value) Set(i interface{}) { C.g_value_set_boolean(v.g(), gBoolean(r.Bool())) case reflect.Int: - if TYPE_GO_INT == TYPE_INT { - C.g_value_set_int(v.g(), C.gint(i.(int))) - } else { - C.g_value_set_long(v.g(), C.glong(i.(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: - if TYPE_GO_INT32 == TYPE_INT { - C.g_value_set_int(v.g(), C.gint(i.(int32))) - } else { - C.g_value_set_long(v.g(), C.glong(i.(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: - if TYPE_GO_INT == TYPE_INT { - C.g_value_set_uint(v.g(), C.guint(i.(uint))) - } else { - C.g_value_set_ulong(v.g(), C.gulong(i.(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: - if TYPE_GO_INT32 == TYPE_INT { - C.g_value_set_uint(v.g(), C.guint(i.(uint32))) - } else { - C.g_value_set_ulong(v.g(), C.gulong(i.(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))) @@ -97,7 +81,7 @@ func (v *Value) Set(i interface{}) { } } -// Initializes value with the default value of type. +// Initializes value with the default value of type. func (v *Value) Init(t Type) { C.g_value_init(v.g(), t.g()) } @@ -125,11 +109,22 @@ func ValueOf(i interface{}) *Value { return v } -// Copies the value into dst. +// Copy copies the value into dst. func (v *Value) Copy(dst *Value) { + if !v.Type().Compatible(dst.Type()) { + panic(fmt.Sprintf("can't copy %s into %s", v.Type(), dst.Type())) + } C.g_value_copy(v.g(), dst.g()) } +// Transform transforms the value into dst. +func (v *Value) Transform(dst *Value) { + if !v.Type().Transformable(dst.Type()) { + panic(fmt.Sprintf("can't transform %s into %s", v.Type(), dst.Type())) + } + C.g_value_transform(v.g(), dst.g()) +} + func (v *Value) Get() interface{} { t := Type(v.g().g_type) switch t { @@ -140,18 +135,10 @@ func (v *Value) Get() interface{} { return C.GoString((*C.char)(C.g_value_get_string(v.g()))) case TYPE_GO_INT: - if TYPE_GO_INT == TYPE_INT { - return int(C.g_value_get_int(v.g())) - } else { - return int(C.g_value_get_long(v.g())) - } + return int(C.g_value_get_long(v.g())) case TYPE_GO_UINT: - if TYPE_GO_INT == TYPE_INT { - return uint(C.g_value_get_uint(v.g())) - } else { - return uint(C.g_value_get_ulong(v.g())) - } + return uint(C.g_value_get_ulong(v.g())) case TYPE_CHAR: return int8(C.g_value_get_schar(v.g())) @@ -160,18 +147,10 @@ func (v *Value) Get() interface{} { return uint8(C.g_value_get_uchar(v.g())) case TYPE_GO_INT32: - if TYPE_GO_INT32 == TYPE_INT { - return int32(C.g_value_get_int(v.g())) - } else { - return int32(C.g_value_get_long(v.g())) - } + return int32(C.g_value_get_int(v.g())) case TYPE_GO_UINT32: - if TYPE_GO_INT32 == TYPE_INT { - return uint32(C.g_value_get_uint(v.g())) - } else { - return uint32(C.g_value_get_ulong(v.g())) - } + return uint32(C.g_value_get_uint(v.g())) case TYPE_INT64: return int64(C.g_value_get_int64(v.g()))