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 
6 namespace 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 
24  struct BooleanTag
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 
32  struct CharacterTag
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 
62  struct IntegerTag
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 constexpr auto UnboxMethodName()
Definition: boxing.hpp:83
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:51
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
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:58
static constexpr auto Name()
Definition: boxing.hpp:73
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:90
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:36
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:29
const Object< Tag > & Unbox(JNIEnv &, const jni::Object< Tag > &o) const
Definition: boxing.hpp:157
static constexpr auto Name()
Definition: boxing.hpp:89
static constexpr auto Name()
Definition: boxing.hpp:65
const Object< Tag > & Box(JNIEnv &, const jni::Object< Tag > &o) const
Definition: boxing.hpp:151
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:37
static constexpr auto Name()
Definition: boxing.hpp:43
Definition: object.hpp:44
static constexpr auto Name()
Definition: boxing.hpp:49
Definition: advanced_ownership.hpp:5
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:59
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:50
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:28
Definition: boxing.hpp:128
decltype(auto) Unbox(JNIEnv &env, const T &boxed)
Definition: boxing.hpp:18
Definition: tagging.hpp:16
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:91
Local< Object< Tag > > Box(JNIEnv &env, Unboxed unboxed) const
Definition: boxing.hpp:109
static const Class & Singleton(JNIEnv &env)
Definition: class.hpp:101
Definition: boxing.hpp:86
Definition: boxing.hpp:54
static constexpr auto Name()
Definition: boxing.hpp:81
Definition: boxing.hpp:70
static constexpr auto Name()
Definition: boxing.hpp:57
Definition: boxing.hpp:78
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:66
Definition: unique.hpp:38
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:75
Unboxed Unbox(JNIEnv &env, const Object< Tag > &boxed) const
Definition: boxing.hpp:130
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:82
Definition: boxing.hpp:40
static constexpr auto Name()
Definition: boxing.hpp:27
Definition: boxing.hpp:24
Definition: boxing.hpp:107
Definition: boxing.hpp:62
static constexpr auto UnboxMethodName()
Definition: boxing.hpp:67
Definition: boxing.hpp:46
decltype(auto) Box(JNIEnv &env, Unboxed &&unboxed)
Definition: boxing.hpp:12
static constexpr auto BoxStaticMethodName()
Definition: boxing.hpp:74
Definition: boxing.hpp:32
Definition: boxing.hpp:8
Definition: boxing.hpp:9
static constexpr auto Name()
Definition: boxing.hpp:35