From 11c21f2abe1d9ad66bf965be2f051a01249e212b Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sat, 24 Jan 2009 05:29:23 +0000 Subject: [PATCH] Add a std::pair to tuple converter Add 'outgoing_ports' to session_settings --- bindings/python/src/converters.cpp | 48 +++++++++++++++++++++++- bindings/python/src/module.cpp | 3 +- bindings/python/src/session_settings.cpp | 1 + 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index f684cc4f3..cae84be75 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -1,5 +1,51 @@ -// Copyright Daniel Wallin 2007. Use, modification and distribution is +// 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(); +} diff --git a/bindings/python/src/module.cpp b/bindings/python/src/module.cpp index e0bbe381d..c512e346a 100644 --- a/bindings/python/src/module.cpp +++ b/bindings/python/src/module.cpp @@ -23,6 +23,7 @@ void bind_torrent(); void bind_peer_info(); void bind_ip_filter(); void bind_magnet_uri(); +void bind_converters(); BOOST_PYTHON_MODULE(libtorrent) { @@ -50,5 +51,5 @@ BOOST_PYTHON_MODULE(libtorrent) bind_peer_info(); bind_ip_filter(); bind_magnet_uri(); + bind_converters(); } - diff --git a/bindings/python/src/session_settings.cpp b/bindings/python/src/session_settings.cpp index 76e078560..8d4974936 100644 --- a/bindings/python/src/session_settings.cpp +++ b/bindings/python/src/session_settings.cpp @@ -45,6 +45,7 @@ void bind_session_settings() .def_readwrite("auto_scraped_interval", &session_settings::auto_scrape_interval) .def_readwrite("peer_tos", &session_settings::peer_tos) .def_readwrite("rate_limit_ip_overhead", &session_settings::rate_limit_ip_overhead) + .def_readwrite("outgoing_ports", &session_settings::outgoing_ports) #ifndef TORRENT_DISABLE_DHT .def_readwrite("use_dht_as_fallback", &session_settings::use_dht_as_fallback) #endif