Add support for fetching vector<bytes> in JNI interface.

GitOrigin-RevId: a7e4a44cb20262b9268a981ebed139e21794d525
This commit is contained in:
levlam 2020-03-25 02:14:08 +03:00
parent 5f5d3355e3
commit bc49ef8330
2 changed files with 26 additions and 7 deletions

View File

@ -94,12 +94,12 @@ std::string TD_TL_writer_jni_cpp::gen_vector_fetch(std::string field_name, const
} }
if (!array_type.empty()) { if (!array_type.empty()) {
return "jni::fetch_vector(env, (" + array_type + ")" + fetch_object + ");"; return "jni::fetch_vector(env, (" + array_type + ")" + fetch_object + ")";
} }
std::string template_type; std::string template_type;
if (vector_type == "string") { if (vector_type == "string") {
template_type = "std::string"; template_type = "string";
} else if (vector_type.compare(0, 11, "std::vector") == 0) { } else if (vector_type.compare(0, 11, "std::vector") == 0) {
const tl::tl_tree_type *child = static_cast<const tl::tl_tree_type *>(t->children[0]); const tl::tl_tree_type *child = static_cast<const tl::tl_tree_type *>(t->children[0]);
template_type = gen_type_name(child); template_type = gen_type_name(child);
@ -108,13 +108,12 @@ std::string TD_TL_writer_jni_cpp::gen_vector_fetch(std::string field_name, const
} }
template_type = "std::vector<" + template_type + ">"; template_type = "std::vector<" + template_type + ">";
} else if (vector_type == "bytes") { } else if (vector_type == "bytes") {
std::fprintf(stderr, "Vector of Bytes is not supported\n"); template_type = "jbyteArray";
assert(false);
} else { } else {
assert(vector_type.compare(0, 10, "object_ptr") == 0); assert(vector_type.compare(0, 10, "object_ptr") == 0);
template_type = gen_main_class_name(t->type); template_type = gen_main_class_name(t->type);
} }
return "jni::FetchVector<" + template_type + ">::fetch(env, (jobjectArray)" + fetch_object + ");"; return "jni::FetchVector<" + template_type + ">::fetch(env, (jobjectArray)" + fetch_object + ")";
} }
std::string TD_TL_writer_jni_cpp::gen_type_fetch(const std::string &field_name, const tl::tl_tree_type *tree_type, std::string TD_TL_writer_jni_cpp::gen_type_fetch(const std::string &field_name, const tl::tl_tree_type *tree_type,
@ -179,7 +178,7 @@ std::string TD_TL_writer_jni_cpp::gen_type_fetch(const std::string &field_name,
return gen_main_class_name(tree_type->type) + "::fetch(env, p)"; return gen_main_class_name(tree_type->type) + "::fetch(env, p)";
} }
res = "jni::fetch_tl_object<" + gen_main_class_name(tree_type->type) + ">(env, jni::fetch_object(env, p, " + res = "jni::fetch_tl_object<" + gen_main_class_name(tree_type->type) + ">(env, jni::fetch_object(env, p, " +
field_name + "fieldID));"; field_name + "fieldID))";
} }
return res_begin + res; return res_begin + res;
} }

View File

@ -210,7 +210,7 @@ struct FetchVector<std::string> {
result.reserve(length); result.reserve(length);
for (jsize i = 0; i < length; i++) { for (jsize i = 0; i < length; i++) {
jstring str = (jstring)env->GetObjectArrayElement(arr, i); jstring str = (jstring)env->GetObjectArrayElement(arr, i);
result.push_back(jni::from_jstring(env, str)); result.push_back(from_jstring(env, str));
if (str) { if (str) {
env->DeleteLocalRef(str); env->DeleteLocalRef(str);
} }
@ -221,6 +221,26 @@ struct FetchVector<std::string> {
} }
}; };
template <>
struct FetchVector<jbyteArray> {
static std::vector<std::string> fetch(JNIEnv *env, jobjectArray arr) {
std::vector<std::string> result;
if (arr != nullptr) {
jsize length = env->GetArrayLength(arr);
result.reserve(length);
for (jsize i = 0; i < length; i++) {
jbyteArray bytes = (jbyteArray)env->GetObjectArrayElement(arr, i);
result.push_back(from_bytes(env, bytes));
if (bytes) {
env->DeleteLocalRef(bytes);
}
}
env->DeleteLocalRef(arr);
}
return result;
}
};
template <class T> template <class T>
struct FetchVector<std::vector<T>> { struct FetchVector<std::vector<T>> {
static auto fetch(JNIEnv *env, jobjectArray arr) { static auto fetch(JNIEnv *env, jobjectArray arr) {