From fef288fed24fc12e22b8f2dee2be943136435e22 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 29 Mar 2005 23:02:45 +0000 Subject: [PATCH] new std::pair to_python_converter example [SVN r27875] --- example/std_pair.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 example/std_pair.cpp diff --git a/example/std_pair.cpp b/example/std_pair.cpp new file mode 100644 index 00000000..4a696ba5 --- /dev/null +++ b/example/std_pair.cpp @@ -0,0 +1,46 @@ +// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost +// Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +namespace { // Avoid cluttering the global namespace. + + // Converts a std::pair instance to a Python tuple. + template + struct std_pair_to_tuple + { + static PyObject* convert(std::pair const& p) + { + return boost::python::incref( + boost::python::make_tuple(p.first, p.second).ptr()); + } + }; + + // Helper for convenience. + template + struct std_pair_to_python_converter + { + std_pair_to_python_converter() + { + boost::python::to_python_converter< + std::pair, + std_pair_to_tuple >(); + } + }; + + // Example function returning a std::pair. + std::pair + foo() { return std::pair(3, 5); } + +} // namespace anonymous + +BOOST_PYTHON_MODULE(std_pair_ext) +{ + using namespace boost::python; + std_pair_to_python_converter(); + def("foo", foo); +}