blob: 1965d6a7fe7a454c37be85e5538bc0359745e595 [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
17#ifndef TO_STRING_ARRAY_H_included
18#define TO_STRING_ARRAY_H_included
19
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000020#include <stddef.h>
21
22#include <jni.h>
23#include "module_api.h"
24
25// Public API for libnativehelper library.
26MODULE_API jobjectArray newStringArray(JNIEnv* env, size_t count);
27MODULE_API jobjectArray toStringArray(JNIEnv* env, const char* const* strings);
28
29#ifdef __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070030
31#include <string>
32#include <vector>
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000033#include "ScopedLocalRef.h"
Brian Carlstromdd8af232012-05-13 23:56:07 -070034
35template <typename Counter, typename Getter>
36jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
37 size_t count = (*counter)();
38 jobjectArray result = newStringArray(env, count);
39 if (result == NULL) {
40 return NULL;
41 }
42 for (size_t i = 0; i < count; ++i) {
43 ScopedLocalRef<jstring> s(env, env->NewStringUTF((*getter)(i)));
44 if (env->ExceptionCheck()) {
45 return NULL;
46 }
47 env->SetObjectArrayElement(result, i, s.get());
48 if (env->ExceptionCheck()) {
49 return NULL;
50 }
51 }
52 return result;
53}
54
Ian Rogers5d6c98a2014-05-16 17:58:48 -070055struct VectorCounter {
56 const std::vector<std::string>& strings;
Chih-Hung Hsiehb493dac2016-06-30 14:50:40 -070057 explicit VectorCounter(const std::vector<std::string>& strings) : strings(strings) {}
Ian Rogers5d6c98a2014-05-16 17:58:48 -070058 size_t operator()() {
59 return strings.size();
60 }
61};
62struct VectorGetter {
63 const std::vector<std::string>& strings;
Chih-Hung Hsiehb493dac2016-06-30 14:50:40 -070064 explicit VectorGetter(const std::vector<std::string>& strings) : strings(strings) {}
Ian Rogers5d6c98a2014-05-16 17:58:48 -070065 const char* operator()(size_t i) {
66 return strings[i].c_str();
67 }
68};
69
70inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
71 VectorCounter counter(strings);
72 VectorGetter getter(strings);
73 return toStringArray<VectorCounter, VectorGetter>(env, &counter, &getter);
74}
75
Orion Hodsonb01e7fe2018-11-07 06:07:50 +000076#endif // __cplusplus
Brian Carlstromdd8af232012-05-13 23:56:07 -070077
78#endif // TO_STRING_ARRAY_H_included