| 14 | #include "pybind11_tests.h" |
| 15 | |
| 16 | TEST_SUBMODULE(buffers, m) { |
| 17 | m.attr("long_double_and_double_have_same_size") = (sizeof(long double) == sizeof(double)); |
| 18 | |
| 19 | m.def("format_descriptor_format_buffer_info_equiv", |
| 20 | [](const std::string &cpp_name, const py::buffer &buffer) { |
| 21 | // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables |
| 22 | static auto *format_table = new std::map<std::string, std::string>; |
| 23 | static auto *equiv_table |
| 24 | = new std::map<std::string, bool (py::buffer_info::*)() const>; |
| 25 | if (format_table->empty()) { |
| 26 | #define PYBIND11_ASSIGN_HELPER(...) \ |
| 27 | (*format_table)[#__VA_ARGS__] = py::format_descriptor<__VA_ARGS__>::format(); \ |
| 28 | (*equiv_table)[#__VA_ARGS__] = &py::buffer_info::item_type_is_equivalent_to<__VA_ARGS__>; |
| 29 | PYBIND11_ASSIGN_HELPER(PyObject *) |
| 30 | PYBIND11_ASSIGN_HELPER(bool) |
| 31 | PYBIND11_ASSIGN_HELPER(std::int8_t) |
| 32 | PYBIND11_ASSIGN_HELPER(std::uint8_t) |
| 33 | PYBIND11_ASSIGN_HELPER(std::int16_t) |
| 34 | PYBIND11_ASSIGN_HELPER(std::uint16_t) |
| 35 | PYBIND11_ASSIGN_HELPER(std::int32_t) |
| 36 | PYBIND11_ASSIGN_HELPER(std::uint32_t) |
| 37 | PYBIND11_ASSIGN_HELPER(std::int64_t) |
| 38 | PYBIND11_ASSIGN_HELPER(std::uint64_t) |
| 39 | PYBIND11_ASSIGN_HELPER(float) |
| 40 | PYBIND11_ASSIGN_HELPER(double) |
| 41 | PYBIND11_ASSIGN_HELPER(long double) |
| 42 | PYBIND11_ASSIGN_HELPER(std::complex<float>) |
| 43 | PYBIND11_ASSIGN_HELPER(std::complex<double>) |
| 44 | PYBIND11_ASSIGN_HELPER(std::complex<long double>) |
| 45 | #undef PYBIND11_ASSIGN_HELPER |
| 46 | } |
| 47 | return std::pair<std::string, bool>( |
| 48 | (*format_table)[cpp_name], (buffer.request().*((*equiv_table)[cpp_name]))()); |
| 49 | }); |
| 50 | |
| 51 | // test_from_python / test_to_python: |
| 52 | class Matrix { |
| 53 | public: |
| 54 | Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) { |
| 55 | print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 56 | // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 57 | m_data = new float[(size_t) (rows * cols)]; |
| 58 | memset(m_data, 0, sizeof(float) * (size_t) (rows * cols)); |
| 59 | } |
| 60 | |
| 61 | Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) { |
| 62 | print_copy_created(this, |
| 63 | std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 64 | // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) |
| 65 | m_data = new float[(size_t) (m_rows * m_cols)]; |
| 66 | memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); |
| 67 | } |
| 68 | |
| 69 | Matrix(Matrix &&s) noexcept : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { |
| 70 | print_move_created(this); |
| 71 | s.m_rows = 0; |
| 72 | s.m_cols = 0; |
| 73 | s.m_data = nullptr; |
nothing calls this directly
no test coverage detected