// 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 "boost_python.hpp" #include "libtorrent/address.hpp" #include "libtorrent/socket.hpp" #include "libtorrent/error_code.hpp" #include "libtorrent/session_stats.hpp" using namespace boost::python; namespace bp = boost::python; template struct pair_to_tuple { static PyObject* convert(const std::pair& p) { return incref(bp::make_tuple(p.first, p.second).ptr()); } }; template struct endpoint_to_tuple { static PyObject* convert(Endpoint const& ep) { return incref(bp::object(bp::make_tuple(ep.address().to_string(), ep.port())).ptr()); } }; struct address_to_tuple { static PyObject* convert(libtorrent::address const& addr) { libtorrent::error_code ec; return incref(bp::object(addr.to_string(ec)).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; } }; template struct vector_to_list { static PyObject* convert(const std::vector& v) { list l; for (int i = 0; i < int(v.size()); ++i) { l.append(v[i]); } return incref(l.ptr()); } }; void bind_converters() { namespace lt = libtorrent; to_python_converter, pair_to_tuple >(); to_python_converter >(); to_python_converter >(); to_python_converter(); tuple_to_pair(); to_python_converter, vector_to_list>(); }