-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
#include <type_traits> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <Python.h> | ||
|
@@ -356,6 +357,15 @@ struct from_object<std::tuple<Ts...>> { | |
} | ||
}; | ||
|
||
template<typename T, typename U> | ||
struct from_object<std::pair<T, U>> { | ||
public: | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include <type_traits> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <Python.h> | ||
|
@@ -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)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
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.