blob: e4eeaca6f5c587ffc404a877fbae43b8164d6962 [file] [log] [blame]
Brian Carlstromdd8af232012-05-13 23:56:07 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Orion Hodsonce487362020-04-22 19:41:46 +010017#pragma once
Brian Carlstromdd8af232012-05-13 23:56:07 -070018
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000019#ifdef __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070020
21#include <string>
22#include <vector>
Orion Hodson511dfd52020-06-02 12:18:23 +010023
24#include "JNIHelp.h"
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000025#include "ScopedLocalRef.h"
Brian Carlstromdd8af232012-05-13 23:56:07 -070026
Orion Hodson17367dc2020-03-19 09:15:51 +000027template <typename StringVisitor>
28jobjectArray toStringArray(JNIEnv* env, size_t count, StringVisitor&& visitor) {
Nicolas Geoffraye3d4d6d2020-09-23 16:14:17 +010029 jclass stringClass = env->FindClass("java/lang/String");
30 ScopedLocalRef<jobjectArray> result(env, env->NewObjectArray(count, stringClass, NULL));
31 env->DeleteLocalRef(stringClass);
Orion Hodson17367dc2020-03-19 09:15:51 +000032 if (result == nullptr) {
33 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070034 }
35 for (size_t i = 0; i < count; ++i) {
Orion Hodson17367dc2020-03-19 09:15:51 +000036 ScopedLocalRef<jstring> s(env, env->NewStringUTF(visitor(i)));
Brian Carlstromdd8af232012-05-13 23:56:07 -070037 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000038 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070039 }
Orion Hodson15b4aab2020-06-07 19:09:37 +010040 env->SetObjectArrayElement(result.get(), i, s.get());
Brian Carlstromdd8af232012-05-13 23:56:07 -070041 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000042 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070043 }
44 }
Orion Hodson15b4aab2020-06-07 19:09:37 +010045 return result.release();
Brian Carlstromdd8af232012-05-13 23:56:07 -070046}
47
Ian Rogers5d6c98a2014-05-16 17:58:48 -070048inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
Orion Hodson17367dc2020-03-19 09:15:51 +000049 return toStringArray(env, strings.size(), [&strings](size_t i) { return strings[i].c_str(); });
50}
51
52inline jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
53 size_t count = 0;
54 for (; strings[count] != nullptr; ++count) {}
55 return toStringArray(env, count, [&strings](size_t i) { return strings[i]; });
56}
57
58template <typename Counter, typename Getter>
59jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
60 return toStringArray(env, counter(), [getter](size_t i) { return getter(i); });
Ian Rogers5d6c98a2014-05-16 17:58:48 -070061}
62
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000063#endif // __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070064