Skip to content

Commit

Permalink
Fix type assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2brain committed Jun 2, 2022
1 parent bd3c910 commit 660bbee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions iup/bind_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ func SetAttribute(ih Ihandle, name string, value interface{}) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

switch value.(type) {
switch val := value.(type) {
case nil:
C.IupSetAttribute(ih.ptr(), cName, nil)
case Ihandle:
C.IupSetAttribute(ih.ptr(), cName, cih(value.(Ihandle)))
C.IupSetAttribute(ih.ptr(), cName, cih(val))
case uintptr:
C.IupSetAttribute(ih.ptr(), cName, cih(value.(Ihandle)))
case string:
cValue := C.CString(value.(string))
cValue := C.CString(val)
defer C.free(unsafe.Pointer(cValue))

C.IupSetStrAttribute(ih.ptr(), cName, cValue)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
C.IupSetInt(ih.ptr(), cName, C.int(reflect.ValueOf(value).Int()))
case float32:
C.IupSetFloat(ih.ptr(), cName, C.float(value.(float32)))
C.IupSetFloat(ih.ptr(), cName, C.float(val))
case float64:
C.IupSetDouble(ih.ptr(), cName, C.double(value.(float64)))
C.IupSetDouble(ih.ptr(), cName, C.double(val))
case [3]uint8:
C.IupSetRGB(ih.ptr(), cName, C.uchar(value.([3]uint8)[0]), C.uchar(value.([3]uint8)[1]), C.uchar(value.([3]uint8)[2]))
default:
Expand Down Expand Up @@ -164,24 +164,24 @@ func SetAttributeId(ih Ihandle, name string, id int, value interface{}) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

switch value.(type) {
switch val := value.(type) {
case nil:
C.IupSetAttributeId(ih.ptr(), cName, C.int(id), nil)
case Ihandle:
C.IupSetAttributeId(ih.ptr(), cName, C.int(id), cih(value.(Ihandle)))
case uintptr:
C.IupSetAttributeId(ih.ptr(), cName, C.int(id), cih(value.(Ihandle)))
case string:
cValue := C.CString(value.(string))
cValue := C.CString(val)
defer C.free(unsafe.Pointer(cValue))

C.IupSetStrAttributeId(ih.ptr(), cName, C.int(id), cValue)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
C.IupSetIntId(ih.ptr(), cName, C.int(id), C.int(reflect.ValueOf(value).Int()))
case float32:
C.IupSetFloatId(ih.ptr(), cName, C.int(id), C.float(value.(float32)))
C.IupSetFloatId(ih.ptr(), cName, C.int(id), C.float(val))
case float64:
C.IupSetDoubleId(ih.ptr(), cName, C.int(id), C.double(value.(float64)))
C.IupSetDoubleId(ih.ptr(), cName, C.int(id), C.double(val))
case [3]uint8:
C.IupSetRGBId(ih.ptr(), cName, C.int(id), C.uchar(value.([3]uint8)[0]), C.uchar(value.([3]uint8)[1]), C.uchar(value.([3]uint8)[2]))
default:
Expand All @@ -206,24 +206,24 @@ func SetAttributeId2(ih Ihandle, name string, lin, col int, value interface{}) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

switch value.(type) {
switch val := value.(type) {
case nil:
C.IupSetAttributeId2(ih.ptr(), cName, C.int(lin), C.int(col), nil)
case Ihandle:
C.IupSetAttributeId2(ih.ptr(), cName, C.int(lin), C.int(col), cih(value.(Ihandle)))
case uintptr:
C.IupSetAttributeId2(ih.ptr(), cName, C.int(lin), C.int(col), cih(value.(Ihandle)))
case string:
cValue := C.CString(value.(string))
cValue := C.CString(val)
defer C.free(unsafe.Pointer(cValue))

C.IupSetStrAttributeId2(ih.ptr(), cName, C.int(lin), C.int(col), cValue)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
C.IupSetIntId2(ih.ptr(), cName, C.int(lin), C.int(col), C.int(reflect.ValueOf(value).Int()))
case float32:
C.IupSetFloatId2(ih.ptr(), cName, C.int(lin), C.int(col), C.float(value.(float32)))
C.IupSetFloatId2(ih.ptr(), cName, C.int(lin), C.int(col), C.float(val))
case float64:
C.IupSetDoubleId2(ih.ptr(), cName, C.int(lin), C.int(col), C.double(value.(float64)))
C.IupSetDoubleId2(ih.ptr(), cName, C.int(lin), C.int(col), C.double(val))
case [3]uint8:
C.IupSetRGBId2(ih.ptr(), cName, C.int(lin), C.int(col), C.uchar(value.([3]uint8)[0]), C.uchar(value.([3]uint8)[1]), C.uchar(value.([3]uint8)[2]))
default:
Expand Down Expand Up @@ -259,9 +259,9 @@ func SetGlobal(name string, value interface{}) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

switch value.(type) { //TODO handle number values?
switch val := value.(type) { //TODO handle number values?
case string:
cValue := C.CString(value.(string))
cValue := C.CString(val)
defer C.free(unsafe.Pointer(cValue))

C.IupSetStrGlobal(cName, cValue) // always copy value
Expand Down
8 changes: 4 additions & 4 deletions iup/bind_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func (ih Ihandle) GetAllAttributes() []string {
// https://www.tecgraf.puc-rio.br/iup/en/func/iupsetattributes.html
func (ih Ihandle) SetAttributes(params ...interface{}) Ihandle {
for _, param := range params {
switch param.(type) {
switch param := param.(type) {
case string:
SetAttributes(ih, param.(string))
SetAttributes(ih, param)
case map[string]string:
for key, val := range param.(map[string]string) {
for key, val := range param {
SetAttribute(ih, key, val)
}
case map[string]interface{}:
for key, val := range param.(map[string]interface{}) {
for key, val := range param {
SetAttribute(ih, key, val)
}
}
Expand Down

0 comments on commit 660bbee

Please sign in to comment.