Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
eb9a64e5
Commit
eb9a64e5
authored
Dec 18, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add WeakRef
parent
e90be8b9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
0 deletions
+69
-0
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
...e/src/androidMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
+5
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt
...re/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt
+59
-0
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
...-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
+5
-0
No files found.
mirai-core/src/androidMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
0 → 100644
View file @
eb9a64e5
package
net.mamoe.mirai.utils
import
java.lang.ref.WeakReference
actual
typealias
WeakRef
<
T
>
=
WeakReference
<
T
>
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/WeakRef.kt
0 → 100644
View file @
eb9a64e5
@
file
:
Suppress
(
"unused"
)
package
net.mamoe.mirai.utils
import
kotlin.reflect.KProperty
/**
* WeakRef that `getValue` for delegation throws an [IllegalStateException] if the referent is released by GC. Therefore it returns notnull value only
*/
inline
class
UnsafeWeakRef
<
T
>(
private
val
weakRef
:
WeakRef
<
T
>)
{
fun
get
():
T
=
weakRef
.
get
()
?:
error
(
"WeakRef is released"
)
fun
clear
()
=
weakRef
.
clear
()
operator
fun
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
?
=
this
.
get
()
}
/**
* Weak Reference.
* In JVM, this is implemented as a typealias to `WeakReference` from JDK.
*
* Reference details:
* In JVM, instances of objects are stored in the Heap and are accessed via references.
* GC can automatically collect and release the memory used by objects that are not directly referred by any other.
* WeakReference is not direct reference, therefore it does no influence on garbage collection.
* Using weak reference can help GC with that.
*
* @see weakRef provides a WeakRef
* @see unsafeWeakRef provides a UnsafeWeakRef
*/
expect
class
WeakRef
<
T
>(
referent
:
T
)
{
fun
get
():
T
?
fun
clear
()
}
/**
* Provides a weak reference to [this]
* The `getValue` for delegation returns [this] when [this] is not released by GC
*/
fun
<
T
>
T
.
weakRef
():
WeakRef
<
T
>
=
WeakRef
(
this
)
/**
* 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
*/
fun
<
T
>
T
.
unsafeWeakRef
():
UnsafeWeakRef
<
T
>
=
UnsafeWeakRef
(
this
.
weakRef
())
/**
* Provides delegate value.
*
* ```kotlin
* val bot: Bot? by param.weakRef()
* ```
*/
operator
fun
<
T
>
WeakRef
<
T
>.
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
?
=
this
.
get
()
/**
* Call the block if the referent is absent
*/
inline
fun
<
T
,
R
>
WeakRef
<
T
>.
ifAbsent
(
block
:
(
T
)
->
R
):
R
?
=
this
.
get
()
?.
let
(
block
)
\ No newline at end of file
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/WeakRef.kt
0 → 100644
View file @
eb9a64e5
package
net.mamoe.mirai.utils
import
java.lang.ref.WeakReference
actual
typealias
WeakRef
<
T
>
=
WeakReference
<
T
>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment