summaryrefslogtreecommitdiffstats
path: root/libshiboken/conversions.h
blob: 4d55ef0fe5a61f966eec2df9be3ade3e0704a550 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
 * This file is part of the Shiboken Python Bindings Generator project.
 *
 * Copyright (C) 2009-2010 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 as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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 Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef CONVERSIONS_H
#define CONVERSIONS_H

#include <Python.h>
#include <limits>
#include <memory>
#include <typeinfo>

#include "sbkenum.h"
#include "basewrapper.h"
#include "bindingmanager.h"
#include "sbkdbg.h"

// When the user adds a function with an argument unknown for the typesystem, the generator writes type checks as
// TYPENAME_Check, so this macro allows users to add PyObject arguments to their added functions.
#define PyObject_Check(X) true
#include "autodecref.h"

#define SbkNumber_Check(X) (PyInt_Check(X) || PyFloat_Check(X) || PyLong_Check(X))

namespace Shiboken
{
/**
*   This function template is used to get the PyTypeObject of a C++ type T.
*   All implementations should be provided by template specializations generated by the generator when
*   T isn't a C++ primitive type.
*   \see SpecialCastFunction
*/
template<typename T>
PyTypeObject* SbkType()
{
    return 0;
}

template<> inline PyTypeObject* SbkType<int>() { return &PyInt_Type; }
template<> inline PyTypeObject* SbkType<unsigned int>() { return &PyLong_Type; }
template<> inline PyTypeObject* SbkType<short>() { return &PyInt_Type; }
template<> inline PyTypeObject* SbkType<unsigned short>() { return &PyInt_Type; }
template<> inline PyTypeObject* SbkType<long>() { return &PyLong_Type; }
template<> inline PyTypeObject* SbkType<unsigned long>() { return &PyLong_Type; }
template<> inline PyTypeObject* SbkType<PY_LONG_LONG>() { return &PyLong_Type; }
template<> inline PyTypeObject* SbkType<unsigned PY_LONG_LONG>() { return &PyLong_Type; }
template<> inline PyTypeObject* SbkType<bool>() { return &PyBool_Type; }
template<> inline PyTypeObject* SbkType<float>() { return &PyFloat_Type; }
template<> inline PyTypeObject* SbkType<double>() { return &PyFloat_Type; }
template<> inline PyTypeObject* SbkType<char>() { return &PyInt_Type; }
template<> inline PyTypeObject* SbkType<signed char>() { return &PyInt_Type; }
template<> inline PyTypeObject* SbkType<unsigned char>() { return &PyInt_Type; }

template<typename T>
struct SbkTypeInfo {
    static const bool isCppWrapper = false;
};

/**
 *   This struct template is used to copy a C++ object using the proper
 *   constructor, which could be the same type as used on the wrapped library
 *   or a C++ wrapper type provided by the binding.
 */
template <typename T, bool hasWrapper = SbkTypeInfo<T>::isCppWrapper>
struct CppObjectCopier
{
    static inline T* copy(const T& obj);
};

template<typename T>
struct CppObjectCopier<T, false>
{
    static inline T* copy(const T& obj)
    {
        return new T(obj);
    }
};

template<typename T>
struct CppObjectCopier<T, true>
{
    static inline T* copy(const T& obj)
    {
        return reinterpret_cast<T*>(reinterpret_cast<SbkObjectType*>(SbkType<T>())->obj_copier(&obj));
    }
};


/**
 * Convenience template to create wrappers using the proper Python type for a given C++ class instance.
 */
template<typename T>
inline PyObject* createWrapper(const T* cppobj, bool hasOwnership = false, bool isExactType = false)
{
    const char* typeName = 0;
    if (!isExactType)
        typeName = typeid(*const_cast<T*>(cppobj)).name();
    return Wrapper::newObject(reinterpret_cast<SbkObjectType*>(SbkType<T>()),
                              const_cast<T*>(cppobj), hasOwnership, isExactType, typeName);
}

// Base Conversions ----------------------------------------------------------
// The basic converter must be empty to avoid object types being converted by value.
template <typename T> struct Converter {};

// Pointer conversion specialization for value types.
template <typename T>
struct Converter<T*>
{
    static inline bool checkType(PyObject* pyObj)
    {
        return Converter<T>::checkType(pyObj);
    }

    static inline bool isConvertible(PyObject* pyObj)
    {
        return pyObj == Py_None || PyObject_TypeCheck(pyObj, SbkType<T>());
    }

    static PyObject* toPython(const T* cppobj)
    {
        if (!cppobj)
            Py_RETURN_NONE;
        PyObject* pyobj = reinterpret_cast<PyObject*>(BindingManager::instance().retrieveWrapper(cppobj));
        if (pyobj)
            Py_INCREF(pyobj);
        else
            pyobj = createWrapper<T>(cppobj);
        return pyobj;
    }

    static T* toCpp(PyObject* pyobj)
    {
        if (PyObject_TypeCheck(pyobj, SbkType<T>()))
            return (T*) Wrapper::cppPointer(reinterpret_cast<SbkObject*>(pyobj), SbkType<T>());
        else if (Converter<T>::isConvertible(pyobj))
            return CppObjectCopier<T>::copy(Converter<T>::toCpp(pyobj));
        else if (pyobj == Py_None)
            return 0;

        assert(false);
        return 0;
    }
};
template <typename T> struct Converter<const T*> : Converter<T*> {};

// Specialization for reference conversions.
template <typename T>
struct Converter<T&>
{
    static inline bool checkType(PyObject* pyObj) { return Converter<T>::checkType(pyObj); }
    static inline bool isConvertible(PyObject* pyObj) { return Converter<T>::isConvertible(pyObj); }
    static inline PyObject* toPython(const T& cppobj) { return Converter<T*>::toPython(&cppobj); }
    static inline T& toCpp(PyObject* pyobj) { return *Converter<T*>::toCpp(pyobj); }
};

template <typename T>
struct Converter<const T&> : Converter<T&>
{
    static inline PyObject* toPython(const T& cppobj)
    {
        T* cpy = CppObjectCopier<T>::copy(cppobj);
        return createWrapper<T>(cpy);
    }
};

// Void pointer conversions.
template<>
struct Converter<void*>
{
    static inline bool checkType(PyObject* pyObj) { return false; }
    static inline bool isConvertible(PyObject* pyobj) { return true; }
    static PyObject* toPython(void* cppobj)
    {
        if (!cppobj)
            Py_RETURN_NONE;
        PyObject* result = (PyObject*) cppobj;
        Py_INCREF(result);
        return result;
    }
    static void* toCpp(PyObject* pyobj) { return pyobj; }
};
template <> struct Converter<const void*> : Converter<void*> {};

// Base converter meant to be inherited by converters for classes that could be
// passed by value.
// Example: "struct Converter<ValueTypeClass> : ValueTypeConverter<ValueTypeClass>"
template <typename T>
struct ValueTypeConverter
{
    static inline bool checkType(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<T>()); }

    // The basic version of this method also tries to use the extended 'isConvertible' method.
    static inline bool isConvertible(PyObject* pyobj)
    {
        if (PyObject_TypeCheck(pyobj, SbkType<T>()))
            return true;
        SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType<T>());
        if (shiboType->ext_isconvertible)
            return shiboType->ext_isconvertible(pyobj);
        return false;
    }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<T*>(cppobj)); }
    static inline PyObject* toPython(const T& cppobj)
    {
        PyObject* obj = createWrapper<T>(new T(cppobj), true, true);
//         SbkBaseWrapper_setContainsCppWrapper(obj, SbkTypeInfo<T>::isCppWrapper);
        return obj;
    }
    // Classes with implicit conversions are expected to reimplement 'toCpp' to build T from
    // its various implicit constructors. Even classes without implicit conversions could
    // get some of those via other modules defining conversion operator for them, thus
    // the basic Converter for value types checks for extended conversion and tries to
    // use them if it is the case.
    static inline T toCpp(PyObject* pyobj)
    {
        if (!PyObject_TypeCheck(pyobj, SbkType<T>())) {
            SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(SbkType<T>());
            if (shiboType->ext_tocpp && isConvertible(pyobj)) {
                T* cptr = reinterpret_cast<T*>(shiboType->ext_tocpp(pyobj));
                std::auto_ptr<T> cptr_auto_ptr(cptr);
                return *cptr;
            }
            assert(false);
        }
        return *reinterpret_cast<T*>(Wrapper::cppPointer(reinterpret_cast<SbkObject*>(pyobj), SbkType<T>()));
    }
};

// Base converter meant to be inherited by converters for abstract classes and object types
// (i.e. classes with private copy constructors and = operators).
// Example: "struct Converter<AbstractClass*> : ObjectTypeConverter<AbstractClass>"
template <typename T>
struct ObjectTypeConverter
{
    static inline bool checkType(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<T>()); }
    /// Py_None objects are the only objects convertible to an object type (in the form of a NULL pointer).
    static inline bool isConvertible(PyObject* pyObj) { return pyObj == Py_None || PyObject_TypeCheck(pyObj, SbkType<T>()); }
    /// Convenience overload that calls "toPython(const T*)" method.
    static inline PyObject* toPython(void* cppobj) { return toPython(reinterpret_cast<T*>(cppobj)); }
    /// Returns a new Python wrapper for the C++ object or an existing one with its reference counter incremented.
    static PyObject* toPython(const T* cppobj)
    {
        if (!cppobj)
            Py_RETURN_NONE;
        PyObject* pyobj = reinterpret_cast<PyObject*>(BindingManager::instance().retrieveWrapper(cppobj));
        if (pyobj)
            Py_INCREF(pyobj);
        else
            pyobj = createWrapper<T>(cppobj);
        return pyobj;
    }
    /// Returns the wrapped C++ pointer casted properly, or a NULL pointer if the argument is a Py_None.
    static T* toCpp(PyObject* pyobj)
    {
        if (pyobj == Py_None)
            return 0;
        SbkObjectType* shiboType = reinterpret_cast<SbkObjectType*>(pyobj->ob_type);
        if (shiboType->mi_specialcast)
            return (T*) shiboType->mi_specialcast(
                                    Wrapper::cppPointer(reinterpret_cast<SbkObject*>(pyobj), SbkType<T>()),
                                    reinterpret_cast<SbkObjectType*>(SbkType<T>()));
        return (T*) Wrapper::cppPointer(reinterpret_cast<SbkObject*>(pyobj), SbkType<T>());
    }
};

template <typename T>
struct ObjectTypeReferenceConverter : ObjectTypeConverter<T>
{
    static inline bool checkType(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<T>()); }
    static inline bool isConvertible(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<T>()); }
    static inline PyObject* toPython(const T& cppobj) { return Converter<T*>::toPython(&cppobj); }
    static inline T& toCpp(PyObject* pyobj)
    {
        T* t = Converter<T*>::toCpp(pyobj);
        assert(t);
        return *t;
    }
};

// PyObject* specialization to avoid converting what doesn't need to be converted.
template<>
struct Converter<PyObject*> : ObjectTypeConverter<PyObject*>
{
    static inline PyObject* toCpp(PyObject* pyobj) { return pyobj; }
};
template <> struct Converter<const PyObject*> : Converter<PyObject*> {};

// Primitive Conversions ------------------------------------------------------
template <>
struct Converter<bool>
{
    static inline bool checkType(PyObject* pyobj) { return PyBool_Check(pyobj); }
    static inline bool isConvertible(PyObject* pyobj) { return PyInt_Check(pyobj); }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<bool*>(cppobj)); }
    static inline PyObject* toPython(bool cppobj) { return PyBool_FromLong(cppobj); }
    static inline bool toCpp(PyObject* pyobj) { return PyInt_AS_LONG(pyobj); }
};

/**
 * Helper template for checking if a value overflows when casted to type T
 */
template<typename T, bool isSigned = std::numeric_limits<T>::is_signed >
struct OverFlowChecker;

template<typename T>
struct OverFlowChecker<T, true>
{
    static bool check(const PY_LONG_LONG& value)
    {
        return value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max();
    }
};

template<typename T>
struct OverFlowChecker<T, false>
{
    static bool check(const PY_LONG_LONG& value)
    {
        return value < 0 || static_cast<unsigned long long>(value) > std::numeric_limits<T>::max();
    }
};

template<>
struct OverFlowChecker<PY_LONG_LONG, true>
{
    static bool check(const PY_LONG_LONG& value)
    {
        return false;
    }
};

template<>
struct OverFlowChecker<double, true>
{
    static bool check(const double& value)
    {
        return false;
    }
};

template<>
struct OverFlowChecker<float, true>
{
    static bool check(const double& value)
    {
        return value < std::numeric_limits<float>::min() || value > std::numeric_limits<float>::max();
    }
};

template <typename PyIntEquiv>
struct Converter_PyInt
{
    static inline bool checkType(PyObject* pyobj) { return PyInt_Check(pyobj); }
    static inline bool isConvertible(PyObject* pyobj) { return SbkNumber_Check(pyobj); }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<PyIntEquiv*>(cppobj)); }
    static inline PyObject* toPython(const PyIntEquiv& cppobj) { return PyInt_FromLong((long) cppobj); }
    static PyIntEquiv toCpp(PyObject* pyobj)
    {
        if (PyFloat_Check(pyobj)) {
            double d_result = PyFloat_AS_DOUBLE(pyobj);
            // If cast to long directly it could overflow silently
            if (OverFlowChecker<PyIntEquiv>::check(d_result))
                PyErr_SetObject(PyExc_OverflowError, 0);
            return static_cast<PyIntEquiv>(d_result);
        } else {
            PY_LONG_LONG result = PyLong_AsLongLong(pyobj);
            if (OverFlowChecker<PyIntEquiv>::check(result))
                PyErr_SetObject(PyExc_OverflowError, 0);
            return static_cast<PyIntEquiv>(result);
        }
    }
};

template <typename T>
struct Converter_PyULongInt : Converter_PyInt<T>
{
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<T*>(cppobj)); }
    static inline PyObject* toPython(const T& cppobj) { return PyLong_FromUnsignedLong(cppobj); }
};


/// Check if we can treat the pyobj as a char, i.e. it's a number or a string with just one character.
#define SbkChar_Check(pyobj) (SbkNumber_Check(pyobj) || (PyString_Check(pyobj) && PyString_Size(pyobj) == 1))

/// Specialization to convert char and unsigned char, it accepts Python numbers and strings with just one character.
template <typename CharType>
struct CharConverter
{
    static inline bool checkType(PyObject* pyobj) { return SbkChar_Check(pyobj); }
    static inline bool isConvertible(PyObject* pyobj) { return SbkChar_Check(pyobj); }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<CharType*>(cppobj)); }
    static inline PyObject* toPython(const CharType& cppobj) { return PyInt_FromLong(cppobj); }
    static CharType toCpp(PyObject* pyobj)
    {
        if (PyString_Check(pyobj)) {
            assert(PyString_Size(pyobj) == 1); // This check is made on SbkChar_Check
            return PyString_AS_STRING(pyobj)[0];
        } else {
            PY_LONG_LONG result = PyLong_AsLongLong(pyobj);
            if (OverFlowChecker<CharType>::check(result))
                PyErr_SetObject(PyExc_OverflowError, 0);
            return result;
        }
    }
};

template <> struct Converter<unsigned long> : Converter_PyULongInt<unsigned long> {};
template <> struct Converter<unsigned int> : Converter_PyULongInt<unsigned int> {};
template <> struct Converter<char> : CharConverter<char> {
    // Should we really return a string?
    using CharConverter<char>::toPython;
    static inline PyObject* toPython(const char& cppObj) {
        return PyString_FromFormat("%c", cppObj);
    }
};
template <> struct Converter<signed char> : CharConverter<signed char> {};
template <> struct Converter<unsigned char> : CharConverter<unsigned char> {};
template <> struct Converter<int> : Converter_PyInt<int> {};
template <> struct Converter<short> : Converter_PyInt<short> {};
template <> struct Converter<unsigned short> : Converter_PyInt<unsigned short> {};
template <> struct Converter<long> : Converter_PyInt<long> {};

template <>
struct Converter<PY_LONG_LONG>
{
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<PY_LONG_LONG*>(cppobj)); }
    static inline PyObject* toPython(PY_LONG_LONG cppobj) { return PyLong_FromLongLong(cppobj); }
    static inline PY_LONG_LONG toCpp(PyObject* pyobj) { return (PY_LONG_LONG) PyLong_AsLongLong(pyobj); }
};

template <>
struct Converter<unsigned PY_LONG_LONG>
{
    static inline PyObject* toPython(void* cppobj)
    {
        return toPython(*reinterpret_cast<unsigned PY_LONG_LONG*>(cppobj));
    }
    static inline PyObject* toPython(unsigned PY_LONG_LONG cppobj)
    {
        return PyLong_FromUnsignedLongLong(cppobj);
    }
    static inline unsigned PY_LONG_LONG toCpp(PyObject* pyobj)
    {
        return (unsigned PY_LONG_LONG) PyLong_AsUnsignedLongLong(pyobj);
    }
};

template <typename PyFloatEquiv>
struct Converter_PyFloat
{
    static inline bool checkType(PyObject* obj) { return PyFloat_Check(obj); }
    static inline bool isConvertible(PyObject* obj) { return SbkNumber_Check(obj); }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<PyFloatEquiv*>(cppobj)); }
    static inline PyObject* toPython(PyFloatEquiv cppobj) { return PyFloat_FromDouble((double) cppobj); }
    static inline PyFloatEquiv toCpp(PyObject* pyobj)
    {
        if (PyInt_Check(pyobj) || PyLong_Check(pyobj))
            return (PyFloatEquiv) PyLong_AsLong(pyobj);
        return (PyFloatEquiv) PyFloat_AsDouble(pyobj);
    }
};

template <> struct Converter<float> : Converter_PyFloat<float> {};
template <> struct Converter<double> : Converter_PyFloat<double> {};

// PyEnum Conversions ---------------------------------------------------------
template <typename CppEnum>
struct EnumConverter
{
    static inline bool checkType(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<CppEnum>()); }
    static inline bool isConvertible(PyObject* pyObj) { return PyObject_TypeCheck(pyObj, SbkType<CppEnum>()); }
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<CppEnum*>(cppobj)); }
    static inline PyObject* toPython(CppEnum cppenum)
    {
        return Shiboken::Enum::newItem(Shiboken::SbkType<CppEnum>(), (long) cppenum);
    }
    static inline CppEnum toCpp(PyObject* pyObj)
    {
        return (CppEnum) Shiboken::Enum::getValue(pyObj);;
    }
};

// C Sting Types --------------------------------------------------------------
template <typename CString>
struct Converter_CString
{
    static inline bool checkType(PyObject* pyObj) { return PyString_Check(pyObj); }
    static inline bool isConvertible(PyObject* pyObj) { return pyObj == Py_None || PyString_Check(pyObj); }
    static inline PyObject* toPython(void* cppobj) { return toPython(reinterpret_cast<CString>(cppobj)); }
    static inline PyObject* toPython(CString cppobj)
    {
        if (!cppobj)
            Py_RETURN_NONE;
        return PyString_FromString(cppobj);
    }
    static inline CString toCpp(PyObject* pyobj) {
        if (pyobj == Py_None)
            return 0;
        return PyString_AsString(pyobj);
    }
};

template <> struct Converter<const char*> : Converter_CString<const char*> {};

template <> struct Converter<std::string> : Converter_CString<std::string>
{
    static inline PyObject* toPython(void* cppobj) { return toPython(*reinterpret_cast<std::string*>(cppobj)); }
    static inline PyObject* toPython(std::string cppObj)
    {
        return PyString_FromString(cppObj.c_str());
    }

    static inline std::string toCpp(PyObject* pyobj)
    {
        if (pyobj == Py_None)
            return 0;
        return std::string(PyString_AsString(pyobj));
    }
};

// C++ containers -------------------------------------------------------------
// The following container converters are meant to be used for pairs, lists and maps
// that are similar to the STL containers of the same name.

// For example to create a converter for a std::list the following code is enough:
// template<typename T> struct Converter<std::list<T> > : StdListConverter<std::list<T> > {};

// And this for a std::map:
// template<typename KT, typename VT>
// struct Converter<std::map<KT, VT> > : StdMapConverter<std::map<KT, VT> > {};

template <typename StdList>
struct StdListConverter
{
    static inline bool checkType(PyObject* pyObj)
    {
        return isConvertible(pyObj);
    }

    static inline bool isConvertible(PyObject* pyObj)
    {
        if (PyObject_TypeCheck(pyObj, SbkType<StdList>()))
            return true;
        // Sequence conversion are made ONLY for python sequences, not for
        // binded types implementing sequence protocol, otherwise this will
        // cause a mess like QBitArray being accepted by someone expecting a
        // QStringList.
        if ((SbkType<StdList>() && isShibokenType(pyObj)) || !PySequence_Check(pyObj))
            return false;
        for (int i = 0, max = PySequence_Length(pyObj); i < max; ++i) {
            AutoDecRef item(PySequence_GetItem(pyObj, i));
            if (!Converter<typename StdList::value_type>::isConvertible(item))
                return false;
        }
        return true;
    }
    static PyObject* toPython(void* cppObj) { return toPython(*reinterpret_cast<StdList*>(cppObj)); }
    static PyObject* toPython(const StdList& cppobj)
    {
        PyObject* result = PyList_New((int) cppobj.size());
        typename StdList::const_iterator it = cppobj.begin();
        for (int idx = 0; it != cppobj.end(); ++it, ++idx) {
            typename StdList::value_type vh(*it);
            PyList_SET_ITEM(result, idx, Converter<typename StdList::value_type>::toPython(vh));
        }
        return result;
    }
    static StdList toCpp(PyObject* pyobj)
    {
        if (PyObject_TypeCheck(pyobj, SbkType<StdList>()))
            return *reinterpret_cast<StdList*>(Wrapper::cppPointer(reinterpret_cast<SbkObject*>(pyobj), SbkType<StdList>()));

        StdList result;
        for (int i = 0; i < PySequence_Size(pyobj); i++) {
            AutoDecRef pyItem(PySequence_GetItem(pyobj, i));
            result.push_back(Converter<typename StdList::value_type>::toCpp(pyItem));
        }
        return result;
    }
};

template <typename StdPair>
struct StdPairConverter
{
    static inline bool checkType(PyObject* pyObj)
    {
        return isConvertible(pyObj);
    }

    static inline bool isConvertible(PyObject* pyObj)
    {
        if (PyObject_TypeCheck(pyObj, SbkType<StdPair>()))
            return true;
        if ((SbkType<StdPair>() && isShibokenType(pyObj)) || !PySequence_Check(pyObj) || PySequence_Length(pyObj) != 2)
            return false;

        AutoDecRef item1(PySequence_GetItem(pyObj, 0));
        AutoDecRef item2(PySequence_GetItem(pyObj, 1));

        if (!Converter<typename StdPair::first_type>::isConvertible(item1)
            && !Converter<typename StdPair::second_type>::isConvertible(item2)) {
            return false;
        }
        return true;
    }
    static PyObject* toPython(void* cppObj) { return toPython(*reinterpret_cast<StdPair*>(cppObj)); }
    static PyObject* toPython(const StdPair& cppobj)
    {
        typename StdPair::first_type first(cppobj.first);
        typename StdPair::second_type second(cppobj.second);
        PyObject* tuple = PyTuple_New(2);
        PyTuple_SET_ITEM(tuple, 0, Converter<typename StdPair::first_type>::toPython(first));
        PyTuple_SET_ITEM(tuple, 1, Converter<typename StdPair::second_type>::toPython(second));
        return tuple;
    }
    static StdPair toCpp(PyObject* pyobj)
    {
        StdPair result;
        AutoDecRef pyFirst(PySequence_GetItem(pyobj, 0));
        AutoDecRef pySecond(PySequence_GetItem(pyobj, 1));
        result.first = Converter<typename StdPair::first_type>::toCpp(pyFirst);
        result.second = Converter<typename StdPair::second_type>::toCpp(pySecond);
        return result;
    }
};

template <typename StdMap>
struct StdMapConverter
{
    static inline bool checkType(PyObject* pyObj)
    {
        return isConvertible(pyObj);
    }

    static inline bool isConvertible(PyObject* pyObj)
    {
        if (PyObject_TypeCheck(pyObj, SbkType<StdMap>()))
            return true;
        if ((SbkType<StdMap>() && isShibokenType(pyObj)) || !PyDict_Check(pyObj))
            return false;

        PyObject* key;
        PyObject* value;
        Py_ssize_t pos = 0;

        while (PyDict_Next(pyObj, &pos, &key, &value)) {
            if (!Converter<typename StdMap::key_type>::isConvertible(key)
                && !Converter<typename StdMap::mapped_type>::isConvertible(value)) {
                return false;
            }
        }
        return true;
    }

    static PyObject* toPython(void* cppObj) { return toPython(*reinterpret_cast<StdMap*>(cppObj)); }
    static PyObject* toPython(const StdMap& cppobj)
    {
        PyObject* result = PyDict_New();
        typename StdMap::const_iterator it = cppobj.begin();

        for (; it != cppobj.end(); ++it) {
            PyDict_SetItem(result,
                           Converter<typename StdMap::key_type>::toPython(it->first),
                           Converter<typename StdMap::mapped_type>::toPython(it->second));
        }

        return result;
    }
    static StdMap toCpp(PyObject* pyobj)
    {
        StdMap result;

        PyObject* key;
        PyObject* value;
        Py_ssize_t pos = 0;

        while (PyDict_Next(pyobj, &pos, &key, &value)) {
            result.insert(typename StdMap::value_type(
                    Converter<typename StdMap::key_type>::toCpp(key),
                    Converter<typename StdMap::mapped_type>::toCpp(value)));
        }
        return result;
    }
};


// class used to translate python objects to another type
template <typename T> struct PythonConverter {};

} // namespace Shiboken

#endif // CONVERSIONS_H