summaryrefslogtreecommitdiffstats
path: root/lib/kross/python/pythonextension.h
blob: b19a11a1fe48b06b13dcacc06742dfed0aaabf5b (plain)
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
/***************************************************************************
 * pythonextension.h
 * This file is part of the KDE project
 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * This program 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
 * Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 ***************************************************************************/

#ifndef KROSS_PYTHON_EXTENSION_H
#define KROSS_PYTHON_EXTENSION_H

#include "pythonconfig.h"

#include "../api/object.h"
#include "../api/list.h"
#include "../api/dict.h"
#include "../api/class.h"

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>
#include <tqvaluevector.h>
#include <tqmap.h>
#include <tqvariant.h>

namespace Kross { namespace Python {

    // Forward declaration.
    class PythonScript;

    /**
     * The PythonExtension is a wrapper-object to let C++ and
     * Python interact together.
     * Instances of this class are used everytime if we send
     * or got something to/from python.
     */
    class PythonExtension : public Py::PythonExtension<PythonExtension>
    {
            friend class PythonScript;
            friend class PythonObject;
            friend class PythonModule;

        public:

            /**
             * Constructor.
             *
             * \param object The \a Kross::Api::Object object
             *        this instance is the wrapper for.
             */
            explicit PythonExtension(Kross::Api::Object::Ptr object);

            /**
             * Destructor.
             */
            virtual ~PythonExtension();

#if 0
            /**
             * Overloaded method to return the string-representation
             * of this object.
             *
             * \return The string representation.
             */
            virtual Py::Object str();

            /**
             * Overloaded method to return the string-representation
             * of the value this object has.
             *
             * \return A string representation of the value.
             */
            virtual Py::Object repr();
#endif

            /**
             * Overloaded method to handle attribute calls
             * from within python.
             *
             * \param name The name of the attribute that
             *        should be handled.
             * \return An \a Py::Object that could be
             *         a value or a callable object. Python
             *         will decide what to do with the
             *         returnvalue.
             */
            virtual Py::Object getattr(const char* name);

            //virtual Py::Object getattr_methods(const char* name);
            //virtual int setattr(const char* name, const Py::Object& value);

        private:

            /**
             * Converts a \a Py::Tuple into a \a Kross::Api::List .
             *
             * \param tuple The Py::Tuple to convert.
             * \return The to a Kross::Api::List converted Py::Tuple .
             */
            static Kross::Api::List::Ptr toObject(const Py::Tuple& tuple);

            /**
             * Converts a \a Py::List into a \a Kross::Api::List .
             *
             * \param list The Py::List to convert.
             * \return The to a Kross::Api::List converted Py::List .
             */
            static Kross::Api::List::Ptr toObject(const Py::List& list);

            /**
             * Converts a \a Py::Dict into a \a Kross::Api::Dict .
             *
             * \param dict The Py::Dict to convert.
             * \return The to a Kross::Api::Dict converted Py::Dict .
             */
            static Kross::Api::Dict::Ptr toObject(const Py::Dict& dict);

            /**
             * Converts a \a Py::Object into a \a Kross::Api::Object.
             *
             * \param object The Py::Object to convert.
             * \return The to a Kross::Api::Object converted Py::Object.
             */
            static Kross::Api::Object::Ptr toObject(const Py::Object& object);

            /**
             * Converts a TQString to a Py::Object. If
             * the TQString isNull() then Py::None() will
             * be returned.
             *
             * \param s The TQString to convert.
             * \return The to a Py::String converted TQString.
             */
            static const Py::Object toPyObject(const TQString& s);

            /**
             * Converts a TQStringList to a Py::List.
             *
             * \param list The TQStringList to convert.
             * \return The to a Py::List converted TQStringList.
             */
            static const Py::List toPyObject(const TQStringList& list);

            /**
             * Converts a TQMap to a Py::Dict.
             *
             * \param map The TQMap to convert.
             * \return The to a Py::Dict converted TQMap.
             */
            static const Py::Dict toPyObject(const TQMap<TQString, TQVariant>& map);

            /**
             * Converts a TQValueList to a Py::List.
             *
             * \param list The TQValueList to convert.
             * \return The to a Py::List converted TQValueList.
             */
            static const Py::List toPyObject(const TQValueList<TQVariant>& list);

            /**
             * Converts a TQVariant to a Py::Object.
             *
             * \param variant The TQVariant to convert.
             * \return The to a Py::Object converted TQVariant.
             */
            static const Py::Object toPyObject(const TQVariant& variant);

            /**
             * Converts a \a Kross::Api::Object to a Py::Object.
             *
             * \param object The Kross::Api::Object to convert.
             * \return The to a Py::Object converted Kross::Api::Object.
             */
            static const Py::Object toPyObject(Kross::Api::Object::Ptr object);

            /**
             * Converts a \a Kross::Api::List into a Py::Tuple.
             *
             * \param list The Kross::Api::List to convert.
             * \return The to a Py::Tuple converted Kross::Api::List.
             */
            static const Py::Tuple toPyTuple(Kross::Api::List::Ptr list);

            /// The \a Kross::Api::Object this PythonExtension wraps.
            Kross::Api::Object::Ptr m_object;

            /**
             * The proxymethod which will handle all calls to our
             * \a PythonExtension instance.
             */
            Py::MethodDefExt<PythonExtension>* m_proxymethod;

            /**
             * The static proxy-handler which will be used to dispatch
             * a call to our \a PythonExtension instance and redirect
             * the call to the matching method.
             *
             * \param _self_and_name_tuple A tuple containing as first
             *        argument a reference to our \a PythonExtension
             *        instance.
             * \param _args The optional passed arguments for the method
             *        which should be called.
             * \return The returnvalue of the methodcall.
             */
            static PyObject* proxyhandler(PyObject* _self_and_name_tuple, PyObject* _args);
    };

}}

#endif