Compare commits
1 Commits
master/k2.
...
master/k1.
Author | SHA1 | Date | |
---|---|---|---|
a5cff108ec |
@ -1,19 +1,17 @@
|
||||
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform") version "2.0.20"
|
||||
kotlin("multiplatform") version "1.7.20"
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
group = "ru.landgrafhomyak.kotlin.utilities"
|
||||
version = "v1.0k2.0.20"
|
||||
version = "v1.0k1.7.20"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven("https://maven.landgrafhomyak.ru/")
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalWasmDsl::class)
|
||||
kotlin {
|
||||
explicitApi()
|
||||
|
||||
@ -24,24 +22,21 @@ kotlin {
|
||||
browser()
|
||||
nodejs()
|
||||
}
|
||||
wasmWasi {
|
||||
nodejs()
|
||||
}
|
||||
wasmJs {
|
||||
nodejs()
|
||||
browser()
|
||||
d8()
|
||||
}
|
||||
|
||||
macosArm64()
|
||||
macosX64()
|
||||
linuxArm32Hfp()
|
||||
linuxMips32()
|
||||
linuxMipsel32()
|
||||
linuxArm64()
|
||||
linuxX64()
|
||||
mingwX86()
|
||||
mingwX64()
|
||||
androidNativeArm32()
|
||||
androidNativeArm64()
|
||||
androidNativeX64()
|
||||
androidNativeX86()
|
||||
iosArm32()
|
||||
iosArm64()
|
||||
iosSimulatorArm64()
|
||||
iosX64()
|
||||
@ -51,7 +46,6 @@ kotlin {
|
||||
watchosX64()
|
||||
watchosArm32()
|
||||
watchosArm64()
|
||||
watchosDeviceArm64()
|
||||
watchosSimulatorArm64()
|
||||
|
||||
sourceSets {
|
||||
@ -67,7 +61,7 @@ publishing {
|
||||
repositories {
|
||||
maven {
|
||||
name = "XomrkMaven"
|
||||
url = uri("https://maven.landgrafhomyak.ru/")
|
||||
url = uri("https://git.landgrafhomyak.ru/api/packages/xomrk/maven")
|
||||
|
||||
credentials(HttpHeaderCredentials::class) {
|
||||
name = "Authorization"
|
||||
|
@ -1 +1,2 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.internal.mpp.hierarchicalStructureByDefault=true
|
@ -53,10 +53,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNative(native: () -> R, notNative: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notNative, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(native, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notNative()
|
||||
return native()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,10 +64,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(not32, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(is32, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return not32()
|
||||
return is32()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,10 +97,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNativeArm(arm: () -> R, notArm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notArm, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(arm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notArm()
|
||||
return arm()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -165,10 +165,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIos(ios: () -> R, notIos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIos, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(ios, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIos()
|
||||
return ios()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,9 +176,9 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(wasm, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return wasm()
|
||||
return notWasm()
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
package ru.landrafhomyak.kotlin.multiplatform_switches
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
/**
|
||||
* Utility class containing intrinsics for compiling platform-specific calls in simple cases without the actual/expect mechanism.
|
||||
*
|
||||
* All used symbols should be accessible in common (not necessary "commonMain" but any other set that commonizes several targets).
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
public actual object KotlinPlatforms1 {
|
||||
/**
|
||||
* Calls [jvm] if function called in code compiled for **Kotlin/JVM** and [notJvm] for all other targets (including **Kotlin/Android**).
|
||||
*/
|
||||
public actual inline fun <R> ifJvm(jvm: () -> R, notJvm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJvm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJvm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [java] if function called in code compiled for **Kotlin/JVM**, **Kotlin/Android** or whatever platform in future that compiles to java, otherwise calls [notJava].
|
||||
*
|
||||
* To clarify which target being compiled, recursively use functions like [ifJvm][KotlinPlatforms1.ifJvm], [ifAnyAndroid][KotlinPlatforms1.ifAnyAndroid] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJava(java: () -> R, notJava: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJava, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJava()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [android] if function called in code compiled for **Kotlin/Android** and **Kotlin/Native** for the android platform, otherwise calls [notAndroid].
|
||||
*
|
||||
* To determine which **Kotlin/Android** target being compiled, recursively use functions like [ifNative][KotlinPlatforms1.ifNative], [ifAnyJava][KotlinPlatforms1.ifAnyJava] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyAndroid(android: () -> R, notAndroid: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notAndroid, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notAndroid()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [native] if function called in code compiled for any **Kotlin/Native** target, otherwise calls [notNative].
|
||||
*/
|
||||
public actual inline fun <R> ifNative(native: () -> R, notNative: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(native, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return native()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is32] if function called in code compiled for 32-bit **Kotlin/Native** target, otherwise calls [not32].
|
||||
*/
|
||||
public actual inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(is32, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return is32()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is64] if function called in code compiled for 64-bit **Kotlin/Native** target, otherwise calls [not64].
|
||||
*/
|
||||
public actual inline fun <R> ifNative64(is64: () -> R, not64: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(not64, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return not64()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [intel] if function called in code compiled for **Intel X86** or **AMD x86-64** **Kotlin/Native** target, otherwise calls [notIntel].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIntel(intel: () -> R, notIntel: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIntel, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIntel()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [arm] if function called in code compiled for any **ARM** **Kotlin/Native** target, otherwise calls [notArm].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeArm(arm: () -> R, notArm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(arm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return arm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [js] if function called in code compiled for any **Kotlin/JS** target, otherwise calls [notJs].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJs(js: () -> R, notJs: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJs, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [windows] if function called in code compiled for Microsoft Windows **Kotlin/Native** target, otherwise calls [notWindows].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeWindows(windows: () -> R, notWindows: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notWindows, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notWindows()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [linux] if function called in code compiled for GNU Linux **Kotlin/Native** target, otherwise calls [notLinux].
|
||||
*
|
||||
* On **Kotlin/Android** targets will be called [notLinux].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeLinux(linux: () -> R, notLinux: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [macos] if function called in code compiled for Apple Mac OS X **Kotlin/Native** target, otherwise calls [notMacos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeMacOS(macos: () -> R, notMacos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notMacos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notMacos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to function dependent on OS of **Kotlin/Native** target for which code being compiled.
|
||||
*/
|
||||
public actual inline fun <R> switchNativeWindowsLinuxMacos(windows: () -> R, linux: () -> R, macos: () -> R, other: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [ios] if function called in code compiled for Apple iOS **Kotlin/Native** target, otherwise calls [notIos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIos(ios: () -> R, notIos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [wasm] if function called in code compiled for **Kotlin/WASM** target, otherwise calls [notWasm].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notWasm()
|
||||
}
|
||||
}
|
@ -53,10 +53,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNative(native: () -> R, notNative: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notNative, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(native, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notNative()
|
||||
return native()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,10 +64,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(not32, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(is32, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return not32()
|
||||
return is32()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -132,10 +132,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifNativeLinux(linux: () -> R, notLinux: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notLinux, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notLinux()
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,10 +154,10 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> switchNativeWindowsLinuxMacos(windows: () -> R, linux: () -> R, macos: () -> R, other: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(other, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return other()
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,9 +176,9 @@ public actual object KotlinPlatforms1 {
|
||||
*/
|
||||
public actual inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(wasm, InvocationKind.EXACTLY_ONCE)
|
||||
callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return wasm()
|
||||
return notWasm()
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
package ru.landrafhomyak.kotlin.multiplatform_switches
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
/**
|
||||
* Utility class containing intrinsics for compiling platform-specific calls in simple cases without the actual/expect mechanism.
|
||||
*
|
||||
* All used symbols should be accessible in common (not necessary "commonMain" but any other set that commonizes several targets).
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
public actual object KotlinPlatforms1 {
|
||||
/**
|
||||
* Calls [jvm] if function called in code compiled for **Kotlin/JVM** and [notJvm] for all other targets (including **Kotlin/Android**).
|
||||
*/
|
||||
public actual inline fun <R> ifJvm(jvm: () -> R, notJvm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJvm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJvm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [java] if function called in code compiled for **Kotlin/JVM**, **Kotlin/Android** or whatever platform in future that compiles to java, otherwise calls [notJava].
|
||||
*
|
||||
* To clarify which target being compiled, recursively use functions like [ifJvm][KotlinPlatforms1.ifJvm], [ifAnyAndroid][KotlinPlatforms1.ifAnyAndroid] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJava(java: () -> R, notJava: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJava, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJava()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [android] if function called in code compiled for **Kotlin/Android** and **Kotlin/Native** for the android platform, otherwise calls [notAndroid].
|
||||
*
|
||||
* To determine which **Kotlin/Android** target being compiled, recursively use functions like [ifNative][KotlinPlatforms1.ifNative], [ifAnyJava][KotlinPlatforms1.ifAnyJava] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyAndroid(android: () -> R, notAndroid: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notAndroid, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notAndroid()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [native] if function called in code compiled for any **Kotlin/Native** target, otherwise calls [notNative].
|
||||
*/
|
||||
public actual inline fun <R> ifNative(native: () -> R, notNative: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(native, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return native()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is32] if function called in code compiled for 32-bit **Kotlin/Native** target, otherwise calls [not32].
|
||||
*/
|
||||
public actual inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(is32, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return is32()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is64] if function called in code compiled for 64-bit **Kotlin/Native** target, otherwise calls [not64].
|
||||
*/
|
||||
public actual inline fun <R> ifNative64(is64: () -> R, not64: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(not64, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return not64()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [intel] if function called in code compiled for **Intel X86** or **AMD x86-64** **Kotlin/Native** target, otherwise calls [notIntel].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIntel(intel: () -> R, notIntel: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIntel, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIntel()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [arm] if function called in code compiled for any **ARM** **Kotlin/Native** target, otherwise calls [notArm].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeArm(arm: () -> R, notArm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notArm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notArm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [js] if function called in code compiled for any **Kotlin/JS** target, otherwise calls [notJs].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJs(js: () -> R, notJs: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJs, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [windows] if function called in code compiled for Microsoft Windows **Kotlin/Native** target, otherwise calls [notWindows].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeWindows(windows: () -> R, notWindows: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notWindows, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notWindows()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [linux] if function called in code compiled for GNU Linux **Kotlin/Native** target, otherwise calls [notLinux].
|
||||
*
|
||||
* On **Kotlin/Android** targets will be called [notLinux].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeLinux(linux: () -> R, notLinux: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [macos] if function called in code compiled for Apple Mac OS X **Kotlin/Native** target, otherwise calls [notMacos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeMacOS(macos: () -> R, notMacos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notMacos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notMacos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to function dependent on OS of **Kotlin/Native** target for which code being compiled.
|
||||
*/
|
||||
public actual inline fun <R> switchNativeWindowsLinuxMacos(windows: () -> R, linux: () -> R, macos: () -> R, other: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(linux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return linux()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [ios] if function called in code compiled for Apple iOS **Kotlin/Native** target, otherwise calls [notIos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIos(ios: () -> R, notIos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [wasm] if function called in code compiled for **Kotlin/WASM** target, otherwise calls [notWasm].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notWasm()
|
||||
}
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
package ru.landrafhomyak.kotlin.multiplatform_switches
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
/**
|
||||
* Utility class containing intrinsics for compiling platform-specific calls in simple cases without the actual/expect mechanism.
|
||||
*
|
||||
* All used symbols should be accessible in common (not necessary "commonMain" but any other set that commonizes several targets).
|
||||
*/
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
public actual object KotlinPlatforms1 {
|
||||
/**
|
||||
* Calls [jvm] if function called in code compiled for **Kotlin/JVM** and [notJvm] for all other targets (including **Kotlin/Android**).
|
||||
*/
|
||||
public actual inline fun <R> ifJvm(jvm: () -> R, notJvm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJvm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJvm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [java] if function called in code compiled for **Kotlin/JVM**, **Kotlin/Android** or whatever platform in future that compiles to java, otherwise calls [notJava].
|
||||
*
|
||||
* To clarify which target being compiled, recursively use functions like [ifJvm][KotlinPlatforms1.ifJvm], [ifAnyAndroid][KotlinPlatforms1.ifAnyAndroid] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJava(java: () -> R, notJava: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJava, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJava()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [android] if function called in code compiled for **Kotlin/Android** and **Kotlin/Native** for the android platform, otherwise calls [notAndroid].
|
||||
*
|
||||
* To determine which **Kotlin/Android** target being compiled, recursively use functions like [ifNative][KotlinPlatforms1.ifNative], [ifAnyJava][KotlinPlatforms1.ifAnyJava] and others.
|
||||
*/
|
||||
public actual inline fun <R> ifAnyAndroid(android: () -> R, notAndroid: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notAndroid, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notAndroid()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [native] if function called in code compiled for any **Kotlin/Native** target, otherwise calls [notNative].
|
||||
*/
|
||||
public actual inline fun <R> ifNative(native: () -> R, notNative: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(native, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return native()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is32] if function called in code compiled for 32-bit **Kotlin/Native** target, otherwise calls [not32].
|
||||
*/
|
||||
public actual inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(is32, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return is32()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [is64] if function called in code compiled for 64-bit **Kotlin/Native** target, otherwise calls [not64].
|
||||
*/
|
||||
public actual inline fun <R> ifNative64(is64: () -> R, not64: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(not64, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return not64()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [intel] if function called in code compiled for **Intel X86** or **AMD x86-64** **Kotlin/Native** target, otherwise calls [notIntel].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIntel(intel: () -> R, notIntel: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(intel, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return intel()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [arm] if function called in code compiled for any **ARM** **Kotlin/Native** target, otherwise calls [notArm].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeArm(arm: () -> R, notArm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notArm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notArm()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [js] if function called in code compiled for any **Kotlin/JS** target, otherwise calls [notJs].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyJs(js: () -> R, notJs: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notJs, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notJs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [windows] if function called in code compiled for Microsoft Windows **Kotlin/Native** target, otherwise calls [notWindows].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeWindows(windows: () -> R, notWindows: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(windows, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return windows()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [linux] if function called in code compiled for GNU Linux **Kotlin/Native** target, otherwise calls [notLinux].
|
||||
*
|
||||
* On **Kotlin/Android** targets will be called [notLinux].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeLinux(linux: () -> R, notLinux: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notLinux, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notLinux()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [macos] if function called in code compiled for Apple Mac OS X **Kotlin/Native** target, otherwise calls [notMacos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeMacOS(macos: () -> R, notMacos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notMacos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notMacos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches to function dependent on OS of **Kotlin/Native** target for which code being compiled.
|
||||
*/
|
||||
public actual inline fun <R> switchNativeWindowsLinuxMacos(windows: () -> R, linux: () -> R, macos: () -> R, other: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(windows, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return windows()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [ios] if function called in code compiled for Apple iOS **Kotlin/Native** target, otherwise calls [notIos].
|
||||
*/
|
||||
public actual inline fun <R> ifNativeIos(ios: () -> R, notIos: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notIos, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notIos()
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls [wasm] if function called in code compiled for **Kotlin/WASM** target, otherwise calls [notWasm].
|
||||
*/
|
||||
public actual inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
|
||||
return notWasm()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user