diff --git a/build.gradle.kts b/build.gradle.kts index c8dc9ab..bf84b41 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "ru.landgrafhomyak.kotlin.utilities" -version = "v0.1k2.0.20" +version = "v1.0k2.0.20" repositories { mavenCentral() diff --git a/src/androidNativeArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/androidNativeArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..22b5a57 --- /dev/null +++ b/src/androidNativeArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/androidNativeArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/androidNativeArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..f071368 --- /dev/null +++ b/src/androidNativeArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/androidNativeX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/androidNativeX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..45b9769 --- /dev/null +++ b/src/androidNativeX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/androidNativeX86Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/androidNativeX86Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..1e172ce --- /dev/null +++ b/src/androidNativeX86Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..7911192 --- /dev/null +++ b/src/commonMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 ifNativeMacOS(macos: () -> R, notMacos: () -> R): R + + /** + * Switches to function dependent on OS of **Kotlin/Native** target for which code being compiled. + */ + public inline fun 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 ifNativeIos(ios: () -> R, notIos: () -> R): R + + /** + * Calls [wasm] if function called in code compiled for **Kotlin/WASM** target, otherwise calls [notWasm]. + */ + public inline fun ifAnyWasm(wasm: () -> R, notWasm: () -> R): R +} \ No newline at end of file diff --git a/src/iosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/iosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..35ccecb --- /dev/null +++ b/src/iosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/iosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/iosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..35ccecb --- /dev/null +++ b/src/iosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/iosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/iosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..17dab10 --- /dev/null +++ b/src/iosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/jsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/jsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..f0c374f --- /dev/null +++ b/src/jsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/jvmMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/jvmMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..ac70471 --- /dev/null +++ b/src/jvmMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/linuxArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/linuxArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..f9af816 --- /dev/null +++ b/src/linuxArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/linuxX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/linuxX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..f76047d --- /dev/null +++ b/src/linuxX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/macosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/macosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..2e8c653 --- /dev/null +++ b/src/macosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/macosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/macosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..2b3f3aa --- /dev/null +++ b/src/macosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/mingwMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/mingwMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..27e7b54 --- /dev/null +++ b/src/mingwMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/tvosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/tvosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..4d1cb1c --- /dev/null +++ b/src/tvosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/tvosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/tvosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..7e262a3 --- /dev/null +++ b/src/tvosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/wasmJsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/wasmJsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..e888574 --- /dev/null +++ b/src/wasmJsMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(wasm, InvocationKind.EXACTLY_ONCE) + } + + return wasm() + } +} \ No newline at end of file diff --git a/src/wasmWasiMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/wasmWasiMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..e888574 --- /dev/null +++ b/src/wasmWasiMain/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(wasm, InvocationKind.EXACTLY_ONCE) + } + + return wasm() + } +} \ No newline at end of file diff --git a/src/watchosArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/watchosArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..7721de5 --- /dev/null +++ b/src/watchosArm32Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/watchosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/watchosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..4d1cb1c --- /dev/null +++ b/src/watchosArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/watchosDeviceArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/watchosDeviceArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..4d1cb1c --- /dev/null +++ b/src/watchosDeviceArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/watchosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/watchosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..4d1cb1c --- /dev/null +++ b/src/watchosSimulatorArm64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file diff --git a/src/watchosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt b/src/watchosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt new file mode 100644 index 0000000..7e262a3 --- /dev/null +++ b/src/watchosX64Main/kotlin/ru/landrafhomyak/kotlin/multiplatform_switches/KotlinPlatforms1.kt @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 ifAnyWasm(wasm: () -> R, notWasm: () -> R): R { + contract { + callsInPlace(notWasm, InvocationKind.EXACTLY_ONCE) + } + + return notWasm() + } +} \ No newline at end of file