1 /* 2 * Copyright (C) 2010 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 #ifndef SCOPED_PRIMITIVE_ARRAY_H_included 18 #define SCOPED_PRIMITIVE_ARRAY_H_included 19 20 #include "JNIHelp.h" 21 22 // ScopedBooleanArrayRO, ScopedByteArrayRO, ScopedCharArrayRO, ScopedDoubleArrayRO, 23 // ScopedFloatArrayRO, ScopedIntArrayRO, ScopedLongArrayRO, and ScopedShortArrayRO provide 24 // convenient read-only access to Java arrays from JNI code. This is cheaper than read-write 25 // access and should be used by default. 26 #define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(PRIMITIVE_TYPE, NAME) \ 27 class Scoped ## NAME ## ArrayRO { \ 28 public: \ 29 explicit Scoped ## NAME ## ArrayRO(JNIEnv* env) \ 30 : mEnv(env), mJavaArray(NULL), mRawArray(NULL) {} \ 31 Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ 32 : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ 33 if (mJavaArray == NULL) { \ 34 jniThrowNullPointerException(mEnv, NULL); \ 35 } else { \ 36 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ 37 } \ 38 } \ 39 ~Scoped ## NAME ## ArrayRO() { \ 40 if (mRawArray) { \ 41 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \ 42 } \ 43 } \ 44 void reset(PRIMITIVE_TYPE ## Array javaArray) { \ 45 mJavaArray = javaArray; \ 46 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ 47 } \ 48 const PRIMITIVE_TYPE* get() const { return mRawArray; } \ 49 PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \ 50 const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ 51 size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ 52 private: \ 53 JNIEnv* mEnv; \ 54 PRIMITIVE_TYPE ## Array mJavaArray; \ 55 PRIMITIVE_TYPE* mRawArray; \ 56 Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \ 57 void operator=(const Scoped ## NAME ## ArrayRO&); \ 58 } 59 60 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean); 61 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte); 62 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char); 63 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double); 64 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float); 65 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int); 66 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long); 67 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); 68 69 #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO 70 71 // ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW, 72 // ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide 73 // convenient read-write access to Java arrays from JNI code. These are more expensive, 74 // since they entail a copy back onto the Java heap, and should only be used when necessary. 75 #define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \ 76 class Scoped ## NAME ## ArrayRW { \ 77 public: \ 78 explicit Scoped ## NAME ## ArrayRW(JNIEnv* env) \ 79 : mEnv(env), mJavaArray(NULL), mRawArray(NULL) {} \ 80 Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ 81 : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ 82 if (mJavaArray == NULL) { \ 83 jniThrowNullPointerException(mEnv, NULL); \ 84 } else { \ 85 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ 86 } \ 87 } \ 88 ~Scoped ## NAME ## ArrayRW() { \ 89 if (mRawArray) { \ 90 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \ 91 } \ 92 } \ 93 void reset(PRIMITIVE_TYPE ## Array javaArray) { \ 94 mJavaArray = javaArray; \ 95 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ 96 } \ 97 const PRIMITIVE_TYPE* get() const { return mRawArray; } \ 98 PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \ 99 const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ 100 PRIMITIVE_TYPE* get() { return mRawArray; } \ 101 PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \ 102 size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ 103 private: \ 104 JNIEnv* mEnv; \ 105 PRIMITIVE_TYPE ## Array mJavaArray; \ 106 PRIMITIVE_TYPE* mRawArray; \ 107 Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \ 108 void operator=(const Scoped ## NAME ## ArrayRW&); \ 109 } 110 111 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean); 112 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte); 113 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char); 114 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double); 115 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float); 116 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int); 117 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long); 118 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short); 119 120 #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW 121 122 #endif // SCOPED_PRIMITIVE_ARRAY_H_included 123