Switches implementation for Kotlin 2.0.20

This commit is contained in:
Andrew Golovashevich 2024-11-04 21:15:23 +03:00
parent b6ccb14fd8
commit e2e847fa45
25 changed files with 4323 additions and 1 deletions

View File

@ -6,7 +6,7 @@ plugins {
} }
group = "ru.landgrafhomyak.kotlin.utilities" group = "ru.landgrafhomyak.kotlin.utilities"
version = "v0.1k2.0.20" version = "v1.0k2.0.20"
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -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(android, InvocationKind.EXACTLY_ONCE)
}
return android()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(android, InvocationKind.EXACTLY_ONCE)
}
return android()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(android, InvocationKind.EXACTLY_ONCE)
}
return android()
}
/**
* 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(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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(android, InvocationKind.EXACTLY_ONCE)
}
return android()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -0,0 +1,90 @@
package ru.landrafhomyak.kotlin.multiplatform_switches
/**
* 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).
*/
public expect object KotlinPlatforms1 {
/**
* Calls [jvm] if function called in code compiled for **Kotlin/JVM** and [notJvm] for all other targets (including **Kotlin/Android**).
*/
public inline fun <R> ifJvm(jvm: () -> R, notJvm: () -> R): R
/**
* 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 inline fun <R> ifAnyJava(java: () -> R, notJava: () -> R): R
/**
* 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 inline fun <R> ifAnyAndroid(android: () -> R, notAndroid: () -> R): R
/**
* Calls [native] if function called in code compiled for any **Kotlin/Native** target, otherwise calls [notNative].
*/
public inline fun <R> ifNative(native: () -> R, notNative: () -> R): R
/**
* Calls [is32] if function called in code compiled for 32-bit **Kotlin/Native** target, otherwise calls [not32].
*/
public inline fun <R> ifNative32(is32: () -> R, not32: () -> R): R
/**
* Calls [is64] if function called in code compiled for 64-bit **Kotlin/Native** target, otherwise calls [not64].
*/
public inline fun <R> ifNative64(is64: () -> R, not64: () -> R): R
/**
* Calls [intel] if function called in code compiled for **Intel X86** or **AMD x86-64** **Kotlin/Native** target, otherwise calls [notIntel].
*/
public inline fun <R> ifNativeIntel(intel: () -> R, notIntel: () -> R): R
/**
* Calls [arm] if function called in code compiled for any **ARM** **Kotlin/Native** target, otherwise calls [notArm].
*/
public inline fun <R> ifNativeArm(arm: () -> R, notArm: () -> R): R
/**
* Calls [js] if function called in code compiled for any **Kotlin/JS** target, otherwise calls [notJs].
*/
public inline fun <R> ifAnyJs(js: () -> R, notJs: () -> R): R
/**
* Calls [windows] if function called in code compiled for Microsoft Windows **Kotlin/Native** target, otherwise calls [notWindows].
*/
public inline fun <R> ifNativeWindows(windows: () -> R, notWindows: () -> R): R
/**
* 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 inline fun <R> ifNativeLinux(linux: () -> R, notLinux: () -> R): R
/**
* Calls [macos] if function called in code compiled for Apple Mac OS X **Kotlin/Native** target, otherwise calls [notMacos].
*/
public inline fun <R> ifNativeMacOS(macos: () -> R, notMacos: () -> R): R
/**
* Switches to function dependent on OS of **Kotlin/Native** target for which code being compiled.
*/
public inline fun <R> switchNativeWindowsLinuxMacos(windows: () -> R, linux: () -> R, macos: () -> R, other: () -> R): R
/**
* Calls [ios] if function called in code compiled for Apple iOS **Kotlin/Native** target, otherwise calls [notIos].
*/
public inline fun <R> ifNativeIos(ios: () -> R, notIos: () -> R): R
/**
* Calls [wasm] if function called in code compiled for **Kotlin/WASM** target, otherwise calls [notWasm].
*/
public inline fun <R> ifAnyWasm(wasm: () -> R, notWasm: () -> R): R
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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(ios, InvocationKind.EXACTLY_ONCE)
}
return ios()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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(ios, InvocationKind.EXACTLY_ONCE)
}
return ios()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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(ios, InvocationKind.EXACTLY_ONCE)
}
return ios()
}
/**
* 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()
}
}

View File

@ -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(notNative, InvocationKind.EXACTLY_ONCE)
}
return notNative()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(js, InvocationKind.EXACTLY_ONCE)
}
return js()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(jvm, InvocationKind.EXACTLY_ONCE)
}
return jvm()
}
/**
* 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(java, InvocationKind.EXACTLY_ONCE)
}
return java()
}
/**
* 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(notNative, InvocationKind.EXACTLY_ONCE)
}
return notNative()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(macos, InvocationKind.EXACTLY_ONCE)
}
return macos()
}
/**
* 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(macos, InvocationKind.EXACTLY_ONCE)
}
return macos()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(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(macos, InvocationKind.EXACTLY_ONCE)
}
return macos()
}
/**
* 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(macos, InvocationKind.EXACTLY_ONCE)
}
return macos()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(notNative, InvocationKind.EXACTLY_ONCE)
}
return notNative()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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(wasm, InvocationKind.EXACTLY_ONCE)
}
return wasm()
}
}

View File

@ -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(notNative, InvocationKind.EXACTLY_ONCE)
}
return notNative()
}
/**
* 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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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(wasm, InvocationKind.EXACTLY_ONCE)
}
return wasm()
}
}

View File

@ -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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}

View File

@ -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(not32, InvocationKind.EXACTLY_ONCE)
}
return not32()
}
/**
* 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(is64, InvocationKind.EXACTLY_ONCE)
}
return is64()
}
/**
* 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(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(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(other, InvocationKind.EXACTLY_ONCE)
}
return other()
}
/**
* 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()
}
}