array.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // This file is a modified array.hpp from https://github.com/JuliaInterop/libcxxwrap-julia
  2. // required for the hack that allows automated conversion of OpenCV types.
  3. // Shouldn't be needed once CxxWrap gets inbuilt support
  4. // Here is the original copyright and the license:
  5. /*
  6. ==
  7. Copyright (c) 2015: Bart Janssens.
  8. 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:
  9. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  10. 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.
  11. ==
  12. */
  13. #ifndef JLCXX_ARRAY_HPP
  14. #define JLCXX_ARRAY_HPP
  15. #include "jlcxx/type_conversion.hpp"
  16. #include "jlcxx/tuple.hpp"
  17. namespace jlcxx
  18. {
  19. template<typename PointedT, typename CppT>
  20. struct ValueExtractor
  21. {
  22. inline CppT operator()(PointedT* p)
  23. {
  24. return convert_to_cpp<CppT>(*p);
  25. }
  26. };
  27. template<typename PointedT>
  28. struct ValueExtractor<PointedT, PointedT>
  29. {
  30. inline PointedT& operator()(PointedT* p)
  31. {
  32. return *p;
  33. }
  34. };
  35. template<typename PointedT, typename CppT>
  36. class array_iterator_base : public std::iterator<std::random_access_iterator_tag, CppT>
  37. {
  38. private:
  39. PointedT* m_ptr;
  40. public:
  41. array_iterator_base() : m_ptr(nullptr)
  42. {
  43. }
  44. explicit array_iterator_base(PointedT* p) : m_ptr(p)
  45. {
  46. }
  47. template <class OtherPointedT, class OtherCppT>
  48. array_iterator_base(array_iterator_base<OtherPointedT, OtherCppT> const& other) : m_ptr(other.m_ptr) {}
  49. auto operator*() -> decltype(ValueExtractor<PointedT,CppT>()(m_ptr))
  50. {
  51. return ValueExtractor<PointedT,CppT>()(m_ptr);
  52. }
  53. array_iterator_base<PointedT, CppT>& operator++()
  54. {
  55. ++m_ptr;
  56. return *this;
  57. }
  58. array_iterator_base<PointedT, CppT>& operator--()
  59. {
  60. --m_ptr;
  61. return *this;
  62. }
  63. array_iterator_base<PointedT, CppT>& operator+=(std::ptrdiff_t n)
  64. {
  65. m_ptr += n;
  66. return *this;
  67. }
  68. array_iterator_base<PointedT, CppT>& operator-=(std::ptrdiff_t n)
  69. {
  70. m_ptr -= n;
  71. return *this;
  72. }
  73. PointedT* ptr() const
  74. {
  75. return m_ptr;
  76. }
  77. };
  78. /// Wrap a Julia 1D array in a C++ class. Array is allocated on the C++ side
  79. template<typename ValueT>
  80. class Array
  81. {
  82. public:
  83. Array(const size_t n = 0)
  84. {
  85. jl_value_t* array_type = apply_array_type(julia_type<ValueT>(), 1);
  86. m_array = jl_alloc_array_1d(array_type, n);
  87. }
  88. Array(jl_datatype_t* applied_type, const size_t n = 0)
  89. {
  90. jl_value_t* array_type = apply_array_type(applied_type, 1);
  91. m_array = jl_alloc_array_1d(array_type, n);
  92. }
  93. /// Append an element to the end of the list
  94. template<typename VT>
  95. void push_back(VT&& val)
  96. {
  97. JL_GC_PUSH1(&m_array);
  98. const size_t pos = jl_array_len(m_array);
  99. jl_array_grow_end(m_array, 1);
  100. jl_arrayset(m_array, box<ValueT>(val), pos);
  101. JL_GC_POP();
  102. }
  103. /// Access to the wrapped array
  104. jl_array_t* wrapped()
  105. {
  106. return m_array;
  107. }
  108. // access to the pointer for GC macros
  109. jl_array_t** gc_pointer()
  110. {
  111. return &m_array;
  112. }
  113. private:
  114. jl_array_t* m_array;
  115. };
  116. namespace detail
  117. {
  118. template<typename T, typename TraitT=mapping_trait<T>>
  119. struct ArrayElementType
  120. {
  121. using type = static_julia_type<T>;
  122. };
  123. template<typename T>
  124. struct ArrayElementType<T,WrappedPtrTrait>
  125. {
  126. using type = T;
  127. };
  128. }
  129. /// Reference a Julia array in an STL-compatible wrapper
  130. template<typename ValueT, int Dim = 1>
  131. class ArrayRef
  132. {
  133. public:
  134. using julia_t = typename detail::ArrayElementType<ValueT>::type;
  135. ArrayRef(jl_array_t* arr) : m_array(arr)
  136. {
  137. assert(wrapped() != nullptr);
  138. }
  139. /// Convert from existing C-array (memory owned by C++)
  140. template<typename... SizesT>
  141. ArrayRef(julia_t* ptr, const SizesT... sizes);
  142. /// Convert from existing C-array, explicitly setting Julia ownership
  143. template<typename... SizesT>
  144. ArrayRef(const bool julia_owned, julia_t* ptr, const SizesT... sizes);
  145. typedef array_iterator_base<julia_t, ValueT> iterator;
  146. typedef array_iterator_base<julia_t const, ValueT const> const_iterator;
  147. inline jl_array_t* wrapped() const
  148. {
  149. return m_array;
  150. }
  151. iterator begin()
  152. {
  153. return iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
  154. }
  155. const_iterator begin() const
  156. {
  157. return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())));
  158. }
  159. iterator end()
  160. {
  161. return iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
  162. }
  163. const_iterator end() const
  164. {
  165. return const_iterator(static_cast<julia_t*>(jl_array_data(wrapped())) + jl_array_len(wrapped()));
  166. }
  167. void push_back(const ValueT& val)
  168. {
  169. static_assert(Dim == 1, "ArrayRef::push_back is only for 1D ArrayRef");
  170. static_assert(std::is_same<julia_t,ValueT>::value, "ArrayRef::push_back is only for arrays of fundamental types");
  171. jl_array_t* arr_ptr = wrapped();
  172. JL_GC_PUSH1(&arr_ptr);
  173. const size_t pos = size();
  174. jl_array_grow_end(arr_ptr, 1);
  175. jl_arrayset(arr_ptr, box<ValueT>(val), pos);
  176. JL_GC_POP();
  177. }
  178. const julia_t* data() const
  179. {
  180. return (julia_t*)jl_array_data(wrapped());
  181. }
  182. julia_t* data()
  183. {
  184. return (julia_t*)jl_array_data(wrapped());
  185. }
  186. std::size_t size() const
  187. {
  188. return jl_array_len(wrapped());
  189. }
  190. ValueT& operator[](const std::size_t i)
  191. {
  192. if constexpr(std::is_same<julia_t, ValueT>::value)
  193. {
  194. return data()[i];
  195. }
  196. else if constexpr(std::is_same<julia_t, static_julia_type<ValueT>>::value && !std::is_same<julia_t, WrappedCppPtr>::value)
  197. {
  198. return *reinterpret_cast<ValueT*>(&data()[i]);
  199. }
  200. else
  201. {
  202. return *extract_pointer_nonull<ValueT>(data()[i]);
  203. }
  204. }
  205. const ValueT& operator[](const std::size_t i) const
  206. {
  207. if constexpr(std::is_same<julia_t, ValueT>::value)
  208. {
  209. return data()[i];
  210. }
  211. else if constexpr(std::is_same<julia_t, static_julia_type<ValueT>>::value && !std::is_same<julia_t, WrappedCppPtr>::value)
  212. {
  213. return *reinterpret_cast<ValueT*>(&data()[i]);
  214. }
  215. else
  216. {
  217. return *extract_pointer_nonull<ValueT>(data()[i]);
  218. }
  219. }
  220. jl_array_t* m_array;
  221. };
  222. // Conversions
  223. template<typename T, int Dim, typename SubTraitT>
  224. struct static_type_mapping<ArrayRef<T, Dim>, CxxWrappedTrait<SubTraitT>>
  225. {
  226. typedef jl_array_t* type;
  227. };
  228. namespace detail
  229. {
  230. template<typename T, typename TraitT=mapping_trait<T>>
  231. struct PackedArrayType
  232. {
  233. static jl_datatype_t* type()
  234. {
  235. return julia_type<T>();
  236. }
  237. };
  238. template<typename T>
  239. struct PackedArrayType<T*, WrappedPtrTrait>
  240. {
  241. static jl_datatype_t* type()
  242. {
  243. return (jl_datatype_t*)apply_type((jl_value_t*)jlcxx::julia_type("Ptr"), jl_svec1(julia_base_type<T>()));
  244. }
  245. };
  246. template<typename T, typename SubTraitT>
  247. struct PackedArrayType<T,CxxWrappedTrait<SubTraitT>>
  248. {
  249. static jl_datatype_t* type()
  250. {
  251. create_if_not_exists<T&>();
  252. return julia_type<T&>();
  253. }
  254. };
  255. }
  256. template<typename T, int Dim>
  257. struct julia_type_factory<ArrayRef<T, Dim>>
  258. {
  259. static inline jl_datatype_t* julia_type()
  260. {
  261. create_if_not_exists<T>();
  262. return (jl_datatype_t*)apply_array_type(detail::PackedArrayType<T>::type(), Dim);
  263. }
  264. };
  265. template<typename ValueT, typename... SizesT>
  266. jl_array_t* wrap_array(const bool julia_owned, ValueT* c_ptr, const SizesT... sizes)
  267. {
  268. jl_datatype_t* dt = julia_type<ArrayRef<ValueT, sizeof...(SizesT)>>();
  269. jl_value_t *dims = nullptr;
  270. JL_GC_PUSH1(&dims);
  271. dims = convert_to_julia(std::make_tuple(static_cast<cxxint_t>(sizes)...));
  272. jl_array_t* result = jl_ptr_to_array((jl_value_t*)dt, c_ptr, dims, julia_owned);
  273. JL_GC_POP();
  274. return result;
  275. }
  276. template<typename ValueT, int Dim>
  277. template<typename... SizesT>
  278. ArrayRef<ValueT, Dim>::ArrayRef(julia_t* c_ptr, const SizesT... sizes) : m_array(wrap_array(false, c_ptr, sizes...))
  279. {
  280. }
  281. template<typename ValueT, int Dim>
  282. template<typename... SizesT>
  283. ArrayRef<ValueT, Dim>::ArrayRef(const bool julia_owned, julia_t* c_ptr, const SizesT... sizes) : m_array(wrap_array(julia_owned, c_ptr, sizes...))
  284. {
  285. }
  286. template<typename ValueT, typename... SizesT>
  287. auto make_julia_array(ValueT* c_ptr, const SizesT... sizes) -> ArrayRef<ValueT, sizeof...(SizesT)>
  288. {
  289. return ArrayRef<ValueT, sizeof...(SizesT)>(false, c_ptr, sizes...);
  290. }
  291. template<typename T, typename SubTraitT>
  292. struct static_type_mapping<Array<T>, CxxWrappedTrait<SubTraitT>>
  293. {
  294. typedef jl_array_t* type;
  295. };
  296. template<typename T>
  297. struct julia_type_factory<Array<T>>
  298. {
  299. static inline jl_datatype_t* julia_type()
  300. {
  301. create_if_not_exists<T>();
  302. return (jl_datatype_t*)apply_array_type(jlcxx::julia_type<T>(), 1);
  303. }
  304. };
  305. template<typename T, int Dim>
  306. struct ConvertToJulia<ArrayRef<T,Dim>>
  307. {
  308. template<typename ArrayRefT>
  309. jl_array_t* operator()(ArrayRefT&& arr) const
  310. {
  311. return arr.wrapped();
  312. }
  313. };
  314. template<typename T>
  315. struct ConvertToJulia<Array<T>>
  316. {
  317. jl_value_t* operator()(Array<T>&& arr) const
  318. {
  319. return (jl_value_t*)arr.wrapped();
  320. }
  321. };
  322. template<typename T, int Dim, typename SubTraitT>
  323. struct ConvertToCpp<ArrayRef<T,Dim>, CxxWrappedTrait<SubTraitT>>
  324. {
  325. ArrayRef<T,Dim> operator()(jl_array_t* arr) const
  326. {
  327. return ArrayRef<T,Dim>(arr);
  328. }
  329. };
  330. // Iterator operator implementation
  331. template<typename PointedT, typename CppT>
  332. bool operator!=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  333. {
  334. return r.ptr() != l.ptr();
  335. }
  336. template<typename PointedT, typename CppT>
  337. bool operator==(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  338. {
  339. return r.ptr() == l.ptr();
  340. }
  341. template<typename PointedT, typename CppT>
  342. bool operator<=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  343. {
  344. return l.ptr() <= r.ptr();
  345. }
  346. template<typename PointedT, typename CppT>
  347. bool operator>=(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  348. {
  349. return l.ptr() >= r.ptr();
  350. }
  351. template<typename PointedT, typename CppT>
  352. bool operator>(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  353. {
  354. return l.ptr() > r.ptr();
  355. }
  356. template<typename PointedT, typename CppT>
  357. bool operator<(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  358. {
  359. return l.ptr() < r.ptr();
  360. }
  361. template<typename PointedT, typename CppT>
  362. array_iterator_base<PointedT, CppT> operator+(const array_iterator_base<PointedT, CppT>& l, const std::ptrdiff_t n)
  363. {
  364. return array_iterator_base<PointedT, CppT>(l.ptr() + n);
  365. }
  366. template<typename PointedT, typename CppT>
  367. array_iterator_base<PointedT, CppT> operator+(const std::ptrdiff_t n, const array_iterator_base<PointedT, CppT>& r)
  368. {
  369. return array_iterator_base<PointedT, CppT>(r.ptr() + n);
  370. }
  371. template<typename PointedT, typename CppT>
  372. array_iterator_base<PointedT, CppT> operator-(const array_iterator_base<PointedT, CppT>& l, const std::ptrdiff_t n)
  373. {
  374. return array_iterator_base<PointedT, CppT>(l.ptr() - n);
  375. }
  376. template<typename PointedT, typename CppT>
  377. std::ptrdiff_t operator-(const array_iterator_base<PointedT, CppT>& l, const array_iterator_base<PointedT, CppT>& r)
  378. {
  379. return l.ptr() - r.ptr();
  380. }
  381. }
  382. #endif