Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add to and from object support for std::pair. #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions include/libpy/from_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <Python.h>
Expand Down Expand Up @@ -356,6 +357,15 @@ struct from_object<std::tuple<Ts...>> {
}
};

template<typename T, typename U>
struct from_object<std::pair<T, U>> {
public:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can drop the publics from these structs.

static std::pair<T, U> f(PyObject* tup) {
std::tuple<T, U> tmp = from_object<std::tuple<T, U>>(tup);
return std::make_pair(std::get<0>(tmp), std::get<1>(tmp));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move the values from the old tuple in the resulting pair. For things like vector and string this will trigger a copy.

}
};

template<typename T>
struct from_object<std::optional<T>> {
static std::optional<T> f(PyObject* maybe_value) {
Expand Down
11 changes: 11 additions & 0 deletions include/libpy/to_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <Python.h>
Expand Down Expand Up @@ -296,6 +297,16 @@ struct to_object<std::tuple<Ts...>> {
}
};

template<typename T, typename U>
struct to_object<std::pair<T, U>> {
public:
static PyObject* f(const std::pair<T, U>& p) {
// Delegate to std::tuple dispatch.
return std::move(py::to_object(std::make_tuple(std::get<0>(p), std::get<1>(p))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will already be an rvalue, so we don't need to move

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is triggering an extra copy. Should we just pull the tuple to_object into a helper base class that accepts tuple-likes and subclass from that? This is how we handle mappings and sequences.

.escape();
}
};

template<typename T>
struct to_object<std::optional<T>> {
static PyObject* f(const std::optional<T>& maybe_value) {
Expand Down