Skip to content

Commit 92af986

Browse files
committed
Array: avoid using arrayset and array_ptr_set
1 parent 7308b40 commit 92af986

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

include/jlcxx/array.hpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
namespace jlcxx
88
{
99

10+
/// wrapper for julia jl_array_data for different julia versions
11+
template <typename T>
12+
T* jlcxx_array_data(jl_array_t* arr) {
13+
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
14+
return jl_array_data(arr, T);
15+
#else
16+
return static_cast<T*>(jl_array_data(arr));
17+
#endif
18+
}
19+
1020
template<typename PointedT, typename CppT>
1121
struct ValueExtractor
1222
{
@@ -123,11 +133,10 @@ class Array
123133
JL_GC_PUSH1(&m_array);
124134
const size_t pos = jl_array_len(m_array);
125135
jl_array_grow_end(m_array, 1);
126-
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
127-
jl_array_ptr_set(m_array, pos, box<ValueT>(val));
128-
#else
129-
jl_arrayset(m_array, box<ValueT>(val), pos);
130-
#endif
136+
jl_value_t** data = jlcxx_array_data<jl_value_t*>(m_array);
137+
jl_value_t* jval = box<ValueT>(val);
138+
data[pos] = jval;
139+
jl_gc_wb(m_array, jval);
131140
JL_GC_POP();
132141
}
133142

@@ -171,17 +180,6 @@ class ArrayRef
171180
public:
172181
using julia_t = typename detail::ArrayElementType<ValueT>::type;
173182

174-
private:
175-
/// wrapper for julia jl_array_data for different julia versions
176-
static julia_t* jlcxx_array_data(jl_array_t* arr) {
177-
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
178-
return jl_array_data(arr, julia_t);
179-
#else
180-
return static_cast<julia_t*>(jl_array_data(arr));
181-
#endif
182-
}
183-
184-
public:
185183
ArrayRef(jl_array_t* arr) : m_array(arr)
186184
{
187185
assert(wrapped() != nullptr);
@@ -205,22 +203,22 @@ class ArrayRef
205203

206204
iterator begin()
207205
{
208-
return iterator(jlcxx_array_data(wrapped()));
206+
return iterator(jlcxx_array_data<julia_t>(wrapped()));
209207
}
210208

211209
const_iterator begin() const
212210
{
213-
return const_iterator(jlcxx_array_data(wrapped()));
211+
return const_iterator(jlcxx_array_data<julia_t>(wrapped()));
214212
}
215213

216214
iterator end()
217215
{
218-
return iterator(jlcxx_array_data(wrapped()) + jl_array_len(wrapped()));
216+
return iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
219217
}
220218

221219
const_iterator end() const
222220
{
223-
return const_iterator(jlcxx_array_data(wrapped()) + jl_array_len(wrapped()));
221+
return const_iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
224222
}
225223

226224
void push_back(const ValueT& val)
@@ -237,12 +235,12 @@ class ArrayRef
237235

238236
const julia_t* data() const
239237
{
240-
return jlcxx_array_data(wrapped());
238+
return jlcxx_array_data<julia_t>(wrapped());
241239
}
242240

243241
julia_t* data()
244242
{
245-
return jlcxx_array_data(wrapped());
243+
return jlcxx_array_data<julia_t>(wrapped());
246244
}
247245

248246
std::size_t size() const

0 commit comments

Comments
 (0)