Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e2be0b0

Browse files
committedJun 19, 2018
Add weak ref promotions
1 parent 2a68c26 commit e2be0b0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
 

‎include/jni/functions.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ namespace jni
138138
return UniqueGlobalRef<T>(reinterpret_cast<T*>(obj), GlobalRefDeleter(env));
139139
}
140140

141+
// Attempt to promote a weak reference to a strong one. Returns an empty result
142+
// if the weak reference has expired.
143+
template < class T >
144+
UniqueGlobalRef<T> NewGlobalRef(JNIEnv& env, const UniqueWeakGlobalRef<T>& t)
145+
{
146+
jobject* obj = Wrap<jobject*>(env.NewGlobalRef(Unwrap(t)));
147+
CheckJavaException(env);
148+
return UniqueGlobalRef<T>(reinterpret_cast<T*>(obj), GlobalRefDeleter(env));
149+
}
150+
141151
inline void DeleteGlobalRef(JNIEnv& env, UniqueGlobalRef<jobject>&& ref)
142152
{
143153
env.DeleteGlobalRef(Unwrap(ref.release()));
@@ -155,6 +165,16 @@ namespace jni
155165
return UniqueLocalRef<T>(reinterpret_cast<T*>(obj), LocalRefDeleter(env));
156166
}
157167

168+
// Attempt to promote a weak reference to a strong one. Returns an empty result
169+
// if the weak reference has expired.
170+
template < class T >
171+
UniqueLocalRef<T> NewLocalRef(JNIEnv& env, const UniqueWeakGlobalRef<T>& t)
172+
{
173+
jobject* obj = Wrap<jobject*>(env.NewLocalRef(Unwrap(t)));
174+
CheckJavaException(env);
175+
return UniqueLocalRef<T>(reinterpret_cast<T*>(obj), LocalRefDeleter(env));
176+
}
177+
158178
inline void DeleteLocalRef(JNIEnv& env, UniqueLocalRef<jobject>&& ref)
159179
{
160180
env.DeleteLocalRef(Unwrap(ref.release()));

‎include/jni/unique_pointerlike.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,24 @@ namespace jni
103103
{
104104
return Local<T>(std::move(t), LocalRefDeleter(env));
105105
}
106+
107+
// Attempt to promote a weak reference to a strong one. Returns an empty result
108+
// if the weak reference has expired.
109+
template < class T >
110+
Global<T> NewGlobal(JNIEnv& env, const Weak<T>& t)
111+
{
112+
jobject* obj = Wrap<jobject*>(env.NewGlobalRef(Unwrap(t->Get())));
113+
CheckJavaException(env);
114+
return SeizeGlobal(env, T(obj));
115+
}
116+
117+
// Attempt to promote a weak reference to a strong one. Returns an empty result
118+
// if the weak reference has expired.
119+
template < class T >
120+
Local<T> NewLocal(JNIEnv& env, const Weak<T>& t)
121+
{
122+
jobject* obj = Wrap<jobject*>(env.NewLocalRef(Unwrap(t->Get())));
123+
CheckJavaException(env);
124+
return SeizeLocal(env, T(obj));
125+
}
106126
}

0 commit comments

Comments
 (0)
Please sign in to comment.