-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathobject.hpp
261 lines (209 loc) · 8.93 KB
/
object.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#pragma once
#include <jni/functions.hpp>
#include <jni/tagging.hpp>
#include <jni/pointer_to_value.hpp>
#include <cstddef>
namespace jni
{
template < class TheTag > class Class;
template < class TheTag, class > class Field;
template < class TheTag, class > class Method;
struct ObjectTag { static constexpr auto Name() { return "java/lang/Object"; } };
template < class TagType >
struct UntaggedObjectType { using Type = jobject; };
template < class TheTag >
class Object;
template < class TagType >
class ObjectDeleter;
template < class TagType = ObjectTag >
using UniqueObject = std::unique_ptr< const Object<TagType>, ObjectDeleter<TagType> >;
template < class TagType >
class WeakObjectRefDeleter;
template < class TagType = ObjectTag >
using UniqueWeakObject = std::unique_ptr< const Object<TagType>, WeakObjectRefDeleter<TagType> >;
template < class TagType >
class LocalObjectRefDeleter;
template < class TagType = ObjectTag >
using UniqueLocalObject = std::unique_ptr< const Object<TagType>, LocalObjectRefDeleter<TagType> >;
template < class TheTag = ObjectTag >
class Object
{
public:
using TagType = TheTag;
using UntaggedObjectType = typename UntaggedObjectType<TagType>::Type;
private:
UntaggedObjectType* obj = nullptr;
public:
explicit Object(std::nullptr_t = nullptr)
{}
explicit Object(UntaggedObjectType* o)
: obj(o)
{}
explicit Object(UntaggedObjectType& o)
: obj(&o)
{}
template < class Tag >
Object(const Object<Tag>& o, std::enable_if_t< std::is_convertible<Tag, TagType>::value >* = nullptr)
: obj(o.Get())
{}
explicit operator bool() const { return obj; }
operator UntaggedObjectType*() const { return obj; }
UntaggedObjectType& operator*() const { return *obj; }
UntaggedObjectType* Get() const { return obj; }
friend bool operator==( const Object& a, const Object& b ) { return a.Get() == b.Get(); }
friend bool operator!=( const Object& a, const Object& b ) { return !( a == b ); }
template < class T >
auto Get(JNIEnv& env, const Field<TagType, T>& field) const
-> std::enable_if_t< IsPrimitive<T>::value, T >
{
return GetField<T>(env, obj, field);
}
template < class T >
auto Get(JNIEnv& env, const Field<TagType, T>& field) const
-> std::enable_if_t< !IsPrimitive<T>::value, T >
{
return T(reinterpret_cast<UntaggedType<T>>(GetField<jobject*>(env, obj, field)));
}
template < class T >
auto Set(JNIEnv& env, const Field<TagType, T>& field, T value) const
-> std::enable_if_t< IsPrimitive<T>::value >
{
SetField<T>(env, obj, field, value);
}
template < class T >
auto Set(JNIEnv& env, const Field<TagType, T>& field, const T& value) const
-> std::enable_if_t< !IsPrimitive<T>::value >
{
SetField<jobject*>(env, obj, field, value.Get());
}
template < class R, class... Args >
auto Call(JNIEnv& env, const Method<TagType, R (Args...)>& method, const Args&... args) const
-> std::enable_if_t< IsPrimitive<R>::value, R >
{
return CallMethod<R>(env, obj, method, RemoveTag(args)...);
}
template < class R, class... Args >
auto Call(JNIEnv& env, const Method<TagType, R (Args...)>& method, const Args&... args) const
-> std::enable_if_t< !IsPrimitive<R>::value && !std::is_void<R>::value, R >
{
return R(reinterpret_cast<UntaggedType<R>>(CallMethod<jobject*>(env, obj, method, RemoveTag(args)...)));
}
template < class... Args >
void Call(JNIEnv& env, const Method<TagType, void (Args...)>& method, const Args&... args) const
{
CallMethod<void>(env, obj, method, RemoveTag(args)...);
}
template < class R, class... Args >
auto CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, R (Args...)>& method, const Args&... args) const
-> std::enable_if_t< IsPrimitive<R>::value, R >
{
return CallNonvirtualMethod<R>(env, obj, clazz, method, RemoveTag(args)...);
}
template < class R, class... Args >
auto CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, R (Args...)>& method, const Args&... args) const
-> std::enable_if_t< !IsPrimitive<R>::value, R >
{
return R(reinterpret_cast<UntaggedType<R>>(CallNonvirtualMethod<jobject*>(env, obj, clazz, method, RemoveTag(args)...)));
}
template < class... Args >
void CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, void (Args...)>& method, const Args&... args) const
{
CallNonvirtualMethod<void>(env, obj, clazz, method, RemoveTag(args)...);
}
UniqueObject<TagType> NewGlobalRef(JNIEnv& env) const
{
return Seize(env, Object(jni::NewGlobalRef(env, obj).release()));
}
UniqueWeakObject<TagType> NewWeakGlobalRef(JNIEnv& env) const
{
return SeizeWeakRef(env, Object(jni::NewWeakGlobalRef(env, obj).release()));
}
UniqueLocalObject<TagType> NewLocalRef(JNIEnv& env) const
{
return SeizeLocalRef(env, Object(jni::NewLocalRef(env, obj).release()));
}
template < class OtherTag >
bool IsInstanceOf(JNIEnv& env, const Class<OtherTag>& clazz) const
{
return jni::IsInstanceOf(env, obj, clazz);
}
};
template < class TagType >
class ObjectDeleter
{
private:
JNIEnv* env = nullptr;
public:
using pointer = PointerToValue< Object<TagType> >;
ObjectDeleter() = default;
ObjectDeleter(JNIEnv& e) : env(&e) {}
void operator()(pointer p) const
{
if (p)
{
assert(env);
env->DeleteGlobalRef(Unwrap(p->Get()));
}
}
};
template < class TagType >
UniqueObject<TagType> Seize(JNIEnv& env, Object<TagType>&& object)
{
return UniqueObject<TagType>(PointerToValue<Object<TagType>>(std::move(object)), ObjectDeleter<TagType>(env));
}
template < class TagType >
class WeakObjectRefDeleter
{
private:
JNIEnv* env = nullptr;
public:
using pointer = PointerToValue< Object<TagType> >;
WeakObjectRefDeleter() = default;
WeakObjectRefDeleter(JNIEnv& e) : env(&e) {}
void operator()(pointer p) const
{
if (p)
{
assert(env);
env->DeleteWeakGlobalRef(Unwrap(p->Get()));
}
}
};
template < class TagType >
UniqueWeakObject<TagType> SeizeWeakRef(JNIEnv& env, Object<TagType>&& object)
{
return UniqueWeakObject<TagType>(PointerToValue<Object<TagType>>(std::move(object)), WeakObjectRefDeleter<TagType>(env));
}
template < class TagType >
class LocalObjectRefDeleter
{
private:
JNIEnv* env = nullptr;
public:
using pointer = PointerToValue< Object<TagType> >;
LocalObjectRefDeleter() = default;
LocalObjectRefDeleter(JNIEnv& e) : env(&e) {}
void operator()(pointer p) const
{
if (p)
{
assert(env);
env->DeleteLocalRef(Unwrap(p->Get()));
}
}
};
template < class TagType >
UniqueLocalObject<TagType> SeizeLocalRef(JNIEnv& env, Object<TagType>&& object)
{
return UniqueLocalObject<TagType>(PointerToValue<Object<TagType>>(std::move(object)), LocalObjectRefDeleter<TagType>(env));
}
template < class OutTagType, class InTagType >
Object<OutTagType> Cast(JNIEnv& env, const Object<InTagType>& object, const Class<OutTagType>& clazz)
{
if (!object.IsInstanceOf(env, clazz))
{
ThrowNew(env, FindClass(env, "java/lang/ClassCastException"));
}
return Object<OutTagType>(object.Get());
}
}