lime
Lime is a C++ library implementing Open Whisper System Signal protocol
array.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <jni/functions.hpp>
4 #include <jni/object.hpp>
5 #include <jni/tagging.hpp>
6 #include <jni/make.hpp>
7 
8 namespace jni
9  {
10  template < class E, class Enable >
11  class Array;
12 
13  template < class E >
14  class Array< E, std::enable_if_t<IsPrimitive<E>::value> > : public Object< ArrayTag<E> >
15  {
16  public:
19  using ElementType = E;
20 
21  protected:
22  explicit Array(std::nullptr_t = nullptr)
23  {}
24 
25  explicit Array(UntaggedType* p)
26  : SuperType(p)
27  {}
28 
29  Array(const Array&) = delete;
30  Array& operator=(const Array&) = delete;
31 
32  public:
33  jsize Length(JNIEnv& env) const
34  {
35  return GetArrayLength(env, SafeDereference(env, this->get()));
36  }
37 
38  ElementType Get(JNIEnv& env, jsize index) const
39  {
40  ElementType e;
41  GetArrayRegion(env, SafeDereference(env, this->get()), index, 1, &e);
42  return e;
43  }
44 
45  void Set(JNIEnv& env, jsize index, const ElementType& value)
46  {
47  SetArrayRegion(env, SafeDereference(env, this->get()), index, 1, &value);
48  }
49 
50  template < class Array >
51  void GetRegion(JNIEnv& env, jsize start, Array& buf) const
52  {
53  GetArrayRegion(env, SafeDereference(env, this->get()), start, buf);
54  }
55 
56  template < class Array >
57  void SetRegion(JNIEnv& env, jsize start, const Array& buf)
58  {
59  SetArrayRegion(env, SafeDereference(env, this->get()), start, buf);
60  }
61 
62  static Local<Array<E>> New(JNIEnv& env, jsize length)
63  {
64  return Local<Array<E>>(env, &NewArray<E>(env, length));
65  }
66  };
67 
68  template < class TheTag >
69  class Array< Object<TheTag> > : public Object< ArrayTag<Object<TheTag>> >
70  {
71  public:
74  using TagType = TheTag;
77 
78  protected:
79  explicit Array(std::nullptr_t = nullptr)
80  {}
81 
82  explicit Array(UntaggedType* p)
83  : SuperType(p)
84  {}
85 
86  Array(const Array&) = delete;
87  Array& operator=(const Array&) = delete;
88 
89  public:
90  jsize Length(JNIEnv& env) const
91  {
92  return GetArrayLength(env, SafeDereference(env, this->get()));
93  }
94 
95  Local<ElementType> Get(JNIEnv& env, jsize index) const
96  {
97  return Local<ElementType>(env,
98  reinterpret_cast<UntaggedElementType*>(
99  GetObjectArrayElement(env, SafeDereference(env, this->get()), index)));
100  }
101 
102  void Set(JNIEnv& env, jsize index, const ElementType& value)
103  {
104  SetObjectArrayElement(env, SafeDereference(env, this->get()), index, Untag(value));
105  }
106 
107  static Local<Array<Object<TheTag>>> New(JNIEnv& env, jsize length, const Object<TheTag>* initialElement = nullptr)
108  {
109  return Local<Array<Object<TheTag>>>(env, &NewObjectArray(env, length, *Class<TheTag>::Singleton(env), initialElement ? initialElement->get() : nullptr));
110  }
111  };
112 
113  template < class T >
114  std::vector<T> MakeAnything(ThingToMake<std::vector<T>>, JNIEnv& env, const Array<T>& array)
115  {
116  NullCheck(env, array.get());
117  std::vector<T> result(GetArrayLength(env, *array));
118  GetArrayRegion(env, *array, 0, result);
119  return result;
120  }
121 
122  template < class T >
123  Local<Array<T>> MakeAnything(ThingToMake<Array<T>>, JNIEnv& env, const std::vector<T>& array)
124  {
125  Local<Array<T>> result = Local<Array<T>>(env, &NewArray<T>(env, array.size()));
126  SetArrayRegion(env, *result, 0, array);
127  return result;
128  }
129 
130  inline
131  std::string MakeAnything(ThingToMake<std::string>, JNIEnv& env, const Array<jbyte>& array)
132  {
133  NullCheck(env, array.get());
134  std::string result;
135  result.resize(GetArrayLength(env, *array));
136  GetArrayRegion(env, *array, 0, result.size(), reinterpret_cast<jbyte*>(&result[0]));
137  return result;
138  }
139 
140  inline
141  Local<Array<jbyte>> MakeAnything(ThingToMake<Array<jbyte>>, JNIEnv& env, const std::string& string)
142  {
143  Local<Array<jbyte>> result(env, &NewArray<jbyte>(env, string.size()));
144  SetArrayRegion(env, *result, 0, string.size(), reinterpret_cast<const jbyte*>(&string[0]));
145  return result;
146  }
147  }
Array(std::nullptr_t=nullptr)
Definition: array.hpp:22
decltype(Untag(std::declval< T >())) UntaggedType
Definition: tagging.hpp:130
UntaggedType * get() const
Definition: object.hpp:63
T & SafeDereference(JNIEnv &env, T *ptr, const char *message=nullptr)
Definition: npe.hpp:19
jsize Length(JNIEnv &env) const
Definition: array.hpp:33
void SetArrayRegion(JNIEnv &env, jarray< T > &array, jsize start, jsize len, const T *buf)
Definition: functions.hpp:539
void SetObjectArrayElement(JNIEnv &env, jarray< jobject > &array, jsize index, jobject *value)
Definition: functions.hpp:565
void GetArrayRegion(JNIEnv &env, jarray< T > &array, jsize start, jsize len, T *buf)
Definition: functions.hpp:525
Definition: array.hpp:11
Definition: errors.hpp:9
jarray< jobject > & NewObjectArray(JNIEnv &env, jsize length, jclass &elementClass, jobject *initialElement=nullptr)
Definition: functions.hpp:553
void GetRegion(JNIEnv &env, jsize start, Array &buf) const
Definition: array.hpp:51
jobject * GetObjectArrayElement(JNIEnv &env, jarray< jobject > &array, jsize index)
Definition: functions.hpp:559
Definition: object.hpp:44
static Local< Array< Object< TheTag > > > New(JNIEnv &env, jsize length, const Object< TheTag > *initialElement=nullptr)
Definition: array.hpp:107
Definition: advanced_ownership.hpp:5
void Set(JNIEnv &env, jsize index, const ElementType &value)
Definition: array.hpp:102
std::size_t jsize
Definition: types.hpp:28
typename SuperType::UntaggedType UntaggedType
Definition: array.hpp:73
auto Untag(T primitive) -> std::enable_if_t< IsPrimitive< T >::value, T >
Definition: tagging.hpp:116
void SetRegion(JNIEnv &env, jsize start, const Array &buf)
Definition: array.hpp:57
typename ElementType::UntaggedType UntaggedElementType
Definition: array.hpp:76
Definition: class.hpp:17
Definition: unique.hpp:38
TheTag TagType
Definition: array.hpp:74
Local< ElementType > Get(JNIEnv &env, jsize index) const
Definition: array.hpp:95
static Local< Array< E > > New(JNIEnv &env, jsize length)
Definition: array.hpp:62
jsize GetArrayLength(JNIEnv &env, jarray< E > &array)
Definition: functions.hpp:465
typename SuperType::UntaggedType UntaggedType
Definition: array.hpp:18
Definition: make.hpp:5
std::vector< T > MakeAnything(ThingToMake< std::vector< T >>, JNIEnv &env, const Array< T > &array)
Definition: array.hpp:114
ElementType Get(JNIEnv &env, jsize index) const
Definition: array.hpp:38
Array(UntaggedType *p)
Definition: array.hpp:82
void Set(JNIEnv &env, jsize index, const ElementType &value)
Definition: array.hpp:45
Array(std::nullptr_t=nullptr)
Definition: array.hpp:79
Array(UntaggedType *p)
Definition: array.hpp:25
jsize Length(JNIEnv &env) const
Definition: array.hpp:90
void NullCheck(JNIEnv &env, T *ptr, const char *message=nullptr)
Definition: npe.hpp:13