From 3302c67fa6bbf0c15c6fd67051ba00e022d3c04a Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sat, 16 Aug 2025 19:33:00 +0300 Subject: [PATCH] Fixed wrong package name --- .../CloseableReferenceCounter.kt | 35 ++++ .../CloseableReferenceCounter_Debug.kt | 161 ++++++++++++++++++ .../MayBeClosedReferenceCounter.kt | 131 ++++++++++++++ .../utility/reference_counter/_AtomicLong.kt | 2 +- .../utility/reference_counter/_Misc.kt | 2 +- .../utility/reference_counter/_Platform.kt | 2 +- .../CloseableReferenceCounter.kt | 22 +-- .../CloseableReferenceCounter_Debug.kt | 121 ++++--------- .../MayBeClosedReferenceCounter.kt | 85 ++++----- .../WrapperReferenceCounter.kt | 20 +++ .../utility/reference_counter/_AtomicLong.kt | 2 +- .../utility/reference_counter/_Platform.kt | 2 +- .../tests/KotlinStdlibDependencyTest.kt | 6 +- .../utility/reference_counter/_AtomicLong.kt | 2 +- .../utility/reference_counter/_Platform.kt | 2 +- 15 files changed, 437 insertions(+), 158 deletions(-) create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt create mode 100644 src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt rename src/commonMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_AtomicLong.kt (85%) rename src/commonMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_Misc.kt (88%) rename src/commonMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_Platform.kt (75%) create mode 100644 src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/WrapperReferenceCounter.kt rename src/jvmMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_AtomicLong.kt (96%) rename src/jvmMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_Platform.kt (86%) rename src/jvmTest/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt (94%) rename src/nonJvmMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_AtomicLong.kt (94%) rename src/nonJvmMain/kotlin/ru/{landrafhomyak => landgrafhomyak}/utility/reference_counter/_Platform.kt (76%) diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt new file mode 100644 index 0000000..41b37a6 --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt @@ -0,0 +1,35 @@ +package ru.landgrafhomyak.utility.reference_counter + +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.jvm.JvmName +import kotlin.jvm.JvmStatic + +public class CloseableReferenceCounter : MayBeClosedReferenceCounter { + public constructor(errMessageClosed: String) : super(errMessageClosed) + + @Suppress("INAPPLICABLE_JVM_NAME") + @JvmName("close") + public override fun close(errExistRefs: String) { + super.close(errExistRefs) + } + + public companion object { + @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND") + @JvmStatic + @JvmName("autoClosed") + public fun autoClosed(errMessageClosed: String, errExistRefs: String, scope: (MayBeClosedReferenceCounter) -> R): R { + contract { + callsInPlace(scope, InvocationKind.EXACTLY_ONCE) + } + _Platform.jvm_assertNotNull(errMessageClosed, "param: errMessageClosed") + _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") + _Platform.jvm_assertNotNull(scope, "param: scope") + + val refcnt = CloseableReferenceCounter(errMessageClosed) + val ret = scope(refcnt) + refcnt.close(errExistRefs) + return ret + } + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt new file mode 100644 index 0000000..1577725 --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt @@ -0,0 +1,161 @@ +@file:OptIn(ExperimentalContracts::class) + +package ru.landgrafhomyak.utility.reference_counter + +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.jvm.JvmName +import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 +import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 + +@CloseableReferenceCounter_Debug.RequiresExplicitDebug +public class CloseableReferenceCounter_Debug { + public fun interface Observer { + public fun observeState(instance: CloseableReferenceCounter_Debug, actions: String) + } + + @RequiresOptIn(level = RequiresOptIn.Level.WARNING) + @Retention(AnnotationRetention.BINARY) + public annotation class RequiresExplicitDebug + + private val _dbgName: String + private val _errMessage: String + private val _logger: Observer? + private val _value: _AtomicLong + private val _id: Long + + public constructor(dbgName: String, errMessage: String, logger: Observer? = null) { + _Platform.jvm_assertNotNull(dbgName, "param: dbgName") + _Platform.jvm_assertNotNull(errMessage, "param: errMessage") + + this._dbgName = dbgName + this._errMessage = dbgName + this._logger = logger + this._value = _AtomicLong(0L) + @Suppress("RemoveRedundantQualifierName") + this._id = CloseableReferenceCounter_Debug._nextId.getAndUpdate(Long::inc) + } + + + @Suppress("NOTHING_TO_INLINE") + private inline fun _throwErrors(valueToCheck: Long) { + when { + valueToCheck >= 0 || valueToCheck == _Misc.CLOSED_STATE_VALUE -> {} + valueToCheck < _Misc.CLOSED_STATE_VALUE -> throw RuntimeException("Too many references") + valueToCheck > _Misc.CLOSED_STATE_VALUE -> throw RuntimeException(".decref called more times than .incref") + } + } + + @RequiresExplicitDebug + @JvmName("throwErrors") + public fun throwErrors() { + this._throwErrors(this._value.get()) + } + + @JvmName("throwClosed") + public fun throwClosed() { + throw IllegalStateException(this._errMessage) + } + + @JvmName("incref") + public fun incref() { + this._value.update { o -> + if (o < 0) { + this._throwErrors(o) + this.throwClosed() + this._throwErrors(o + 1) + } + return@update o + 1 + } + this._logger?.observeState(this, "incref") + } + + @JvmName("tryIncref\$kt") + public inline fun tryIncref(block: () -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + this.incref() + return safeAutoClose2(onError = this::decref, action = block) + } + + @JvmName("assertNotClosed") + public fun assertNotClosed() { + if (this._value.get() < 0) this.throwClosed() + } + + @JvmName("decref") + public fun decref() { + this._value.update { o -> + if (o < 0) { + this._throwErrors(o) + this.throwClosed() + this._throwErrors(o - 1) + } + return@update o - 1 + } + this._logger?.observeState(this, "decref") + } + + @JvmName("tryDecref\$kt") + public inline fun tryDecref(block: () -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + this.assertNotClosed() + return safeAutoClose2(onSuccess = this::decref, action = block) + } + + @get:JvmName("isClosed") + public val isClosed: Boolean + get() { + val state = this._value.get() + this._throwErrors(state) + return state < 0; + } + + @JvmName("close") + public fun close(errExistRefs: String) { + _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") + val state = this._value.compareAndExchange(0, _Misc.CLOSED_STATE_VALUE) + this._throwErrors(state) + when { + state > 0 -> throw IllegalStateException(errExistRefs) + state < 0 -> this.throwClosed() + } + this._logger?.observeState(this, "closed") + } + + @JvmName("withRef\$kt") + public inline fun withRef(protected: () -> R): R { + this.incref() + return safeAutoClose1(finally = this::decref, action = protected) + } + + override fun toString(): String { + val refcntCached = this._value.get() + val stateRepr: String + @Suppress("LiftReturnOrAssignment") + when { + refcntCached >= 0 -> stateRepr = refcntCached.toString() + refcntCached == _Misc.CLOSED_STATE_VALUE -> stateRepr = "closed" + refcntCached < _Misc.CLOSED_STATE_VALUE -> stateRepr = "overflow" + refcntCached > _Misc.CLOSED_STATE_VALUE -> stateRepr = "underflow" + else -> throw Error("Unreachable") + } + return "" + } + + public companion object { + private val _nextId = _AtomicLong(0L) + } + + public object ObserveToStdout : Observer { + override fun observeState(instance: CloseableReferenceCounter_Debug, actions: String) { + _Platform.jvm_assertNotNull(instance, "param: instance") + _Platform.jvm_assertNotNull(actions, "param: actions") + print("${instance} ${actions}\n") + } + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt new file mode 100644 index 0000000..9b0015a --- /dev/null +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt @@ -0,0 +1,131 @@ +@file:OptIn(ExperimentalContracts::class) + +package ru.landgrafhomyak.utility.reference_counter + +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.jvm.JvmName +import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 +import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 + +public sealed class MayBeClosedReferenceCounter { + private val _value: _AtomicLong + private val _errMessageClosed: String + + protected constructor(errMessageClosed: String) { + _Platform.jvm_assertNotNull(errMessageClosed, "param: errMessageClosed") + this._errMessageClosed = errMessageClosed + this._value = _AtomicLong(0L) + } + + @JvmName("throwClosed") + public fun throwClosed() { + throw IllegalStateException(this._errMessageClosed) + } + + @JvmName("incref") + public fun incref() { + this._value.update { o -> + if (o < 0) this.throwClosed() + return@update o + 1 + } + } + + @JvmName("tryIncref\$kt") + public inline fun tryIncref(block: () -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + this.incref() + return safeAutoClose2(onError = this::decref, action = block) + } + + @JvmName("assertNotClosed") + public fun assertNotClosed() { + if (this._value.get() < 0) this.throwClosed() + } + + @JvmName("decref") + public fun decref() { + this._value.update(Long::dec) + } + + @JvmName("tryDecref\$kt") + public inline fun tryDecref(block: () -> R): R { + contract { + callsInPlace(block, InvocationKind.EXACTLY_ONCE) + } + this.assertNotClosed() + return safeAutoClose2(onSuccess = this::decref, action = block) + } + + @get:JvmName("isClosed") + public val isClosed: Boolean get() = this._value.get() < 0 + + @Suppress("INAPPLICABLE_JVM_NAME") + @JvmName("close") + protected open fun close(errExistRefs: String) { + _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") + val state = this._value.compareAndExchange(0, _Misc.CLOSED_STATE_VALUE) + when { + state > 0 -> throw IllegalStateException(errExistRefs) + state < 0 -> this.throwClosed() + } + } + + @JvmName("withRef\$kt") + public inline fun withRef(protected: () -> R): R { + contract { + callsInPlace(protected, InvocationKind.EXACTLY_ONCE) + } + this.incref() + return safeAutoClose1(finally = this::decref, action = protected) + } + + override fun toString(): String { + val refcntCached = this._value.get() + @Suppress("LiftReturnOrAssignment") + if (refcntCached < 0) + return "" + else + return "" + } + + @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND") + @JvmName("child") + public fun child(errMessageClosed: String, errExistRefs: String, scope: (MayBeClosedReferenceCounter) -> R): R { + contract { + callsInPlace(scope, InvocationKind.EXACTLY_ONCE) + } + + _Platform.jvm_assertNotNull(errMessageClosed, "param: errMessageClosed") + _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") + _Platform.jvm_assertNotNull(scope, "param: scope") + + this.withRef { + return CloseableReferenceCounter.autoClosed( + errMessageClosed = errMessageClosed, + errExistRefs = errExistRefs, + scope = scope + ) + } + } + + @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND") + @JvmName("child_inheritErrMessage") + public fun child_inheritErrMessage(errExistRefs: String, scope: (MayBeClosedReferenceCounter) -> R): R { + contract { + callsInPlace(scope, InvocationKind.EXACTLY_ONCE) + } + + _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") + _Platform.jvm_assertNotNull(scope, "param: scope") + + return this.child( + errMessageClosed = this._errMessageClosed, + errExistRefs = errExistRefs, + scope = scope + ) + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt similarity index 85% rename from src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt index 3aa6174..2acbfed 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter internal expect class _AtomicLong { constructor(initial: Long) diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Misc.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Misc.kt similarity index 88% rename from src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Misc.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Misc.kt index 8729f77..4801381 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Misc.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Misc.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter import kotlin.jvm.JvmStatic diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt similarity index 75% rename from src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt rename to src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt index fb0bbc7..6518cec 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt +++ b/src/commonMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter import kotlin.jvm.JvmStatic diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt index 0c6edc8..0ce2894 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter.kt @@ -4,14 +4,21 @@ import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.jvm.JvmName import kotlin.jvm.JvmStatic +import ru.landgrafhomyak.utility.reference_counter._Platform +import ru.landgrafhomyak.utility.reference_counter.CloseableReferenceCounter as moved_CloseableReferenceCounter +@Suppress("DEPRECATION_ERROR") +@Deprecated( + message = "Class moved to another package", + level = DeprecationLevel.ERROR +) public class CloseableReferenceCounter : MayBeClosedReferenceCounter { - public constructor(errMessageClosed: String) : super(errMessageClosed) + public constructor(errMessageClosed: String) : super(moved_CloseableReferenceCounter(errMessageClosed)) - @Suppress("INAPPLICABLE_JVM_NAME") + @Suppress("INAPPLICABLE_JVM_NAME", "NON_FINAL_MEMBER_IN_FINAL_CLASS") @JvmName("close") - public override fun close(errExistRefs: String) { - super.close(errExistRefs) + public open fun close(errExistRefs: String) { + (this._moved as moved_CloseableReferenceCounter).close(errExistRefs) } public companion object { @@ -22,14 +29,9 @@ public class CloseableReferenceCounter : MayBeClosedReferenceCounter { contract { callsInPlace(scope, InvocationKind.EXACTLY_ONCE) } - _Platform.jvm_assertNotNull(errMessageClosed, "param: errMessageClosed") - _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") _Platform.jvm_assertNotNull(scope, "param: scope") - val refcnt = CloseableReferenceCounter(errMessageClosed) - val ret = scope(refcnt) - refcnt.close(errExistRefs) - return ret + return moved_CloseableReferenceCounter.autoClosed(errMessageClosed, errExistRefs, WrapperReferenceCounter.wrapperLambda(scope)) } } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt index 2f0c97c..9168cfc 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/CloseableReferenceCounter_Debug.kt @@ -5,70 +5,57 @@ package ru.landrafhomyak.utility.reference_counter import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract +import kotlin.jvm.JvmField import kotlin.jvm.JvmName -import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 -import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.reference_counter.CloseableReferenceCounter_Debug as moved_CloseableReferenceCounter_Debug +@Suppress("DEPRECATION_ERROR") +@Deprecated( + message = "Class moved to another package", + level = DeprecationLevel.ERROR +) +@OptIn(moved_CloseableReferenceCounter_Debug.RequiresExplicitDebug::class) @CloseableReferenceCounter_Debug.RequiresExplicitDebug public class CloseableReferenceCounter_Debug { public fun interface Observer { public fun observeState(instance: CloseableReferenceCounter_Debug, actions: String) } + private class ObserverAdapter(private val old: Observer) : moved_CloseableReferenceCounter_Debug.Observer { + lateinit var wrapper: CloseableReferenceCounter_Debug + override fun observeState(instance: moved_CloseableReferenceCounter_Debug, actions: String) { + this.old.observeState(this.wrapper, actions) + } + } + @RequiresOptIn(level = RequiresOptIn.Level.WARNING) @Retention(AnnotationRetention.BINARY) public annotation class RequiresExplicitDebug - private val _dbgName: String - private val _errMessage: String - private val _logger: Observer? - private val _value: _AtomicLong - private val _id: Long + @PublishedApi + @JvmField + internal val _moved: moved_CloseableReferenceCounter_Debug public constructor(dbgName: String, errMessage: String, logger: Observer? = null) { - _Platform.jvm_assertNotNull(dbgName, "param: dbgName") - _Platform.jvm_assertNotNull(errMessage, "param: errMessage") - - this._dbgName = dbgName - this._errMessage = dbgName - this._logger = logger - this._value = _AtomicLong(0L) - @Suppress("RemoveRedundantQualifierName") - this._id = CloseableReferenceCounter_Debug._nextId.getAndUpdate(Long::inc) - } - - - @Suppress("NOTHING_TO_INLINE") - private inline fun _throwErrors(valueToCheck: Long) { - when { - valueToCheck >= 0 || valueToCheck == _Misc.CLOSED_STATE_VALUE -> {} - valueToCheck < _Misc.CLOSED_STATE_VALUE -> throw RuntimeException("Too many references") - valueToCheck > _Misc.CLOSED_STATE_VALUE -> throw RuntimeException(".decref called more times than .incref") - } + val obs = logger?.let(::ObserverAdapter) + this._moved = moved_CloseableReferenceCounter_Debug(dbgName, errMessage, obs) + obs?.wrapper = this } @RequiresExplicitDebug @JvmName("throwErrors") public fun throwErrors() { - this._throwErrors(this._value.get()) + this._moved.throwErrors() } @JvmName("throwClosed") public fun throwClosed() { - throw IllegalStateException(this._errMessage) + this._moved.throwClosed() } @JvmName("incref") public fun incref() { - this._value.update { o -> - if (o < 0) { - this._throwErrors(o) - this.throwClosed() - this._throwErrors(o + 1) - } - return@update o + 1 - } - this._logger?.observeState(this, "incref") + this._moved.incref() } @JvmName("tryIncref\$kt") @@ -76,26 +63,17 @@ public class CloseableReferenceCounter_Debug { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - this.incref() - return safeAutoClose2(onError = this::decref, action = block) + return this._moved.tryIncref(block) } @JvmName("assertNotClosed") public fun assertNotClosed() { - if (this._value.get() < 0) this.throwClosed() + this._moved.assertNotClosed() } @JvmName("decref") public fun decref() { - this._value.update { o -> - if (o < 0) { - this._throwErrors(o) - this.throwClosed() - this._throwErrors(o - 1) - } - return@update o - 1 - } - this._logger?.observeState(this, "decref") + this._moved.decref() } @JvmName("tryDecref\$kt") @@ -103,59 +81,34 @@ public class CloseableReferenceCounter_Debug { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - this.assertNotClosed() - return safeAutoClose2(onSuccess = this::decref, action = block) + return this._moved.tryDecref(block) } @get:JvmName("isClosed") public val isClosed: Boolean - get() { - val state = this._value.get() - this._throwErrors(state) - return state < 0; - } + get() = this._moved.isClosed @JvmName("close") public fun close(errExistRefs: String) { - _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") - val state = this._value.compareAndExchange(0, _Misc.CLOSED_STATE_VALUE) - this._throwErrors(state) - when { - state > 0 -> throw IllegalStateException(errExistRefs) - state < 0 -> this.throwClosed() - } - this._logger?.observeState(this, "closed") + this._moved.close(errExistRefs) } @JvmName("withRef\$kt") public inline fun withRef(protected: () -> R): R { - this.incref() - return safeAutoClose1(finally = this::decref, action = protected) + /*contract { + callsInPlace(protected, InvocationKind.EXACTLY_ONCE) + }*/ + return this._moved.withRef(protected) } override fun toString(): String { - val refcntCached = this._value.get() - val stateRepr: String - @Suppress("LiftReturnOrAssignment") - when { - refcntCached >= 0 -> stateRepr = refcntCached.toString() - refcntCached == _Misc.CLOSED_STATE_VALUE -> stateRepr = "closed" - refcntCached < _Misc.CLOSED_STATE_VALUE -> stateRepr = "overflow" - refcntCached > _Misc.CLOSED_STATE_VALUE -> stateRepr = "underflow" - else -> throw Error("Unreachable") - } - return "" - } - - public companion object { - private val _nextId = _AtomicLong(0L) + val orig = this._moved.toString() + return orig.substring(0, orig.length - 1) + " !deprecated class used!>" } public object ObserveToStdout : Observer { override fun observeState(instance: CloseableReferenceCounter_Debug, actions: String) { - _Platform.jvm_assertNotNull(instance, "param: instance") - _Platform.jvm_assertNotNull(actions, "param: actions") - print("${instance} ${actions}\n") + moved_CloseableReferenceCounter_Debug.ObserveToStdout.observeState(instance._moved, actions) } } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt index e70faf8..28f6a00 100644 --- a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt +++ b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/MayBeClosedReferenceCounter.kt @@ -5,31 +5,36 @@ package ru.landrafhomyak.utility.reference_counter import kotlin.contracts.ExperimentalContracts import kotlin.contracts.InvocationKind import kotlin.contracts.contract +import kotlin.jvm.JvmField import kotlin.jvm.JvmName -import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 -import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose2 +import ru.landgrafhomyak.utility.reference_counter._Platform +import ru.landgrafhomyak.utility.reference_counter.MayBeClosedReferenceCounter as moved_MayBeClosedReferenceCounter +@Deprecated( + message = "Class moved to another package", + /*replaceWith = ReplaceWith( + imports = ["ru.landgrafhomyak.utility.reference_counter.MayBeClosedReferenceCounter"], + expression = "MayBeClosedReferenceCounter" + ),*/ + level = DeprecationLevel.ERROR +) public sealed class MayBeClosedReferenceCounter { - private val _value: _AtomicLong - private val _errMessageClosed: String + @PublishedApi + @JvmField + internal val _moved: moved_MayBeClosedReferenceCounter - protected constructor(errMessageClosed: String) { - _Platform.jvm_assertNotNull(errMessageClosed, "param: errMessageClosed") - this._errMessageClosed = errMessageClosed - this._value = _AtomicLong(0L) + protected constructor(moved: moved_MayBeClosedReferenceCounter) { + this._moved = moved } @JvmName("throwClosed") public fun throwClosed() { - throw IllegalStateException(this._errMessageClosed) + this._moved.throwClosed() } @JvmName("incref") public fun incref() { - this._value.update { o -> - if (o < 0) this.throwClosed() - return@update o + 1 - } + this._moved.incref() } @JvmName("tryIncref\$kt") @@ -37,18 +42,17 @@ public sealed class MayBeClosedReferenceCounter { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - this.incref() - return safeAutoClose2(onError = this::decref, action = block) + return this._moved.tryIncref(block) } @JvmName("assertNotClosed") public fun assertNotClosed() { - if (this._value.get() < 0) this.throwClosed() + this._moved.assertNotClosed() } @JvmName("decref") public fun decref() { - this._value.update(Long::dec) + this._moved.decref() } @JvmName("tryDecref\$kt") @@ -56,43 +60,27 @@ public sealed class MayBeClosedReferenceCounter { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - this.assertNotClosed() - return safeAutoClose2(onSuccess = this::decref, action = block) + return this._moved.tryDecref(block) } @get:JvmName("isClosed") - public val isClosed: Boolean get() = this._value.get() < 0 - - @Suppress("INAPPLICABLE_JVM_NAME") - @JvmName("close") - protected open fun close(errExistRefs: String) { - _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") - val state = this._value.compareAndExchange(0, _Misc.CLOSED_STATE_VALUE) - when { - state > 0 -> throw IllegalStateException(errExistRefs) - state < 0 -> this.throwClosed() - } - } + public val isClosed: Boolean get() = + this._moved.isClosed @JvmName("withRef\$kt") public inline fun withRef(protected: () -> R): R { contract { callsInPlace(protected, InvocationKind.EXACTLY_ONCE) } - this.incref() - return safeAutoClose1(finally = this::decref, action = protected) + return this._moved.withRef(protected) } override fun toString(): String { - val refcntCached = this._value.get() - @Suppress("LiftReturnOrAssignment") - if (refcntCached < 0) - return "" - else - return "" + val orig = this._moved.toString() + return orig.substring(0, orig.length - 1) + " !deprecated class used!>" } - @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND") + @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND", "DEPRECATION_ERROR") @JvmName("child") public fun child(errMessageClosed: String, errExistRefs: String, scope: (MayBeClosedReferenceCounter) -> R): R { contract { @@ -103,29 +91,18 @@ public sealed class MayBeClosedReferenceCounter { _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") _Platform.jvm_assertNotNull(scope, "param: scope") - this.withRef { - return CloseableReferenceCounter.autoClosed( - errMessageClosed = errMessageClosed, - errExistRefs = errExistRefs, - scope = scope - ) - } + return this._moved.child(errMessageClosed, errExistRefs, WrapperReferenceCounter.wrapperLambda(scope)) } - @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND") + @Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND", "DEPRECATION_ERROR") @JvmName("child_inheritErrMessage") public fun child_inheritErrMessage(errExistRefs: String, scope: (MayBeClosedReferenceCounter) -> R): R { contract { callsInPlace(scope, InvocationKind.EXACTLY_ONCE) } - _Platform.jvm_assertNotNull(errExistRefs, "param: errExistRefs") _Platform.jvm_assertNotNull(scope, "param: scope") - return this.child( - errMessageClosed = this._errMessageClosed, - errExistRefs = errExistRefs, - scope = scope - ) + return this._moved.child_inheritErrMessage(errExistRefs, WrapperReferenceCounter.wrapperLambda(scope)) } } \ No newline at end of file diff --git a/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/WrapperReferenceCounter.kt b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/WrapperReferenceCounter.kt new file mode 100644 index 0000000..0f1d204 --- /dev/null +++ b/src/commonMain/kotlin/ru/landrafhomyak/utility/reference_counter/WrapperReferenceCounter.kt @@ -0,0 +1,20 @@ +package ru.landrafhomyak.utility.reference_counter + +import kotlin.jvm.JvmStatic +import ru.landgrafhomyak.utility.reference_counter.MayBeClosedReferenceCounter as moved_MayBeClosedReferenceCounter + +@Suppress("DEPRECATION_ERROR") +@Deprecated( + message = "Utility class for deprecated refcounters", + level = DeprecationLevel.ERROR +) +internal class WrapperReferenceCounter : MayBeClosedReferenceCounter { + constructor(refcnt: moved_MayBeClosedReferenceCounter) : super(refcnt) + + companion object { + @JvmStatic + fun wrapperLambda(old: (MayBeClosedReferenceCounter) -> R): (moved_MayBeClosedReferenceCounter) -> R { + return w@{ moved -> return@w old(WrapperReferenceCounter(moved)) } + } + } +} \ No newline at end of file diff --git a/src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt b/src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt similarity index 96% rename from src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt rename to src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt index 5771462..38765c6 100644 --- a/src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt +++ b/src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter import java.util.concurrent.atomic.AtomicLong diff --git a/src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt b/src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt similarity index 86% rename from src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt rename to src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt index ef773ca..e8e15bd 100644 --- a/src/jvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt +++ b/src/jvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter import java.util.Objects import kotlin.contracts.contract diff --git a/src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt b/src/jvmTest/kotlin/ru/landgrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt similarity index 94% rename from src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt rename to src/jvmTest/kotlin/ru/landgrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt index be9cb9c..02cc221 100644 --- a/src/jvmTest/kotlin/ru/landrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt +++ b/src/jvmTest/kotlin/ru/landgrafhomyak/utility/reference_counter/tests/KotlinStdlibDependencyTest.kt @@ -1,11 +1,11 @@ -package ru.landrafhomyak.utility.reference_counter.tests +package ru.landgrafhomyak.utility.reference_counter.tests import java.lang.AssertionError import org.testng.annotations.Test import org.testng.asserts.Assertion import ru.landgrafhomyak.utility.highlevel_try_finally.safeAutoClose1 -import ru.landrafhomyak.utility.reference_counter.CloseableReferenceCounter -import ru.landrafhomyak.utility.reference_counter.CloseableReferenceCounter_Debug +import ru.landgrafhomyak.utility.reference_counter.CloseableReferenceCounter +import ru.landgrafhomyak.utility.reference_counter.CloseableReferenceCounter_Debug @Test class KotlinStdlibDependencyTest { diff --git a/src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt b/src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt similarity index 94% rename from src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt rename to src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt index 9825bca..861ec30 100644 --- a/src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_AtomicLong.kt +++ b/src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_AtomicLong.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter import kotlinx.atomicfu.AtomicLong import kotlinx.atomicfu.atomic diff --git a/src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt b/src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt similarity index 76% rename from src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt rename to src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt index 9dddbe0..308bc97 100644 --- a/src/nonJvmMain/kotlin/ru/landrafhomyak/utility/reference_counter/_Platform.kt +++ b/src/nonJvmMain/kotlin/ru/landgrafhomyak/utility/reference_counter/_Platform.kt @@ -1,4 +1,4 @@ -package ru.landrafhomyak.utility.reference_counter +package ru.landgrafhomyak.utility.reference_counter @PublishedApi