PyLucene 3.4.0-1 import
[pylucene.git] / jcc / _jcc / java / lang / String.cpp
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 <string.h>
17 #include "JCCEnv.h"
18 #include "java/lang/Object.h"
19 #include "java/lang/Class.h"
20 #include "java/lang/String.h"
21
22 namespace java {
23     namespace lang {
24
25         enum {
26             mid__init_,
27             mid_toString,
28             mid_length,
29             max_mid
30         };
31
32         Class *String::class$ = NULL;
33         jmethodID *String::_mids = NULL;
34
35         jclass String::initializeClass()
36         {
37             if (!class$)
38             {
39                 jclass cls = env->findClass("java/lang/String");
40
41                 _mids = new jmethodID[max_mid];
42                 _mids[mid__init_] = 
43                     env->getMethodID(cls, "<init>",
44                                      "()V");
45                 _mids[mid_toString] = 
46                     env->getMethodID(cls, "toString",
47                                      "()Ljava/lang/String;");
48                 _mids[mid_length] = 
49                     env->getMethodID(cls, "length",
50                                      "()I");
51
52                 class$ = (Class *) new JObject(cls);
53             }
54
55             return (jclass) class$->this$;
56         }
57
58         String::String() : Object(env->newObject(initializeClass, &_mids, mid__init_)) {
59         }
60
61         int String::length() const
62         {
63             return env->callIntMethod(this$, _mids[mid_length]);
64         }
65     }
66 }
67
68
69 #include "structmember.h"
70 #include "functions.h"
71 #include "macros.h"
72
73 namespace java {
74     namespace lang {
75
76         static int t_String_init(t_String *self,
77                                  PyObject *args, PyObject *kwds);
78         static PyObject *t_String_length(t_String *self);
79
80         static PyMethodDef t_String__methods_[] = {
81             DECLARE_METHOD(t_String, length, METH_NOARGS),
82             { NULL, NULL, 0, NULL }
83         };
84
85         DECLARE_TYPE(String, t_String, Object, java::lang::String,
86                      t_String_init, 0, 0, 0, 0, 0);
87
88         static int t_String_init(t_String *self,
89                                  PyObject *args, PyObject *kwds)
90         {
91             char *bytes;
92
93             switch (PyTuple_Size(args)) {
94               case 0:
95                 INT_CALL(self->object = String());
96                 break;
97               case 1:
98                 if (!PyArg_ParseTuple(args, "s", &bytes))
99                     return -1;
100                 INT_CALL(self->object = String(env->fromUTF(bytes)));
101                 break;
102               default:
103                 PyErr_SetString(PyExc_ValueError, "invalid args");
104                 return -1;
105             }
106         
107             return 0;
108         }
109
110         static PyObject *t_String_length(t_String *self)
111         {
112             jint length;
113
114             OBJ_CALL(length = self->object.length());
115             return PyInt_FromLong(length);
116         }
117     }
118 }