Skip to content

Commit

Permalink
Fix support for uint32 and uint64.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Mar 24, 2014
1 parent d70cbc4 commit 8745c90
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
7 changes: 6 additions & 1 deletion all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type GoType struct {
IntValue int
Int64Value int64
Int32Value int32
Uint32Value uint32
Float64Value float64
Float32Value float32
AnyValue interface{}
Expand Down Expand Up @@ -191,7 +192,11 @@ var getSetTests = []struct{ set, get interface{} }{
{"value", same},
{true, same},
{false, same},
{int64(42), int64(42)},
{int(42), same},
{int32(42), int(42)},
{int64(42), same},
{uint32(42), same},
{uint64(42), same},
{float64(42), same},
{float32(42), same},
{new(GoType), same},
Expand Down
16 changes: 16 additions & 0 deletions cpp/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,12 @@ void unpackDataValue(DataValue *value, QVariant_ *var)
case DTInt32:
*qvar = *(qint32*)(value->data);
break;
case DTUint64:
*qvar = *(quint64*)(value->data);
break;
case DTUint32:
*qvar = *(quint32*)(value->data);
break;
case DTFloat64:
*qvar = *(double*)(value->data);
break;
Expand Down Expand Up @@ -667,13 +673,23 @@ void packDataValue(QVariant_ *var, DataValue *value)
*(qint8*)(value->data) = (qint8)qvar->toInt();
break;
case QMetaType::LongLong:
// Some of these entries will have to be fixed when handling platforms
// where sizeof(long long) != 8 or sizeof(int) != 4.
value->dataType = DTInt64;
*(qint64*)(value->data) = qvar->toLongLong();
break;
case QMetaType::ULongLong:
value->dataType = DTUint64;
*(quint64*)(value->data) = qvar->toLongLong();
break;
case QMetaType::Int:
value->dataType = DTInt32;
*(qint32*)(value->data) = qvar->toInt();
break;
case QMetaType::UInt:
value->dataType = DTUint32;
*(quint32*)(value->data) = qvar->toUInt();
break;
case QMetaType::VoidStar:
value->dataType = DTUintptr;
*(uintptr_t*)(value->data) = (uintptr_t)qvar->value<void *>();
Expand Down
10 changes: 6 additions & 4 deletions cpp/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ typedef enum {
DTBool = 11,
DTInt64 = 12,
DTInt32 = 13,
DTUintptr = 14,
DTFloat64 = 15,
DTFloat32 = 16,
DTColor = 17,
DTUint64 = 14,
DTUint32 = 15,
DTUintptr = 16,
DTFloat64 = 17,
DTFloat32 = 18,
DTColor = 19,

DTGoAddr = 100,
DTObject = 101,
Expand Down
14 changes: 14 additions & 0 deletions datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func packDataValue(value interface{}, dvalue *C.DataValue, engine *Engine, owner
case int32:
dvalue.dataType = C.DTInt32
*(*int32)(datap) = value
case uint64:
dvalue.dataType = C.DTUint64
*(*uint64)(datap) = value
case uint32:
dvalue.dataType = C.DTUint32
*(*uint32)(datap) = value
case float64:
dvalue.dataType = C.DTFloat64
*(*float64)(datap) = value
Expand Down Expand Up @@ -132,6 +138,10 @@ func unpackDataValue(dvalue *C.DataValue, engine *Engine) interface{} {
return *(*int64)(datap)
case C.DTInt32:
return int(*(*int32)(datap))
case C.DTUint64:
return *(*uint64)(datap)
case C.DTUint32:
return *(*uint32)(datap)
case C.DTUintptr:
return *(*uintptr)(datap)
case C.DTFloat64:
Expand Down Expand Up @@ -257,6 +267,10 @@ func typeInfo(v interface{}) *C.GoTypeInfo {
// TODO Only do that if it's a struct?
vtptr := reflect.PtrTo(vt)

if vt.Kind() != reflect.Struct {
panic(fmt.Sprintf("handling of %s (%#v) is incomplete; please report to the developers", vt, v))
}

numField := vt.NumField()
numMethod := vtptr.NumMethod()
privateFields := 0
Expand Down
23 changes: 16 additions & 7 deletions qml.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,13 @@ func (obj *Common) Property(name string) interface{} {
// Int panics if the property cannot be represented as an int.
func (obj *Common) Int(property string) int {
switch value := obj.Property(property).(type) {
case int64:
return int(value)
case int:
return value
case int64:
if int64(int(value)) != value {
panic(fmt.Sprintf("value of property %q is too large for int: %#v", property, value))
}
case uint64:
return int(value)
case uint32:
return int(value)
case uintptr:
return int(value)
Expand All @@ -552,10 +553,14 @@ func (obj *Common) Int(property string) int {
// Int64 panics if the property cannot be represented as an int64.
func (obj *Common) Int64(property string) int64 {
switch value := obj.Property(property).(type) {
case int:
return int64(value)
case int64:
return value
case int:
return int64(value)
case uint64:
return int64(value)
case uint32:
return int64(value)
case uintptr:
return int64(value)
case float32:
Expand All @@ -571,9 +576,13 @@ func (obj *Common) Int64(property string) int64 {
// Float64 panics if the property cannot be represented as float64.
func (obj *Common) Float64(property string) float64 {
switch value := obj.Property(property).(type) {
case int64:
return float64(value)
case int:
return float64(value)
case int64:
case uint64:
return float64(value)
case uint32:
return float64(value)
case uintptr:
return float64(value)
Expand Down

0 comments on commit 8745c90

Please sign in to comment.