c73afa58194dc5d35575648d9845e207a940da5a
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;
45 /**
46 *
47 */
48 class Value
49 {
50 public:
52 /**
53 * Types for this value
54 */
55 typedef enum
56 {
57 BIND_VOID,
58 BIND_INT,
59 BIND_BOOLEAN,
60 BIND_DOUBLE,
61 BIND_STRING,
62 BIND_OBJECT
63 } ValueType;
65 /**
66 *
67 */
68 Value()
69 {
70 init();
71 }
73 /**
74 *
75 */
76 Value(const Value &other)
77 {
78 assign(other);
79 }
81 /**
82 *
83 */
84 Value &operator=(const Value &other)
85 {
86 assign(other);
87 return *this;
88 }
90 /**
91 *
92 */
93 virtual ~Value()
94 {
95 }
97 /**
98 *
99 */
100 int getType()
101 { return type; }
103 /**
104 *
105 */
106 void setBoolean(bool val)
107 { type = BIND_BOOLEAN; ival = (int)val; }
109 /**
110 *
111 */
112 bool getBoolean()
113 {
114 if (type == BIND_BOOLEAN)
115 return (bool)ival;
116 else
117 return false;
118 }
120 /**
121 *
122 */
123 void setInt(int val)
124 { type = BIND_INT; ival = val; }
126 /**
127 *
128 */
129 bool getInt()
130 {
131 if (type == BIND_INT)
132 return ival;
133 else
134 return 0;
135 }
137 /**
138 *
139 */
140 void setDouble(double val)
141 { type = BIND_DOUBLE; dval = val; }
143 /**
144 *
145 */
146 double getDouble()
147 {
148 if (type == BIND_DOUBLE)
149 return dval;
150 else
151 return 0.0;
152 }
154 /**
155 *
156 */
157 void setString(const String &val)
158 { type = BIND_STRING; sval = val; }
160 /**
161 *
162 */
163 String getString()
164 {
165 if (type == BIND_STRING)
166 return sval;
167 else
168 return "";
169 }
172 private:
174 void init()
175 {
176 type = BIND_INT;
177 ival = 0;
178 dval = 0.0;
179 sval = "";
180 }
182 void assign(const Value &other)
183 {
184 type = other.type;
185 ival = other.ival;
186 dval = other.dval;
187 sval = other.sval;
188 }
190 int type;
191 long ival;
192 double dval;
193 String sval;
195 };
201 /**
202 *
203 */
204 class JavaBindery
205 {
206 public:
208 /**
209 *
210 */
211 JavaBindery()
212 {}
214 /**
215 *
216 */
217 virtual ~JavaBindery()
218 {}
220 /**
221 *
222 */
223 virtual bool loadJVM()
224 {
225 return false;
226 }
228 /**
229 *
230 */
231 virtual bool callStatic(int /*type*/,
232 const String &/*className*/,
233 const String &/*methodName*/,
234 const String &/*signature*/,
235 const std::vector<Value> &/*params*/,
236 Value &/*retval*/)
237 {
238 return false;
239 }
241 /**
242 *
243 */
244 virtual bool callMain(const String &/*className*/,
245 const std::vector<String> &/*args*/)
246 {
247 return false;
248 }
250 /**
251 *
252 */
253 virtual bool isLoaded()
254 {
255 return false;
256 }
258 /**
259 *
260 */
261 virtual bool doBinding()
262 {
263 return false;
264 }
266 /**
267 *
268 */
269 virtual String getException()
270 {
271 return "";
272 }
274 virtual String stdOutGet()
275 {
276 return stdOutBuf;
277 }
279 virtual void stdOutClear()
280 {
281 stdOutBuf.clear();
282 }
284 virtual void stdOut(int ch)
285 {
286 stdOutBuf.push_back((char)ch);
287 }
289 virtual String stdErrGet()
290 {
291 return stdErrBuf;
292 }
294 virtual void stdErrClear()
295 {
296 stdErrBuf.clear();
297 }
299 virtual void stdErr(int ch)
300 {
301 stdErrBuf.push_back((char)ch);
302 }
304 virtual String logGet()
305 {
306 return logBuf;
307 }
309 virtual void logClear()
310 {
311 logBuf.clear();
312 }
314 virtual void log(int ch)
315 {
316 logBuf.push_back((char)ch);
317 if (ch == '\n' || ch == '\r')
318 {
319 g_message("%s", logBuf.c_str());
320 logBuf.clear();
321 }
322 }
325 /**
326 * Return a singleton instance of this bindery
327 */
328 static JavaBindery *getInstance();
330 protected:
333 String stdOutBuf;
334 String stdErrBuf;
335 String logBuf;
337 };
343 } // namespace Bind
344 } // namespace Inkscape
346 #endif /* __JAVABIND_H__ */
347 //########################################################################
348 //# E N D O F F I L E
349 //########################################################################