Code

cd622960b6824b8129a8c3e528dbd16b41a8e34f
[inkscape.git] / src / bind / javabind.h
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         }
96         
97     /**
98      *
99      */
100     int getType()
101         { return type; }
102                 
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; }
159                 
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
206 public:
208     /**
209      *
210      */
211     JavaBindery()
212         {}
213     
214     /**
215      *
216      */
217     virtual ~JavaBindery()
218         {}
219     
220     /**
221      *
222      */
223     virtual bool loadJVM()
224         {
225         return false;
226         }
227     
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         {
246         return false;
247         }
249     /**
250      *
251      */
252     virtual bool isLoaded()
253         {
254         return false;
255         }
257     /**
258      *
259      */
260     virtual bool doBinding()
261         {
262         return false;
263         }
264         
265     /**
266      *
267      */
268     virtual String getException()
269         {
270                 return "";
271                 }
272         
273     virtual String stdOutGet()
274         {
275         return stdOutBuf;
276         }
278     virtual void stdOutClear()
279         {
280         stdOutBuf.clear();
281         }
283     virtual void stdOut(int ch)
284         {
285         stdOutBuf.push_back((char)ch);
286         }
288     virtual String stdErrGet()
289         {
290         return stdErrBuf;
291         }
293     virtual void stdErrClear()
294         {
295         stdErrBuf.clear();
296         }
298     virtual void stdErr(int ch)
299         {
300         stdErrBuf.push_back((char)ch);
301         }
303     virtual String logGet()
304         {
305         return logBuf;
306         }
308     virtual void logClear()
309         {
310         logBuf.clear();
311         }
313     virtual void log(int ch)
314         {
315         logBuf.push_back((char)ch);
316         if (ch == '\n' || ch == '\r')
317             {
318             g_message("%s", logBuf.c_str());
319             logBuf.clear();
320                         }
321         }
324     /**
325      *  Return a singleton instance of this bindery
326      */
327     static JavaBindery *getInstance();
328     
329 protected:
332     String stdOutBuf;
333     String stdErrBuf;
334     String logBuf;
336 };
342 } // namespace Bind
343 } // namespace Inkscape
345 #endif  /* __JAVABIND_H__ */
346 //########################################################################
347 //# E N D    O F    F I L E
348 //########################################################################