Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 // This file contain convenience functions and classes for JNI. | 11 // This file contain convenience functions and classes for JNI. |
| 12 // Before using any of the methods, InitGlobalJniVariables must be called. | 12 // Before using any of the methods, InitGlobalJniVariables must be called. |
| 13 | 13 |
| 14 #ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 14 #ifndef WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| 15 #define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 15 #define WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| 16 | 16 |
| 17 #include <jni.h> | 17 #include <jni.h> |
| 18 #include <string> | 18 #include <string> |
| 19 | 19 |
| 20 #include "webrtc/base/constructormagic.h" | |
| 20 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
| 21 | 22 |
| 22 // Abort the process if |jni| has a Java exception pending. | 23 // Abort the process if |jni| has a Java exception pending. |
| 23 // This macros uses the comma operator to execute ExceptionDescribe | 24 // This macros uses the comma operator to execute ExceptionDescribe |
| 24 // and ExceptionClear ignoring their return values and sending "" | 25 // and ExceptionClear ignoring their return values and sending "" |
| 25 // to the error stream. | 26 // to the error stream. |
| 26 #define CHECK_EXCEPTION(jni) \ | 27 #define CHECK_EXCEPTION(jni) \ |
| 27 RTC_CHECK(!jni->ExceptionCheck()) \ | 28 RTC_CHECK(!jni->ExceptionCheck()) \ |
| 28 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") | 29 << (jni->ExceptionDescribe(), jni->ExceptionClear(), "") |
| 29 | 30 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 ~ScopedGlobalRef() { | 116 ~ScopedGlobalRef() { |
| 116 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_); | 117 DeleteGlobalRef(AttachCurrentThreadIfNeeded(), obj_); |
| 117 } | 118 } |
| 118 T operator*() const { | 119 T operator*() const { |
| 119 return obj_; | 120 return obj_; |
| 120 } | 121 } |
| 121 private: | 122 private: |
| 122 T obj_; | 123 T obj_; |
| 123 }; | 124 }; |
| 124 | 125 |
| 126 // Provides a convenient way to iterate over a Java Iterable using the | |
| 127 // C++ range-for loop. | |
| 128 // E.g. for (jobject value : Iterable(jni, j_iterable)) { ... } | |
| 129 // Note: Since Java iterators cannot be duplicated, this class is not copyable | |
| 130 // to prevent creating multiple C++ iterators that refer to the same Java | |
| 131 // iterator. | |
| 132 class Iterator { | |
| 133 public: | |
| 134 // Creates an iterator representing the end of any collection. | |
| 135 Iterator(); | |
| 136 // Creates an iterator pointing to the beginning of the specified collection. | |
| 137 Iterator(JNIEnv* jni, jobject iterable); | |
| 138 | |
| 139 // Move constructor - necessary to be able to return iterator types from | |
| 140 // functions | |
|
Taylor Brandstetter
2016/03/22 01:16:36
nit: period at end of comments (in cc file too)
skvlad
2016/03/22 02:20:00
Done.
| |
| 141 Iterator(Iterator&& other); | |
| 142 | |
| 143 // Move assignment should not be used. | |
| 144 Iterator& operator=(Iterator&&) = delete; | |
| 145 | |
| 146 // Advances the iterator one step | |
| 147 Iterator& operator++(); | |
| 148 | |
| 149 bool operator==(const Iterator& other) { | |
| 150 return (iterator_ == other.iterator_); | |
| 151 } | |
| 152 bool operator!=(const Iterator& other) { return !(*this == other); } | |
| 153 | |
| 154 jobject operator*() { return value_; } | |
| 155 | |
| 156 private: | |
| 157 JNIEnv* jni_ = NULL; | |
| 158 jobject iterator_ = NULL; | |
| 159 jobject value_ = NULL; | |
| 160 jmethodID has_next_id_ = NULL; | |
| 161 jmethodID next_id_ = NULL; | |
| 162 | |
| 163 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator); | |
| 164 }; | |
| 165 | |
| 166 class Iterable { | |
| 167 public: | |
| 168 explicit Iterable(JNIEnv* jni, jobject iterable) | |
| 169 : jni_(jni), iterable_(iterable) {} | |
| 170 | |
| 171 Iterator begin() { return Iterator(jni_, iterable_); } | |
| 172 | |
| 173 Iterator end() { return Iterator(); } | |
| 174 | |
| 175 private: | |
| 176 JNIEnv* jni_; | |
| 177 jobject iterable_; | |
| 178 | |
| 179 RTC_DISALLOW_COPY_AND_ASSIGN(Iterable); | |
| 180 }; | |
| 181 | |
| 125 } // namespace webrtc_jni | 182 } // namespace webrtc_jni |
| 126 | 183 |
| 127 #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ | 184 #endif // WEBRTC_API_JAVA_JNI_JNI_HELPERS_H_ |
| OLD | NEW |