Code

Finished multiple inheritance delegation
[inkscape.git] / src / bind / dobinding.cpp
1 /**
2  * This is a simple mechanism to bind Inkscape to Java, and thence
3  * to all of the nice things that can be layered upon that. 
4  *
5  * Authors:
6  *   Bob Jamison
7  *
8  * Copyright (C) 2007-2008 Bob Jamison
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 3 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <jni.h>
32 #ifdef __WIN32__
33 #include <windows.h>
34 #else
35 #include <dlfcn.h>
36 #include <errno.h>
37 #endif
39 #include "javabind.h"
40 #include "javabind-private.h"
42 #include <dom/dom.h>
43 #include <dom/domimpl.h>
45 namespace Inkscape
46 {
47 namespace Bind
48 {
50 using namespace org::w3c::dom;
52 /**
53  * This file has the actual C++ --> Java bindings
54  * This file can get quite large!
55  */  
57 /**
58  * This struct associates a class name with its native
59  * bindings.  Since C++ does not allow "flexible" arrays,
60  * we will separate each of the tables into a JNINativeMethod
61  * array, and a class with a name and a pointer to that array.  
62  */  
63 typedef struct
64 {
65     const char *className;
66     JNINativeMethod *methods;
67 } NativeClass;
69 /**
70  * Although I dislike macros, this one seems reasonable
71  */ 
72 #define EXCEPTION (getExceptionString(env).c_str())
74 //########################################################################
75 //# BASE OBJECT
76 //########################################################################
78 static jmethodID _getPointer_id = NULL;
80 static jlong getPointer(JNIEnv *env, jobject obj)
81 {
82     if (!_getPointer_id)
83         {
84         _getPointer_id = env->GetMethodID(env->GetObjectClass(obj), "getPointer", "()J");
85         if (!_getPointer_id)
86             {
87             err("getPointer(): %s", EXCEPTION);
88             return 0;
89                 }
90             }
91     jlong val = env->CallLongMethod(obj, _getPointer_id);
92     return val;
93 }
96 static jmethodID _setPointer_id = NULL;
98 static void setPointer(JNIEnv *env, jobject obj, jlong val)
99 {
100     if (!_setPointer_id)
101         {
102         _setPointer_id = env->GetMethodID(env->GetObjectClass(obj), "setPointer", "(J)V");
103         if (!_setPointer_id)
104             {
105             err("setPointer(): %s", EXCEPTION);
106             return;
107                     }
108                 }
109     env->CallVoidMethod(obj, _setPointer_id, val);
113 static void JNICALL BaseObject_construct
114   (JNIEnv *env, jobject obj)
116     setPointer(env, obj, 0L);
119 static void JNICALL BaseObject_destruct
120   (JNIEnv *env, jobject obj)
122     BaseObject *ptr = (BaseObject *)getPointer(env, obj);
123     if (ptr)
124         {
125         delete ptr;
126         }
127     setPointer(env, obj, 0L);
131 static JNINativeMethod nm_BaseObject[] =
133 { (char *)"construct", (char *)"()V", (void *)BaseObject_construct },
134 { (char *)"destruct",  (char *)"()V", (void *)BaseObject_destruct  },
135 { NULL,  NULL, NULL }
136 };
138 static NativeClass nc_BaseObject =
140     "org/inkscape/cmn/BaseObject",
141     nm_BaseObject
142 };
144 //########################################################################
145 //# BASE OBJECT
146 //########################################################################
148 static void JNICALL DOMBase_construct
149   (JNIEnv *env, jobject obj)
151     setPointer(env, obj, 0L);
154 static void JNICALL DOMBase_destruct
155   (JNIEnv *env, jobject obj)
157     NodePtr *ptr = (NodePtr *)getPointer(env, obj);
158     if (ptr)
159         {
160         delete ptr;
161         }
162     setPointer(env, obj, 0L);
166 static JNINativeMethod nm_DOMBase[] =
168 { (char *)"construct", (char *)"()V", (void *)DOMBase_construct },
169 { (char *)"destruct",  (char *)"()V", (void *)DOMBase_destruct  },
170 { NULL,  NULL, NULL }
171 };
173 static NativeClass nc_DOMBase =
175     "org/inkscape/dom/DOMBase",
176     nm_DOMBase
177 };
180 //########################################################################
181 //# DOMImplementation
182 //########################################################################
185 void JNICALL DOMImplementation_nCreateDocument
186   (JNIEnv *env, jobject obj)
188     DOMImplementationImpl domImpl;
189     DocumentTypePtr docType = domImpl.createDocumentType("", "", "");
190     DocumentPtr doc = domImpl.createDocument("", "", docType);
191     DocumentPtr *ptr = new DocumentPtr(doc);
192     setPointer(env, obj, (jlong)ptr);
197 static JNINativeMethod nm_DOMImplementation[] =
199 { (char *)"construct", (char *)"()V", (void *)DOMImplementation_nCreateDocument },
200 { NULL,  NULL, NULL }
201 };
203 static NativeClass nc_DOMImplementation =
205     "org/inkscape/dom/DOMImplementation",
206     nm_DOMImplementation
207 };
211 //########################################################################
212 //# MAIN
213 //########################################################################
216 /**
217  * This is a table-of-tables, matching a class name to its
218  * table of native methods.  We can probably think of a cleaner way
219  * of doing this
220  */   
221 static NativeClass *allClasses[] =
223     &nc_BaseObject,
224     &nc_DOMBase,
225     &nc_DOMImplementation,
226     NULL
227 };
231 bool JavaBinderyImpl::doBinding()
233     for (NativeClass **nc = allClasses ; *nc ; nc++)
234         {
235         bool ret = registerNatives((*nc)->className, (*nc)->methods);
236         if (!ret)
237             {
238             err("Could not bind native methods");
239             return false;
240             }
241         }
242     return true;
248 } // namespace Bind
249 } // namespace Inkscape
251 //########################################################################
252 //# E N D    O F    F I L E
253 //########################################################################