aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/pybind/pod.h
blob: 90165fdbec63ebe6061edefb223c53d854f5a879 (plain) (blame)
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
#pragma once

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "attr.h"
#include "typedesc.h"

namespace NPyBind {
    struct TPOD {
        TPyObjectPtr Dict;

        TPOD()
            : Dict(PyDict_New(), true)
        {
        }
        bool SetAttr(const char* name, PyObject* value) {
            return PyDict_SetItemString(Dict.Get(), name, value) == 0;
        }
        PyObject* GetAttr(const char* name) const {
            PyObject* res = PyDict_GetItemString(Dict.Get(), name);
            Py_XINCREF(res);
            return res;
        }
    };

    class TPODTraits: public NPyBind::TPythonType<TPOD, TPOD, TPODTraits> {
    private:
        typedef TPythonType<TPOD, TPOD, TPODTraits> MyParent;
        friend class TPythonType<TPOD, TPOD, TPODTraits>;
        TPODTraits();

    public:
        static TPOD* GetObject(TPOD& obj) {
            return &obj;
        }
    };

    template <>
    inline bool FromPyObject<TPOD*>(PyObject* obj, TPOD*& res) {
        res = TPODTraits::CastToObject(obj);
        if (res == nullptr)
            return false;
        return true;
    }
    template <>
    inline bool FromPyObject<const TPOD*>(PyObject* obj, const TPOD*& res) {
        res = TPODTraits::CastToObject(obj);
        if (res == nullptr)
            return false;
        return true;
    }

}