Skip to content

Commit 8d042ae

Browse files
committed
Add safe Cast operation
1 parent 6c2fdc7 commit 8d042ae

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/jni/object.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ namespace jni
128128
{
129129
return Seize(env, Object(jni::NewGlobalRef(env, obj).release()));
130130
}
131+
132+
template < class OtherTag >
133+
bool IsInstanceOf(JNIEnv& env, const Class<OtherTag>& clazz) const
134+
{
135+
return jni::IsInstanceOf(env, obj, clazz);
136+
}
131137
};
132138

133139
template < class TagType >
@@ -157,4 +163,15 @@ namespace jni
157163
{
158164
return UniqueObject<TagType>(PointerToValue<Object<TagType>>(std::move(object)), ObjectDeleter<TagType>(env));
159165
};
166+
167+
168+
template < class OutTagType, class InTagType >
169+
Object<OutTagType> Cast(JNIEnv& env, const Object<InTagType>& object, const Class<OutTagType>& clazz)
170+
{
171+
if (!object.IsInstanceOf(env, clazz))
172+
{
173+
ThrowNew(env, FindClass(env, "java/lang/ClassCastException"));
174+
}
175+
return Object<OutTagType>(object.Get());
176+
}
160177
}

0 commit comments

Comments
 (0)