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* const mEnv; \
54         PRIMITIVE_TYPE ## Array mJavaArray; \
55         PRIMITIVE_TYPE* mRawArray; \
56         DISALLOW_COPY_AND_ASSIGN(Scoped ## NAME ## ArrayRO); \
57     }
58 
59 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
60 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte);
61 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char);
62 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double);
63 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float);
64 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int);
65 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long);
66 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short);
67 
68 #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO
69 
70 // ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW,
71 // ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide
72 // convenient read-write access to Java arrays from JNI code. These are more expensive,
73 // since they entail a copy back onto the Java heap, and should only be used when necessary.
74 #define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \
75     class Scoped ## NAME ## ArrayRW { \
76     public: \
77         explicit Scoped ## NAME ## ArrayRW(JNIEnv* env) \
78         : mEnv(env), mJavaArray(NULL), mRawArray(NULL) {} \
79         Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
80         : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
81             if (mJavaArray == NULL) { \
82                 jniThrowNullPointerException(mEnv, NULL); \
83             } else { \
84                 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
85             } \
86         } \
87         ~Scoped ## NAME ## ArrayRW() { \
88             if (mRawArray) { \
89                 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \
90             } \
91         } \
92         void reset(PRIMITIVE_TYPE ## Array javaArray) { \
93             mJavaArray = javaArray; \
94             mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
95         } \
96         const PRIMITIVE_TYPE* get() const { return mRawArray; } \
97         PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \
98         const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
99         PRIMITIVE_TYPE* get() { return mRawArray; } \
100         PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \
101         size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
102     private: \
103         JNIEnv* const mEnv; \
104         PRIMITIVE_TYPE ## Array mJavaArray; \
105         PRIMITIVE_TYPE* mRawArray; \
106         DISALLOW_COPY_AND_ASSIGN(Scoped ## NAME ## ArrayRW); \
107     }
108 
109 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
110 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte);
111 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char);
112 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double);
113 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float);
114 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int);
115 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long);
116 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short);
117 
118 #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW
119 
120 #endif  // SCOPED_PRIMITIVE_ARRAY_H_included
121