1 #ifndef __JAVABIND_H__
2 #define __JAVABIND_H__
3 /**
4 * This is a simple mechanism to bind Inkscape to Java, and thence
5 * to all of the nice things that can be layered upon that.
6 *
7 * Authors:
8 * Bob Jamison
9 *
10 * Copyright (C) 2007-2008 Bob Jamison
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
27 #include <glibmm.h>
28 #include <vector>
31 namespace Inkscape
32 {
34 namespace Bind
35 {
38 /**
39 * Select which String implementation we want to use
40 */
41 typedef Glib::ustring String;
44 /**
45 * This is the base class of all things which will be C++ object
46 * instances
47 */
48 class BaseObject
49 {
50 public:
52 /**
53 * Simple constructor
54 */
55 BaseObject()
56 {}
58 /**
59 * Destructor
60 */
61 virtual ~BaseObject()
62 {}
64 };
67 /**
68 *
69 */
70 class Value
71 {
72 public:
74 /**
75 * Types for this value
76 */
77 typedef enum
78 {
79 BIND_VOID,
80 BIND_INT,
81 BIND_BOOLEAN,
82 BIND_DOUBLE,
83 BIND_STRING,
84 BIND_OBJECT
85 } ValueType;
87 /**
88 *
89 */
90 Value()
91 {
92 init();
93 }
95 /**
96 *
97 */
98 Value(int ival)
99 {
100 init();
101 setInt(ival);
102 }
104 /**
105 *
106 */
107 Value(bool bval)
108 {
109 init();
110 setBoolean(bval);
111 }
113 /**
114 *
115 */
116 Value(double dval)
117 {
118 init();
119 setDouble(dval);
120 }
122 /**
123 *
124 */
125 Value(const String &sval)
126 {
127 init();
128 setString(sval);
129 }
131 /**
132 *
133 */
134 Value(const Value &other)
135 {
136 assign(other);
137 }
139 /**
140 *
141 */
142 Value &operator=(const Value &other)
143 {
144 assign(other);
145 return *this;
146 }
148 /**
149 *
150 */
151 virtual ~Value()
152 {
153 }
155 /**
156 *
157 */
158 int getType()
159 { return type; }
161 /**
162 *
163 */
164 void setBoolean(bool val)
165 { type = BIND_BOOLEAN; ival = (int)val; }
167 /**
168 *
169 */
170 bool getBoolean()
171 {
172 if (type == BIND_BOOLEAN)
173 return (bool)ival;
174 else
175 return false;
176 }
178 /**
179 *
180 */
181 void setInt(int val)
182 { type = BIND_INT; ival = val; }
184 /**
185 *
186 */
187 bool getInt()
188 {
189 if (type == BIND_INT)
190 return ival;
191 else
192 return 0;
193 }
195 /**
196 *
197 */
198 void setDouble(double val)
199 { type = BIND_DOUBLE; dval = val; }
201 /**
202 *
203 */
204 double getDouble()
205 {
206 if (type == BIND_DOUBLE)
207 return dval;
208 else
209 return 0.0;
210 }
212 /**
213 *
214 */
215 void setString(const String &val)
216 { type = BIND_STRING; sval = val; }
218 /**
219 *
220 */
221 String getString()
222 {
223 if (type == BIND_STRING)
224 return sval;
225 else
226 return "";
227 }
230 private:
232 void init()
233 {
234 type = BIND_INT;
235 ival = 0;
236 dval = 0.0;
237 sval = "";
238 }
240 void assign(const Value &other)
241 {
242 type = other.type;
243 ival = other.ival;
244 dval = other.dval;
245 sval = other.sval;
246 }
248 int type;
249 long ival;
250 double dval;
251 String sval;
253 };
259 /**
260 *
261 */
262 class JavaBindery
263 {
264 public:
266 /**
267 *
268 */
269 JavaBindery()
270 {}
272 /**
273 *
274 */
275 virtual ~JavaBindery()
276 {}
278 /**
279 *
280 */
281 virtual bool loadJVM()
282 {
283 return false;
284 }
286 /**
287 *
288 */
289 virtual bool callStatic(int /*type*/,
290 const String &/*className*/,
291 const String &/*methodName*/,
292 const String &/*signature*/,
293 const std::vector<Value> &/*params*/,
294 Value &/*retval*/)
295 {
296 return false;
297 }
299 /**
300 *
301 */
302 virtual bool callMain(const String &/*className*/,
303 const std::vector<String> &/*args*/)
304 {
305 return false;
306 }
308 /**
309 *
310 */
311 virtual bool isLoaded()
312 {
313 return false;
314 }
316 /**
317 *
318 */
319 virtual bool scriptRun(const String &/*lang*/, const String &/*script*/)
320 {
321 return false;
322 }
324 /**
325 *
326 */
327 virtual bool scriptRunFile(const String &/*lang*/, const String &/*fileName*/)
328 {
329 return false;
330 }
332 /**
333 *
334 */
335 virtual bool showConsole()
336 {
337 return false;
338 }
340 /**
341 *
342 */
343 virtual bool doBinding()
344 {
345 return false;
346 }
348 /**
349 *
350 */
351 virtual String getException()
352 {
353 return "";
354 }
356 virtual String logGet()
357 {
358 return logBuf;
359 }
361 virtual void logClear()
362 {
363 logBuf.clear();
364 }
366 virtual void log(int ch)
367 {
368 logBuf.push_back((char)ch);
369 if (ch == '\n' || ch == '\r')
370 {
371 g_message("%s", logBuf.c_str());
372 logBuf.clear();
373 }
374 }
377 /**
378 * Return a singleton instance of this bindery
379 */
380 static JavaBindery *getInstance();
382 protected:
385 String stdOutBuf;
386 String stdErrBuf;
387 String logBuf;
389 };
395 } // namespace Bind
396 } // namespace Inkscape
398 #endif /* __JAVABIND_H__ */
399 //########################################################################
400 //# E N D O F F I L E
401 //########################################################################