google / google/jni-bind

Suggestion/idea: Helper classes for java.util.Map and java.util.List

Open
#471 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
333
Forks
40
PR merge metrics
No merged PRs in 30d

Description

java.util.{List,Map} are very common interfaces that get passed around in Java codebases.

I'm asking for helper classes for these objects, so that it would be comfortable to just accept them in binding code.

For instance: I want to be able to iterate over the elements of a java.util.List:

```cpp

Java_com_foo_bar(JNIEnv* env, jobject self, jobject someList, jobject someMap) {
jni::List wrappedList(someList);
for (const jni::LocalObject& element : wrappedList) {
// do something with element
}

wrappedList.push_back(...); // appends to the list
wrappedList.size(); // gets the size
wrappedList.front(); // get the first element
wrappedList.back(); // get the last element

jni::Map wrappedMap(someMap);
for (const auto& [k, v] : wrappedMap) {
// k has type jni::LocalObject
// v has type jni::LocalObject
}
wrappedMap[...] = ... // operator[](jobject) returns some proxy that is able to call the java.util.Map#put method)
}

```

Compare this with [pybind11](https://pybind11.readthedocs.io/en/stable/index.html): we can take as input, return, and manipulate `pybind11::object` / `pybind11::dict` (corresponding to java.util.Map, sort of) and `pybind11::list` (corresponding to java.util.List) instances.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.