aboutsummaryrefslogtreecommitdiff
path: root/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_qflags.h
blob: f246ed32d80e971ed71ae316ffc3b0156ad1cbf7 (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
#ifndef PYTHON_PYBIND11_QT_QFLAGS_HPP
#define PYTHON_PYBIND11_QT_QFLAGS_HPP

#include <QFlags>

#include <pybind11/pybind11.h>

namespace pybind11::detail {

    // QFlags
    //
    template <class T>
    struct type_caster<QFlags<T>> {
        PYBIND11_TYPE_CASTER(QFlags<T>, const_name("QFlags[") + make_caster<T>::name +
                                            const_name("]"));

        /**
         * Conversion part 1 (Python->C++): convert a PyObject into a QString
         * instance or return false upon failure. The second argument
         * indicates whether implicit conversions should be applied.
         */
        bool load(handle src, bool)
        {
            PyObject* tmp = PyNumber_Long(src.ptr());

            if (!tmp) {
                return false;
            }

            // we do an intermediate extraction to T but this actually
            // can contains multiple values
            T flag_value = static_cast<T>(PyLong_AsLong(tmp));
            Py_DECREF(tmp);

            value = QFlags<T>(flag_value);

            return !PyErr_Occurred();
        }

        /**
         * Conversion part 2 (C++ -> Python): convert an QString instance into
         * a Python object. The second and third arguments are used to
         * indicate the return value policy and parent object (for
         * ``return_value_policy::reference_internal``) and are generally
         * ignored by implicit casters.
         */
        static handle cast(QFlags<T> const& src, return_value_policy /* policy */,
                           handle /* parent */)
        {
            return PyLong_FromLong(static_cast<int>(src));
        }
    };

}  // namespace pybind11::detail

#endif