Bug 1343027 - 1. Add jni::Ref::Cast function; r=snorp

Add a Cast function to jni::Ref to cast a reference to a different type
(e.g. if ref is a jni::Object::Ref referring to a String instance, use |
ref.Cast<jni::String>()->Length() | to get the length of the string).

In MOZ_CHECK_JNI builds, Cast() checks that an instance of the source
class can indeed be cast to the target class.
This commit is contained in:
Jim Chen 2017-03-06 15:32:36 -05:00
Родитель fd2114caa6
Коммит 0d3d8e6fd1
1 изменённых файлов: 11 добавлений и 0 удалений

Просмотреть файл

@ -106,6 +106,17 @@ public:
mInstance, typename T::Context().ClassRef()); mInstance, typename T::Context().ClassRef());
} }
template<class T>
typename T::Ref Cast() const
{
#ifdef MOZ_CHECK_JNI
MOZ_RELEASE_ASSERT(FindEnv()->IsAssignableFrom(
Context<Cls, Type>().ClassRef(),
typename T::Context().ClassRef()));
#endif
return T::Ref::From(*this);
}
bool operator==(const Ref& other) const bool operator==(const Ref& other) const
{ {
// Treat two references of the same object as being the same. // Treat two references of the same object as being the same.