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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
// This file is a modified array.hpp from https://github.com/JuliaInterop/libcxxwrap-julia
// required for the hack that allows automated conversion of OpenCV types.
// Shouldn't be needed once CxxWrap gets inbuilt support
// Here is the original copyright and the license:
/*
==
Copyright (c) 2015: Bart Janssens.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==
*/
#ifndef JLCXX_ARRAY_HPP
#define JLCXX_ARRAY_HPP
#include "jlcxx/type_conversion.hpp"
#include "jlcxx/tuple.hpp"
namespace jlcxx
{
template<typename PointedT, typename CppT>
struct ValueExtractor
{
inline CppT operator()(PointedT* p)
{
return convert_to_cpp<CppT>(*p);
}
};
template<typename PointedT>
struct ValueExtractor<PointedT, PointedT>
{
inline PointedT& operator()(PointedT* p)
{
return *p;
}
};
template<typename PointedT, typename CppT>
class array_iterator_base : public std::iterator<std::random_access_iterator_tag, CppT>
{
private:
PointedT* m_ptr;
public:
array_iterator_base() : m_ptr(nullptr)
{
}
explicit array_iterator_base(PointedT* p) : m_ptr(p)
{
}
template <class OtherPointedT, class OtherCppT>
array_iterator_base(array_iterator_base<OtherPointedT, OtherCppT> const& other) : m_ptr(other.m_ptr) {}
auto operator*() -> decltype(ValueExtractor<PointedT,CppT>()(m_ptr))
{
return ValueExtractor<PointedT,CppT>()(m_ptr);
}
array_iterator_base<PointedT, CppT>& operator++()
{
++m_ptr;
return *this;
}
array_iterator_base<PointedT, CppT>& operator--()
{
--m_ptr;
return *this;
}
array_iterator_base<PointedT, CppT>& operator+=(std::ptrdiff_t n)
{
m_ptr += n;
return *this;
}
array_iterator_base<PointedT, CppT>& operator-=(std::ptrdiff_t n)
{
m_ptr -= n;
return *this;
}
PointedT* ptr() const
{
return m_ptr;
}
};
/// Wrap a Julia 1D array in a C++ class. Array is allocated on the C++ side
template<typename ValueT>
class Array
{
public:
Array(const size_t n = 0)
{
jl_value_t* array_type = apply_array_type(julia_type<ValueT>(), 1);
m_array = jl_alloc_array_1d(array_type, n);
}
Array(jl_datatype_t* applied_type, const size_t n = 0)
{
jl_value_t* array_type = apply_array_type(applied_type, 1);
m_array = jl_alloc_array_1d(array_type, n);
}
/// Append an element to the end of the list
template<typename VT>
void push_back(VT&& val)
{
JL_GC_PUSH1(&m_array);
const size_t pos = jl_array_len(m_array);
jl_array_grow_end(m_array, 1);
jl_arrayset(m_array, box<ValueT>(val), pos);
JL_GC_POP();
}
/// Access to the wrapped array
jl_array_t* wrapped()
{
return m_array;
}
// access to the pointer for GC macros
jl_array_t** gc_pointer()
{
return &m_array;
}
private:
jl_array_t* m_array;
};
namespace detail
{
template<typename T, typename TraitT=mapping_trait<T>>
struct ArrayElementType
{
using type = static_julia_type<T>;
};
template<typename T>
struct ArrayElementType<T,WrappedPtrTrait>
{
using type = T;
};
}
/// Reference a Julia array in an STL-compatible wrapper
template<typename ValueT, int Dim = 1>
class ArrayRef
{
public:
using julia_t = typename detail::ArrayElementType<ValueT>::type;
ArrayRef(jl_array_t* arr) : m_array(arr)
{
assert(wrapped() != nullptr);
}
/// Convert from existing C-array (memory owned by C++)
template<typename... SizesT>
ArrayRef(julia_t* ptr, const SizesT... sizes);
/// Convert from existing C-array, explicitly setting Julia ownership
template<typename... SizesT>
ArrayRef(const bool julia_owned, julia_t* ptr, const SizesT... sizes);
typedef array_iterator_base<julia_t, ValueT> iterator;
typedef array_iterator_base<julia_t const, ValueT const> const_iterator;
inline jl_array_t* wrapped() const
{
return m_array;
}
iterator begin()
{
return iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
}
const_iterator begin() const
{
return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
}
iterator end()
{
return iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
}
const_iterator end() const
{
return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
}
void push_back(const ValueT& val)
{
static_assert(Dim == 1, "ArrayRef::push_back is only for 1D ArrayRef");
static_assert(std::is_same<julia_t,ValueT>::value, "ArrayRef::push_back is only for arrays of fundamental types");
jl_array_t* arr_ptr = wrapped();
JL_GC_PUSH1(&arr_ptr);
const size_t pos = size();
jl_array_grow_end(arr_ptr, 1);
jl_arrayset(arr_ptr, box<ValueT>(val), pos);
JL_GC_POP();
}
const julia_t* data() const
{
return (julia_t*)jl_array_data(wrapped());
}
julia_t* data()
{
return (julia_t*)jl_array_data(wrapped());
}
std::size_t size() const
{
return jl_array_len(wrapped());
}
ValueT& operator[](const std::size_t i)
{
if constexpr(std::is_same<julia_t, ValueT>::value)
{
return data()[i];
}
else if constexpr(std::is_same<julia_t, static_julia_type<ValueT>>::value && !std::is_same<julia_t, WrappedCppPtr>::value)
{
return *reinterpret_cast<ValueT*>(&data()[i]);
}
else
{
return *extract_pointer_nonull<ValueT>(data()[i]);
}
}
const ValueT& operator[](const std::size_t i) const
{
if constexpr(std::is_same<julia_t, ValueT>::value)
{
return data()[i];
}
else if constexpr(std::is_same<julia_t, static_julia_type<ValueT>>::value && !std::is_same<julia_t, WrappedCppPtr>::value)
{
return *reinterpret_cast<ValueT*>(&data()[i]);
}
else
{
return *extract_pointer_nonull<ValueT>(data()[i]);
}
}
jl_array_t* m_array;
};
// Conversions
template<typename T, int Dim, typename SubTraitT>
struct static_type_mapping<ArrayRef<T, Dim>, CxxWrappedTrait<SubTraitT>>
{
typedef jl_array_t* type;
};
namespace detail
{
template<typename T, typename TraitT=mapping_trait<T>>
struct PackedArrayType
{
static jl_datatype_t* type()
{
return julia_type<T>();
}
};
template<typename T>
struct PackedArrayType<T*, WrappedPtrTrait>
{
static jl_datatype_t* type()
{
return (jl_datatype_t*)apply_type((jl_value_t*)jlcxx::julia_type("Ptr"), jl_svec1(julia_base_type<T>()));
}
};
template<typename T, typename SubTraitT>
struct PackedArrayType<T,CxxWrappedTrait<SubTraitT>>
{
static jl_datatype_t* type()
{
create_if_not_exists<T&>();
return julia_type<T&>();
}
};
}
template<typename T, int Dim>
struct julia_type_factory<ArrayRef<T, Dim>>
{
static inline jl_datatype_t* julia_type()
{
create_if_not_exists<T>();
return (jl_datatype_t*)apply_array_type(detail::PackedArrayType<T>::type(), Dim);
}
};
template<typename ValueT, typename... SizesT>
jl_array_t* wrap_array(const bool julia_owned, ValueT* c_ptr, const SizesT... sizes)
{
jl_datatype_t* dt = julia_type<ArrayRef<ValueT, sizeof...(SizesT)>>();
jl_value_t *dims = nullptr;
JL_GC_PUSH1(&dims);
dims = convert_to_julia(std::make_tuple(static_cast<cxxint_t>(sizes)...));
jl_array_t* result = jl_ptr_to_array((jl_value_t*)dt, c_ptr, dims, julia_owned);
JL_GC_POP();
return result;
}
template<typename ValueT, int Dim>
template<typename... SizesT>
ArrayRef<ValueT, Dim>::ArrayRef(julia_t* c_ptr, const SizesT... sizes) : m_array(wrap_array(false, c_ptr, sizes...))
{
}
template<typename ValueT, int Dim>
template<typename... SizesT>
ArrayRef<ValueT, Dim>::ArrayRef(const bool julia_owned, julia_t* c_ptr, const SizesT... sizes) : m_array(wrap_array(julia_owned, c_ptr, sizes...))
{
}
template<typename ValueT, typename... SizesT>
auto make_julia_array(ValueT* c_ptr, const SizesT... sizes) -> ArrayRef<ValueT, sizeof...(SizesT)>
{
return ArrayRef<ValueT, sizeof...(SizesT)>(false, c_ptr, sizes...);
}
template<typename T, typename SubTraitT>
struct static_type_mapping<Array<T>, CxxWrappedTrait<SubTraitT>>
{
typedef jl_array_t* type;
};
template<typename T>
struct julia_type_factory<Array<T>>
{
static inline jl_datatype_t* julia_type()
{
create_if_not_exists<T>();
return (jl_datatype_t*)apply_array_type(jlcxx::julia_type<T>(), 1);
}
};
template<typename T, int Dim>
struct ConvertToJulia<ArrayRef<T,Dim>>
{
template<typename ArrayRefT>
jl_array_t* operator()(ArrayRefT&& arr) const
{
return arr.wrapped();
}
};
template<typename T>
struct ConvertToJulia<Array<T>>
{
jl_value_t* operator()(Array<T>&& arr) const
{
return (jl_value_t*)arr.wrapped();
}
};
template<typename T, int Dim, typename SubTraitT>
struct ConvertToCpp<ArrayRef<T,Dim>, CxxWrappedTrait<SubTraitT>>
{
ArrayRef<T,Dim> operator()(jl_array_t* arr) const
{
return ArrayRef<T,Dim>(arr);
}
};
// Iterator operator implementation
template<typename PointedT, typename CppT>
bool operator!=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return r.ptr() != l.ptr();
}
template<typename PointedT, typename CppT>
bool operator==(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return r.ptr() == l.ptr();
}
template<typename PointedT, typename CppT>
bool operator<=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return l.ptr() <= r.ptr();
}
template<typename PointedT, typename CppT>
bool operator>=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return l.ptr() >= r.ptr();
}
template<typename PointedT, typename CppT>
bool operator>(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return l.ptr() > r.ptr();
}
template<typename PointedT, typename CppT>
bool operator<(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return l.ptr() < r.ptr();
}
template<typename PointedT, typename CppT>
array_iterator_base<PointedT, CppT> operator+(const array_iterator_base<PointedT, CppT>& l, const std::ptrdiff_t n)
{
return array_iterator_base<PointedT, CppT>(l.ptr() + n);
}
template<typename PointedT, typename CppT>
array_iterator_base<PointedT, CppT> operator+(const std::ptrdiff_t n, const array_iterator_base<PointedT, CppT>& r)
{
return array_iterator_base<PointedT, CppT>(r.ptr() + n);
}
template<typename PointedT, typename CppT>
array_iterator_base<PointedT, CppT> operator-(const array_iterator_base<PointedT, CppT>& l, const std::ptrdiff_t n)
{
return array_iterator_base<PointedT, CppT>(l.ptr() - n);
}
template<typename PointedT, typename CppT>
std::ptrdiff_t operator-(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
{
return l.ptr() - r.ptr();
}
}
#endif
|