2020-02-09 01:01:36 +08:00
|
|
|
/*
|
2020-08-16 01:31:14 +08:00
|
|
|
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
2020-02-09 01:01:36 +08:00
|
|
|
*
|
2020-11-01 15:07:32 +08:00
|
|
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
|
|
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
2020-02-09 01:01:36 +08:00
|
|
|
*
|
2020-11-01 15:07:32 +08:00
|
|
|
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
2020-02-09 01:01:36 +08:00
|
|
|
*/
|
|
|
|
|
2020-04-02 09:43:50 +08:00
|
|
|
@file:Suppress("unused", "NOTHING_TO_INLINE")
|
2019-12-18 12:49:51 +08:00
|
|
|
|
|
|
|
package net.mamoe.mirai.utils
|
|
|
|
|
2020-04-02 09:43:50 +08:00
|
|
|
import kotlin.jvm.JvmSynthetic
|
2019-12-18 12:49:51 +08:00
|
|
|
import kotlin.reflect.KProperty
|
|
|
|
|
2020-02-10 16:01:59 +08:00
|
|
|
// TODO: 2020/2/10 添加中文 doc
|
|
|
|
|
2019-12-18 12:49:51 +08:00
|
|
|
/**
|
|
|
|
* WeakRef that `getValue` for delegation throws an [IllegalStateException] if the referent is released by GC. Therefore it returns notnull value only
|
|
|
|
*/
|
2020-07-20 15:06:30 +08:00
|
|
|
public class UnsafeWeakRef<T>(private val weakRef: WeakRef<T>) {
|
|
|
|
public fun get(): T = weakRef.get() ?: error("WeakRef is released")
|
|
|
|
public fun clear(): Unit = weakRef.clear()
|
2020-01-17 16:24:11 +08:00
|
|
|
}
|
2019-12-18 12:49:51 +08:00
|
|
|
|
2020-01-17 16:24:11 +08:00
|
|
|
/**
|
|
|
|
* Provides delegate value.
|
|
|
|
*
|
|
|
|
* ```kotlin
|
|
|
|
* val bot: Bot by param.unsafeWeakRef()
|
|
|
|
* ```
|
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline operator fun <T> UnsafeWeakRef<T>.getValue(thisRef: Any?, property: KProperty<*>): T = get()
|
2019-12-18 12:49:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Weak Reference.
|
2020-02-10 16:01:59 +08:00
|
|
|
* On JVM, it is implemented as a typealias referring to `WeakReference` from JDK.
|
2019-12-18 12:49:51 +08:00
|
|
|
*
|
2020-02-10 16:01:59 +08:00
|
|
|
* Details:
|
|
|
|
* On JVM, instances of objects are stored in the JVM Heap and are accessed via references.
|
|
|
|
* GC(garbage collection) can automatically collect and release the memory used by objects that are not directly referred by any other.
|
|
|
|
* [WeakRef] is not a direct reference, therefore it doesn't hinder GC.
|
2019-12-18 12:49:51 +08:00
|
|
|
*
|
|
|
|
* @see weakRef provides a WeakRef
|
|
|
|
* @see unsafeWeakRef provides a UnsafeWeakRef
|
|
|
|
*/
|
2020-07-20 15:06:30 +08:00
|
|
|
public expect class WeakRef<T>(referent: T) {
|
|
|
|
public fun get(): T?
|
|
|
|
public fun clear()
|
2019-12-18 12:49:51 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 17:25:49 +08:00
|
|
|
/**
|
|
|
|
* Indicates that the property is delegated by a [WeakRef]
|
|
|
|
*
|
|
|
|
* @see weakRef
|
|
|
|
*/
|
|
|
|
@Target(AnnotationTarget.PROPERTY)
|
|
|
|
@Retention(AnnotationRetention.SOURCE)
|
2020-07-20 15:06:30 +08:00
|
|
|
public annotation class WeakRefProperty
|
2019-12-19 17:25:49 +08:00
|
|
|
|
2019-12-18 12:49:51 +08:00
|
|
|
/**
|
|
|
|
* Provides a weak reference to [this]
|
|
|
|
* The `getValue` for delegation returns [this] when [this] is not released by GC
|
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline fun <T> T.weakRef(): WeakRef<T> = WeakRef(this)
|
2019-12-18 12:49:51 +08:00
|
|
|
|
2019-12-19 17:25:49 +08:00
|
|
|
/**
|
|
|
|
* Constructs an unsafe inline delegate for [this]
|
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline fun <T> WeakRef<T>.unsafe(): UnsafeWeakRef<T> = UnsafeWeakRef(this)
|
2019-12-19 17:25:49 +08:00
|
|
|
|
2019-12-18 12:49:51 +08:00
|
|
|
/**
|
|
|
|
* Provides a weak reference to [this].
|
|
|
|
* The `getValue` for delegation throws an [IllegalStateException] if the referent is released by GC. Therefore it returns notnull value only
|
2020-01-07 13:51:16 +08:00
|
|
|
*
|
|
|
|
* **UNSTABLE API**: It is strongly suggested not to use this api
|
2019-12-18 12:49:51 +08:00
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline fun <T> T.unsafeWeakRef(): UnsafeWeakRef<T> = UnsafeWeakRef(this.weakRef())
|
2019-12-18 12:49:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides delegate value.
|
|
|
|
*
|
|
|
|
* ```kotlin
|
|
|
|
* val bot: Bot? by param.weakRef()
|
|
|
|
* ```
|
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline operator fun <T> WeakRef<T>.getValue(thisRef: Any?, property: KProperty<*>): T? = this.get()
|
2019-12-18 12:49:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the block if the referent is absent
|
|
|
|
*/
|
2020-04-02 09:43:50 +08:00
|
|
|
@JvmSynthetic
|
2020-07-20 15:06:30 +08:00
|
|
|
public inline fun <T, R> WeakRef<T>.ifAbsent(block: (T) -> R): R? = this.get()?.let(block)
|