Code

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