Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions include/libpy/to_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,28 @@ py::owned_ref<> to_object(T&& ob) {
}

namespace dispatch {
template<typename C>
py::owned_ref<> sequence_to_list(const C& v) {
py::owned_ref out(PyList_New(v.size()));
if (!out) {
return nullptr;
}

std::size_t ix = 0;
for (const auto& elem : v) {
PyObject* ob = py::to_object(elem).escape();
if (!ob) {
template<typename C>
struct sequence_to_object {
static py::owned_ref<> f(const C& v) {
py::owned_ref out(PyList_New(v.size()));
if (!out) {
return nullptr;
}

PyList_SET_ITEM(out.get(), ix++, ob);
}
std::size_t ix = 0;
for (const auto& elem : v) {
PyObject* ob = py::to_object(elem).escape();
if (!ob) {
return nullptr;
}

return out;
}
PyList_SET_ITEM(out.get(), ix++, ob);
}

return out;
}
};

template<std::size_t n>
struct to_object<std::array<char, n>> {
Expand All @@ -102,11 +105,7 @@ struct to_object<char> {
};

template<typename T, std::size_t n>
struct to_object<std::array<T, n>> {
static py::owned_ref<> f(const std::array<T, n>& v) {
return sequence_to_list(v);
}
};
struct to_object<std::array<T, n>> : public sequence_to_object<std::array<T, n>> {};

/** Identity conversion for `owned_ref`.
*/
Expand Down Expand Up @@ -273,11 +272,7 @@ struct to_object<std::unordered_map<K, V, Hash, KeyEqual>>
: public map_to_object<std::unordered_map<K, V, Hash, KeyEqual>> {};

template<typename T>
struct to_object<std::vector<T>> {
static py::owned_ref<> f(const std::vector<T>& v) {
return sequence_to_list(v);
}
};
struct to_object<std::vector<T>> : public sequence_to_object<std::vector<T>> {};

template<typename S>
struct set_to_object {
Expand Down