web/lib/arch/osx/jcc/sources/JObject.cpp
changeset 29 cc9b7e14412b
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     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 #include <jni.h>
       
    16 #include "JCCEnv.h"
       
    17 
       
    18 #ifdef PYTHON
       
    19 
       
    20 #include <Python.h>
       
    21 #include "structmember.h"
       
    22 
       
    23 #include "JObject.h"
       
    24 #include "macros.h"
       
    25 
       
    26 
       
    27 /* JObject */
       
    28 
       
    29 static void t_JObject_dealloc(t_JObject *self);
       
    30 static PyObject *t_JObject_new(PyTypeObject *type,
       
    31                                PyObject *args, PyObject *kwds);
       
    32 
       
    33 static PyObject *t_JObject_richcmp(t_JObject *, PyObject *o2, int op);
       
    34 static PyObject *t_JObject_str(t_JObject *self);
       
    35 static PyObject *t_JObject_repr(t_JObject *self);
       
    36 static int t_JObject_hash(t_JObject *self);
       
    37 static PyObject *t_JObject__getJObject(t_JObject *self, void *data);
       
    38 
       
    39 static PyMemberDef t_JObject_members[] = {
       
    40     { NULL, 0, 0, 0, NULL }
       
    41 };
       
    42 
       
    43 static PyMethodDef t_JObject_methods[] = {
       
    44     { NULL, NULL, 0, NULL }
       
    45 };
       
    46 
       
    47 static PyGetSetDef t_JObject_properties[] = {
       
    48     { "_jobject", (getter) t_JObject__getJObject, NULL, NULL, NULL },
       
    49     { NULL, NULL, NULL, NULL, NULL }
       
    50 };
       
    51 
       
    52 PyTypeObject JObject$$Type = {
       
    53     PyObject_HEAD_INIT(NULL)
       
    54     0,                                   /* ob_size */
       
    55     "jcc.JObject",                       /* tp_name */
       
    56     sizeof(t_JObject),                   /* tp_basicsize */
       
    57     0,                                   /* tp_itemsize */
       
    58     (destructor)t_JObject_dealloc,       /* tp_dealloc */
       
    59     0,                                   /* tp_print */
       
    60     0,                                   /* tp_getattr */
       
    61     0,                                   /* tp_setattr */
       
    62     0,                                   /* tp_compare */
       
    63     (reprfunc)t_JObject_repr,            /* tp_repr */
       
    64     0,                                   /* tp_as_number */
       
    65     0,                                   /* tp_as_sequence */
       
    66     0,                                   /* tp_as_mapping */
       
    67     (hashfunc)t_JObject_hash,            /* tp_hash  */
       
    68     0,                                   /* tp_call */
       
    69     (reprfunc)t_JObject_str,             /* tp_str */
       
    70     0,                                   /* tp_getattro */
       
    71     0,                                   /* tp_setattro */
       
    72     0,                                   /* tp_as_buffer */
       
    73     (Py_TPFLAGS_DEFAULT |
       
    74      Py_TPFLAGS_BASETYPE),               /* tp_flags */
       
    75     "t_JObject objects",                 /* tp_doc */
       
    76     0,                                   /* tp_traverse */
       
    77     0,                                   /* tp_clear */
       
    78     (richcmpfunc)t_JObject_richcmp,      /* tp_richcompare */
       
    79     0,                                   /* tp_weaklistoffset */
       
    80     0,                                   /* tp_iter */
       
    81     0,                                   /* tp_iternext */
       
    82     t_JObject_methods,                   /* tp_methods */
       
    83     t_JObject_members,                   /* tp_members */
       
    84     t_JObject_properties,                /* tp_getset */
       
    85     0,                                   /* tp_base */
       
    86     0,                                   /* tp_dict */
       
    87     0,                                   /* tp_descr_get */
       
    88     0,                                   /* tp_descr_set */
       
    89     0,                                   /* tp_dictoffset */
       
    90     0,                                   /* tp_init */
       
    91     0,                                   /* tp_alloc */
       
    92     (newfunc)t_JObject_new,              /* tp_new */
       
    93 };
       
    94 
       
    95 
       
    96 static void t_JObject_dealloc(t_JObject *self)
       
    97 {
       
    98     self->object = JObject(NULL);
       
    99     self->ob_type->tp_free((PyObject *) self);
       
   100 }
       
   101 
       
   102 static PyObject *t_JObject_new(PyTypeObject *type,
       
   103                                PyObject *args, PyObject *kwds)
       
   104 {
       
   105     t_JObject *self = (t_JObject *) type->tp_alloc(type, 0);
       
   106 
       
   107     self->object = JObject(NULL);
       
   108 
       
   109     return (PyObject *) self;
       
   110 }
       
   111 
       
   112 static PyObject *t_JObject_richcmp(t_JObject *self, PyObject *arg, int op)
       
   113 {
       
   114     int b = 0;
       
   115 
       
   116     switch (op) {
       
   117       case Py_EQ:
       
   118       case Py_NE:
       
   119         if (PyObject_TypeCheck(arg, &JObject$$Type))
       
   120             b = self->object == ((t_JObject *) arg)->object;
       
   121         if (op == Py_EQ)
       
   122             Py_RETURN_BOOL(b);
       
   123         Py_RETURN_BOOL(!b);
       
   124       case Py_LT:
       
   125         PyErr_SetString(PyExc_NotImplementedError, "<");
       
   126         return NULL;
       
   127       case Py_LE:
       
   128         PyErr_SetString(PyExc_NotImplementedError, "<=");
       
   129         return NULL;
       
   130       case Py_GT:
       
   131         PyErr_SetString(PyExc_NotImplementedError, ">");
       
   132         return NULL;
       
   133       case Py_GE:
       
   134         PyErr_SetString(PyExc_NotImplementedError, ">=");
       
   135         return NULL;
       
   136     }
       
   137 
       
   138     return NULL;
       
   139 }
       
   140 
       
   141 static PyObject *t_JObject_str(t_JObject *self)
       
   142 {
       
   143     if (self->object.this$)
       
   144     {
       
   145         char *utf = env->toString(self->object.this$);
       
   146         PyObject *unicode = PyUnicode_DecodeUTF8(utf, strlen(utf), "strict");
       
   147 
       
   148         delete utf;
       
   149         return unicode;
       
   150     }
       
   151 
       
   152     return PyString_FromString("<null>");
       
   153 }
       
   154 
       
   155 static PyObject *t_JObject_repr(t_JObject *self)
       
   156 {
       
   157     PyObject *name = PyObject_GetAttrString((PyObject *) self->ob_type,
       
   158                                             "__name__");
       
   159     PyObject *str = self->ob_type->tp_str((PyObject *) self);
       
   160 #if PY_VERSION_HEX < 0x02040000
       
   161     PyObject *args = Py_BuildValue("(OO)", name, str);
       
   162 #else
       
   163     PyObject *args = PyTuple_Pack(2, name, str);
       
   164 #endif
       
   165     PyObject *format = PyString_FromString("<%s: %s>");
       
   166     PyObject *repr = PyString_Format(format, args);
       
   167 
       
   168     Py_DECREF(name);
       
   169     Py_DECREF(str);
       
   170     Py_DECREF(args);
       
   171     Py_DECREF(format);
       
   172 
       
   173     return repr;
       
   174 }
       
   175 
       
   176 static int t_JObject_hash(t_JObject *self)
       
   177 {
       
   178     return env->hash(self->object.this$);
       
   179 }
       
   180 
       
   181 static PyObject *t_JObject__getJObject(t_JObject *self, void *data)
       
   182 {
       
   183     return PyCObject_FromVoidPtr((void *) self->object.this$, NULL);
       
   184 }
       
   185 
       
   186 #endif /* PYTHON */