Code

Finished multiple inheritance delegation
[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;
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(const Value &other)
99         {
100         assign(other);
101         }
103     /**
104      *
105      */
106     Value &operator=(const Value &other)
107         {
108         assign(other);
109         return *this;
110         }
112     /**
113      *
114      */
115     virtual ~Value()
116         {
117         }
118         
119     /**
120      *
121      */
122     int getType()
123         { return type; }
124                 
125     /**
126      *
127      */
128     void setBoolean(bool val)
129         { type = BIND_BOOLEAN; ival = (int)val; }
131     /**
132      *
133      */
134     bool getBoolean()
135         {
136         if (type == BIND_BOOLEAN)
137             return (bool)ival;
138         else
139             return false;
140         }
142     /**
143      *
144      */
145     void setInt(int val)
146         { type = BIND_INT; ival = val; }
148     /**
149      *
150      */
151     bool getInt()
152         {
153         if (type == BIND_INT)
154             return ival;
155         else
156             return 0;
157         }
159     /**
160      *
161      */
162     void setDouble(double val)
163         { type = BIND_DOUBLE; dval = val; }
165     /**
166      *
167      */
168     double getDouble()
169         {
170         if (type == BIND_DOUBLE)
171             return dval;
172         else
173             return 0.0;
174         }
176     /**
177      *
178      */
179     void setString(const String &val)
180         { type = BIND_STRING; sval = val; }
181                 
182     /**
183      *
184      */
185     String getString()
186         {
187         if (type == BIND_STRING)
188             return sval;
189         else
190             return "";
191         }
194 private:
196     void init()
197         {
198         type = BIND_INT;
199         ival = 0;
200         dval = 0.0;
201         sval = "";
202         }
204     void assign(const Value &other)
205         {
206         type = other.type;
207         ival = other.ival;
208         dval = other.dval;
209         sval = other.sval;
210         }
212     int    type;
213     long   ival;
214     double dval;
215     String sval;
217 };
223 /**
224  *
225  */
226 class JavaBindery
228 public:
230     /**
231      *
232      */
233     JavaBindery()
234         {}
235     
236     /**
237      *
238      */
239     virtual ~JavaBindery()
240         {}
241     
242     /**
243      *
244      */
245     virtual bool loadJVM()
246         {
247         return false;
248         }
249     
250     /**
251      *
252      */
253     virtual bool callStatic(int /*type*/,
254                             const String &/*className*/,
255                             const String &/*methodName*/,
256                             const String &/*signature*/,
257                             const std::vector<Value> &/*params*/,
258                             Value &/*retval*/)
259         {
260         return false;
261         }
263     /**
264      *
265      */
266     virtual bool callMain(const String &/*className*/,
267                               const std::vector<String> &/*args*/)
268         {
269         return false;
270         }
272     /**
273      *
274      */
275     virtual bool isLoaded()
276         {
277         return false;
278         }
280     /**
281      *
282      */
283     virtual bool doBinding()
284         {
285         return false;
286         }
287         
288     /**
289      *
290      */
291     virtual String getException()
292         {
293                 return "";
294                 }
295         
296     virtual String stdOutGet()
297         {
298         return stdOutBuf;
299         }
301     virtual void stdOutClear()
302         {
303         stdOutBuf.clear();
304         }
306     virtual void stdOut(int ch)
307         {
308         stdOutBuf.push_back((char)ch);
309         }
311     virtual String stdErrGet()
312         {
313         return stdErrBuf;
314         }
316     virtual void stdErrClear()
317         {
318         stdErrBuf.clear();
319         }
321     virtual void stdErr(int ch)
322         {
323         stdErrBuf.push_back((char)ch);
324         }
326     virtual String logGet()
327         {
328         return logBuf;
329         }
331     virtual void logClear()
332         {
333         logBuf.clear();
334         }
336     virtual void log(int ch)
337         {
338         logBuf.push_back((char)ch);
339         if (ch == '\n' || ch == '\r')
340             {
341             g_message("%s", logBuf.c_str());
342             logBuf.clear();
343                         }
344         }
347     /**
348      *  Return a singleton instance of this bindery
349      */
350     static JavaBindery *getInstance();
351     
352 protected:
355     String stdOutBuf;
356     String stdErrBuf;
357     String logBuf;
359 };
365 } // namespace Bind
366 } // namespace Inkscape
368 #endif  /* __JAVABIND_H__ */
369 //########################################################################
370 //# E N D    O F    F I L E
371 //########################################################################