~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to bindings/python/src/entry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright Daniel Wallin 2006. Use, modification and distribution is
 
2
// subject to the Boost Software License, Version 1.0. (See accompanying
 
3
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
4
 
 
5
#include <libtorrent/session.hpp>
 
6
#include <boost/python.hpp>
 
7
 
 
8
using namespace boost::python;
 
9
using namespace libtorrent;
 
10
 
 
11
struct entry_to_python
 
12
{
 
13
    static object convert(entry::list_type const& l)
 
14
    {
 
15
        list result;
 
16
 
 
17
        for (entry::list_type::const_iterator i(l.begin()), e(l.end()); i != e; ++i)
 
18
        {
 
19
            result.append(*i);
 
20
        }
 
21
 
 
22
        return result;
 
23
    }
 
24
 
 
25
    static object convert(entry::dictionary_type const& d)
 
26
    {
 
27
        dict result;
 
28
 
 
29
        for (entry::dictionary_type::const_iterator i(d.begin()), e(d.end()); i != e; ++i)
 
30
            result[i->first] = i->second;
 
31
 
 
32
        return result;
 
33
    }
 
34
 
 
35
    static object convert0(entry const& e)
 
36
    {
 
37
        switch (e.type())
 
38
        {
 
39
        case entry::int_t:
 
40
            return object(e.integer());
 
41
        case entry::string_t:
 
42
            return object(e.string());
 
43
        case entry::list_t:
 
44
            return convert(e.list());
 
45
        case entry::dictionary_t:
 
46
            return convert(e.dict());
 
47
        default:
 
48
            return object();
 
49
        }
 
50
    }
 
51
 
 
52
    static PyObject* convert(entry const& e)
 
53
    {
 
54
        return incref(convert0(e).ptr());
 
55
    }
 
56
};
 
57
 
 
58
struct entry_from_python
 
59
{
 
60
    entry_from_python()
 
61
    {
 
62
        converter::registry::push_back(
 
63
            &convertible, &construct, type_id<entry>()
 
64
        );
 
65
    }
 
66
 
 
67
    static void* convertible(PyObject* e)
 
68
    {
 
69
        return e;
 
70
    }
 
71
 
 
72
    static entry construct0(object e)
 
73
    {
 
74
        if (extract<dict>(e).check())
 
75
        {
 
76
            dict d = extract<dict>(e);
 
77
            list items(d.items());
 
78
            std::size_t length = extract<std::size_t>(items.attr("__len__")());
 
79
            entry result(entry::dictionary_t);
 
80
 
 
81
            for (std::size_t i = 0; i < length; ++i)
 
82
            {
 
83
                result.dict().insert(
 
84
                    std::make_pair(
 
85
                        extract<char const*>(items[i][0])()
 
86
                      , construct0(items[i][1])
 
87
                    )
 
88
                );
 
89
            }
 
90
 
 
91
            return result;
 
92
        }
 
93
        else if (extract<list>(e).check())
 
94
        {
 
95
            list l = extract<list>(e);
 
96
 
 
97
            std::size_t length = extract<std::size_t>(l.attr("__len__")());
 
98
            entry result(entry::list_t);
 
99
 
 
100
            for (std::size_t i = 0; i < length; ++i)
 
101
            {
 
102
                result.list().push_back(construct0(l[i]));
 
103
            }
 
104
 
 
105
            return result;
 
106
        }
 
107
        else if (extract<str>(e).check())
 
108
        {
 
109
            return entry(extract<std::string>(e)());
 
110
        }
 
111
        else if (extract<entry::integer_type>(e).check())
 
112
        {
 
113
            return entry(extract<entry::integer_type>(e)());
 
114
        }
 
115
 
 
116
        return entry();
 
117
    }
 
118
 
 
119
    static void construct(PyObject* e, converter::rvalue_from_python_stage1_data* data)
 
120
    {
 
121
        void* storage = ((converter::rvalue_from_python_storage<entry>*)data)->storage.bytes;
 
122
        new (storage) entry(construct0(object(borrowed(e))));
 
123
        data->convertible = storage;
 
124
    }
 
125
};
 
126
 
 
127
void bind_entry()
 
128
{
 
129
    to_python_converter<entry, entry_to_python>();
 
130
    entry_from_python();
 
131
}
 
132