Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
wrapped-pyobject.h
Go to the documentation of this file.
1#include "Python.h"
4#include "2geom/d2.h"
5
6
7namespace Geom{
8
9//TODO! looks like memory leak
11private:
12 PyObject* self;
13public:
14 //~ int code;
15
17 //~ printf("Created empty WPO at address %p\n", this);
18 self = NULL;
19 //~ code = 0;
20 }
21
23 self = other.getObj();
24 //~ code = other.code;
25 //~ printf("COPY-CONSTRUCTOR %p, this WPO %p, other WPO %p\n", self, this, &other);
26 Py_XINCREF(self);
27 }
28
29 WrappedPyObject(PyObject* arg){
30 if (arg == NULL){
31 //PyErr_Print();
32 //~ code = c;
33 }
34 else{
35 //~ printf("CONSTRUCTOR %p\n", arg);
36 Py_INCREF(arg);
37 self = arg;
38 //~ code = c;
39 }
40 }
41
43 self = Py_BuildValue("i", c);
44 //~ printf("INT-OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
45 Py_INCREF(self);
46 }
47
48
50 //TODO Leaking memory
51 //~ printf("DECREF %p\n", self);
52 //Py_DECREF(self);
53 }
54
55 PyObject* getObj() const {
56 return self;
57 }
58
60 if (this != &other){
61 self = other.getObj();
62 //~ printf("OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
63 Py_XINCREF(self);
64 }
65 return *this;
66 }
67
69
70 self = Py_BuildValue("i", other);
71 //~ printf("INT-OPERATOR= %p, this WPO %p, other WPO %p\n", self, this, &other);
72 Py_INCREF(self);
73
74 return *this;
75 }
76
78 PyObject * ret;
79 ret = PyObject_CallMethodObjArgs(self, Py_BuildValue("s", "__neg__"), NULL);
80 if (ret == NULL){
81 Py_INCREF(Py_None);
82 return WrappedPyObject(Py_None);
83 }
84 //Py_INCREF(ret);
85 WrappedPyObject * retw = new WrappedPyObject(ret);
86 //Py_DECREF(ret);
87 return *retw;
88 }
89
90 WrappedPyObject arithmetic(PyObject* const other, std::string method, std::string rmethod) const {
91 PyObject * ret;
92 //printf("%p %p\n", self, other);
93 ret = PyObject_CallMethodObjArgs(self, Py_BuildValue("s", method.c_str()), other, NULL);
94 if (ret == NULL){
95 Py_INCREF(Py_None);
96 return WrappedPyObject(Py_None);
97 }
98 PyObject * isNI = PyObject_RichCompare(ret, Py_NotImplemented, Py_EQ);
99 if ( PyInt_AsLong(isNI) ){
100 ret = PyObject_CallMethodObjArgs(other, Py_BuildValue("s", rmethod.c_str()), self, NULL);
101 }
102 if (ret == NULL){
103 Py_INCREF(Py_None);
104 return WrappedPyObject(Py_None);
105 }
106 WrappedPyObject * retw = new WrappedPyObject(ret);
107 return *retw;
108
109 }
110
112 return arithmetic(other.getObj(), "__add__", "__radd__");
113 }
114
116 return arithmetic(other.getObj(), "__sub__", "__rsub__");
117 }
118
120 return arithmetic(other.getObj(), "__mul__", "__rmul__");
121 }
122
124 PyObject* other_obj = Py_BuildValue("i", other);
125 //Py_INCREF(other_obj);
126 WrappedPyObject ret = arithmetic(other_obj, "__mul__", "__rmul__");
127 //Py_DECREF(other_obj);
128 return ret;
129 }
130
132 PyObject* other_obj = Py_BuildValue("i", other);
133 Py_INCREF(other_obj);
134 WrappedPyObject ret = arithmetic(other_obj, "__div__", "__rdiv__");
135 Py_DECREF(other_obj);
136 return ret;
137 }
138
139 bool richcmp(WrappedPyObject const &other, int op) const {
140 PyObject * ret;
141 long retv;
142 ret = PyObject_RichCompare(self, other.getObj(), op);
143 retv = PyInt_AsLong(ret);
144 return retv;
145 }
146
147 bool operator<(WrappedPyObject const &other) const {
148 return richcmp(other, Py_LT);
149 }
150
151 bool operator<=(WrappedPyObject const &other) const {
152 return richcmp(other, Py_LE);
153 }
154
155 bool operator>=(WrappedPyObject const &other) const{
156 return richcmp(other, Py_GE);
157 }
158
159 bool operator>(WrappedPyObject const &other) const {
160 return richcmp(other, Py_GT);
161 }
162
163 bool operator==(WrappedPyObject const &other) const {
164 return richcmp(other, Py_EQ);
165 }
166
167
168 bool operator<(int const c) const {
169 return richcmp(WrappedPyObject(c), Py_LT);
170 }
171
172 bool operator<=(int const c) const {
173 return richcmp(WrappedPyObject(c), Py_LE);
174 }
175
176 bool operator>=(int const c) const{
177 return richcmp(WrappedPyObject(c), Py_GE);
178 }
179
180 bool operator>(int const c) const {
181 return richcmp(WrappedPyObject(c), Py_GT);
182 }
183
184 bool operator==(int const c) const {
185 return richcmp(WrappedPyObject(c), Py_EQ);
186 }
187
188
190 *this = *this + other;
191 return *this;
192
193 }
194
196 *this = *this - other;
197 return *this;
198 }
199};
200
203
206
208
209template<>
216
217 typedef
218 boost::equality_comparable< PyInterval
219 , boost::additive< PyInterval
220 , boost::multipliable< PyInterval
221 , boost::orable< PyInterval
222 , boost::arithmetic< PyInterval, WrappedPyObject
223 > > > > >
225
226 typedef
227 boost::equality_comparable< PyRect
228 //, boost::equality_comparable< PyRect, IntRect
229 , boost::orable< PyRect
230 , boost::orable< PyRect, PyOptRect
231 , boost::additive< PyRect, PyPoint
232 //, boost::multipliable< Rect, Affine
233 > > > > //> >
235};
236
237}
Adaptor that creates 2D functions from 1D ones.
Definition d2.h:55
A range of numbers which is never empty.
A range of numbers that can be empty.
Axis-aligned generic rectangle that can be empty.
Axis aligned, non-empty, generic rectangle.
WrappedPyObject operator*(WrappedPyObject const other)
bool operator>=(WrappedPyObject const &other) const
WrappedPyObject operator*(int other)
bool operator>=(int const c) const
bool richcmp(WrappedPyObject const &other, int op) const
WrappedPyObject operator-(WrappedPyObject const other)
bool operator<=(WrappedPyObject const &other) const
WrappedPyObject arithmetic(PyObject *const other, std::string method, std::string rmethod) const
WrappedPyObject operator+=(WrappedPyObject other)
bool operator<(int const c) const
bool operator<=(int const c) const
WrappedPyObject operator=(WrappedPyObject other)
WrappedPyObject operator/(int other)
WrappedPyObject(WrappedPyObject const &other)
WrappedPyObject(PyObject *arg)
PyObject * getObj() const
WrappedPyObject operator=(int other)
WrappedPyObject operator+(WrappedPyObject const other) const
bool operator==(int const c) const
bool operator>(int const c) const
bool operator<(WrappedPyObject const &other) const
WrappedPyObject operator-=(WrappedPyObject other)
bool operator>(WrappedPyObject const &other) const
WrappedPyObject operator-() const
bool operator==(WrappedPyObject const &other) const
double c[8][4]
Lifts one dimensional objects into 2D.
Closed interval of generic values.
Axis-aligned rectangle.
Various utility functions.
Definition affine.h:22
GenericRect< WrappedPyObject > PyRect
GenericOptRect< WrappedPyObject > PyOptRect
GenericInterval< WrappedPyObject > PyInterval
D2< WrappedPyObject > PyPoint
GenericOptInterval< WrappedPyObject > PyOptInterval
boost::equality_comparable< PyInterval, boost::additive< PyInterval, boost::multipliable< PyInterval, boost::orable< PyInterval, boost::arithmetic< PyInterval, WrappedPyObject > > > > > IntervalOps
boost::equality_comparable< PyRect, boost::orable< PyRect, boost::orable< PyRect, PyOptRect, boost::additive< PyRect, PyPoint > > > > RectOps
Traits class used with coordinate types.
Definition coord.h:105