Apache Mesos
construct.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __CONSTRUCT_HPP__
18 #define __CONSTRUCT_HPP__
19 
20 #include <vector>
21 
22 #include <jni.h>
23 
24 bool construct(JNIEnv* env, jboolean jbool);
25 
26 template <typename T>
27 T construct(JNIEnv* env, jobject jobj);
28 
29 template <typename T>
30 std::vector<T> constructFromIterable(JNIEnv* env, jobject jiterable)
31 {
32  std::vector<T> result;
33 
34  jclass clazz = env->GetObjectClass(jiterable);
35 
36  // Iterator iterator = iterable.iterator();
37  jmethodID iterator =
38  env->GetMethodID(clazz, "iterator", "()Ljava/util/Iterator;");
39  jobject jiterator = env->CallObjectMethod(jiterable, iterator);
40 
41  jclass iteratorClazz = env->GetObjectClass(jiterator);
42 
43  // while (iterator.hasNext()) {
44  jmethodID hasNext = env->GetMethodID(iteratorClazz, "hasNext", "()Z");
45 
46  jmethodID next =
47  env->GetMethodID(iteratorClazz, "next", "()Ljava/lang/Object;");
48 
49  while (env->CallBooleanMethod(jiterator, hasNext)) {
50  // Object item = iterator.next();
51  jobject jitem = env->CallObjectMethod(jiterator, next);
52  result.emplace_back(construct<T>(env, jitem));
53  }
54 
55  return result;
56 }
57 
58 #endif // __CONSTRUCT_HPP__
bool construct(JNIEnv *env, jboolean jbool)
std::vector< T > constructFromIterable(JNIEnv *env, jobject jiterable)
Definition: construct.hpp:30