|
26 | 26 |
|
27 | 27 | #include <string> |
28 | 28 |
|
| 29 | +namespace internal |
| 30 | +{ |
| 31 | +struct DefineDatasetConstructor |
| 32 | +{ |
| 33 | + static constexpr auto resolve_datatype(Datatype dt) -> Datatype |
| 34 | + { |
| 35 | + return dt; |
| 36 | + } |
| 37 | + |
| 38 | + static auto resolve_datatype(py::dtype dt) -> Datatype |
| 39 | + { |
| 40 | + return dtype_from_numpy(std::move(dt)); |
| 41 | + } |
| 42 | + |
| 43 | + static constexpr auto resolve_options(std::string const &str) |
| 44 | + -> std::string const & |
| 45 | + { |
| 46 | + return str; |
| 47 | + } |
| 48 | + |
| 49 | + static auto resolve_options(py::object const &obj) -> std::string |
| 50 | + { |
| 51 | + return ::auxiliary::json_dumps(obj); |
| 52 | + } |
| 53 | + |
| 54 | + template <typename Datatype_t, typename Options_t> |
| 55 | + static auto call(py::class_<Dataset> &ds) -> py::class_<Dataset> & |
| 56 | + { |
| 57 | + return ds.def( |
| 58 | + py::init([](Datatype_t dt, Extent e, Options_t const &options) { |
| 59 | + auto resolved_dtype = resolve_datatype(dt); |
| 60 | + decltype(auto) resolved_options = resolve_options(options); |
| 61 | + return new Dataset{ |
| 62 | + resolved_dtype, std::move(e), resolved_options}; |
| 63 | + }), |
| 64 | + py::arg("dtype"), |
| 65 | + py::arg("extent"), |
| 66 | + py::arg("options") = "{}"); |
| 67 | + } |
| 68 | +}; |
| 69 | +} // namespace internal |
| 70 | + |
29 | 71 | void init_Dataset(py::module &m) |
30 | 72 | { |
31 | 73 | auto pyDataset = |
32 | 74 | py::class_<Dataset>(m, "Dataset") |
33 | 75 | .def(py::init<Extent>(), py::arg("extent")) |
34 | | - .def( |
35 | | - py::init<Datatype, Extent, std::string>(), |
36 | | - py::arg("dtype"), |
37 | | - py::arg("extent"), |
38 | | - py::arg("options") = "{}") |
39 | | - .def( |
40 | | - py::init([](py::dtype dt, Extent e, std::string options) { |
41 | | - auto const d = dtype_from_numpy(std::move(dt)); |
42 | | - return new Dataset{d, std::move(e), std::move(options)}; |
43 | | - }), |
44 | | - py::arg("dtype"), |
45 | | - py::arg("extent"), |
46 | | - py::arg("options") = "{}") |
47 | | - .def( |
48 | | - py::init([](Datatype dt, Extent e, py::object const &options) { |
49 | | - auto resolved_options = ::auxiliary::json_dumps(options); |
50 | | - return new Dataset{ |
51 | | - dt, std::move(e), std::move(resolved_options)}; |
52 | | - }), |
53 | | - py::arg("dtype"), |
54 | | - py::arg("extent"), |
55 | | - py::arg("options")) |
56 | | - .def( |
57 | | - py::init([](py::dtype dt, Extent e, py::object const &options) { |
58 | | - auto const d = dtype_from_numpy(std::move(dt)); |
59 | | - auto resolved_options = ::auxiliary::json_dumps(options); |
60 | | - return new Dataset{ |
61 | | - d, std::move(e), std::move(resolved_options)}; |
62 | | - }), |
63 | | - py::arg("dtype"), |
64 | | - py::arg("extent"), |
65 | | - py::arg("options")) |
66 | | - |
67 | 76 | .def( |
68 | 77 | "__repr__", |
69 | 78 | [](const Dataset &d) { |
@@ -96,6 +105,12 @@ void init_Dataset(py::module &m) |
96 | 105 | "dtype", |
97 | 106 | [](const Dataset &d) { return dtype_to_numpy(d.dtype); }) |
98 | 107 | .def_readwrite("options", &Dataset::options); |
| 108 | + ::auxiliary::ForEachTypeNested< |
| 109 | + ::internal::DefineDatasetConstructor, |
| 110 | + // types for Datatype param |
| 111 | + std::tuple<Datatype, py::dtype>, |
| 112 | + // types for options param |
| 113 | + std::tuple<std::string, py::object>>::call(pyDataset); |
99 | 114 | pyDataset.attr("JOINED_DIMENSION") = |
100 | 115 | py::int_(uint64_t(Dataset::JOINED_DIMENSION)); |
101 | 116 | } |
0 commit comments