lime
Lime is a C++ library implementing Open Whisper System Signal protocol
boxing.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <jni/class.hpp>
4#include <jni/object.hpp>
5
6namespace jni
7 {
8 template < class > struct Boxer;
9 template < class > struct Unboxer;
10
11 template < class Unboxed >
12 decltype(auto) Box(JNIEnv& env, Unboxed&& unboxed)
13 {
14 return Boxer<typename std::decay<Unboxed>::type>().Box(env, std::forward<Unboxed>(unboxed));
15 }
16
17 template < class T >
18 decltype(auto) Unbox(JNIEnv& env, const T& boxed)
19 {
20 return Unboxer<typename T::TagType>().Unbox(env, boxed);
21 }
22
23
25 {
27 static constexpr auto Name() { return "java/lang/Boolean"; }
28 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
29 static constexpr auto UnboxMethodName() { return "booleanValue"; }
30 };
31
33 {
35 static constexpr auto Name() { return "java/lang/Character"; }
36 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
37 static constexpr auto UnboxMethodName() { return "charValue"; }
38 };
39
40 struct NumberTag
41 {
43 static constexpr auto Name() { return "java/lang/Number"; }
44 };
45
46 struct ByteTag
47 {
49 static constexpr auto Name() { return "java/lang/Byte"; }
50 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
51 static constexpr auto UnboxMethodName() { return "byteValue"; }
52 };
53
54 struct ShortTag
55 {
57 static constexpr auto Name() { return "java/lang/Short"; }
58 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
59 static constexpr auto UnboxMethodName() { return "shortValue"; }
60 };
61
63 {
65 static constexpr auto Name() { return "java/lang/Integer"; }
66 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
67 static constexpr auto UnboxMethodName() { return "intValue"; }
68 };
69
70 struct LongTag
71 {
73 static constexpr auto Name() { return "java/lang/Long"; }
74 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
75 static constexpr auto UnboxMethodName() { return "longValue"; }
76 };
77
78 struct FloatTag
79 {
81 static constexpr auto Name() { return "java/lang/Float"; }
82 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
83 static constexpr auto UnboxMethodName() { return "floatValue"; }
84 };
85
86 struct DoubleTag
87 {
89 static constexpr auto Name() { return "java/lang/Double"; }
90 static constexpr auto BoxStaticMethodName() { return "valueOf"; }
91 static constexpr auto UnboxMethodName() { return "doubleValue"; }
92 };
93
94
104
105
106 template < class Tag, class Unboxed >
108 {
109 Local<Object<Tag>> Box(JNIEnv& env, Unboxed unboxed) const
110 {
111 static auto& klass = Class<Tag>::Singleton(env);
112 static auto box = klass.template GetStaticMethod<Object<Tag> (Unboxed)>(env, Tag::BoxStaticMethodName());
113 return klass.Call(env, box, unboxed);
114 }
115 };
116
117 template <> struct Boxer< jboolean > : PrimitiveTypeBoxer< BooleanTag , jboolean > {};
118 template <> struct Boxer< jbyte > : PrimitiveTypeBoxer< ByteTag , jbyte > {};
119 template <> struct Boxer< jchar > : PrimitiveTypeBoxer< CharacterTag , jchar > {};
120 template <> struct Boxer< jshort > : PrimitiveTypeBoxer< ShortTag , jshort > {};
121 template <> struct Boxer< jint > : PrimitiveTypeBoxer< IntegerTag , jint > {};
122 template <> struct Boxer< jlong > : PrimitiveTypeBoxer< LongTag , jlong > {};
123 template <> struct Boxer< jfloat > : PrimitiveTypeBoxer< FloatTag , jfloat > {};
124 template <> struct Boxer< jdouble > : PrimitiveTypeBoxer< DoubleTag , jdouble > {};
125
126
127 template < class Tag, class Unboxed >
129 {
130 Unboxed Unbox(JNIEnv& env, const Object<Tag>& boxed) const
131 {
132 static auto& klass = Class<Tag>::Singleton(env);
133 static auto unbox = klass.template GetMethod<Unboxed ()>(env, Tag::UnboxMethodName());
134 return boxed.Call(env, unbox);
135 }
136 };
137
138 template <> struct Unboxer< BooleanTag > : PrimitiveTypeUnboxer< BooleanTag , jboolean > {};
139 template <> struct Unboxer< ByteTag > : PrimitiveTypeUnboxer< ByteTag , jbyte > {};
140 template <> struct Unboxer< CharacterTag > : PrimitiveTypeUnboxer< CharacterTag , jchar > {};
141 template <> struct Unboxer< ShortTag > : PrimitiveTypeUnboxer< ShortTag , jshort > {};
142 template <> struct Unboxer< IntegerTag > : PrimitiveTypeUnboxer< IntegerTag , jint > {};
143 template <> struct Unboxer< LongTag > : PrimitiveTypeUnboxer< LongTag , jlong > {};
144 template <> struct Unboxer< FloatTag > : PrimitiveTypeUnboxer< FloatTag , jfloat > {};
145 template <> struct Unboxer< DoubleTag > : PrimitiveTypeUnboxer< DoubleTag , jdouble > {};
146
147
148 template < class Tag >
149 struct Boxer<jni::Object<Tag>>
150 {
151 const Object<Tag>& Box(JNIEnv&, const jni::Object<Tag>& o) const { return o; }
152 };
153
154 template < class Tag >
155 struct Unboxer
156 {
157 const Object<Tag>& Unbox(JNIEnv&, const jni::Object<Tag>& o) const { return o; }
158 };
159 }
static const Class & Singleton(JNIEnv &env)
Definition: class.hpp:101
Definition: object.hpp:45
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
Definition: unique.hpp:39
Definition: advanced_ownership.hpp:6
decltype(auto) Box(JNIEnv &env, Unboxed &&unboxed)
Definition: boxing.hpp:12
decltype(auto) Unbox(JNIEnv &env, const T &boxed)
Definition: boxing.hpp:18
Definition: boxing.hpp:25
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:29
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:28
static constexpr auto Name()
Definition: boxing.hpp:27
const Object< Tag > & Box(JNIEnv &, const jni::Object< Tag > &o) const
Definition: boxing.hpp:151
Definition: boxing.hpp:8
Definition: boxing.hpp:47
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:50
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:51
static constexpr auto Name()
Definition: boxing.hpp:49
Definition: boxing.hpp:33
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:36
static constexpr auto Name()
Definition: boxing.hpp:35
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:37
Definition: boxing.hpp:87
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:91
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:90
static constexpr auto Name()
Definition: boxing.hpp:89
Definition: boxing.hpp:79
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:83
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:82
static constexpr auto Name()
Definition: boxing.hpp:81
Definition: boxing.hpp:63
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:66
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:67
static constexpr auto Name()
Definition: boxing.hpp:65
Definition: boxing.hpp:71
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:75
static constexpr auto Name()
Definition: boxing.hpp:73
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:74
Definition: boxing.hpp:41
static constexpr auto Name()
Definition: boxing.hpp:43
Definition: tagging.hpp:17
Definition: boxing.hpp:108
Local< Object< Tag > > Box(JNIEnv &env, Unboxed unboxed) const
Definition: boxing.hpp:109
Definition: boxing.hpp:129
Unboxed Unbox(JNIEnv &env, const Object< Tag > &boxed) const
Definition: boxing.hpp:130
Definition: boxing.hpp:55
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:58
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:59
static constexpr auto Name()
Definition: boxing.hpp:57
Definition: boxing.hpp:156
const Object< Tag > & Unbox(JNIEnv &, const jni::Object< Tag > &o) const
Definition: boxing.hpp:157