Skip to content

Commit

Permalink
Array: avoid using arrayset and array_ptr_set
Browse files Browse the repository at this point in the history
  • Loading branch information
benlorenz committed Oct 29, 2023
1 parent 7308b40 commit 92af986
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions include/jlcxx/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
namespace jlcxx
{

/// wrapper for julia jl_array_data for different julia versions
template <typename T>
T* jlcxx_array_data(jl_array_t* arr) {
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
return jl_array_data(arr, T);
#else
return static_cast<T*>(jl_array_data(arr));
#endif
}

template<typename PointedT, typename CppT>
struct ValueExtractor
{
Expand Down Expand Up @@ -123,11 +133,10 @@ class Array
JL_GC_PUSH1(&m_array);
const size_t pos = jl_array_len(m_array);
jl_array_grow_end(m_array, 1);
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
jl_array_ptr_set(m_array, pos, box<ValueT>(val));
#else
jl_arrayset(m_array, box<ValueT>(val), pos);
#endif
jl_value_t** data = jlcxx_array_data<jl_value_t*>(m_array);
jl_value_t* jval = box<ValueT>(val);
data[pos] = jval;
jl_gc_wb(m_array, jval);
JL_GC_POP();
}

Expand Down Expand Up @@ -171,17 +180,6 @@ class ArrayRef
public:
using julia_t = typename detail::ArrayElementType<ValueT>::type;

private:
/// wrapper for julia jl_array_data for different julia versions
static julia_t* jlcxx_array_data(jl_array_t* arr) {
#if (JULIA_VERSION_MAJOR * 100 + JULIA_VERSION_MINOR) >= 111
return jl_array_data(arr, julia_t);
#else
return static_cast<julia_t*>(jl_array_data(arr));
#endif
}

public:
ArrayRef(jl_array_t* arr) : m_array(arr)
{
assert(wrapped() != nullptr);
Expand All @@ -205,22 +203,22 @@ class ArrayRef

iterator begin()
{
return iterator(jlcxx_array_data(wrapped()));
return iterator(jlcxx_array_data<julia_t>(wrapped()));
}

const_iterator begin() const
{
return const_iterator(jlcxx_array_data(wrapped()));
return const_iterator(jlcxx_array_data<julia_t>(wrapped()));
}

iterator end()
{
return iterator(jlcxx_array_data(wrapped()) + jl_array_len(wrapped()));
return iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
}

const_iterator end() const
{
return const_iterator(jlcxx_array_data(wrapped()) + jl_array_len(wrapped()));
return const_iterator(jlcxx_array_data<julia_t>(wrapped()) + jl_array_len(wrapped()));
}

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

const julia_t* data() const
{
return jlcxx_array_data(wrapped());
return jlcxx_array_data<julia_t>(wrapped());
}

julia_t* data()
{
return jlcxx_array_data(wrapped());
return jlcxx_array_data<julia_t>(wrapped());
}

std::size_t size() const
Expand Down

0 comments on commit 92af986

Please sign in to comment.