Code

Improve native method table structure
[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;
70 //########################################################################
71 //# BASE OBJECT
72 //########################################################################
74 static jlong getPointer(JNIEnv *env, jobject obj)
75 {
76     jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
77     jlong val = env->GetLongField(obj, id);
78     return val;
79 }
81 static void setPointer(JNIEnv *env, jobject obj, jlong val)
82 {
83     jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
84     env->SetLongField(obj, id, val);
85 }
87 static void JNICALL DOMBase_construct
88   (JNIEnv *env, jobject obj)
89 {
90     setPointer(env, obj, 0L);
91 }
93 static void JNICALL DOMBase_destruct
94   (JNIEnv *env, jobject obj)
95 {
96     NodePtr *ptr = (NodePtr *)getPointer(env, obj);
97     if (ptr)
98         {
99         delete ptr;
100         }
101     setPointer(env, obj, 0L);
105 static JNINativeMethod nm_DOMBase[] =
107 { (char *)"construct", (char *)"()V", (void *)DOMBase_construct },
108 { (char *)"destruct",  (char *)"()V", (void *)DOMBase_destruct  },
109 { NULL,  NULL, NULL }
110 };
112 static NativeClass nc_DOMBase =
114     "org/inkscape/dom/DOMBase",
115     nm_DOMBase
116 };
117 //########################################################################
118 //# DOMImplementation
119 //########################################################################
122 void JNICALL DOMImplementation_nCreateDocument
123   (JNIEnv *env, jobject obj)
125     DOMImplementationImpl domImpl;
126     DocumentTypePtr docType = domImpl.createDocumentType("", "", "");
127     DocumentPtr doc = domImpl.createDocument("", "", docType);
128     DocumentPtr *ptr = new DocumentPtr(doc);
129     setPointer(env, obj, (jlong)ptr);
134 static JNINativeMethod nm_DOMImplementation[] =
136 { (char *)"construct", (char *)"()V", (void *)DOMImplementation_nCreateDocument },
137 { NULL,  NULL, NULL }
138 };
140 static NativeClass nc_DOMImplementation =
142     "org/inkscape/dom/DOMImplementation",
143     nm_DOMImplementation
144 };
148 //########################################################################
149 //# MAIN
150 //########################################################################
153 /**
154  * This is a table-of-tables, matching a class name to its
155  * table of native methods.  We can probably think of a cleaner way
156  * of doing this
157  */   
158 static NativeClass *allClasses[] =
160     &nc_DOMBase,
161     &nc_DOMImplementation,
162     NULL
163 };
167 bool JavaBinderyImpl::doBinding()
169     for (NativeClass **nc = allClasses ; *nc ; nc++)
170         {
171         bool ret = registerNatives((*nc)->className, (*nc)->methods);
172         if (!ret)
173             {
174             err("Could not bind native methods");
175             return false;
176             }
177         }
178     return true;
184 } // namespace Bind
185 } // namespace Inkscape
187 //########################################################################
188 //# E N D    O F    F I L E
189 //########################################################################