sbkarrayconverter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2017 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. #ifndef SBKARRAYCONVERTERS_H
  4. #define SBKARRAYCONVERTERS_H
  5. #include "sbkpython.h"
  6. #include "shibokenmacros.h"
  7. extern "C" {
  8. struct SbkArrayConverter;
  9. }
  10. namespace Shiboken::Conversions {
  11. enum : int {
  12. SBK_UNIMPLEMENTED_ARRAY_IDX,
  13. SBK_DOUBLE_ARRAY_IDX,
  14. SBK_FLOAT_ARRAY_IDX,
  15. SBK_SHORT_ARRAY_IDX,
  16. SBK_UNSIGNEDSHORT_ARRAY_IDX,
  17. SBK_INT_ARRAY_IDX,
  18. SBK_UNSIGNEDINT_ARRAY_IDX,
  19. SBK_LONGLONG_ARRAY_IDX,
  20. SBK_UNSIGNEDLONGLONG_ARRAY_IDX,
  21. SBK_ARRAY_IDX_SIZE
  22. };
  23. /**
  24. * ArrayHandle is the type expected by shiboken6's array converter
  25. * functions. It provides access to array data which it may own
  26. * (in the case of conversions from PySequence) or a flat pointer
  27. * to internal data (in the case of array modules like numpy).
  28. */
  29. template <class T>
  30. class ArrayHandle
  31. {
  32. public:
  33. ArrayHandle(const ArrayHandle &) = delete;
  34. ArrayHandle& operator=(const ArrayHandle &) = delete;
  35. ArrayHandle(ArrayHandle &&) = delete;
  36. ArrayHandle& operator=(ArrayHandle &&) = delete;
  37. ArrayHandle() = default;
  38. ~ArrayHandle() { destroy(); }
  39. void allocate(Py_ssize_t size);
  40. void setData(T *d, size_t size);
  41. size_t size() const { return m_size; }
  42. T *data() const { return m_data; }
  43. operator T *() const { return m_data; }
  44. private:
  45. void destroy();
  46. T *m_data = nullptr;
  47. Py_ssize_t m_size = 0;
  48. bool m_owned = false;
  49. };
  50. /**
  51. * Similar to ArrayHandle for fixed size 2 dimensional arrays.
  52. * columns is the size of the last dimension
  53. * It only has a setData() methods since it will be used for numpy only.
  54. */
  55. template <class T, int columns>
  56. class Array2Handle
  57. {
  58. public:
  59. using RowType = T[columns];
  60. Array2Handle() = default;
  61. operator RowType *() const { return m_rows; }
  62. void setData(RowType *d) { m_rows = d; }
  63. private:
  64. RowType *m_rows = nullptr;
  65. };
  66. /// Returns the converter for an array type.
  67. LIBSHIBOKEN_API SbkArrayConverter *arrayTypeConverter(int index, int dimension = 1);
  68. template <class T>
  69. struct ArrayTypeIndex{
  70. enum : int { index = SBK_UNIMPLEMENTED_ARRAY_IDX };
  71. };
  72. template <> struct ArrayTypeIndex<double> { enum : int { index = SBK_DOUBLE_ARRAY_IDX }; };
  73. template <> struct ArrayTypeIndex<float> { enum : int { index = SBK_FLOAT_ARRAY_IDX };};
  74. template <> struct ArrayTypeIndex<short> { enum : int { index = SBK_SHORT_ARRAY_IDX };};
  75. template <> struct ArrayTypeIndex<unsigned short> { enum : int { index = SBK_UNSIGNEDSHORT_ARRAY_IDX };};
  76. template <> struct ArrayTypeIndex<int> { enum : int { index = SBK_INT_ARRAY_IDX };};
  77. template <> struct ArrayTypeIndex<unsigned> { enum : int { index = SBK_UNSIGNEDINT_ARRAY_IDX };};
  78. template <> struct ArrayTypeIndex<long long> { enum : int { index = SBK_LONGLONG_ARRAY_IDX };};
  79. template <> struct ArrayTypeIndex<unsigned long long> { enum : int { index = SBK_UNSIGNEDLONGLONG_ARRAY_IDX };};
  80. template<typename T> SbkArrayConverter *ArrayTypeConverter(int dimension)
  81. { return arrayTypeConverter(ArrayTypeIndex<T>::index, dimension); }
  82. // ArrayHandle methods
  83. template<class T>
  84. void ArrayHandle<T>::allocate(Py_ssize_t size)
  85. {
  86. destroy();
  87. m_data = new T[size];
  88. m_size = size;
  89. m_owned = true;
  90. }
  91. template<class T>
  92. void ArrayHandle<T>::setData(T *d, size_t size)
  93. {
  94. destroy();
  95. m_data = d;
  96. m_size = size;
  97. m_owned = false;
  98. }
  99. template<class T>
  100. void ArrayHandle<T>::destroy()
  101. {
  102. if (m_owned)
  103. delete [] m_data;
  104. m_data = nullptr;
  105. m_size = 0;
  106. m_owned = false;
  107. }
  108. } // namespace Shiboken::Conversions
  109. #endif // SBKARRAYCONVERTERS_H