Code

Remember to add the jni files
[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         } ValueType;
64     /**
65      *
66      */
67     Value()
68         {
69         init();
70         }
72     /**
73      *
74      */
75     Value(const Value &other)
76         {
77         assign(other);
78         }
80     /**
81      *
82      */
83     Value &operator=(const Value &other)
84         {
85         assign(other);
86         return *this;
87         }
89     /**
90      *
91      */
92     virtual ~Value()
93         {
94         }
95         
96     /**
97      *
98      */
99     int getType()
100         { return type; }
101                 
102     /**
103      *
104      */
105     void setBoolean(bool val)
106         { type = BIND_BOOLEAN; ival = (int)val; }
108     /**
109      *
110      */
111     bool getBoolean()
112         {
113         if (type == BIND_BOOLEAN)
114             return (bool)ival;
115         else
116             return false;
117         }
119     /**
120      *
121      */
122     void setInt(int val)
123         { type = BIND_INT; ival = val; }
125     /**
126      *
127      */
128     bool getInt()
129         {
130         if (type == BIND_INT)
131             return ival;
132         else
133             return 0;
134         }
136     /**
137      *
138      */
139     void setDouble(double val)
140         { type = BIND_DOUBLE; dval = val; }
142     /**
143      *
144      */
145     double getDouble()
146         {
147         if (type == BIND_DOUBLE)
148             return dval;
149         else
150             return 0.0;
151         }
153     /**
154      *
155      */
156     void setString(const String &val)
157         { type = BIND_STRING; sval = val; }
158                 
159     /**
160      *
161      */
162     String getString()
163         {
164         if (type == BIND_STRING)
165             return sval;
166         else
167             return "";
168         }
171 private:
173     void init()
174         {
175         type = BIND_INT;
176         ival = 0;
177         dval = 0.0;
178         sval = "";
179         }
181     void assign(const Value &other)
182         {
183         type = other.type;
184         ival = other.ival;
185         dval = other.dval;
186         sval = other.sval;
187         }
189     int    type;
190     long   ival;
191     double dval;
192     String sval;
194 };
200 /**
201  *
202  */
203 class JavaBindery
205 public:
207     /**
208      *
209      */
210     JavaBindery()
211         {}
212     
213     /**
214      *
215      */
216     virtual ~JavaBindery()
217         {}
218     
219     /**
220      *
221      */
222     virtual bool loadJVM()
223         {
224         return false;
225         }
226     
227     /**
228      *
229      */
230     virtual bool callStatic(int /*type*/,
231                             const String &/*className*/,
232                             const String &/*methodName*/,
233                             const String &/*signature*/,
234                             const std::vector<Value> &/*params*/,
235                             Value &/*retval*/)
236         {
237         return false;
238         }
240     /**
241      *
242      */
243     virtual bool callMain(const String &/*className*/)
244         {
245         return false;
246         }
248     /**
249      *
250      */
251     virtual bool isLoaded()
252         {
253         return false;
254         }
256     /**
257      *
258      */
259     virtual bool doBinding()
260         {
261         return false;
262         }
263         
264     virtual String stdOutGet()
265         {
266         return stdOutBuf;
267         }
269     virtual void stdOutClear()
270         {
271         stdOutBuf.clear();
272         }
274     virtual String stdErrGet()
275         {
276         return stdErrBuf;
277         }
279     virtual void stdErrClear()
280         {
281         stdErrBuf.clear();
282         }
284     virtual void stdOut(int ch)
285         {
286         stdOutBuf.push_back((char)ch);
287         }
289     virtual void stdErr(int ch)
290         {
291         stdErrBuf.push_back((char)ch);
292         }
295     /**
296      *  Return a singleton instance of this bindery
297      */
298     static JavaBindery *getInstance();
299     
300 protected:
303     String stdOutBuf;
304     String stdErrBuf;
306 };
312 } // namespace Bind
313 } // namespace Inkscape
315 #endif  /* __JAVABIND_H__ */
316 //########################################################################
317 //# E N D    O F    F I L E
318 //########################################################################