Method to create child refcounters

This commit is contained in:
Andrew Golovashevich 2025-08-14 03:09:40 +03:00
parent 8bf13d0f69
commit 5ac9133813
2 changed files with 38 additions and 1 deletions

View File

@ -15,7 +15,7 @@ buildscript {
}
group = "ru.landgrafhomyak.utility"
version = "0.3"
version = "0.4"
repositories {
mavenCentral()

View File

@ -91,4 +91,41 @@ public sealed class MayBeClosedReferenceCounter {
else
return "<ref counter [${refcntCached}]>"
}
@Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND")
@JvmName("child")
public fun <R> 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 <R> 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
)
}
}