HandleWrapper

This commit is contained in:
Andrew Golovashevich 2025-09-04 21:19:18 +03:00
parent 6bbade401f
commit bdb1fe56fa

View File

@ -0,0 +1,27 @@
package ru.landgrafhomyak.utility.closeable_state_1
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
public class HandleWrapper<H>(
handle: H,
state: CloseableState,
) {
private val _unprotected = handle
private val _state: CloseableState = state
public fun <R> useHandle(method: (H) -> R): R {
contract {
callsInPlace(method, InvocationKind.EXACTLY_ONCE)
}
return this._state.withUse { method(this._unprotected) }
}
@ManualStateManipulation
private fun close() {
this._state.close()
}
override fun toString(): String = "<wrapper of handle=${this._unprotected} protected with state=${this._state}>"
}