• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "JniConstants.h"
18 #include "toStringArray.h"
19 
20 #include <string>
21 #include <vector>
22 
23 struct VectorCounter {
24     const std::vector<std::string>& strings;
VectorCounterVectorCounter25     VectorCounter(const std::vector<std::string>& strings) : strings(strings) {}
operator ()VectorCounter26     size_t operator()() {
27         return strings.size();
28     }
29 };
30 struct VectorGetter {
31     const std::vector<std::string>& strings;
VectorGetterVectorGetter32     VectorGetter(const std::vector<std::string>& strings) : strings(strings) {}
operator ()VectorGetter33     const char* operator()(size_t i) {
34         return strings[i].c_str();
35     }
36 };
37 
toStringArray(JNIEnv * env,const std::vector<std::string> & strings)38 jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
39     VectorCounter counter(strings);
40     VectorGetter getter(strings);
41     return toStringArray<VectorCounter, VectorGetter>(env, &counter, &getter);
42 }
43 
44 struct ArrayCounter {
45     const char* const* strings;
ArrayCounterArrayCounter46     ArrayCounter(const char* const* strings) : strings(strings) {}
operator ()ArrayCounter47     size_t operator()() {
48         size_t count = 0;
49         while (strings[count] != NULL) {
50             ++count;
51         }
52         return count;
53     }
54 };
55 
56 struct ArrayGetter {
57     const char* const* strings;
ArrayGetterArrayGetter58     ArrayGetter(const char* const* strings) : strings(strings) {}
operator ()ArrayGetter59     const char* operator()(size_t i) {
60         return strings[i];
61     }
62 };
63 
toStringArray(JNIEnv * env,const char * const * strings)64 jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
65     ArrayCounter counter(strings);
66     ArrayGetter getter(strings);
67     return toStringArray(env, &counter, &getter);
68 }
69