blob: 0ff45ec463f8e84752759dc3e769f39c320bfc08 [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 Hodson85cdde82019-10-24 15:48:11 +010019#include "libnativehelper_api.h"
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000020
21#ifdef __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070022
23#include <string>
24#include <vector>
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) {
Orion Hodson15b4aab2020-06-07 19:09:37 +010029 C_JNIEnv* c_env = static_cast<C_JNIEnv*>(&env->functions);
30 ScopedLocalRef<jobjectArray> result(env, jniCreateStringArray(c_env, count));
Orion Hodson17367dc2020-03-19 09:15:51 +000031 if (result == nullptr) {
32 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070033 }
34 for (size_t i = 0; i < count; ++i) {
Orion Hodson17367dc2020-03-19 09:15:51 +000035 ScopedLocalRef<jstring> s(env, env->NewStringUTF(visitor(i)));
Brian Carlstromdd8af232012-05-13 23:56:07 -070036 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000037 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070038 }
Orion Hodson15b4aab2020-06-07 19:09:37 +010039 env->SetObjectArrayElement(result.get(), i, s.get());
Brian Carlstromdd8af232012-05-13 23:56:07 -070040 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000041 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070042 }
43 }
Orion Hodson15b4aab2020-06-07 19:09:37 +010044 return result.release();
Brian Carlstromdd8af232012-05-13 23:56:07 -070045}
46
Ian Rogers5d6c98a2014-05-16 17:58:48 -070047inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
Orion Hodson17367dc2020-03-19 09:15:51 +000048 return toStringArray(env, strings.size(), [&strings](size_t i) { return strings[i].c_str(); });
49}
50
51inline jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
52 size_t count = 0;
53 for (; strings[count] != nullptr; ++count) {}
54 return toStringArray(env, count, [&strings](size_t i) { return strings[i]; });
55}
56
57template <typename Counter, typename Getter>
58jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
59 return toStringArray(env, counter(), [getter](size_t i) { return getter(i); });
Ian Rogers5d6c98a2014-05-16 17:58:48 -070060}
61
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000062#endif // __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070063