nativeMain.WeakRef.native.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weak Show documentation
Show all versions of weak Show documentation
Weak references and maps for Kotlin Multiplatform
The newest version!
package opensavvy.pedestal.weak
import opensavvy.pedestal.weak.algorithms.EmptyWeakRef
import kotlin.experimental.ExperimentalNativeApi
import kotlin.native.ref.WeakReference
@ExperimentalNativeApi
private class NativeWeakRef(
value: T,
) : WeakRef {
private val reference = WeakReference(value)
override fun read(): T? =
reference.get()
}
/**
* Implementation of [WeakRef] backed by a native [WeakReference].
*
* Kotlin Native doesn't make a difference between weak and soft references.
*/
@ExperimentalWeakApi
@ExperimentalNativeApi
actual fun WeakRef(value: T): WeakRef =
if (value == null) EmptyWeakRef(value)
else NativeWeakRef(value)
/**
* Implementation of [WeakRef] backed by a native [WeakReference].
*
* Kotlin Native doesn't make a difference between weak and soft references.
*/
@ExperimentalWeakApi
@ExperimentalNativeApi
@Suppress("FunctionName")
actual fun SoftRef(value: T): WeakRef =
WeakRef(value)