~ubuntu-branches/ubuntu/natty/pyside/natty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * This file is part of PySide: Python for Qt
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: PySide team <contact@pyside.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */


#ifndef TYPE_DETAILS_H
#define TYPE_DETAILS_H

#include <QtCore/QString>
#include <QtCore/QGenericArgument>

#include <boost_headers.hpp>
#include <pyside_global.hpp>
#include <parent_policy.hpp>

namespace PySide
{

class PYSIDE_LOCAL type_details
{
    typedef boost::python::object(*func_cpp_to_python_type)(void*);
    typedef QGenericArgument(*func_python_to_cpp_type)(const boost::python::object&, const char*);
    typedef void(*func_delete_type)(void*);

    // disable object copy
    type_details() {}
    type_details(const type_details&);
    type_details& operator=(const type_details&);

public:
    template<typename T>
    static type_details*
    create_object_type_details(const char* type_name) {
        type_details* self = new type_details();
        self->m_type_name = type_name;
        self->m_type_object = boost::python::converter::registry::query(boost::python::type_id<T>())->get_class_object();
        self->m_func_cpp_to_python = &objecttype_to_python<T>;
        self->m_func_python_to_cpp = &python_to_objecttype<T*>;
        self->m_func_delete = 0;
        return self;
    }

    template<typename T>
    static type_details*
    create_value_type_details(const char* type_name) {
        type_details* self = new type_details();
        self->m_type_name = type_name;
        self->m_type_object = boost::python::converter::registry::query(boost::python::type_id<T>())->get_class_object();
        self->m_func_cpp_to_python = &valuetype_to_python<T>;
        self->m_func_python_to_cpp = &python_to_value_type<T>;
        self->m_func_delete = &func_delete_data<T>;
        return self;
    }
    template<typename T>
    static type_details*
    create_native_type_details(const char* type_name) {
        type_details* self = new type_details();
        self->m_type_name = type_name;
        self->m_type_object = 0;
        self->m_func_cpp_to_python = &native_to_python<T>;
        self->m_func_python_to_cpp = &python_to_value_type<T>;
        self->m_func_delete = &func_delete_data<T>;
        return self;
    }

    template<typename T>
    static type_details*
    create_container_type_details(const char* type_name) {
        type_details* self = new type_details();
        self->m_type_name = type_name;
        self->m_type_object = 0;
        self->m_func_cpp_to_python = &container_to_python<T>;
        self->m_func_python_to_cpp = &python_to_container<T>;
        self->m_func_delete = &func_delete_data<T>;
        return self;
    }

    boost::python::object
    to_python(void *data) const
    {
        return m_func_cpp_to_python(data);
    }

    QGenericArgument
    to_cpp(const boost::python::object &obj) const
    {
        return m_func_python_to_cpp(obj, m_type_name);
    }

    const PyTypeObject*
    get_python_type_object() const
    {
        return m_type_object;
    }

    void
    delete_data(QGenericArgument &arg) const
    {
        if (m_func_delete)
            m_func_delete(arg.data());
    }

private:
    const char* m_type_name;
    const PyTypeObject *m_type_object;

    func_cpp_to_python_type m_func_cpp_to_python;
    func_python_to_cpp_type m_func_python_to_cpp;
    func_delete_type m_func_delete;

    /* Object-Type Convertion functions */
    template <class T>
    static boost::python::object
    objecttype_to_python(void* p)
    {
        T* obj = *(reinterpret_cast< T*(*)>(p));
        boost::python::object py_obj(PySide::ptr(obj));
        if (!py_obj.ptr())
        {
            fprintf(stderr, "Fail to create python object from object in address: %p\n", obj);
            py_obj = boost::python::object();
        }
        else
        {
            boost::python::incref(py_obj.ptr());
        }

        return py_obj;
    }

    template <class T>
    static QGenericArgument
    python_to_objecttype(const boost::python::object &obj, const char *type_name)
    {
        T val = boost::python::extract<T>(obj);
        return QGenericArgument(type_name, val);
    }

    /* value type convertion functions */
    template <class T>
    static boost::python::object
    valuetype_to_python(void* p)
    {
        T* val = reinterpret_cast<T*>(p);
        return boost::python::object(val);
    }

    template <class T>
    static QGenericArgument
    python_to_value_type(const boost::python::object& obj, const char *type_name)
    {
        T* val = new T(boost::python::extract<T>(obj));
        return QGenericArgument(type_name, static_cast<const void*>(val));
    }

    template <typename T>
    static QGenericArgument
    python_to_container(const boost::python::object& obj, const char* type_name)
    {
        T* val = new T(boost::python::extract<T>(obj));
        return QGenericArgument(type_name, static_cast<const void*>(val));
    }

    template <typename T>
    static boost::python::object
    container_to_python(void* p)
    {
        return boost::python::object(reinterpret_cast<T*>(p));
    }

    template<typename T>
    static boost::python::object
    container_value_to_python(T t)
    {
        return valuetype_to_python<T>(&t);
    }

    template<typename T>
    static boost::python::object
    container_value_to_python(T* t)
    {
        return objecttype_to_python<T>(&t);
    }

    template <class T>
    static void
    func_delete_data(void *data)
    {
        delete reinterpret_cast<T*>(data);
    }

    template<typename T>
    static boost::python::object
    native_to_python(void* p)
    {
        T* val = new T(*reinterpret_cast<T*>(p));
        return boost::python::object(*val);
    }
};

} // namespace PySide

#endif