MCPcopy
hub / github.com/go-qml/qml / packDataValue

Function packDataValue

datatype.go:64–119  ·  view source on GitHub ↗

packDataValue packs the provided Go value into a C.DataValue for shiping into C++ land. For simple types (bool, int, etc) value is converted into a native C++ value. For anything else, including cases when value has a type that has an underlying simple type, the Go value itself is encapsulated into

(value interface{}, dvalue *C.DataValue, engine *Engine, owner valueOwner)

Source from the content-addressed store, hash-verified

62// This must be run from the main GUI thread due to the cases where
63// calling wrapGoValue is necessary.
64func packDataValue(value interface{}, dvalue *C.DataValue, engine *Engine, owner valueOwner) {
65 datap := unsafe.Pointer(&dvalue.data)
66 if value == nil {
67 dvalue.dataType = C.DTInvalid
68 return
69 }
70 switch value := value.(type) {
71 case string:
72 dvalue.dataType = C.DTString
73 cstr, cstrlen := unsafeStringData(value)
74 *(**C.char)(datap) = cstr
75 dvalue.len = cstrlen
76 case bool:
77 dvalue.dataType = C.DTBool
78 *(*bool)(datap) = value
79 case int:
80 if value > 1<<31-1 {
81 dvalue.dataType = C.DTInt64
82 *(*int64)(datap) = int64(value)
83 } else {
84 dvalue.dataType = C.DTInt32
85 *(*int32)(datap) = int32(value)
86 }
87 case int64:
88 dvalue.dataType = C.DTInt64
89 *(*int64)(datap) = value
90 case int32:
91 dvalue.dataType = C.DTInt32
92 *(*int32)(datap) = value
93 case uint64:
94 dvalue.dataType = C.DTUint64
95 *(*uint64)(datap) = value
96 case uint32:
97 dvalue.dataType = C.DTUint32
98 *(*uint32)(datap) = value
99 case float64:
100 dvalue.dataType = C.DTFloat64
101 *(*float64)(datap) = value
102 case float32:
103 dvalue.dataType = C.DTFloat32
104 *(*float32)(datap) = value
105 case *Common:
106 dvalue.dataType = C.DTObject
107 *(*unsafe.Pointer)(datap) = value.addr
108 case color.RGBA:
109 dvalue.dataType = C.DTColor
110 *(*uint32)(datap) = uint32(value.A)<<24 | uint32(value.R)<<16 | uint32(value.G)<<8 | uint32(value.B)
111 default:
112 dvalue.dataType = C.DTObject
113 if obj, ok := value.(Object); ok {
114 *(*unsafe.Pointer)(datap) = obj.Common().addr
115 } else {
116 *(*unsafe.Pointer)(datap) = wrapGoValue(engine, value, owner)
117 }
118 }
119}
120
121// TODO Handle byte slices.

Callers 5

SetVarMethod · 0.85
SetMethod · 0.85
CallMethod · 0.85
hookGoValueReadFieldFunction · 0.85
hookGoValueCallMethodFunction · 0.85

Calls 3

unsafeStringDataFunction · 0.85
wrapGoValueFunction · 0.85
CommonMethod · 0.65

Tested by

no test coverage detected