core-foundation-extensions
This is a Kotlin MultiPlatform library that provides extensions for core foundation framework on MacOS.
Setup
repositories {
mavenCentral()
}
kotlin {
sourceSets {
val appleMain by getting {
dependencies {
implementation("nl.ncaj:core-foundation-extensions:0.3.1")
}
}
}
}
Usage
Convert kotlin types to cf types. Each of these calls transfers ownership of the values to the caller.
val cfByte = 1.toByte().toCFNumber()
val cfShort = 2.toShort().toCFNumber()
val cfInt = 3.toCFNumber()
val cfLong = 4L.toCFNumber()
val cfFloat = 5f.toCFNumber()
val cfDouble = 6.7.toCFNumber()
val cfString = "string".toCFString()
val cfBoolean = true.toCFBoolean()
val cfDictionary = mapOf<COpaquePointer?, COpaquePointer?>().toCFDictionary()
val cfMutableDictionary = mutableMapOf<COpaquePointer?, COpaquePointer?>().toCFMutableDictionary()
val cfArray = listOf<COpaquePointer?>().toCFArray()
val cfMutableArray = mutableListOf<COpaquePointer?>().toCFMutableArray()
val cfSet = setOf<COpaquePointer?>().toCFSet()
val cfMutableSet = mutableSetOf<COpaquePointer?>().toCFMutableSet()
val cfData = listOf(1.toUbyte()).toCFData()
val cfMutableData = mutableListOf(1.toUbyte()).toCFMutableData()
val cfRange = (0 .. 10).toCFRange()
Create CF data structures.
val cfDictionary = cfDictionaryOf()
val cfMutableDictionary = cfMutableDictionaryOf()
val cfArray = cfArrayOf()
cfMutableArray = cfMutableArrayOf()
cfSet = cfSetOf()
cfMutableSet = cfMutableSetOf()
cfBag = cfBagOf()
cfMutableBag = cfMutableBagOf()
cfData = cfDataOf()
cfMutableData = cfMutableDataOf()
cfBinaryHeap = cfBinaryHeapOf(kCFStringBinaryHeapCallBacks)
cfBitVector = cfBitVectorOf()
cfMutableBitVector = cfMutableBitVectorOf()
cfDate = CFDate()
When reading data from the above data structures there are helpers to work with the correct type.
val key = "string2".toCFString()
val cfDictionary = cfDictionaryOf(key to "string2".toCFString())
val pointer: COpaquePointer? = cfDictionary[key]
val string2: CFStringRef = pointer.asCFString()
A helper lambda can be used to automatically release the allocated values so you don't need to CFRelease().
import nl.ncaj.cf.extension.CFMemScope.Companion.cfMemScoped
fun func() = cfMemScoped {
val arr = cfMutableArray()
}