-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharrays.h
148 lines (119 loc) · 3.52 KB
/
arrays.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#pragma once
#include "class_wrapper.h"
#include "jni_types.h"
namespace jnipp::java::array_extractors {
namespace detail {
template<return_type T>
inline auto get_element(::jarray, ::jsize)
{
}
template<>
inline auto get_element<return_type::object_>(::jarray instance, ::jsize i)
{
return GetJNI()->GetObjectArrayElement(
reinterpret_cast<jobjectArray>(instance), i);
}
#define DEFINE_GET_ELEMENT(JAVA_TYPE, JAVA_NAME, RETURN_TYPE) \
template<> \
inline auto get_element<RETURN_TYPE>(::jarray instance, ::jsize i) \
{ \
JAVA_TYPE out; \
GetJNI()->Get##JAVA_NAME##ArrayRegion( \
reinterpret_cast<JAVA_TYPE##Array>(instance), i, 1, &out); \
return out; \
}
DEFINE_GET_ELEMENT(::jboolean, Boolean, return_type::bool_)
DEFINE_GET_ELEMENT(::jbyte, Byte, return_type::byte_)
DEFINE_GET_ELEMENT(::jchar, Char, return_type::char_)
DEFINE_GET_ELEMENT(::jshort, Short, return_type::short_)
DEFINE_GET_ELEMENT(::jint, Int, return_type::int_)
DEFINE_GET_ELEMENT(::jlong, Long, return_type::long_)
DEFINE_GET_ELEMENT(::jfloat, Float, return_type::float_)
DEFINE_GET_ELEMENT(::jdouble, Double, return_type::double_)
#undef DEFINE_GET_ELEMENT
} // namespace detail
template<return_type T>
struct extract_type
{
extract_type(java::array array)
: ref(array)
{
}
jlong length() const
{
return ref.length();
}
auto operator[](jsize index)
{
if(index >= length())
throw std::out_of_range(
std::to_string(index) + " >= " + std::to_string(length()));
if constexpr(T == return_type::object_)
return wrapping::jobject(java::object{
ref.value_class,
GetJNI()->GetObjectArrayElement(
reinterpret_cast<jobjectArray>(ref.instance), index),
});
else if constexpr(T != return_type::object_)
return detail::get_element<T>(ref.instance, index);
else
return java::value();
}
java::array ref;
};
template<return_type T>
struct container
{
container(java::array arrayObject)
: m_extractor(arrayObject)
, m_end(m_extractor.length())
{
}
struct iterator
{
iterator(container<T>& container, jsize idx)
: m_ref(container)
, m_idx(idx)
{
}
iterator(container<T>& container)
: m_ref(container)
, m_idx(m_ref.m_end)
{
}
iterator& operator++()
{
if(m_idx >= m_ref.m_end)
throw std::out_of_range("no more elements");
m_idx++;
return *this;
}
auto operator*()
{
return m_ref.m_extractor[m_idx];
}
bool operator==(iterator const& other) const
{
return other.m_idx == m_idx;
}
bool operator!=(iterator const& other) const
{
return other.m_idx != m_idx;
}
private:
container<T> m_ref;
jsize m_idx;
};
iterator begin()
{
return iterator(*this, 0);
}
iterator end()
{
return iterator(*this);
}
private:
extract_type<T> m_extractor;
jsize m_end;
};
} // namespace jnipp::java::array_extractors