blob: 80ea7978f5898a9a063f36757da0da59fe3792a5 [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 Hodson44dbb402019-10-25 10:59:51 +010017#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_
18#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_
Brian Carlstromdd8af232012-05-13 23:56:07 -070019
Orion Hodson85cdde82019-10-24 15:48:11 +010020#include "libnativehelper_api.h"
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000021
22#ifdef __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070023
24#include <string>
25#include <vector>
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000026#include "ScopedLocalRef.h"
Brian Carlstromdd8af232012-05-13 23:56:07 -070027
Orion Hodson17367dc2020-03-19 09:15:51 +000028template <typename StringVisitor>
29jobjectArray toStringArray(JNIEnv* env, size_t count, StringVisitor&& visitor) {
30 jobjectArray result = jniCreateStringArray(env, count);
31 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 }
39 env->SetObjectArrayElement(result, i, s.get());
40 if (env->ExceptionCheck()) {
Orion Hodson17367dc2020-03-19 09:15:51 +000041 return nullptr;
Brian Carlstromdd8af232012-05-13 23:56:07 -070042 }
43 }
44 return result;
45}
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
Orion Hodson44dbb402019-10-25 10:59:51 +010064#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_