Skip to content

Commit

Permalink
Fix (u)int(32) type maping.
Browse files Browse the repository at this point in the history
  • Loading branch information
michalderkacz committed Nov 5, 2014
1 parent fa9c60f commit a43f7a9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 89 deletions.
18 changes: 9 additions & 9 deletions glib_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package glib

import (
"testing"
"fmt"
"testing"
)

// GLib values
Expand All @@ -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)")
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
53 changes: 16 additions & 37 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ package glib
#include <stdlib.h>
#include <glib-object.h>
#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
*/
import "C"

import (
"reflect"
"strconv"
"unsafe"
)

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
65 changes: 22 additions & 43 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package glib
import "C"

import (
"reflect"
"fmt"
"reflect"
)

type ValueGetter interface {
Expand Down Expand Up @@ -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)))
Expand All @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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()))
Expand All @@ -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()))
Expand Down

0 comments on commit a43f7a9

Please sign in to comment.