Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 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 { \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 28 | public: \ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 29 | Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 30 | : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 31 | if (mJavaArray == NULL) { \ |
| 32 | jniThrowNullPointerException(mEnv, NULL); \ |
| 33 | } else { \ |
| 34 | mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ |
| 35 | } \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 36 | } \ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 37 | ~Scoped ## NAME ## ArrayRO() { \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 38 | if (mRawArray) { \ |
| 39 | mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \ |
| 40 | } \ |
| 41 | } \ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 42 | const PRIMITIVE_TYPE* get() const { return mRawArray; } \ |
| 43 | const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ |
| 44 | size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 45 | private: \ |
| 46 | JNIEnv* mEnv; \ |
| 47 | PRIMITIVE_TYPE ## Array mJavaArray; \ |
| 48 | PRIMITIVE_TYPE* mRawArray; \ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 49 | Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \ |
| 50 | void operator=(const Scoped ## NAME ## ArrayRO&); \ |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 53 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean); |
| 54 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte); |
| 55 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char); |
| 56 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double); |
| 57 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float); |
| 58 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int); |
| 59 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long); |
| 60 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 62 | #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO |
| 63 | |
| 64 | // ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW, |
| 65 | // ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide |
| 66 | // convenient read-write access to Java arrays from JNI code. These are more expensive, |
| 67 | // since they entail a copy back onto the Java heap, and should only be used when necessary. |
| 68 | #define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \ |
| 69 | class Scoped ## NAME ## ArrayRW { \ |
| 70 | public: \ |
| 71 | Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ |
| 72 | : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 73 | if (mJavaArray == NULL) { \ |
| 74 | jniThrowNullPointerException(mEnv, NULL); \ |
| 75 | } else { \ |
| 76 | mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ |
| 77 | } \ |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 78 | } \ |
| 79 | ~Scoped ## NAME ## ArrayRW() { \ |
| 80 | if (mRawArray) { \ |
| 81 | mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \ |
| 82 | } \ |
| 83 | } \ |
| 84 | const PRIMITIVE_TYPE* get() const { return mRawArray; } \ |
| 85 | const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ |
| 86 | PRIMITIVE_TYPE* get() { return mRawArray; } \ |
| 87 | PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \ |
| 88 | size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ |
| 89 | private: \ |
| 90 | JNIEnv* mEnv; \ |
| 91 | PRIMITIVE_TYPE ## Array mJavaArray; \ |
| 92 | PRIMITIVE_TYPE* mRawArray; \ |
| 93 | Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \ |
| 94 | void operator=(const Scoped ## NAME ## ArrayRW&); \ |
| 95 | } |
| 96 | |
| 97 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean); |
| 98 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte); |
| 99 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char); |
| 100 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double); |
| 101 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float); |
| 102 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int); |
| 103 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long); |
| 104 | INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short); |
| 105 | |
| 106 | #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 107 | |
| 108 | #endif // SCOPED_PRIMITIVE_ARRAY_H_included |