YSTest: JNI.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

JNI.h
浏览该文件的文档.
1 /*
2 © 2014-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
29 #ifndef YCL_INC_JNI_h_
30 #define YCL_INC_JNI_h_ 1
31
32 #include "YModules.h"
33 #include YFM_YCLib_Platform
34 #if YF_Use_JNI
35 # include YFM_YCLib_Reference // for lref;
36 # include <jni.h> // for ::JavaVM, ::JNIEnv;
37 # include YFM_YCLib_Container // for pair;
38 # include <stdexcept>
39 # include <utility>
40
41 namespace platform_ex
42 {
43
45
46 namespace JNI
47 {
48
50 class YF_API JNIException : public std::runtime_error
51 {
52 public:
53 using runtime_error::runtime_error;
54 };
55
56
58 # define YF_Raise_JNIExceptionOnNull(_ref, ...) \
59 { \
60 if(!_ref) \
61 throw platform_ex::JNI::JNIException(__VA_ARGS__); \
62 }
63
64
69 class YF_API JNIBase
70 {
71 private:
73 lref<::JavaVM> vm_ref;
75 lref<::JNIEnv> env_ref;
77 bool owns{};
78
79 public:
85 JNIBase(::JavaVM&, ::jint = YF_Use_JNI);
87 JNIBase(::JavaVM& vm, ::JNIEnv& env)
88 : vm_ref(vm), env_ref(env), owns()
89 {}
90
91 private:
96 JNIBase(::JavaVM& vm, const pair<::JNIEnv&, bool>& pr)
97 : vm_ref(vm), env_ref(pr.first), owns(pr.second)
98 {}
99
100 public:
106 ~JNIBase();
107
108 DefGetter(const ynothrow, ::JNIEnv&, EnvRef, env_ref)
109 DefGetter(const ynothrow, ::JavaVM&, VMRef, vm_ref)
110
115 template<typename _type>
116 static inline auto
117 Deref(const _type& p) -> decltype(*p)
118 {
119 return *Nonnull(p);
120 }
121
127 static void
128 EnsureDetachJNIAtThreadExit(::JavaVM&, ::JNIEnv&);
129
134 template<typename _type>
135 static inline _type
136 Nonnull(const _type& p)
137 {
138 YF_Raise_JNIExceptionOnNull(p, "Specific object not found.")
139 return p;
140 }
141
147 void
148 ThrowOnException() ythrow(JNIException);
149 };
150
151
156 class YF_API JNI : private JNIBase
157 {
158 public:
159 using Boolean = ::jboolean;
160 using Byte = ::jbyte;
161 using Char = ::jchar;
162 using Short = ::jshort;
163 using Int = ::jint;
164 using Long = ::jlong;
165 using Float = ::jfloat;
166 using Double = ::jdouble;
167 using Object = ystdex::remove_pointer_t<::jobject>;
168 using Class = ystdex::remove_pointer_t<::jclass>;
169 using String = ystdex::remove_pointer_t<::jstring>;
170 using Array = ystdex::remove_pointer_t<::jarray>;
171 using ObjectArray = ystdex::remove_pointer_t<::jobjectArray>;
172 using BooleanArray = ystdex::remove_pointer_t<::jbooleanArray>;
173 using ByteArray = ystdex::remove_pointer_t<::jbyteArray>;
174 using CharArray = ystdex::remove_pointer_t<::jcharArray>;
175 using ShortArray = ystdex::remove_pointer_t<::jshortArray>;
176 using IntArray = ystdex::remove_pointer_t<::jintArray>;
177 using LongArray = ystdex::remove_pointer_t<::jlongArray>;
178 using FloatArray = ystdex::remove_pointer_t<::jfloatArray>;
179 using DoubleArray = ystdex::remove_pointer_t<::jdoubleArray>;
180 using Throwable = ystdex::remove_pointer_t<::jthrowable>;
181 using Weak = ystdex::remove_pointer_t<::jweak>;
182 using FieldID = ystdex::remove_pointer_t<::jfieldID>;
183 using MethodID = ystdex::remove_pointer_t<::jmethodID>;
184 using Value = ::jvalue;
185 using ObjectRefType = ::jobjectRefType;
186 using NativeMethod = ::JNINativeMethod;
187
188 using JNIBase::JNIBase;
189
191
192 PDefH(Boolean, IsAssignableFrom, Class& clazz1, Class& clazz2) const
193 ynothrow
194 ImplRet(GetEnvRef().IsAssignableFrom(&clazz1, &clazz2))
195 PDefH(Boolean, IsInstanceOf, Object& obj, Class& clazz) const ynothrow
196 ImplRet(GetEnvRef().IsInstanceOf(&obj, &clazz))
197 PDefH(Boolean, IsSameObject, Object& ref1, Object& ref2) const ynothrow
198 ImplRet(GetEnvRef().IsSameObject(&ref1, &ref2))
200
201 using JNIBase::GetEnvRef;
202 using JNIBase::GetVMRef;
203
204 using JNIBase::Deref;
205
206 using JNIBase::EnsureDetachJNIAtThreadExit;
207
208 using JNIBase::Nonnull;
209
210 using JNIBase::ThrowOnException;
211 };
212
213 } // namespace JNI;
215
216 } // namespace platform_ex;
217
218 #endif
219
220 #endif
221
platform::p
while p p
Definition: YCLib/FileSystem.h:225
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
NPL::SXML::ParseOption::String
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YModules.h
JNI.h
Java 本机接口包装。
YF_API
#define YF_API
Definition: Platform.h:56
ythrow
#define ythrow(...)
YSLib 动态异常规范：根据是否使用异常规范宏指定或忽略动态异常规范。
Definition: ydef.h:791