Loading...
Searching...
No Matches
object.hpp
Go to the documentation of this file.
#pragma once
#include <jni/functions.hpp>
#include <jni/tagging.hpp>
#include <cstddef>
namespace jni
{
template < class TheTag > class Class;
template < class TheTag, class > class Field;
template < class TheTag, class > class Method;
class ObjectBase
{
protected:
jobject* ptr = nullptr;
explicit ObjectBase(std::nullptr_t = nullptr)
{}
explicit ObjectBase(jobject* p)
: ptr(p)
{}
~ObjectBase() = default;
void reset(jobject* p) { ptr = p; }
public:
explicit operator bool() const { return ptr; }
friend bool operator==(const ObjectBase& a, const ObjectBase& b) { return a.ptr == b.ptr; }
friend bool operator!=(const ObjectBase& a, const ObjectBase& b) { return !( a == b ); }
template < class OtherTag >
bool IsInstanceOf(JNIEnv& env, const Class<OtherTag>& clazz) const
{
return jni::IsInstanceOf(env, ptr, *clazz);
}
};
template < class TheTag = ObjectTag >
class Object : public TagTraits<TheTag>::SuperType
{
public:
using TagType = TheTag;
using SuperType = typename TagTraits<TheTag>::SuperType;
using UntaggedType = typename TagTraits<TheTag>::UntaggedType;
protected:
explicit Object(std::nullptr_t = nullptr)
{}
explicit Object(UntaggedType* p)
: SuperType(p)
{}
Object(const Object&) = delete;
Object& operator=(const Object&) = delete;
public:
UntaggedType* get() const { return reinterpret_cast<UntaggedType*>(this->ptr); }
UntaggedType& operator*() const { return *get(); }
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, get(), field);
}
template < class T >
auto Get(JNIEnv& env, const Field<TagType, T>& field) const
-> std::enable_if_t< !IsPrimitive<T>::value, Local<T> >
{
return Local<T>(env, reinterpret_cast<typename T::UntaggedType*>(GetField<jobject*>(env, get(), 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, get(), field, value);
}
template < class Expected, class Actual >
auto Set(JNIEnv& env, const Field<TagType, Expected>& field, const Actual& value) const
-> std::enable_if_t< !IsPrimitive<Expected>::value
&& std::is_convertible<const Actual&, const Expected&>::value >
{
SetField<jobject*>(env, get(), field, value.get());
}
template < class R, class... ExpectedArgs, class... ActualArgs >
auto Call(JNIEnv& env, const Method<TagType, R (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< IsPrimitive<R>::value
&& Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value, R >
{
return CallMethod<R>(env, get(), method, Untag(args)...);
}
template < class R, class... ExpectedArgs, class... ActualArgs >
auto Call(JNIEnv& env, const Method<TagType, R (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< !IsPrimitive<R>::value
&& !std::is_void<R>::value
&& Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value, Local<R> >
{
return Local<R>(env, reinterpret_cast<typename R::UntaggedType*>(CallMethod<jobject*>(env, get(), method, Untag(args)...)));
}
template < class... ExpectedArgs, class... ActualArgs >
auto Call(JNIEnv& env, const Method<TagType, void (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value >
{
CallMethod<void>(env, get(), method, Untag(args)...);
}
template < class R, class... ExpectedArgs, class... ActualArgs >
auto CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, R (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< IsPrimitive<R>::value
&& Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value, R >
{
return CallNonvirtualMethod<R>(env, get(), clazz, method, Untag(args)...);
}
template < class R, class... ExpectedArgs, class... ActualArgs >
auto CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, R (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< !IsPrimitive<R>::value
&& !std::is_void<R>::value
&& Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value, Local<R> >
{
return Local<R>(env, reinterpret_cast<typename R::UntaggedType*>(CallNonvirtualMethod<jobject*>(env, get(), clazz, method, Untag(args)...)));
}
template < class... ExpectedArgs, class... ActualArgs >
auto CallNonvirtual(JNIEnv& env, const Class<TagType>& clazz, const Method<TagType, void (ExpectedArgs...)>& method, const ActualArgs&... args) const
-> std::enable_if_t< Conjunction<std::is_convertible<const ActualArgs&, const ExpectedArgs&>...>::value >
{
CallNonvirtualMethod<void>(env, get(), clazz, method, Untag(args)...);
}
};
template < class OutTagType, class T >
Local<Object<OutTagType>> Cast(JNIEnv& env, const Class<OutTagType>& clazz, const T& object)
{
if (!object.IsInstanceOf(env, clazz))
{
ThrowNew(env, FindClass(env, "java/lang/ClassCastException"));
}
return Local<Object<OutTagType>>(env, reinterpret_cast<typename Object<OutTagType>::UntaggedType*>(NewLocal(env, object).release()));
}
}
jni::Class
Definition class.hpp:18
jni::Field
Definition field.hpp:13
jni::Method
Definition class.hpp:13
jni::ObjectBase::ObjectBase
ObjectBase(std::nullptr_t=nullptr)
Definition object.hpp:19
jni::ObjectBase::reset
void reset(jobject *p)
Definition object.hpp:28
jni::ObjectBase::ptr
jobject * ptr
Definition object.hpp:17
jni::ObjectBase::ObjectBase
ObjectBase(jobject *p)
Definition object.hpp:22
jni::ObjectBase::IsInstanceOf
bool IsInstanceOf(JNIEnv &env, const Class< OtherTag > &clazz) const
Definition object.hpp:37
jni::ObjectBase::~ObjectBase
~ObjectBase()=default
jni::ObjectBase::operator==
friend bool operator==(const ObjectBase &a, const ObjectBase &b)
Definition object.hpp:33
jni::ObjectBase::operator!=
friend bool operator!=(const ObjectBase &a, const ObjectBase &b)
Definition object.hpp:34
jni::Object::Object
Object(const Object &)=delete
jni::Object::get
UntaggedType * get() const
Definition object.hpp:63
jni::Object::Object
Object(std::nullptr_t=nullptr)
Definition object.hpp:52
jni::Object::operator*
UntaggedType & operator*() const
Definition object.hpp:64
jni::Object::Get
auto Get(JNIEnv &env, const Field< TagType, T > &field) const -> std::enable_if_t< !IsPrimitive< T >::value, Local< T > >
Definition object.hpp:74
jni::Object::CallNonvirtual
auto CallNonvirtual(JNIEnv &env, const Class< TagType > &clazz, const Method< TagType, R(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< !IsPrimitive< R >::value &&!std::is_void< R >::value &&Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value, Local< R > >
Definition object.hpp:128
jni::Object::Call
auto Call(JNIEnv &env, const Method< TagType, void(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value >
Definition object.hpp:113
jni::Object< ArrayTag< E > >::TagType
ArrayTag< E > TagType
Definition object.hpp:47
jni::Object::Set
auto Set(JNIEnv &env, const Field< TagType, Expected > &field, const Actual &value) const -> std::enable_if_t< !IsPrimitive< Expected >::value &&std::is_convertible< const Actual &, const Expected & >::value >
Definition object.hpp:88
jni::Object::SuperType
typename TagTraits< TheTag >::SuperType SuperType
Definition object.hpp:48
jni::Object::CallNonvirtual
auto CallNonvirtual(JNIEnv &env, const Class< TagType > &clazz, const Method< TagType, void(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value >
Definition object.hpp:137
jni::Object::Object
Object(UntaggedType *p)
Definition object.hpp:55
jni::Object::Call
auto Call(JNIEnv &env, const Method< TagType, R(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< !IsPrimitive< R >::value &&!std::is_void< R >::value &&Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value, Local< R > >
Definition object.hpp:104
jni::Object::Call
auto Call(JNIEnv &env, const Method< TagType, R(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< IsPrimitive< R >::value &&Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value, R >
Definition object.hpp:96
jni::Object::Get
auto Get(JNIEnv &env, const Field< TagType, T > &field) const -> std::enable_if_t< IsPrimitive< T >::value, T >
Definition object.hpp:67
jni::Object::Set
auto Set(JNIEnv &env, const Field< TagType, T > &field, T value) const -> std::enable_if_t< IsPrimitive< T >::value >
Definition object.hpp:81
jni::Object< ArrayTag< E > >::UntaggedType
typename TagTraits< ArrayTag< E > >::UntaggedType UntaggedType
Definition object.hpp:49
jni::Object::CallNonvirtual
auto CallNonvirtual(JNIEnv &env, const Class< TagType > &clazz, const Method< TagType, R(ExpectedArgs...)> &method, const ActualArgs &... args) const -> std::enable_if_t< IsPrimitive< R >::value &&Conjunction< std::is_convertible< const ActualArgs &, const ExpectedArgs & >... >::value, R >
Definition object.hpp:120
jni::Object::operator=
Object & operator=(const Object &)=delete
functions.hpp
jni
Definition advanced_ownership.hpp:6
jni::SetField
void SetField(JNIEnv &env, jobject *obj, jfieldID &field, T value)
Definition functions.hpp:313
jni::Cast
Local< Object< OutTagType > > Cast(JNIEnv &env, const Class< OutTagType > &clazz, const T &object)
Definition object.hpp:145
jni::FindClass
jclass & FindClass(JNIEnv &env, const char *name)
Definition functions.hpp:34
jni::CallNonvirtualMethod
std::enable_if_t<!std::is_void< R >::value, R > CallNonvirtualMethod(JNIEnv &env, jobject *obj, jclass &clazz, jmethodID &method, Args &&... args)
Definition functions.hpp:284
jni::ThrowNew
void ThrowNew(JNIEnv &env, jclass &clazz, const char *msg=nullptr)
Definition functions.hpp:84
jni::NewLocal
auto NewLocal(JNIEnv &env, const T &t)
Definition unique.hpp:202
jni::CallMethod
std::enable_if_t<!std::is_void< R >::value, R > CallMethod(JNIEnv &env, jobject *obj, jmethodID &method, Args &&... args)
Definition functions.hpp:268
jni::IsInstanceOf
bool IsInstanceOf(JNIEnv &env, jobject *obj, jclass &clazz)
Definition functions.hpp:253
jni::Local
Unique< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef > > Local
Definition unique.hpp:110
jni::Untag
auto Untag(T primitive) -> std::enable_if_t< IsPrimitive< T >::value, T >
Definition tagging.hpp:116
jni::GetField
T GetField(JNIEnv &env, jobject *obj, jfieldID &field)
Definition functions.hpp:306
jni::Conjunction
Definition traits.hpp:28
jni::TagTraits
Definition tagging.hpp:52
jni::TagTraits::UntaggedType
jobject UntaggedType
Definition tagging.hpp:54
jni::TagTraits::SuperType
Object< typename SuperTag< Tag >::Type > SuperType
Definition tagging.hpp:53
jni::jobject
Definition types.hpp:31
tagging.hpp