blob: f6626b259736473f48bb728124c872ab24e5235d [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; } \
Jeff Sharkey74adde42013-02-08 13:13:18 -080043 PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070044 const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
45 size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070046 private: \
47 JNIEnv* mEnv; \
48 PRIMITIVE_TYPE ## Array mJavaArray; \
49 PRIMITIVE_TYPE* mRawArray; \
Elliott Hughesebca53a2010-05-20 20:54:45 -070050 Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \
51 void operator=(const Scoped ## NAME ## ArrayRO&); \
Elliott Hughes99c59bf2010-05-17 16:22:04 -070052 }
53
Elliott Hughesebca53a2010-05-20 20:54:45 -070054INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
55INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte);
56INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char);
57INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double);
58INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float);
59INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int);
60INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long);
61INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short);
Elliott Hughes99c59bf2010-05-17 16:22:04 -070062
Elliott Hughesebca53a2010-05-20 20:54:45 -070063#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO
64
65// ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW,
66// ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide
67// convenient read-write access to Java arrays from JNI code. These are more expensive,
68// since they entail a copy back onto the Java heap, and should only be used when necessary.
69#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \
70 class Scoped ## NAME ## ArrayRW { \
71 public: \
72 Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
73 : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
Elliott Hughes64101122010-07-12 09:53:54 -070074 if (mJavaArray == NULL) { \
75 jniThrowNullPointerException(mEnv, NULL); \
76 } else { \
77 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
78 } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070079 } \
80 ~Scoped ## NAME ## ArrayRW() { \
81 if (mRawArray) { \
82 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \
83 } \
84 } \
85 const PRIMITIVE_TYPE* get() const { return mRawArray; } \
Jeff Sharkey74adde42013-02-08 13:13:18 -080086 PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \
Elliott Hughesebca53a2010-05-20 20:54:45 -070087 const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
88 PRIMITIVE_TYPE* get() { return mRawArray; } \
89 PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \
90 size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
91 private: \
92 JNIEnv* mEnv; \
93 PRIMITIVE_TYPE ## Array mJavaArray; \
94 PRIMITIVE_TYPE* mRawArray; \
95 Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \
96 void operator=(const Scoped ## NAME ## ArrayRW&); \
97 }
98
99INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
100INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte);
101INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char);
102INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double);
103INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float);
104INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int);
105INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long);
106INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short);
107
108#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW
Elliott Hughes99c59bf2010-05-17 16:22:04 -0700109
110#endif // SCOPED_PRIMITIVE_ARRAY_H_included