QPy
Python-Qt dynamic bindings
|
00001 #pragma once 00002 // QPy - Copyright (c) 2012,2013 Ugo Varetto 00003 // All rights reserved. 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are met: 00007 // * Redistributions of source code must retain the above copyright 00008 // notice, this list of conditions and the following disclaimer. 00009 // * Redistributions in binary form must reproduce the above copyright 00010 // notice, this list of conditions and the following disclaimer in the 00011 // documentation and/or other materials provided with the distribution. 00012 // * Neither the name of the author and copyright holder nor the 00013 // names of contributors to the project may be used to endorse or promote products 00014 // derived from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 // DISCLAIMED. IN NO EVENT SHALL UGO VARETTO BE LIABLE FOR ANY 00020 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00030 00031 #include <Python.h> 00032 #include <stdexcept> 00033 #include <QString> 00034 #include <QVariantMap> 00035 #include <QVariantList> 00036 #include <QString> 00037 #include <QList> 00038 #include <QGenericArgument> 00039 #include <QGenericReturnArgument> 00040 #include <QVector> 00041 #include <QVariant> 00042 00043 #include "../PyQArgConstructor.h" 00044 #include "../PyArgConstructor.h" 00045 00047 namespace qpy { 00048 00049 00051 class IntQArgConstructor : public QArgConstructor { 00052 public: 00058 QGenericArgument Create( PyObject* pyobj ) const { 00059 i_ = PyInt_AsLong( pyobj ); 00060 return Q_ARG( int, i_ ); 00061 } 00063 IntQArgConstructor* Clone() const { 00064 return new IntQArgConstructor( *this ); 00065 } 00066 private: 00068 mutable int i_; 00069 }; 00071 class StringQArgConstructor : public QArgConstructor { 00072 public: 00078 QGenericArgument Create( PyObject* pyobj ) const { 00079 s_ = PyString_AsString( pyobj ); 00080 return Q_ARG( QString, s_ ); 00081 } 00083 StringQArgConstructor* Clone() const { 00084 return new StringQArgConstructor( *this ); 00085 } 00086 private: 00088 mutable QString s_; 00089 }; 00091 class DoubleQArgConstructor : public QArgConstructor { 00092 public: 00098 QGenericArgument Create( PyObject* pyobj ) const { 00099 d_ = PyFloat_AsDouble( pyobj ); 00100 return Q_ARG( double, d_ ); 00101 } 00103 DoubleQArgConstructor* Clone() const { 00104 return new DoubleQArgConstructor( *this ); 00105 } 00106 private: 00108 mutable double d_; 00109 }; 00111 class FloatQArgConstructor : public QArgConstructor { 00112 public: 00118 QGenericArgument Create( PyObject* pyobj ) const { 00119 f_ = float( PyFloat_AsDouble( pyobj ) ); 00120 return Q_ARG( float, f_ ); 00121 } 00123 FloatQArgConstructor* Clone() const { 00124 return new FloatQArgConstructor( *this ); 00125 } 00126 private: 00128 mutable float f_; 00129 }; 00131 class VoidStarQArgConstructor : public QArgConstructor { 00132 public: 00138 QGenericArgument Create( PyObject* pyobj ) const { 00139 i_ = PyLong_AsVoidPtr( pyobj ); 00140 return Q_ARG( void*, i_ ); 00141 } 00143 VoidStarQArgConstructor* Clone() const { 00144 return new VoidStarQArgConstructor( *this ); 00145 } 00146 private: 00148 mutable void* i_; 00149 }; 00151 class ObjectStarQArgConstructor : public QArgConstructor { 00152 public: 00158 QGenericArgument Create( PyObject* pyobj ) const; 00159 00161 ObjectStarQArgConstructor* Clone() const { 00162 return new ObjectStarQArgConstructor( *this ); 00163 } 00164 private: 00166 mutable QObject* obj_; 00167 }; 00168 00170 class IntPyArgConstructor : public PyArgConstructor { 00171 public: 00172 IntPyArgConstructor() { 00173 SetArg( i_ ); 00174 } 00175 IntPyArgConstructor( const IntPyArgConstructor& other ) : i_( other.i_ ) { 00176 SetArg( i_ ); 00177 } 00178 PyObject* Create( void* p ) const { 00179 int i = *reinterpret_cast< int* >( p ); 00180 return PyInt_FromLong( i ); 00181 } 00182 PyObject* Create() const { 00183 return PyInt_FromLong( i_ ); 00184 } 00185 IntPyArgConstructor* Clone() const { 00186 return new IntPyArgConstructor( *this ); 00187 } 00188 bool IsQObjectPtr() const { return false; } 00189 QMetaType::Type Type() const { return QMetaType::Int; } 00190 private: 00191 int i_; 00192 }; 00194 class StringPyArgConstructor : public PyArgConstructor { 00195 public: 00196 StringPyArgConstructor() { 00197 SetArg( s_ ); 00198 } 00199 StringPyArgConstructor( const StringPyArgConstructor& other ) : s_( other.s_ ) { 00200 SetArg( s_ ); 00201 } 00202 PyObject* Create( void* p ) const { 00203 QString s = *reinterpret_cast< QString* >( p ); 00204 return PyString_FromString( s.toAscii().constData() ); 00205 } 00206 PyObject* Create() const { 00207 return PyString_FromString( s_.toAscii().constData() ); 00208 } 00209 StringPyArgConstructor* Clone() const { 00210 return new StringPyArgConstructor( *this ); 00211 } 00212 bool IsQObjectPtr() const { return false; } 00213 QMetaType::Type Type() const { return QMetaType::QString; } 00214 private: 00215 QString s_; 00216 }; 00218 class DoublePyArgConstructor : public PyArgConstructor { 00219 public: 00220 DoublePyArgConstructor() { 00221 SetArg( d_ ); 00222 } 00223 DoublePyArgConstructor( const DoublePyArgConstructor& other ) : d_( other.d_ ) { 00224 SetArg( d_ ); 00225 } 00226 PyObject* Create( void* p ) const { 00227 double d = *reinterpret_cast< double* >( p ); 00228 return PyFloat_FromDouble( d ); 00229 } 00230 PyObject* Create() const { 00231 return PyFloat_FromDouble( d_ ); 00232 } 00233 DoublePyArgConstructor* Clone() const { 00234 return new DoublePyArgConstructor( *this ); 00235 } 00236 bool IsQObjectPtr() const { return false; } 00237 QMetaType::Type Type() const { return QMetaType::Double; } 00238 private: 00239 double d_; 00240 }; 00242 class FloatPyArgConstructor : public PyArgConstructor { 00243 public: 00244 FloatPyArgConstructor() { 00245 SetArg( f_ ); 00246 } 00247 FloatPyArgConstructor( const FloatPyArgConstructor& other ) : f_( other.f_ ) { 00248 SetArg( f_ ); 00249 } 00250 PyObject* Create( void* p ) const { 00251 float f = *reinterpret_cast< float* >( p ); 00252 return PyFloat_FromDouble( f ); 00253 } 00254 PyObject* Create() const { 00255 return PyFloat_FromDouble( f_ ); 00256 } 00257 FloatPyArgConstructor* Clone() const { 00258 return new FloatPyArgConstructor( *this ); 00259 } 00260 bool IsQObjectPtr() const { return false; } 00261 QMetaType::Type Type() const { return QMetaType::Float; } 00262 private: 00263 float f_; 00264 }; 00265 00267 class VoidPyArgConstructor : public PyArgConstructor { 00268 public: 00269 PyObject* Create() const { 00270 Py_RETURN_NONE; 00271 } 00272 PyObject* Create( void* ) const { return 0; } 00273 VoidPyArgConstructor* Clone() const { 00274 return new VoidPyArgConstructor( *this ); 00275 } 00276 bool IsQObjectPtr() const { return false; } 00277 QMetaType::Type Type() const { return QMetaType::Void; } 00278 }; 00279 00281 class ObjectStarPyArgConstructor : public PyArgConstructor { 00282 public: 00283 ObjectStarPyArgConstructor() { 00284 SetArg( obj_ ); 00285 } 00286 ObjectStarPyArgConstructor( const ObjectStarPyArgConstructor& other ) : obj_( other.obj_ ) { 00287 SetArg( obj_ ); 00288 } 00289 PyObject* Create() const { 00290 Py_RETURN_NONE; 00291 } 00292 PyObject* Create( void* ) const { 00293 Py_RETURN_NONE; 00294 } 00295 ObjectStarPyArgConstructor* Clone() const { 00296 return new ObjectStarPyArgConstructor( *this ); 00297 } 00298 bool IsQObjectPtr() const { return true; } 00299 QMetaType::Type Type() const { return QMetaType::QObjectStar; } 00300 private: 00301 QObject* obj_; 00302 }; 00303 00304 }