Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/HandleMap.kt

raw
1 // Map handles to objects
2 //
3 // This is used pass an opaque 64-bit handle representing a foreign object to the Rust code.
4 internal class UniffiHandleMap<T: Any> {
5 private val map = ConcurrentHashMap<Long, T>()
6 private val counter = java.util.concurrent.atomic.AtomicLong(0)
7
8 val size: Int
9 get() = map.size
10
11 // Insert a new object into the handle map and get a handle for it
12 fun insert(obj: T): Long {
13 val handle = counter.getAndAdd(1)
14 map.put(handle, obj)
15 return handle
16 }
17
18 // Get an object from the handle map
19 fun get(handle: Long): T {
20 return map.get(handle) ?: throw InternalException("UniffiHandleMap.get: Invalid handle")
21 }
22
23 // Remove an entry from the handlemap and get the Kotlin object back
24 fun remove(handle: Long): T {
25 return map.remove(handle) ?: throw InternalException("UniffiHandleMap: Invalid handle")
26 }
27 }