blob: 8d259746ef0f66001f0ce3603c9b5facb7b4cf39 [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 Hodsonc2d541b2020-03-24 11:26:36 +000029 jobjectArray result = jniCreateStringArray(static_cast<C_JNIEnv*>(&env->functions), count);
Orion Hodson17367dc2020-03-19 09:15:51 +000030 if (result == nullptr) {
31 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070032 }
33 for (size_t i = 0; i < count; ++i) {
Orion Hodson17367dc2020-03-19 09:15:51 +000034 ScopedLocalRef<jstring> s(env, env->NewStringUTF(visitor(i)));
Brian Carlstromdd8af232012-05-13 23:56:07 -070035 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000036 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070037 }
38 env->SetObjectArrayElement(result, i, s.get());
39 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000040 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070041 }
42 }
43 return result;
44}
45
Ian Rogers5d6c98a2014-05-16 17:58:48 -070046inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
Orion Hodson17367dc2020-03-19 09:15:51 +000047 return toStringArray(env, strings.size(), [&strings](size_t i) { return strings[i].c_str(); });
48}
49
50inline jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
51 size_t count = 0;
52 for (; strings[count] != nullptr; ++count) {}
53 return toStringArray(env, count, [&strings](size_t i) { return strings[i]; });
54}
55
56template <typename Counter, typename Getter>
57jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
58 return toStringArray(env, counter(), [getter](size_t i) { return getter(i); });
Ian Rogers5d6c98a2014-05-16 17:58:48 -070059}
60
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000061#endif // __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070062