Skip to content

Commit fe8e9aa

Browse files
committed
Same trick for Dataset constructor
1 parent d4dd817 commit fe8e9aa

1 file changed

Lines changed: 48 additions & 33 deletions

File tree

src/binding/python/Dataset.cpp

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,53 @@
2626

2727
#include <string>
2828

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+
2971
void init_Dataset(py::module &m)
3072
{
3173
auto pyDataset =
3274
py::class_<Dataset>(m, "Dataset")
3375
.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-
6776
.def(
6877
"__repr__",
6978
[](const Dataset &d) {
@@ -96,6 +105,12 @@ void init_Dataset(py::module &m)
96105
"dtype",
97106
[](const Dataset &d) { return dtype_to_numpy(d.dtype); })
98107
.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);
99114
pyDataset.attr("JOINED_DIMENSION") =
100115
py::int_(uint64_t(Dataset::JOINED_DIMENSION));
101116
}

0 commit comments

Comments
 (0)