forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServices.kt
66 lines (53 loc) · 2.41 KB
/
Services.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 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.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:JvmName("ServicesKt_common")
package net.mamoe.mirai.utils
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlin.jvm.JvmName
import kotlin.reflect.KClass
public object Services {
private val lock = reentrantLock()
public fun <T : Any> qualifiedNameOrFail(clazz: KClass<out T>): String =
clazz.qualifiedName ?: error("Could not find qualifiedName for $clazz")
private class Implementation(
val implementationClass: String,
val instance: Lazy<Any>
)
private val registered: MutableMap<String, MutableList<Implementation>> = mutableMapOf()
public fun register(baseClass: String, implementationClass: String, implementation: () -> Any) {
lock.withLock {
registered.getOrPut(baseClass, ::mutableListOf)
.add(Implementation(implementationClass, lazy(implementation)))
}
}
public fun firstImplementationOrNull(baseClass: String): Any? {
lock.withLock {
return registered[baseClass]?.firstOrNull()?.instance?.value
}
}
public fun implementations(baseClass: String): List<Lazy<Any>>? {
lock.withLock {
return registered[baseClass]?.map { it.instance }
}
}
public fun print(): String {
lock.withLock {
return registered.entries.joinToString { "${it.key}:${it.value}" }
}
}
}
public expect fun <T : Any> loadServiceOrNull(clazz: KClass<out T>, fallbackImplementation: String? = null): T?
public expect fun <T : Any> loadService(clazz: KClass<out T>, fallbackImplementation: String? = null): T
public expect fun <T : Any> loadServices(clazz: KClass<out T>): Sequence<T>
public inline fun <reified T : Any> loadService(fallbackImplementation: String? = null): T =
loadService(T::class, fallbackImplementation)
// do not inline: T will be inferred to returning type of `fallbackImplementation`
public fun <T : Any> loadService(clazz: KClass<out T>, fallbackImplementation: () -> T): T =
loadServiceOrNull(clazz) ?: fallbackImplementation()