• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
30         : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
31             if (mJavaArray == NULL) { \
32                 jniThrowNullPointerException(mEnv, NULL); \
33             } else { \
34                 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
35             } \
36         } \
37         ~Scoped ## NAME ## ArrayRO() { \
38             if (mRawArray) { \
39                 mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \
40             } \
41         } \
42         const PRIMITIVE_TYPE* get() const { return mRawArray; } \
43         PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \
44         const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
45         size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
46     private: \
47         JNIEnv* mEnv; \
48         PRIMITIVE_TYPE ## Array mJavaArray; \
49         PRIMITIVE_TYPE* mRawArray; \
50         Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \
51         void operator=(const Scoped ## NAME ## ArrayRO&); \
52     }
53 
54 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
55 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte);
56 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char);
57 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double);
58 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float);
59 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int);
60 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long);
61 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short);
62 
63 #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) { \
74             if (mJavaArray == NULL) { \
75                 jniThrowNullPointerException(mEnv, NULL); \
76             } else { \
77                 mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
78             } \
79         } \
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; } \
86         PRIMITIVE_TYPE ## Array getJavaArray() const { return mJavaArray; } \
87         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 
99 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
100 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte);
101 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char);
102 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double);
103 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float);
104 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int);
105 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long);
106 INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short);
107 
108 #undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW
109 
110 #endif  // SCOPED_PRIMITIVE_ARRAY_H_included
111