2009-01-24 06:29:23 +01:00
|
|
|
// Copyright Andrew Resch 2009. Use, modification and distribution is
|
2007-01-10 17:11:43 +01:00
|
|
|
// 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)
|
|
|
|
|
2016-04-08 04:45:23 +02:00
|
|
|
#include "boost_python.hpp"
|
2016-11-14 02:52:56 +01:00
|
|
|
#include "libtorrent/address.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
2016-12-23 03:43:05 +01:00
|
|
|
#include "libtorrent/session_stats.hpp"
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2009-01-24 06:29:23 +01:00
|
|
|
using namespace boost::python;
|
2016-11-14 02:52:56 +01:00
|
|
|
namespace bp = boost::python;
|
2009-01-24 06:29:23 +01:00
|
|
|
|
|
|
|
template<class T1, class T2>
|
|
|
|
struct pair_to_tuple
|
|
|
|
{
|
|
|
|
static PyObject* convert(const std::pair<T1, T2>& p)
|
|
|
|
{
|
2016-11-14 02:52:56 +01:00
|
|
|
return incref(bp::make_tuple(p.first, p.second).ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Endpoint>
|
|
|
|
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());
|
2009-01-24 06:29:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T1, class T2>
|
|
|
|
struct tuple_to_pair
|
|
|
|
{
|
|
|
|
tuple_to_pair()
|
|
|
|
{
|
|
|
|
converter::registry::push_back(
|
|
|
|
&convertible, &construct, type_id<std::pair<T1, T2> >()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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<T1, T2> >*)data)->storage.bytes;
|
|
|
|
|
|
|
|
object o(borrowed(x));
|
|
|
|
std::pair<T1, T2> p;
|
|
|
|
p.first = extract<T1>(o[0]);
|
|
|
|
p.second = extract<T2>(o[1]);
|
|
|
|
new (storage) std::pair<T1, T2>(p);
|
|
|
|
data->convertible = storage;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-23 03:43:05 +01:00
|
|
|
template<class T>
|
|
|
|
struct vector_to_list
|
|
|
|
{
|
|
|
|
static PyObject* convert(const std::vector<T>& v)
|
|
|
|
{
|
|
|
|
list l;
|
|
|
|
for (int i = 0; i < int(v.size()); ++i)
|
|
|
|
{
|
|
|
|
l.append(v[i]);
|
|
|
|
}
|
|
|
|
return incref(l.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-24 06:29:23 +01:00
|
|
|
void bind_converters()
|
|
|
|
{
|
2016-11-14 02:52:56 +01:00
|
|
|
namespace lt = libtorrent;
|
2009-01-24 06:29:23 +01:00
|
|
|
to_python_converter<std::pair<int, int>, pair_to_tuple<int, int> >();
|
2016-11-14 02:52:56 +01:00
|
|
|
to_python_converter<lt::tcp::endpoint, endpoint_to_tuple<lt::tcp::endpoint> >();
|
|
|
|
to_python_converter<lt::udp::endpoint, endpoint_to_tuple<lt::udp::endpoint> >();
|
|
|
|
to_python_converter<lt::address, address_to_tuple>();
|
2009-01-24 06:29:23 +01:00
|
|
|
tuple_to_pair<int, int>();
|
2016-12-23 03:43:05 +01:00
|
|
|
|
|
|
|
to_python_converter<std::vector<lt::stats_metric>, vector_to_list<lt::stats_metric>>();
|
2009-01-24 06:29:23 +01:00
|
|
|
}
|