Статей имеет довольно сложный конфиг.iOS и Macos используют один и тот же код.
Для структурирования проекта существует commonMain
, nativeCommonMain
, зависящее от этого, и фактически appleMain
, которое зависит от nativeCommonMain
.
* 1008.*
Эта структура, вероятно, глубже, чем вам нужно, но нам нужно было что-то для linux и windows, которое было бы другим.Мне кажется, что за ответом Егора легче следовать.
На самом деле мы определяем многоплатформенную атомику в Stately, поэтому вы можете использовать их как источник вдохновения или просто использовать саму библиотеку.
https://github.com/touchlab/Stately
Общий
expect class AtomicInt(initialValue: Int) {
fun get(): Int
fun set(newValue: Int)
fun incrementAndGet(): Int
fun decrementAndGet(): Int
fun addAndGet(delta: Int): Int
fun compareAndSet(expected: Int, new: Int): Boolean
}
JVM
actual typealias AtomicInt = AtomicInteger
Родной
actual class AtomicInt actual constructor(initialValue:Int){
private val atom = AtomicInt(initialValue)
actual fun get(): Int = atom.value
actual fun set(newValue: Int) {
atom.value = newValue
}
actual fun incrementAndGet(): Int = atom.addAndGet(1)
actual fun decrementAndGet(): Int = atom.addAndGet(-1)
actual fun addAndGet(delta: Int): Int = atom.addAndGet(delta)
actual fun compareAndSet(expected: Int, new: Int): Boolean = atom.compareAndSet(expected, new)
}