PyLucene 3.4.0-1 import
[pylucene.git] / jcc / jcc / sources / macros.h
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 (int e) {                                               \
24             switch (e) {                                                \
25               case _EXC_PYTHON:                                         \
26                 return NULL;                                            \
27               case _EXC_JAVA:                                           \
28                 return PyErr_SetJavaError();                            \
29               default:                                                  \
30                 throw;                                                  \
31             }                                                           \
32         }                                                               \
33     }
34
35 #define INT_CALL(action)                                                \
36     {                                                                   \
37         try {                                                           \
38             PythonThreadState state(1);                                 \
39             action;                                                     \
40         } catch (int e) {                                               \
41             switch (e) {                                                \
42               case _EXC_PYTHON:                                         \
43                 return -1;                                              \
44               case _EXC_JAVA:                                           \
45                 PyErr_SetJavaError();                                   \
46                 return -1;                                              \
47               default:                                                  \
48                 throw;                                                  \
49             }                                                           \
50         }                                                               \
51     }
52
53
54 #define DECLARE_METHOD(type, name, flags)               \
55     { #name, (PyCFunction) type##_##name, flags, "" }
56
57 #define DECLARE_GET_FIELD(type, name)           \
58     { #name, (getter) type##_get__##name, NULL, "", NULL }
59
60 #define DECLARE_SET_FIELD(type, name)           \
61     { #name, NULL, (setter) type##_set__##name, "", NULL }
62
63 #define DECLARE_GETSET_FIELD(type, name)        \
64     { #name, (getter) type##_get__##name, (setter) type##_set__##name, "", NULL }
65
66 #define PY_TYPE(name) name##$$Type
67
68 #define DECLARE_TYPE(name, t_name, base, javaClass,                         \
69                      init, iter, iternext, getset, mapping, sequence)       \
70 PyTypeObject PY_TYPE(name) = {                                              \
71     PyObject_HEAD_INIT(NULL)                                                \
72     /* ob_size            */   0,                                           \
73     /* tp_name            */   #name,                                       \
74     /* tp_basicsize       */   sizeof(t_name),                              \
75     /* tp_itemsize        */   0,                                           \
76     /* tp_dealloc         */   0,                                           \
77     /* tp_print           */   0,                                           \
78     /* tp_getattr         */   0,                                           \
79     /* tp_setattr         */   0,                                           \
80     /* tp_compare         */   0,                                           \
81     /* tp_repr            */   0,                                           \
82     /* tp_as_number       */   0,                                           \
83     /* tp_as_sequence     */   sequence,                                    \
84     /* tp_as_mapping      */   mapping,                                     \
85     /* tp_hash            */   0,                                           \
86     /* tp_call            */   0,                                           \
87     /* tp_str             */   0,                                           \
88     /* tp_getattro        */   0,                                           \
89     /* tp_setattro        */   0,                                           \
90     /* tp_as_buffer       */   0,                                           \
91     /* tp_flags           */   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,    \
92     /* tp_doc             */   #t_name" objects",                           \
93     /* tp_traverse        */   0,                                           \
94     /* tp_clear           */   0,                                           \
95     /* tp_richcompare     */   0,                                           \
96     /* tp_weaklistoffset  */   0,                                           \
97     /* tp_iter            */   (getiterfunc) iter,                          \
98     /* tp_iternext        */   (iternextfunc) iternext,                     \
99     /* tp_methods         */   t_name##__methods_,                          \
100     /* tp_members         */   0,                                           \
101     /* tp_getset          */   getset,                                      \
102     /* tp_base            */   &PY_TYPE(base),                              \
103     /* tp_dict            */   0,                                           \
104     /* tp_descr_get       */   0,                                           \
105     /* tp_descr_set       */   0,                                           \
106     /* tp_dictoffset      */   0,                                           \
107     /* tp_init            */   (initproc)init,                              \
108     /* tp_alloc           */   0,                                           \
109     /* tp_new             */   0,                                           \
110 };                                                                          \
111 PyObject *t_name::wrap_Object(const javaClass& object)                  \
112 {                                                                       \
113     if (!!object)                                                       \
114     {                                                                   \
115         t_name *self =                                                  \
116             (t_name *) PY_TYPE(name).tp_alloc(&PY_TYPE(name), 0);       \
117         if (self)                                                       \
118             self->object = object;                                      \
119         return (PyObject *) self;                                       \
120     }                                                                   \
121     Py_RETURN_NONE;                                                     \
122 }                                                                       \
123 PyObject *t_name::wrap_jobject(const jobject& object)                   \
124 {                                                                       \
125     if (!!object)                                                       \
126     {                                                                   \
127         if (!env->isInstanceOf(object, javaClass::initializeClass))     \
128         {                                                               \
129             PyErr_SetObject(PyExc_TypeError,                            \
130                             (PyObject *) &PY_TYPE(name));               \
131             return NULL;                                                \
132         }                                                               \
133         t_name *self = (t_name *)                                       \
134             PY_TYPE(name).tp_alloc(&PY_TYPE(name), 0);                  \
135         if (self)                                                       \
136             self->object = javaClass(object);                           \
137         return (PyObject *) self;                                       \
138     }                                                                   \
139     Py_RETURN_NONE;                                                     \
140 }                                                                       \
141
142
143 #define INSTALL_TYPE(name, module)                                      \
144     if (PyType_Ready(&PY_TYPE(name)) == 0)                              \
145     {                                                                   \
146         Py_INCREF(&PY_TYPE(name));                                      \
147         PyModule_AddObject(module, #name, (PyObject *) &PY_TYPE(name)); \
148     }
149
150
151 #define Py_RETURN_BOOL(b)                       \
152     {                                           \
153         if (b)                                  \
154             Py_RETURN_TRUE;                     \
155         else                                    \
156             Py_RETURN_FALSE;                    \
157     }
158
159 #define Py_RETURN_SELF                                      \
160     {                                                       \
161         Py_INCREF(self);                                    \
162         return (PyObject *) self;                           \
163     }
164
165
166 #if PY_VERSION_HEX < 0x02040000
167
168 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
169 #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
170 #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
171
172 #define Py_CLEAR(op)                            \
173     do {                                        \
174         if (op) {                               \
175             PyObject *tmp = (PyObject *)(op);   \
176             (op) = NULL;                        \
177             Py_DECREF(tmp);                     \
178         }                                       \
179     } while (0)
180
181 #define Py_VISIT(op)                                    \
182     do {                                                \
183         if (op) {                                       \
184             int vret = visit((PyObject *)(op), arg);    \
185             if (vret)                                   \
186                 return vret;                            \
187         }                                               \
188     } while (0)
189           
190 #endif /* Python 2.3.5 */
191
192
193 #endif /* _macros_H */