Loading...
Searching...
No Matches
boxing.hpp
Go to the documentation of this file.
#pragma once
#include <jni/class.hpp>
#include <jni/object.hpp>
namespace jni
{
template < class > struct Boxer;
template < class > struct Unboxer;
template < class Unboxed >
decltype(auto) Box(JNIEnv& env, Unboxed&& unboxed)
{
return Boxer<typename std::decay<Unboxed>::type>().Box(env, std::forward<Unboxed>(unboxed));
}
template < class T >
decltype(auto) Unbox(JNIEnv& env, const T& boxed)
{
return Unboxer<typename T::TagType>().Unbox(env, boxed);
}
struct BooleanTag
{
using SuperTag = ObjectTag;
static constexpr auto Name() { return "java/lang/Boolean"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "booleanValue"; }
};
struct CharacterTag
{
using SuperTag = ObjectTag;
static constexpr auto Name() { return "java/lang/Character"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "charValue"; }
};
struct NumberTag
{
using SuperTag = ObjectTag;
static constexpr auto Name() { return "java/lang/Number"; }
};
struct ByteTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Byte"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "byteValue"; }
};
struct ShortTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Short"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "shortValue"; }
};
struct IntegerTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Integer"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "intValue"; }
};
struct LongTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Long"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "longValue"; }
};
struct FloatTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Float"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "floatValue"; }
};
struct DoubleTag
{
using SuperTag = NumberTag;
static constexpr auto Name() { return "java/lang/Double"; }
static constexpr auto BoxStaticMethodName() { return "valueOf"; }
static constexpr auto UnboxMethodName() { return "doubleValue"; }
};
using Boolean = Object<BooleanTag>;
using Character = Object<CharacterTag>;
using Number = Object<NumberTag>;
using Byte = Object<ByteTag>;
using Short = Object<ShortTag>;
using Integer = Object<IntegerTag>;
using Long = Object<LongTag>;
using Float = Object<FloatTag>;
using Double = Object<DoubleTag>;
template < class Tag, class Unboxed >
struct PrimitiveTypeBoxer
{
Local<Object<Tag>> Box(JNIEnv& env, Unboxed unboxed) const
{
static auto& klass = Class<Tag>::Singleton(env);
static auto box = klass.template GetStaticMethod<Object<Tag> (Unboxed)>(env, Tag::BoxStaticMethodName());
return klass.Call(env, box, unboxed);
}
};
template <> struct Boxer< jboolean > : PrimitiveTypeBoxer< BooleanTag , jboolean > {};
template <> struct Boxer< jbyte > : PrimitiveTypeBoxer< ByteTag , jbyte > {};
template <> struct Boxer< jchar > : PrimitiveTypeBoxer< CharacterTag , jchar > {};
template <> struct Boxer< jshort > : PrimitiveTypeBoxer< ShortTag , jshort > {};
template <> struct Boxer< jint > : PrimitiveTypeBoxer< IntegerTag , jint > {};
template <> struct Boxer< jlong > : PrimitiveTypeBoxer< LongTag , jlong > {};
template <> struct Boxer< jfloat > : PrimitiveTypeBoxer< FloatTag , jfloat > {};
template <> struct Boxer< jdouble > : PrimitiveTypeBoxer< DoubleTag , jdouble > {};
template < class Tag, class Unboxed >
struct PrimitiveTypeUnboxer
{
Unboxed Unbox(JNIEnv& env, const Object<Tag>& boxed) const
{
static auto& klass = Class<Tag>::Singleton(env);
static auto unbox = klass.template GetMethod<Unboxed ()>(env, Tag::UnboxMethodName());
return boxed.Call(env, unbox);
}
};
template <> struct Unboxer< BooleanTag > : PrimitiveTypeUnboxer< BooleanTag , jboolean > {};
template <> struct Unboxer< ByteTag > : PrimitiveTypeUnboxer< ByteTag , jbyte > {};
template <> struct Unboxer< CharacterTag > : PrimitiveTypeUnboxer< CharacterTag , jchar > {};
template <> struct Unboxer< ShortTag > : PrimitiveTypeUnboxer< ShortTag , jshort > {};
template <> struct Unboxer< IntegerTag > : PrimitiveTypeUnboxer< IntegerTag , jint > {};
template <> struct Unboxer< LongTag > : PrimitiveTypeUnboxer< LongTag , jlong > {};
template <> struct Unboxer< FloatTag > : PrimitiveTypeUnboxer< FloatTag , jfloat > {};
template <> struct Unboxer< DoubleTag > : PrimitiveTypeUnboxer< DoubleTag , jdouble > {};
template < class Tag >
struct Boxer<jni::Object<Tag>>
{
const Object<Tag>& Box(JNIEnv&, const jni::Object<Tag>& o) const { return o; }
};
template < class Tag >
struct Unboxer
{
const Object<Tag>& Unbox(JNIEnv&, const jni::Object<Tag>& o) const { return o; }
};
}
class.hpp
jni::Class::Singleton
static const Class & Singleton(JNIEnv &env)
Definition class.hpp:101
jni::Object
Definition object.hpp:45
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
Definition advanced_ownership.hpp:6
jni::Float
Object< FloatTag > Float
Definition boxing.hpp:102
jni::Number
Object< NumberTag > Number
Definition boxing.hpp:97
jni::Character
Object< CharacterTag > Character
Definition boxing.hpp:96
jni::Box
decltype(auto) Box(JNIEnv &env, Unboxed &&unboxed)
Definition boxing.hpp:12
jni::Double
Object< DoubleTag > Double
Definition boxing.hpp:103
jni::Long
Object< LongTag > Long
Definition boxing.hpp:101
jni::Boolean
Object< BooleanTag > Boolean
Definition boxing.hpp:95
jni::Integer
Object< IntegerTag > Integer
Definition boxing.hpp:100
jni::Short
Object< ShortTag > Short
Definition boxing.hpp:99
jni::Unbox
decltype(auto) Unbox(JNIEnv &env, const T &boxed)
Definition boxing.hpp:18
jni::Local
Unique< T, DefaultRefDeleter<&JNIEnv::DeleteLocalRef > > Local
Definition unique.hpp:110
jni::Byte
Object< ByteTag > Byte
Definition boxing.hpp:98
object.hpp
jni::BooleanTag
Definition boxing.hpp:25
jni::BooleanTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:29
jni::BooleanTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:28
jni::BooleanTag::Name
static constexpr auto Name()
Definition boxing.hpp:27
jni::BooleanTag::SuperTag
ObjectTag SuperTag
Definition boxing.hpp:26
jni::Boxer< jni::Object< Tag > >::Box
const Object< Tag > & Box(JNIEnv &, const jni::Object< Tag > &o) const
Definition boxing.hpp:151
jni::Boxer
Definition boxing.hpp:8
jni::ByteTag
Definition boxing.hpp:47
jni::ByteTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:50
jni::ByteTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:51
jni::ByteTag::Name
static constexpr auto Name()
Definition boxing.hpp:49
jni::ByteTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:48
jni::CharacterTag
Definition boxing.hpp:33
jni::CharacterTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:36
jni::CharacterTag::Name
static constexpr auto Name()
Definition boxing.hpp:35
jni::CharacterTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:37
jni::CharacterTag::SuperTag
ObjectTag SuperTag
Definition boxing.hpp:34
jni::DoubleTag
Definition boxing.hpp:87
jni::DoubleTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:91
jni::DoubleTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:90
jni::DoubleTag::Name
static constexpr auto Name()
Definition boxing.hpp:89
jni::DoubleTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:88
jni::FloatTag
Definition boxing.hpp:79
jni::FloatTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:83
jni::FloatTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:82
jni::FloatTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:80
jni::FloatTag::Name
static constexpr auto Name()
Definition boxing.hpp:81
jni::IntegerTag
Definition boxing.hpp:63
jni::IntegerTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:66
jni::IntegerTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:67
jni::IntegerTag::Name
static constexpr auto Name()
Definition boxing.hpp:65
jni::IntegerTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:64
jni::LongTag
Definition boxing.hpp:71
jni::LongTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:75
jni::LongTag::Name
static constexpr auto Name()
Definition boxing.hpp:73
jni::LongTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:72
jni::LongTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:74
jni::NumberTag
Definition boxing.hpp:41
jni::NumberTag::Name
static constexpr auto Name()
Definition boxing.hpp:43
jni::NumberTag::SuperTag
ObjectTag SuperTag
Definition boxing.hpp:42
jni::ObjectTag
Definition tagging.hpp:17
jni::PrimitiveTypeBoxer
Definition boxing.hpp:108
jni::PrimitiveTypeBoxer::Box
Local< Object< Tag > > Box(JNIEnv &env, Unboxed unboxed) const
Definition boxing.hpp:109
jni::PrimitiveTypeUnboxer
Definition boxing.hpp:129
jni::PrimitiveTypeUnboxer::Unbox
Unboxed Unbox(JNIEnv &env, const Object< Tag > &boxed) const
Definition boxing.hpp:130
jni::ShortTag
Definition boxing.hpp:55
jni::ShortTag::SuperTag
NumberTag SuperTag
Definition boxing.hpp:56
jni::ShortTag::BoxStaticMethodName
static constexpr auto BoxStaticMethodName()
Definition boxing.hpp:58
jni::ShortTag::UnboxMethodName
static constexpr auto UnboxMethodName()
Definition boxing.hpp:59
jni::ShortTag::Name
static constexpr auto Name()
Definition boxing.hpp:57
jni::Unboxer
Definition boxing.hpp:156
jni::Unboxer::Unbox
const Object< Tag > & Unbox(JNIEnv &, const jni::Object< Tag > &o) const
Definition boxing.hpp:157