T
- The type of the Java peer.public class NativeContext<T> extends Object
reserveKey()
method creates a unique token
that the native code can redeem, using getObjFromContext
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 to
getObjFromContext
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 and Description |
---|
NativeContext() |
Modifier and Type | Method and 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.
|
public int reserveKey()
public void bind(int key, @NonNull T obj)
key
- a previously reserved tokenobj
- the object to be bound to the token.public void unbind(int key)
key
- the key to be unbound.@Nullable public T getObjFromContext(long context)
context
- a token created by reserveKey()