summaryrefslogtreecommitdiffstats
path: root/src/cython/wrapped-pyobject.h
blob: bd316fb6a00959765e4c94a0e962b8e3aa518a8e (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
#include "Python.h"
#include "2geom/generic-interval.h" 
#include "2geom/generic-rect.h"
#include "2geom/d2.h"


namespace Geom{

//TODO! looks like memory leak
class WrappedPyObject{
private:
    PyObject* self;
public:
    //~ int code;

    WrappedPyObject(){
        //~ printf("Created empty WPO at address %p\n", this);
        self = NULL;
        //~ code = 0;
    }
    
    WrappedPyObject(WrappedPyObject const &other){
        self = other.getObj();
        //~ code = other.code;
        //~ printf("COPY-CONSTRUCTOR %p, this WPO %p, other WPO %p\n", self, this, &other);
        Py_XINCREF(self);
    }
    
    WrappedPyObject(PyObject* arg){
        if (arg == NULL){
            //PyErr_Print();
            //~ code = c;
        }
        else{
            //~ printf("CONSTRUCTOR %p\n", arg);
            Py_INCREF(arg);
            self = arg;
            //~ code = c;
        }
    }
    
    WrappedPyObject(int c){
        self = Py_BuildValue("i", c);
        //~ printf("INT-OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
        Py_INCREF(self);
    }
        
    
    ~WrappedPyObject(){
        //TODO Leaking memory
        //~ printf("DECREF %p\n", self);
        //Py_DECREF(self);
    }
    
    PyObject* getObj() const {
        return self;
    }
    
    WrappedPyObject operator=(WrappedPyObject other){
        if (this != &other){
            self = other.getObj();
            //~ printf("OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
            Py_XINCREF(self);
        }
        return *this;
    }
    
    WrappedPyObject operator=(int other){
        
            self = Py_BuildValue("i", other);
            //~ printf("INT-OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
            Py_INCREF(self);
        
        return *this;
    }
    
    WrappedPyObject operator-() const {
        PyObject * ret;
        ret = PyObject_CallMethodObjArgs(self, Py_BuildValue("s", "__neg__"), NULL);
        if (ret == NULL){
            Py_INCREF(Py_None);
            return WrappedPyObject(Py_None);
            }
        //Py_INCREF(ret);
        WrappedPyObject * retw = new WrappedPyObject(ret);
        //Py_DECREF(ret);
        return *retw;
    }
    
    WrappedPyObject arithmetic(PyObject* const other, std::string method, std::string rmethod) const {
        PyObject * ret;
        //printf("%p %p\n", self, other);
        ret = PyObject_CallMethodObjArgs(self, Py_BuildValue("s", method.c_str()), other, NULL);
        if (ret == NULL){
            Py_INCREF(Py_None);
            return WrappedPyObject(Py_None);
            }
        PyObject * isNI = PyObject_RichCompare(ret, Py_NotImplemented, Py_EQ);
        if ( PyInt_AsLong(isNI) ){
            ret = PyObject_CallMethodObjArgs(other, Py_BuildValue("s", rmethod.c_str()), self, NULL);
            }
        if (ret == NULL){
            Py_INCREF(Py_None);
            return WrappedPyObject(Py_None);
            }
        WrappedPyObject * retw = new WrappedPyObject(ret);
        return *retw;
        
        }
    
    WrappedPyObject operator+(WrappedPyObject const other) const {
        return arithmetic(other.getObj(), "__add__", "__radd__");
    }

    WrappedPyObject operator-(WrappedPyObject const other){
        return arithmetic(other.getObj(), "__sub__", "__rsub__");
    }
    
    WrappedPyObject operator*(WrappedPyObject const other){
        return arithmetic(other.getObj(), "__mul__", "__rmul__");
    }

    WrappedPyObject operator*(int other){
        PyObject* other_obj = Py_BuildValue("i", other);
        //Py_INCREF(other_obj);
        WrappedPyObject ret =  arithmetic(other_obj, "__mul__", "__rmul__");
        //Py_DECREF(other_obj);
        return ret;
    }

    WrappedPyObject operator/(int other){
        PyObject* other_obj = Py_BuildValue("i", other);
        Py_INCREF(other_obj);
        WrappedPyObject ret =  arithmetic(other_obj, "__div__", "__rdiv__");
        Py_DECREF(other_obj);
        return ret;
    }
    
    bool richcmp(WrappedPyObject const &other, int op) const {
        PyObject * ret;
        long retv;
        ret = PyObject_RichCompare(self, other.getObj(), op);
        retv = PyInt_AsLong(ret);
        return retv;
    }
    
    bool operator<(WrappedPyObject const &other) const {
        return richcmp(other, Py_LT);
    }
    
    bool operator<=(WrappedPyObject const &other) const {
        return richcmp(other, Py_LE);
    }
    
    bool operator>=(WrappedPyObject const &other) const{
        return richcmp(other, Py_GE);
    }

    bool operator>(WrappedPyObject const &other) const {
        return richcmp(other, Py_GT);
    }
    
    bool operator==(WrappedPyObject const &other) const {
        return richcmp(other, Py_EQ);
    }
    

    bool operator<(int const c) const {
        return richcmp(WrappedPyObject(c), Py_LT);
    }
    
    bool operator<=(int const c) const {
        return richcmp(WrappedPyObject(c), Py_LE);
    }
    
    bool operator>=(int const c) const{
        return richcmp(WrappedPyObject(c), Py_GE);
    }

    bool operator>(int const c) const {
        return richcmp(WrappedPyObject(c), Py_GT);
    }
    
    bool operator==(int const c) const {
        return richcmp(WrappedPyObject(c), Py_EQ);
    }


    WrappedPyObject operator+=(WrappedPyObject other){
        *this = *this + other;
        return *this;

    }

    WrappedPyObject operator-=(WrappedPyObject other){
        *this = *this - other;
        return *this;
    }
};
    
typedef GenericInterval<WrappedPyObject> PyInterval;
typedef GenericOptInterval<WrappedPyObject> PyOptInterval;
    
typedef GenericRect<WrappedPyObject> PyRect;
typedef GenericOptRect<WrappedPyObject> PyOptRect;

typedef D2<WrappedPyObject> PyPoint;

template<>
struct CoordTraits<WrappedPyObject> {
    typedef PyPoint PointType;
    typedef PyInterval IntervalType;
    typedef PyOptInterval OptIntervalType;
    typedef PyRect RectType;
    typedef PyOptRect OptRectType;

    typedef
      boost::equality_comparable< PyInterval
    , boost::additive< PyInterval
    , boost::multipliable< PyInterval
    , boost::orable< PyInterval
    , boost::arithmetic< PyInterval, WrappedPyObject
      > > > > >
        IntervalOps;

    typedef
      boost::equality_comparable< PyRect
    //, boost::equality_comparable< PyRect, IntRect
    , boost::orable< PyRect
    , boost::orable< PyRect, PyOptRect
    , boost::additive< PyRect, PyPoint
    //, boost::multipliable< Rect, Affine
      > > > > //> >
        RectOps;
};
    
}