Package com.couchbase.lite.internal.core
Class NativeContext<T>
- java.lang.Object
-
- com.couchbase.lite.internal.core.NativeContext<T>
-
- Type Parameters:
T
- The type of the Java peer.
public class NativeContext<T> extends Object
This class provides a way for native objects to reference their Java peers. ThereserveKey()
method creates a unique token that the native code can redeem, usinggetObjFromContext
for the Java object that is its peer. Note that the token is a 31 bit integer (a positive int) so that it is relatively immune to sign extension. The internal map holds only a weak reference to the Java object. If nobody in java-land cares about the peer-pair anymore, calls togetObjFromContext
will return null. While it would be possible to accomplish something similar, perhaps by passing the actual java reference to the native object, such an implementation would require the native code to manage LocalRefs.... with the distinct possibility of running out.!!! There should be a nanny thread cleaning out all the ref -> null
-
-
Constructor Summary
Constructors Constructor Description NativeContext()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bind(int key, T obj)
Bind an object to a token.T
getObjFromContext(long context)
Get the object bound to the passed token.int
reserveKey()
Reserve a token.void
unbind(int key)
Remove the binding for a key Re-entrant.
-
-
-
Method Detail
-
reserveKey
public int reserveKey()
Reserve a token. Sometimes the object to be put into the map needs to know its own token. Pre-reserving it makes it possible to make it final.- Returns:
- a unique positive int key.
-
bind
public void bind(int key, @NonNull T obj)
Bind an object to a token.- Parameters:
key
- a previously reserved tokenobj
- the object to be bound to the token.
-
unbind
public void unbind(int key)
Remove the binding for a key Re-entrant.- Parameters:
key
- the key to be unbound.
-
getObjFromContext
@Nullable public T getObjFromContext(long context)
Get the object bound to the passed token. Returns null if no object is bound to the key. For legacy reasons, core holds these "contexts" as (void *), so they are longs.- Parameters:
context
- a token created byreserveKey()
- Returns:
- the bound object, or null if none exists.
-
-