// Copyright Andrew Resch 2009. Use, modification and distribution is // subject to 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 using namespace boost::python; template struct pair_to_tuple { static PyObject* convert(const std::pair& p) { return incref(make_tuple(p.first, p.second).ptr()); } }; template struct tuple_to_pair { tuple_to_pair() { converter::registry::push_back( &convertible, &construct, type_id >() ); } static void* convertible(PyObject* x) { return PyTuple_Check(x) ? x: 0; } static void construct(PyObject* x, converter::rvalue_from_python_stage1_data* data) { void* storage = ((converter::rvalue_from_python_storage< std::pair >*)data)->storage.bytes; object o(borrowed(x)); std::pair p; p.first = extract(o[0]); p.second = extract(o[1]); new (storage) std::pair(p); data->convertible = storage; } }; void bind_converters() { to_python_converter, pair_to_tuple >(); tuple_to_pair(); }