HandleWrapper
This commit is contained in:
parent
6bbade401f
commit
bdb1fe56fa
@ -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}>"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user