Index: webrtc/api/java/jni/jni_helpers.cc |
diff --git a/webrtc/api/java/jni/jni_helpers.cc b/webrtc/api/java/jni/jni_helpers.cc |
index 32092e65f8b7e8a2c94cb69b393ccc5f4f2ebb2e..41d34247f2baae1c96a7f5c4022bd90e6eacab07 100644 |
--- a/webrtc/api/java/jni/jni_helpers.cc |
+++ b/webrtc/api/java/jni/jni_helpers.cc |
@@ -272,4 +272,55 @@ ScopedLocalRefFrame::~ScopedLocalRefFrame() { |
jni_->PopLocalFrame(NULL); |
} |
+// Creates an iterator representing the end of any collection. |
+Iterator::Iterator() : iterator_(NULL) {} |
+ |
+// Creates an iterator pointing to the beginning of the specified collection. |
+Iterator::Iterator(JNIEnv* jni, jobject iterable) : jni_(jni) { |
+ jclass j_class = GetObjectClass(jni, iterable); |
+ jmethodID iterator_id = |
+ GetMethodID(jni, j_class, "iterator", "()Ljava/util/Iterator;"); |
+ iterator_ = jni->CallObjectMethod(iterable, iterator_id); |
+ CHECK_EXCEPTION(jni) << "error during CallObjectMethod"; |
+ |
+ if (iterator_ == NULL) { |
+ // TODO(skvlad): Should this be an exception instead? |
Taylor Brandstetter
2016/03/22 01:16:36
That seems reasonable. Just use "RTC_CHECK(iterato
skvlad
2016/03/22 02:19:59
Done.
|
+ return; |
+ } |
+ |
+ jclass iterator_class = GetObjectClass(jni, iterator_); |
+ has_next_id_ = GetMethodID(jni, iterator_class, "hasNext", "()Z"); |
+ next_id_ = GetMethodID(jni, iterator_class, "next", "()Ljava/lang/Object;"); |
+ |
+ // Start at the first element in the collection |
+ ++(*this); |
+} |
+ |
+// Move constructor - necessary to be able to return iterator types from |
+// functions |
+Iterator::Iterator(Iterator&& other) |
+ : jni_(std::move(other.jni_)), |
+ iterator_(std::move(other.iterator_)), |
+ value_(std::move(other.value_)), |
+ has_next_id_(std::move(other.has_next_id_)), |
+ next_id_(std::move(other.next_id_)){}; |
+ |
+// Advances the iterator one step |
+Iterator& Iterator::operator++() { |
+ if (iterator_ == NULL) { |
+ // Can't iterate past the end |
+ return *this; |
+ } |
+ bool has_next = jni_->CallBooleanMethod(iterator_, has_next_id_); |
+ CHECK_EXCEPTION(jni_) << "error during CallBooleanMethod"; |
+ if (!has_next) { |
+ iterator_ = NULL; |
+ return *this; |
+ } |
+ |
+ value_ = jni_->CallObjectMethod(iterator_, next_id_); |
+ CHECK_EXCEPTION(jni_) << "error during CallObjectMethod"; |
+ return *this; |
+} |
+ |
} // namespace webrtc_jni |