example20.cpp 1.28 KB
Newer Older
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
/*
  example/example20.cpp -- Usage of structured numpy dtypes

  Copyright (c) 2016 Ivan Smirnov

  All rights reserved. Use of this source code is governed by a
  BSD-style license that can be found in the LICENSE file.
*/

#include "example.h"

#include <pybind11/numpy.h>
#include <cstdint>
#include <iostream>

namespace py = pybind11;

struct Struct {
    bool x;
    uint32_t y;
    float z;
};

struct PackedStruct {
    bool x;
    uint32_t y;
    float z;
} __attribute__((packed));

struct NestedStruct {
    Struct a;
    PackedStruct b;
};

template <typename S>
py::array_t<S> create_recarray(size_t n) {
    auto arr = py::array(py::buffer_info(nullptr, sizeof(S),
                                         py::format_descriptor<S>::value(),
                                         1, { n }, { sizeof(S) }));
    auto buf = arr.request();
    auto ptr = static_cast<S*>(buf.ptr);
    for (size_t i = 0; i < n; i++) {
        ptr[i].x = i % 2;
        ptr[i].y = i;
        ptr[i].z = i * 1.5;
    }
    return arr;
}

void init_ex20(py::module &m) {
    PYBIND11_DTYPE(Struct, x, y, z);
    PYBIND11_DTYPE(PackedStruct, x, y, z);
    PYBIND11_DTYPE(NestedStruct, a, b);

    m.def("create_rec_simple", &create_recarray<Struct>);
    m.def("create_rec_packed", &create_recarray<PackedStruct>);
}