| 6 | #define BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 7 |
| 8 | #include <jni.h> |
| 9 | #include <stddef.h> |
| 10 |
| 11 | #include <type_traits> |
| 12 | #include <utility> |
| 13 |
| 14 | #include "base/base_export.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/macros.h" |
| 17 |
| 18 | namespace base { |
| 19 | namespace android { |
| 20 |
| 21 | // Creates a new local reference frame, in which at least a given number of |
| 22 | // local references can be created. Note that local references already created |
| 23 | // in previous local frames are still valid in the current local frame. |
| 24 | class BASE_EXPORT ScopedJavaLocalFrame { |
| 25 | public: |
| 26 | explicit ScopedJavaLocalFrame(JNIEnv* env); |
| 27 | ScopedJavaLocalFrame(JNIEnv* env, int capacity); |
| 28 | ~ScopedJavaLocalFrame(); |
| 29 |
| 30 | private: |
| 31 | // This class is only good for use on the thread it was created on so |
| 32 | // it's safe to cache the non-threadsafe JNIEnv* inside this object. |
| 33 | JNIEnv* env_; |
| 34 |
| 35 | DISALLOW_COPY_AND_ASSIGN(ScopedJavaLocalFrame); |
| 36 | }; |
| 37 |
| 38 | // Forward declare the generic java reference template class. |
| 39 | template <typename T> |
| 40 | class JavaRef; |
| 41 |
| 42 | // Template specialization of JavaRef, which acts as the base class for all |
| 43 | // other JavaRef<> template types. This allows you to e.g. pass |
| 44 | // ScopedJavaLocalRef<jstring> into a function taking const JavaRef<jobject>& |
| 45 | template <> |
| 46 | class BASE_EXPORT JavaRef<jobject> { |
| 47 | public: |
| 48 | // Initializes a null reference. |
| 49 | constexpr JavaRef() {} |
| 50 |
| 51 | // Allow nullptr to be converted to JavaRef. This avoids having to declare an |
| 52 | // empty JavaRef just to pass null to a function, and makes C++ "nullptr" and |
| 53 | // Java "null" equivalent. |
| 54 | constexpr JavaRef(std::nullptr_t) {} |
| 55 |
| 56 | // Public to allow destruction of null JavaRef objects. |
| 57 | ~JavaRef() {} |
| 58 |
| 59 | // TODO(torne): maybe rename this to get() for consistency with unique_ptr |
| 60 | // once there's fewer unnecessary uses of it in the codebase. |
| 61 | jobject obj() const { return obj_; } |
| 62 |
| 63 | explicit operator bool() const { return obj_ != nullptr; } |
| 64 |
| 65 | // Deprecated. Just use bool conversion. |
| 66 | // TODO(torne): replace usage and remove this. |
| 67 | bool is_null() const { return obj_ == nullptr; } |
| 68 |
| 69 | protected: |
| 70 | // Takes ownership of the |obj| reference passed; requires it to be a local |
| 71 | // reference type. |
| 72 | #if DCHECK_IS_ON() |
| 73 | // Implementation contains a DCHECK; implement out-of-line when DCHECK_IS_ON. |
| 74 | JavaRef(JNIEnv* env, jobject obj); |
| 75 | #else |
| 76 | JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {} |
| 77 | #endif |
| 78 |
| 79 | // Used for move semantics. obj_ must have been released first if non-null. |
| 80 | void steal(JavaRef&& other) { |
| 81 | obj_ = other.obj_; |
| 82 | other.obj_ = nullptr; |
| 83 | } |
| 84 |
| 85 | // The following are implementation detail convenience methods, for |
| 86 | // use by the sub-classes. |
| 87 | JNIEnv* SetNewLocalRef(JNIEnv* env, jobject obj); |
| 88 | void SetNewGlobalRef(JNIEnv* env, jobject obj); |
| 89 | void ResetLocalRef(JNIEnv* env); |
| 90 | void ResetGlobalRef(); |
| 91 | jobject ReleaseInternal(); |
| 92 |
| 93 | private: |
| 94 | jobject obj_ = nullptr; |
| 95 |
| 96 | DISALLOW_COPY_AND_ASSIGN(JavaRef); |
| 97 | }; |
| 98 |
| 99 | // Forward declare the object array reader for the convenience function. |
| 100 | template <typename T> |
| 101 | class JavaObjectArrayReader; |
| 102 |
| 103 | // Generic base class for ScopedJavaLocalRef and ScopedJavaGlobalRef. Useful |
| 104 | // for allowing functions to accept a reference without having to mandate |
| 105 | // whether it is a local or global type. |
| 106 | template <typename T> |
| 107 | class JavaRef : public JavaRef<jobject> { |
| 108 | public: |
| 109 | constexpr JavaRef() {} |
| 110 | constexpr JavaRef(std::nullptr_t) {} |
| 111 | ~JavaRef() {} |
| 112 |
| 113 | T obj() const { return static_cast<T>(JavaRef<jobject>::obj()); } |
| 114 |
| 115 | // Get a JavaObjectArrayReader for the array pointed to by this reference. |
| 116 | // Only defined for JavaRef<jobjectArray>. |
| 117 | // You must pass the type of the array elements (usually jobject) as the |
| 118 | // template parameter. |
| 119 | template <typename ElementType, |
| 120 | typename T_ = T, |
| 121 | typename = std::enable_if_t<std::is_same<T_, jobjectArray>::value>> |
| 122 | JavaObjectArrayReader<ElementType> ReadElements() const { |
| 123 | return JavaObjectArrayReader<ElementType>(*this); |
| 124 | } |
| 125 |
| 126 | protected: |
| 127 | JavaRef(JNIEnv* env, T obj) : JavaRef<jobject>(env, obj) {} |
| 128 |
| 129 | private: |
| 130 | DISALLOW_COPY_AND_ASSIGN(JavaRef); |
| 131 | }; |
| 132 |
| 133 | // Holds a local reference to a JNI method parameter. |
| 134 | // Method parameters should not be deleted, and so this class exists purely to |
| 135 | // wrap them as a JavaRef<T> in the JNI binding generator. Do not create |
| 136 | // instances manually. |
| 137 | template <typename T> |
| 138 | class JavaParamRef : public JavaRef<T> { |
| 139 | public: |
| 140 | // Assumes that |obj| is a parameter passed to a JNI method from Java. |
| 141 | // Does not assume ownership as parameters should not be deleted. |
| 142 | JavaParamRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj) {} |
| 143 |
| 144 | // Allow nullptr to be converted to JavaParamRef. Some unit tests call JNI |
| 145 | // methods directly from C++ and pass null for objects which are not actually |
| 146 | // used by the implementation (e.g. the caller object); allow this to keep |
| 147 | // working. |
| 148 | JavaParamRef(std::nullptr_t) {} |
| 149 |
| 150 | ~JavaParamRef() {} |
| 151 |
| 152 | // TODO(torne): remove this cast once we're using JavaRef consistently. |
| 153 | // http://crbug.com/506850 |
| 154 | operator T() const { return JavaRef<T>::obj(); } |
| 155 |
| 156 | private: |
| 157 | DISALLOW_COPY_AND_ASSIGN(JavaParamRef); |
| 158 | }; |
| 159 |
| 160 | // Holds a local reference to a Java object. The local reference is scoped |
| 161 | // to the lifetime of this object. |
| 162 | // Instances of this class may hold onto any JNIEnv passed into it until |
| 163 | // destroyed. Therefore, since a JNIEnv is only suitable for use on a single |
| 164 | // thread, objects of this class must be created, used, and destroyed, on a |
| 165 | // single thread. |
| 166 | // Therefore, this class should only be used as a stack-based object and from a |
| 167 | // single thread. If you wish to have the reference outlive the current |
| 168 | // callstack (e.g. as a class member) or you wish to pass it across threads, |
| 169 | // use a ScopedJavaGlobalRef instead. |
| 170 | template <typename T> |
| 171 | class ScopedJavaLocalRef : public JavaRef<T> { |
| 172 | public: |
| 173 | // Take ownership of a bare jobject. This does not create a new reference. |
| 174 | // This should only be used by JNI helper functions, or in cases where code |
| 175 | // must call JNIEnv methods directly. |
| 176 | static ScopedJavaLocalRef Adopt(JNIEnv* env, T obj) { |
| 177 | return ScopedJavaLocalRef(env, obj); |
| 178 | } |
| 179 |
| 180 | constexpr ScopedJavaLocalRef() {} |
| 181 | constexpr ScopedJavaLocalRef(std::nullptr_t) {} |
| 182 |
| 183 | // Copy constructor. This is required in addition to the copy conversion |
| 184 | // constructor below. |
| 185 | ScopedJavaLocalRef(const ScopedJavaLocalRef& other) : env_(other.env_) { |
| 186 | JavaRef<T>::SetNewLocalRef(env_, other.obj()); |
| 187 | } |
| 188 |
| 189 | // Copy conversion constructor. |
| 190 | template <typename U, |
| 191 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 192 | ScopedJavaLocalRef(const ScopedJavaLocalRef<U>& other) : env_(other.env_) { |
| 193 | JavaRef<T>::SetNewLocalRef(env_, other.obj()); |
| 194 | } |
| 195 |
| 196 | // Move constructor. This is required in addition to the move conversion |
| 197 | // constructor below. |
| 198 | ScopedJavaLocalRef(ScopedJavaLocalRef&& other) : env_(other.env_) { |
| 199 | JavaRef<T>::steal(std::move(other)); |
| 200 | } |
| 201 |
| 202 | // Move conversion constructor. |
| 203 | template <typename U, |
| 204 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 205 | ScopedJavaLocalRef(ScopedJavaLocalRef<U>&& other) : env_(other.env_) { |
| 206 | JavaRef<T>::steal(std::move(other)); |
| 207 | } |
| 208 |
| 209 | // Constructor for other JavaRef types. |
| 210 | explicit ScopedJavaLocalRef(const JavaRef<T>& other) { Reset(other); } |
| 211 |
| 212 | // Assumes that |obj| is a local reference to a Java object and takes |
| 213 | // ownership of this local reference. |
| 214 | // TODO(torne): make legitimate uses call Adopt() instead, and make this |
| 215 | // private. |
| 216 | ScopedJavaLocalRef(JNIEnv* env, T obj) : JavaRef<T>(env, obj), env_(env) {} |
| 217 |
| 218 | ~ScopedJavaLocalRef() { Reset(); } |
| 219 |
| 220 | // Null assignment, for disambiguation. |
| 221 | ScopedJavaLocalRef& operator=(std::nullptr_t) { |
| 222 | Reset(); |
| 223 | return *this; |
| 224 | } |
| 225 |
| 226 | // Copy assignment. |
| 227 | ScopedJavaLocalRef& operator=(const ScopedJavaLocalRef& other) { |
| 228 | Reset(other); |
| 229 | return *this; |
| 230 | } |
| 231 |
| 232 | // Copy conversion assignment. |
| 233 | template <typename U, |
| 234 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 235 | ScopedJavaLocalRef& operator=(const ScopedJavaLocalRef<U>& other) { |
| 236 | Reset(other); |
| 237 | return *this; |
| 238 | } |
| 239 |
| 240 | // Move assignment. |
| 241 | template <typename U, |
| 242 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 243 | ScopedJavaLocalRef& operator=(ScopedJavaLocalRef<U>&& other) { |
| 244 | env_ = other.env_; |
| 245 | Reset(); |
| 246 | JavaRef<T>::steal(std::move(other)); |
| 247 | return *this; |
| 248 | } |
| 249 |
| 250 | // Assignment for other JavaRef types. |
| 251 | ScopedJavaLocalRef& operator=(const JavaRef<T>& other) { |
| 252 | Reset(other); |
| 253 | return *this; |
| 254 | } |
| 255 |
| 256 | void Reset() { JavaRef<T>::ResetLocalRef(env_); } |
| 257 |
| 258 | template <typename U, |
| 259 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 260 | void Reset(const ScopedJavaLocalRef<U>& other) { |
| 261 | // We can copy over env_ here as |other| instance must be from the same |
| 262 | // thread as |this| local ref. (See class comment for multi-threading |
| 263 | // limitations, and alternatives). |
| 264 | Reset(other.env_, other.obj()); |
| 265 | } |
| 266 |
| 267 | void Reset(const JavaRef<T>& other) { |
| 268 | // If |env_| was not yet set (is still null) it will be attached to the |
| 269 | // current thread in SetNewLocalRef(). |
| 270 | Reset(env_, other.obj()); |
| 271 | } |
| 272 |
| 273 | // Creates a new local reference to the Java object, unlike the constructor |
| 274 | // with the same parameters that takes ownership of the existing reference. |
| 275 | // Deprecated. Don't use bare jobjects; use a JavaRef as the input. |
| 276 | // TODO(torne): fix existing usage and remove this. |
| 277 | void Reset(JNIEnv* env, T obj) { |
| 278 | env_ = JavaRef<T>::SetNewLocalRef(env, obj); |
| 279 | } |
| 280 |
| 281 | // Releases the local reference to the caller. The caller *must* delete the |
| 282 | // local reference when it is done with it. Note that calling a Java method |
| 283 | // is *not* a transfer of ownership and Release() should not be used. |
| 284 | T Release() { return static_cast<T>(JavaRef<T>::ReleaseInternal()); } |
| 285 |
| 286 | private: |
| 287 | // This class is only good for use on the thread it was created on so |
| 288 | // it's safe to cache the non-threadsafe JNIEnv* inside this object. |
| 289 | JNIEnv* env_ = nullptr; |
| 290 |
| 291 | // Prevent ScopedJavaLocalRef(JNIEnv*, T obj) from being used to take |
| 292 | // ownership of a JavaParamRef's underlying object - parameters are not |
| 293 | // allowed to be deleted and so should not be owned by ScopedJavaLocalRef. |
| 294 | // TODO(torne): this can be removed once JavaParamRef no longer has an |
| 295 | // implicit conversion back to T. |
| 296 | ScopedJavaLocalRef(JNIEnv* env, const JavaParamRef<T>& other); |
| 297 |
| 298 | // Friend required to get env_ from conversions. |
| 299 | template <typename U> |
| 300 | friend class ScopedJavaLocalRef; |
| 301 |
| 302 | // Avoids JavaObjectArrayReader having to accept and store its own env. |
| 303 | template <typename U> |
| 304 | friend class JavaObjectArrayReader; |
| 305 | }; |
| 306 |
| 307 | // Holds a global reference to a Java object. The global reference is scoped |
| 308 | // to the lifetime of this object. This class does not hold onto any JNIEnv* |
| 309 | // passed to it, hence it is safe to use across threads (within the constraints |
| 310 | // imposed by the underlying Java object that it references). |
| 311 | template <typename T> |
| 312 | class ScopedJavaGlobalRef : public JavaRef<T> { |
| 313 | public: |
| 314 | constexpr ScopedJavaGlobalRef() {} |
| 315 | constexpr ScopedJavaGlobalRef(std::nullptr_t) {} |
| 316 |
| 317 | // Copy constructor. This is required in addition to the copy conversion |
| 318 | // constructor below. |
| 319 | ScopedJavaGlobalRef(const ScopedJavaGlobalRef& other) { Reset(other); } |
| 320 |
| 321 | // Copy conversion constructor. |
| 322 | template <typename U, |
| 323 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 324 | ScopedJavaGlobalRef(const ScopedJavaGlobalRef<U>& other) { |
| 325 | Reset(other); |
| 326 | } |
| 327 |
| 328 | // Move constructor. This is required in addition to the move conversion |
| 329 | // constructor below. |
| 330 | ScopedJavaGlobalRef(ScopedJavaGlobalRef&& other) { |
| 331 | JavaRef<T>::steal(std::move(other)); |
| 332 | } |
| 333 |
| 334 | // Move conversion constructor. |
| 335 | template <typename U, |
| 336 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 337 | ScopedJavaGlobalRef(ScopedJavaGlobalRef<U>&& other) { |
| 338 | JavaRef<T>::steal(std::move(other)); |
| 339 | } |
| 340 |
| 341 | // Conversion constructor for other JavaRef types. |
| 342 | explicit ScopedJavaGlobalRef(const JavaRef<T>& other) { Reset(other); } |
| 343 |
| 344 | // Create a new global reference to the object. |
| 345 | // Deprecated. Don't use bare jobjects; use a JavaRef as the input. |
| 346 | ScopedJavaGlobalRef(JNIEnv* env, T obj) { Reset(env, obj); } |
| 347 |
| 348 | ~ScopedJavaGlobalRef() { Reset(); } |
| 349 |
| 350 | // Null assignment, for disambiguation. |
| 351 | ScopedJavaGlobalRef& operator=(std::nullptr_t) { |
| 352 | Reset(); |
| 353 | return *this; |
| 354 | } |
| 355 |
| 356 | // Copy assignment. |
| 357 | ScopedJavaGlobalRef& operator=(const ScopedJavaGlobalRef& other) { |
| 358 | Reset(other); |
| 359 | return *this; |
| 360 | } |
| 361 |
| 362 | // Copy conversion assignment. |
| 363 | template <typename U, |
| 364 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 365 | ScopedJavaGlobalRef& operator=(const ScopedJavaGlobalRef<U>& other) { |
| 366 | Reset(other); |
| 367 | return *this; |
| 368 | } |
| 369 |
| 370 | // Move assignment. |
| 371 | template <typename U, |
| 372 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 373 | ScopedJavaGlobalRef& operator=(ScopedJavaGlobalRef<U>&& other) { |
| 374 | Reset(); |
| 375 | JavaRef<T>::steal(std::move(other)); |
| 376 | return *this; |
| 377 | } |
| 378 |
| 379 | // Assignment for other JavaRef types. |
| 380 | ScopedJavaGlobalRef& operator=(const JavaRef<T>& other) { |
| 381 | Reset(other); |
| 382 | return *this; |
| 383 | } |
| 384 |
| 385 | void Reset() { JavaRef<T>::ResetGlobalRef(); } |
| 386 |
| 387 | template <typename U, |
| 388 | typename = std::enable_if_t<std::is_convertible<U, T>::value>> |
| 389 | void Reset(const ScopedJavaGlobalRef<U>& other) { |
| 390 | Reset(nullptr, other.obj()); |
| 391 | } |
| 392 |
| 393 | void Reset(const JavaRef<T>& other) { Reset(nullptr, other.obj()); } |
| 394 |
| 395 | // Deprecated. You can just use Reset(const JavaRef&). |
| 396 | void Reset(JNIEnv* env, const JavaParamRef<T>& other) { |
| 397 | Reset(env, other.obj()); |
| 398 | } |
| 399 |
| 400 | // Deprecated. Don't use bare jobjects; use a JavaRef as the input. |
| 401 | void Reset(JNIEnv* env, T obj) { JavaRef<T>::SetNewGlobalRef(env, obj); } |
| 402 |
| 403 | // Releases the global reference to the caller. The caller *must* delete the |
| 404 | // global reference when it is done with it. Note that calling a Java method |
| 405 | // is *not* a transfer of ownership and Release() should not be used. |
| 406 | T Release() { return static_cast<T>(JavaRef<T>::ReleaseInternal()); } |
| 407 | }; |
| 408 |
| 409 | // Wrapper for a jobjectArray which supports input iteration, allowing Java |
| 410 | // arrays to be iterated over with a range-based for loop, or used with |
| 411 | // <algorithm> functions that accept input iterators. |
| 412 | // |
| 413 | // The iterator returns each object in the array in turn, wrapped in a |
| 414 | // ScopedJavaLocalRef<T>. T will usually be jobject, but if you know that the |
| 415 | // array contains a more specific type (such as jstring) you can use that |
| 416 | // instead. This does not check the type at runtime! |
| 417 | // |
| 418 | // The wrapper holds a local reference to the array and only queries the size of |
| 419 | // the array once, so must only be used as a stack-based object from the current |
| 420 | // thread. |
| 421 | // |
| 422 | // Note that this does *not* update the contents of the array if you mutate the |
| 423 | // returned ScopedJavaLocalRef. |
| 424 | template <typename T> |
| 425 | class JavaObjectArrayReader { |
| 426 | public: |
| 427 | class iterator { |
| 428 | public: |
| 429 | // We can only be an input iterator, as all richer iterator types must |
| 430 | // implement the multipass guarantee (always returning the same object for |
| 431 | // the same iterator position), which is not practical when returning |
| 432 | // temporary objects. |
| 433 | using iterator_category = std::input_iterator_tag; |
| 434 |
| 435 | using difference_type = ptrdiff_t; |
| 436 | using value_type = ScopedJavaLocalRef<T>; |
| 437 |
| 438 | // It doesn't make sense to return a reference type as the iterator creates |
| 439 | // temporary wrapper objects when dereferenced. Fortunately, it's not |
| 440 | // required that input iterators actually use references, and defining it |
| 441 | // as value_type is valid. |
| 442 | using reference = value_type; |
| 443 |
| 444 | // This exists to make operator-> work as expected: its return value must |
| 445 | // resolve to an actual pointer (otherwise the compiler just keeps calling |
| 446 | // operator-> on the return value until it does), so we need an extra level |
| 447 | // of indirection. This is sometimes called an "arrow proxy" or similar, and |
| 448 | // this version is adapted from base/value_iterators.h. |
| 449 | class pointer { |
| 450 | public: |
| 451 | explicit pointer(const reference& ref) : ref_(ref) {} |
| 452 | pointer(const pointer& ptr) = default; |
| 453 | pointer& operator=(const pointer& ptr) = delete; |
| 454 | reference* operator->() { return &ref_; } |
| 455 |
| 456 | private: |
| 457 | reference ref_; |
| 458 | }; |
| 459 |
| 460 | iterator(const iterator&) = default; |
| 461 | ~iterator() = default; |
| 462 |
| 463 | iterator& operator=(const iterator&) = default; |
| 464 |
| 465 | bool operator==(const iterator& other) const { |
| 466 | DCHECK(reader_ == other.reader_); |
| 467 | return i_ == other.i_; |
| 468 | } |
| 469 |
| 470 | bool operator!=(const iterator& other) const { |
| 471 | DCHECK(reader_ == other.reader_); |
| 472 | return i_ != other.i_; |
| 473 | } |
| 474 |
| 475 | reference operator*() const { |
| 476 | DCHECK(i_ < reader_->size_); |
| 477 | // JNIEnv functions return unowned local references; take ownership with |
| 478 | // Adopt so that ~ScopedJavaLocalRef will release it automatically later. |
| 479 | return value_type::Adopt( |
| 480 | reader_->array_.env_, |
| 481 | static_cast<T>(reader_->array_.env_->GetObjectArrayElement( |
| 482 | reader_->array_.obj(), i_))); |
| 483 | } |
| 484 |
| 485 | pointer operator->() const { return pointer(operator*()); } |
| 486 |
| 487 | iterator& operator++() { |
| 488 | DCHECK(i_ < reader_->size_); |
| 489 | ++i_; |
| 490 | return *this; |
| 491 | } |
| 492 |
| 493 | iterator operator++(int) { |
| 494 | iterator old = *this; |
| 495 | ++*this; |
| 496 | return old; |
| 497 | } |
| 498 |
| 499 | private: |
| 500 | iterator(const JavaObjectArrayReader* reader, jsize i) |
| 501 | : reader_(reader), i_(i) {} |
| 502 | const JavaObjectArrayReader* reader_; |
| 503 | jsize i_; |
| 504 |
| 505 | friend JavaObjectArrayReader; |
| 506 | }; |
| 507 |
| 508 | JavaObjectArrayReader(const JavaRef<jobjectArray>& array) : array_(array) { |
| 509 | size_ = array_.env_->GetArrayLength(array_.obj()); |
| 510 | } |
| 511 |
| 512 | // Copy constructor to allow returning it from JavaRef::ReadElements(). |
| 513 | JavaObjectArrayReader(const JavaObjectArrayReader& other) = default; |
| 514 |
| 515 | // Assignment operator for consistency with copy constructor. |
| 516 | JavaObjectArrayReader& operator=(const JavaObjectArrayReader& other) = |
| 517 | default; |
| 518 |
| 519 | // Allow move constructor and assignment since this owns a local ref. |
| 520 | JavaObjectArrayReader(JavaObjectArrayReader&& other) = default; |
| 521 | JavaObjectArrayReader& operator=(JavaObjectArrayReader&& other) = default; |
| 522 |
| 523 | bool empty() const { return size_ == 0; } |
| 524 |
| 525 | jsize size() const { return size_; } |
| 526 |
| 527 | iterator begin() const { return iterator(this, 0); } |
| 528 |
| 529 | iterator end() const { return iterator(this, size_); } |
| 530 |
| 531 | private: |
| 532 | ScopedJavaLocalRef<jobjectArray> array_; |
| 533 | jsize size_; |
| 534 |
| 535 | friend iterator; |
| 536 | }; |
| 537 |
| 538 | } // namespace android |
| 539 | } // namespace base |
| 540 |
| 541 | #endif // BASE_ANDROID_SCOPED_JAVA_REF_H_ |
| 542 |