| author | ymh <ymh.work@gmail.com> |
| Tue, 25 May 2010 02:43:45 +0200 | |
| changeset 29 | cc9b7e14412b |
| permissions | -rw-r--r-- |
| 29 | 1 |
/* |
2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
3 |
* you may not use this file except in compliance with the License. |
|
4 |
* You may obtain a copy of the License at |
|
5 |
* |
|
6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
7 |
* |
|
8 |
* Unless required by applicable law or agreed to in writing, software |
|
9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
11 |
* See the License for the specific language governing permissions and |
|
12 |
* limitations under the License. |
|
13 |
*/ |
|
14 |
||
15 |
#ifndef _macros_H |
|
16 |
#define _macros_H |
|
17 |
||
18 |
#define OBJ_CALL(action) \ |
|
19 |
{ \ |
|
20 |
try { \ |
|
21 |
PythonThreadState state(1); \ |
|
22 |
action; \ |
|
23 |
} catch (JCCEnv::pythonError) { \ |
|
24 |
return NULL; \ |
|
25 |
} catch (JCCEnv::exception e) { \ |
|
26 |
return PyErr_SetJavaError(e.throwable); \ |
|
27 |
} \ |
|
28 |
} |
|
29 |
||
30 |
#define INT_CALL(action) \ |
|
31 |
{ \ |
|
32 |
try { \ |
|
33 |
PythonThreadState state(1); \ |
|
34 |
action; \ |
|
35 |
} catch (JCCEnv::pythonError) { \ |
|
36 |
return -1; \ |
|
37 |
} catch (JCCEnv::exception e) { \ |
|
38 |
PyErr_SetJavaError(e.throwable); \ |
|
39 |
return -1; \ |
|
40 |
} \ |
|
41 |
} |
|
42 |
||
43 |
||
44 |
#define DECLARE_METHOD(type, name, flags) \ |
|
45 |
{ #name, (PyCFunction) type##_##name, flags, "" } |
|
46 |
||
47 |
#define DECLARE_GET_FIELD(type, name) \ |
|
48 |
{ #name, (getter) type##_get__##name, NULL, "", NULL } |
|
49 |
||
50 |
#define DECLARE_SET_FIELD(type, name) \ |
|
51 |
{ #name, NULL, (setter) type##_set__##name, "", NULL } |
|
52 |
||
53 |
#define DECLARE_GETSET_FIELD(type, name) \ |
|
54 |
{ #name, (getter) type##_get__##name, (setter) type##_set__##name, "", NULL } |
|
55 |
||
56 |
||
57 |
#define DECLARE_TYPE(name, t_name, base, javaClass, \ |
|
58 |
init, iter, iternext, getset, mapping, sequence) \ |
|
59 |
PyTypeObject name##$$Type = { \ |
|
60 |
PyObject_HEAD_INIT(NULL) \ |
|
61 |
/* ob_size */ 0, \ |
|
62 |
/* tp_name */ #name, \ |
|
63 |
/* tp_basicsize */ sizeof(t_name), \ |
|
64 |
/* tp_itemsize */ 0, \ |
|
65 |
/* tp_dealloc */ 0, \ |
|
66 |
/* tp_print */ 0, \ |
|
67 |
/* tp_getattr */ 0, \ |
|
68 |
/* tp_setattr */ 0, \ |
|
69 |
/* tp_compare */ 0, \ |
|
70 |
/* tp_repr */ 0, \ |
|
71 |
/* tp_as_number */ 0, \ |
|
72 |
/* tp_as_sequence */ sequence, \ |
|
73 |
/* tp_as_mapping */ mapping, \ |
|
74 |
/* tp_hash */ 0, \ |
|
75 |
/* tp_call */ 0, \ |
|
76 |
/* tp_str */ 0, \ |
|
77 |
/* tp_getattro */ 0, \ |
|
78 |
/* tp_setattro */ 0, \ |
|
79 |
/* tp_as_buffer */ 0, \ |
|
80 |
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \ |
|
81 |
/* tp_doc */ #t_name" objects", \ |
|
82 |
/* tp_traverse */ 0, \ |
|
83 |
/* tp_clear */ 0, \ |
|
84 |
/* tp_richcompare */ 0, \ |
|
85 |
/* tp_weaklistoffset */ 0, \ |
|
86 |
/* tp_iter */ (getiterfunc) iter, \ |
|
87 |
/* tp_iternext */ (iternextfunc) iternext, \ |
|
88 |
/* tp_methods */ t_name##__methods_, \ |
|
89 |
/* tp_members */ 0, \ |
|
90 |
/* tp_getset */ getset, \ |
|
91 |
/* tp_base */ &base##$$Type, \ |
|
92 |
/* tp_dict */ 0, \ |
|
93 |
/* tp_descr_get */ 0, \ |
|
94 |
/* tp_descr_set */ 0, \ |
|
95 |
/* tp_dictoffset */ 0, \ |
|
96 |
/* tp_init */ (initproc)init, \ |
|
97 |
/* tp_alloc */ 0, \ |
|
98 |
/* tp_new */ 0, \ |
|
99 |
}; \ |
|
100 |
PyObject *t_name::wrap_Object(const javaClass& object) \ |
|
101 |
{ \ |
|
102 |
if (!!object) \ |
|
103 |
{ \ |
|
104 |
t_name *self = \ |
|
105 |
(t_name *) name##$$Type.tp_alloc(&name##$$Type, 0); \ |
|
106 |
if (self) \ |
|
107 |
self->object = object; \ |
|
108 |
return (PyObject *) self; \ |
|
109 |
} \ |
|
110 |
Py_RETURN_NONE; \ |
|
111 |
} \ |
|
112 |
PyObject *t_name::wrap_jobject(const jobject& object) \ |
|
113 |
{ \ |
|
114 |
if (!!object) \ |
|
115 |
{ \ |
|
116 |
t_name *self = \ |
|
117 |
(t_name *) name##$$Type.tp_alloc(&name##$$Type, 0); \ |
|
118 |
if (self) \ |
|
119 |
self->object = javaClass(object); \ |
|
120 |
return (PyObject *) self; \ |
|
121 |
} \ |
|
122 |
Py_RETURN_NONE; \ |
|
123 |
} \ |
|
124 |
||
125 |
||
126 |
#define INSTALL_TYPE(name, module) \ |
|
127 |
if (PyType_Ready(&name##$$Type) == 0) \ |
|
128 |
{ \ |
|
129 |
Py_INCREF(&name##$$Type); \ |
|
130 |
PyModule_AddObject(module, #name, (PyObject *) &name##$$Type); \ |
|
131 |
} |
|
132 |
||
133 |
||
134 |
#define Py_RETURN_BOOL(b) \ |
|
135 |
{ \ |
|
136 |
if (b) \ |
|
137 |
Py_RETURN_TRUE; \ |
|
138 |
else \ |
|
139 |
Py_RETURN_FALSE; \ |
|
140 |
} |
|
141 |
||
142 |
||
143 |
#if PY_VERSION_HEX < 0x02040000 |
|
144 |
||
145 |
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None |
|
146 |
#define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True |
|
147 |
#define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False |
|
148 |
||
149 |
#define Py_CLEAR(op) \ |
|
150 |
do { \ |
|
151 |
if (op) { \ |
|
152 |
PyObject *tmp = (PyObject *)(op); \ |
|
153 |
(op) = NULL; \ |
|
154 |
Py_DECREF(tmp); \ |
|
155 |
} \ |
|
156 |
} while (0) |
|
157 |
||
158 |
#define Py_VISIT(op) \ |
|
159 |
do { \ |
|
160 |
if (op) { \ |
|
161 |
int vret = visit((PyObject *)(op), arg); \ |
|
162 |
if (vret) \ |
|
163 |
return vret; \ |
|
164 |
} \ |
|
165 |
} while (0) |
|
166 |
|
|
167 |
#endif /* Python 2.3.5 */ |
|
168 |
||
169 |
||
170 |
#endif /* _macros_H */ |