blob: 079e98c2ee4ead147f3009b2c709201eb02cc63a [file] [log] [blame]
Elliott Hughes99c59bf2010-05-17 16:22:04 -07001/*
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 Hughesebca53a2010-05-20 20:54:45 -070022// 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 Hughes99c59bf2010-05-17 16:22:04 -070028 public: \
Elliott Hughesebca53a2010-05-20 20:54:45 -070029 Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070030 : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
Elliott Hughes64101122010-07-12 09:53:54 -070031 if (mJavaArray == NULL) { \
32 jniThrowNullPointerException(mEnv, NULL); \
33 } else { \
34 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
35 } \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070036 } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070037 ~Scoped ## NAME ## ArrayRO() { \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070038 if (mRawArray) { \
39 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \
40 } \
41 } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070042 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 Hughes99c59bf2010-05-17 16:22:04 -070045 private: \
46 JNIEnv* mEnv; \
47 PRIMITIVE_TYPE ## Array mJavaArray; \
48 PRIMITIVE_TYPE* mRawArray; \
Elliott Hughesebca53a2010-05-20 20:54:45 -070049 Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \
50 void operator=(const Scoped ## NAME ## ArrayRO&); \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070051 }
52
Elliott Hughesebca53a2010-05-20 20:54:45 -070053INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
54INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte);
55INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char);
56INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double);
57INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float);
58INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int);
59INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long);
60INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short);
Elliott Hughes99c59bf2010-05-17 16:22:04 -070061
Elliott Hughesebca53a2010-05-20 20:54:45 -070062#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 Hughes64101122010-07-12 09:53:54 -070073 if (mJavaArray == NULL) { \
74 jniThrowNullPointerException(mEnv, NULL); \
75 } else { \
76 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
77 } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070078 } \
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
97INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
98INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte);
99INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char);
100INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double);
101INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float);
102INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int);
103INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long);
104INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short);
105
106#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW
Elliott Hughes99c59bf2010-05-17 16:22:04 -0700107
108#endif // SCOPED_PRIMITIVE_ARRAY_H_included