// wrapper for C.CFDictionaryCreate, since referencing the callbacks in 2 separate files // seems to be triggering some sort of "redefinition" error in cgo func createCFDictionary(keys, values []cfTypeRef) C.CFDictionaryRef { if len(keys) != len(values) { panic("plist: unexpected length difference between keys and values") } var keyPtr, valPtr *unsafe.Pointer if len(keys) > 0 { keyPtr = (*unsafe.Pointer)(&keys[0]) valPtr = (*unsafe.Pointer)(&values[0]) } keyCallbacks := (*C.CFDictionaryKeyCallBacks)(&C.kCFTypeDictionaryKeyCallBacks) valCallbacks := (*C.CFDictionaryValueCallBacks)(&C.kCFTypeDictionaryValueCallBacks) return C.CFDictionaryCreate(nil, keyPtr, valPtr, C.CFIndex(len(keys)), keyCallbacks, valCallbacks) }
// The returned CFDictionaryRef, if non-nil, must be released via CFRelease. func mapToCFDictionary(m map[C.CFTypeRef]C.CFTypeRef) C.CFDictionaryRef { var keys, values []unsafe.Pointer for key, value := range m { keys = append(keys, unsafe.Pointer(key)) values = append(values, unsafe.Pointer(value)) } numValues := len(values) var keysPointer, valuesPointer *unsafe.Pointer if numValues > 0 { keysPointer = &keys[0] valuesPointer = &values[0] } return C.CFDictionaryCreate(nil, keysPointer, valuesPointer, C.CFIndex(numValues), &C.kCFTypeDictionaryKeyCallBacks, &C.kCFTypeDictionaryValueCallBacks) }
// MapToCFDictionary will return a CFDictionaryRef and if non-nil, must be // released with Release(ref). func MapToCFDictionary(m map[C.CFTypeRef]C.CFTypeRef) (C.CFDictionaryRef, error) { var keys, values []unsafe.Pointer for key, value := range m { keys = append(keys, unsafe.Pointer(key)) values = append(values, unsafe.Pointer(value)) } numValues := len(values) var keysPointer, valuesPointer *unsafe.Pointer if numValues > 0 { keysPointer = &keys[0] valuesPointer = &values[0] } cfDict := C.CFDictionaryCreate(nil, keysPointer, valuesPointer, C.CFIndex(numValues), &C.kCFTypeDictionaryKeyCallBacks, &C.kCFTypeDictionaryValueCallBacks) if cfDict == nil { return nil, fmt.Errorf("CFDictionaryCreate failed") } return cfDict, nil }