From 59e11d3ada55cb68d7ecc6af4bb25b5ec59f1712 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Fri, 14 Jun 2019 12:27:11 -0400 Subject: [PATCH] ENH: Add to and from object support for std::pair. Delegates to the existing implementation for std::tuple. --- include/libpy/from_object.h | 10 ++++++++++ include/libpy/to_object.h | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/libpy/from_object.h b/include/libpy/from_object.h index 34c5e779..c085977f 100644 --- a/include/libpy/from_object.h +++ b/include/libpy/from_object.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -356,6 +357,15 @@ struct from_object> { } }; +template +struct from_object> { +public: + static std::pair f(PyObject* tup) { + std::tuple tmp = from_object>(tup); + return std::make_pair(std::get<0>(tmp), std::get<1>(tmp)); + } +}; + template struct from_object> { static std::optional f(PyObject* maybe_value) { diff --git a/include/libpy/to_object.h b/include/libpy/to_object.h index e7cac1bc..3414b747 100644 --- a/include/libpy/to_object.h +++ b/include/libpy/to_object.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -296,6 +297,16 @@ struct to_object> { } }; +template +struct to_object> { +public: + static PyObject* f(const std::pair& p) { + // Delegate to std::tuple dispatch. + return std::move(py::to_object(std::make_tuple(std::get<0>(p), std::get<1>(p)))) + .escape(); + } +}; + template struct to_object> { static PyObject* f(const std::optional& maybe_value) {