Skip to content

Instantly share code, notes, and snippets.

@EricRabil
Created January 27, 2022 20:48
Show Gist options
  • Save EricRabil/9ed779148600279e1bfaa8869f486df1 to your computer and use it in GitHub Desktop.
Save EricRabil/9ed779148600279e1bfaa8869f486df1 to your computer and use it in GitHub Desktop.
@_transparent private func CFDictionaryGetValue(_ dictionary: CFDictionary, _ key: CFString) -> UnsafeRawPointer? {
CFDictionaryGetValue(dictionary, Unmanaged.passUnretained(key).toOpaque())
}
@_transparent private func CFDictionaryUnwrap(_ type: CFTypeRef) -> CFDictionary? {
guard CFGetTypeID(type) == CFDictionaryGetTypeID() else {
return nil
}
return unsafeBitCast(type, to: CFDictionary.self)
}
@_transparent private func CFBooleanUnwrap(_ type: CFTypeRef) -> CFBoolean? {
guard CFGetTypeID(type) == CFBooleanGetTypeID() else {
return nil
}
return unsafeBitCast(type, to: CFBoolean.self)
}
@_transparent private func CFDictionaryGetType(_ dictionary: CFDictionary, _ key: CFString) -> CFTypeRef? {
guard let returnValue = CFDictionaryGetValue(dictionary, key) else {
return nil
}
return Unmanaged.fromOpaque(returnValue).takeUnretainedValue()
}
@_transparent private func CFDictionaryGetDictionary(_ dictionary: CFDictionary, _ key: CFString) -> CFDictionary? {
guard let type = CFDictionaryGetType(dictionary, key) else {
return nil
}
return CFDictionaryUnwrap(type)
}
@_transparent private func CFDictionaryGetBoolean(_ dictionary: CFDictionary, _ key: CFString) -> CFBoolean? {
guard let type = CFDictionaryGetType(dictionary, key) else {
return nil
}
return CFBooleanUnwrap(type)
}
private let kCFDictionaryEmpty = CFDictionaryCreate(kCFAllocatorSystemDefault, nil, nil, 0, nil, nil)!
private func CFDictionaryCreateMutable() -> CFMutableDictionary {
CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, [kCFTypeDictionaryKeyCallBacks], [kCFTypeDictionaryValueCallBacks])
}
private func CFDictionarySetValue(_ dictionary: CFMutableDictionary, _ key: CFTypeRef, _ value: CFTypeRef) {
CFDictionarySetValue(dictionary, Unmanaged.passUnretained(key).toOpaque(), Unmanaged.passUnretained(value).toOpaque())
}
private func CFDictionaryAddValue(_ dictionary: CFMutableDictionary, _ key: CFTypeRef, _ value: CFTypeRef) {
CFDictionaryAddValue(dictionary, Unmanaged.passUnretained(key).toOpaque(), Unmanaged.passUnretained(value).toOpaque())
}
private func CFDictionaryContainsKey(_ dictionary: CFDictionary, _ key: CFTypeRef) -> Bool {
CFDictionaryContainsKey(dictionary, Unmanaged.passUnretained(key).toOpaque())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment