1 // Map handles to objects
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)
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)
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")
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")