]> icculus.org git repositories - duncan/yast2-ruby-bindings.git/blob - src/swig/yast.i
- Lot of fixes and support for the UI!!!
[duncan/yast2-ruby-bindings.git] / src / swig / yast.i
1 %module ryast
2 %include std_string.i
3 %include stl.i
4  %{
5 /* Includes the header in the wrapper code */
6 #include <ycp/YCPCode.h>
7 #include <ycp/YCPElement.h>
8 #include <ycp/YCPExternal.h>
9 #include <ycp/YCPValue.h>
10 #include <ycp/YCPBoolean.h>
11 #include <ycp/YCPList.h>
12 #include <ycp/YCPMap.h>
13 #include <ycp/YCPString.h>
14 #include <ycp/YCPInteger.h>
15 #include <ycp/YCPFloat.h>
16
17 #include <y2/Y2ComponentBroker.h>
18 #include <y2/Y2Namespace.h>
19 #include <y2/Y2Component.h>
20 #include <y2/Y2Function.h>
21 #include <y2/SymbolEntry.h>
22 #include <y2util/Ustring.h>
23 #include <Y2.h>
24
25 #include <ycp/Import.h>
26
27 #include <ycp/pathsearch.h>
28
29 #define y2log_component "Y2Ruby"
30
31 //static swig_type_info _swigt__p_YCPValue;
32
33  %}
34
35 #ifdef SWIGRUBY
36 //%include "ruby.i"
37 #endif
38
39 %rename("+") "operator+";
40 %rename("<<") "operator<<";
41 %rename("!=") "operator!=";
42 %rename("!") "operator!";
43 %rename("==") "operator==";
44     
45 %typemap(in) int YCPValue {
46   // TODO conver integers, and add support for lists, ah, and boleans!
47   switch (TYPE($1))
48   {
49     case T_STRING:
50     $result = YCPString(RSTRING ($1)->ptr);
51     break;
52     case T_TRUE:
53     $result = YCPBoolean(true);
54     break;
55     case T_FALSE:
56     $result = YCPBoolean(false);
57     break;
58     case T_FIXNUM:
59     $result = YCPInteger(NUM2LONG($1));
60     break;
61     case T_FLOAT:
62     $result = YCPFloat(NUM2DBL($1));
63     break;
64     case T_ARRAY:
65     // FIXME
66     break;
67     case T_HASH:
68     // FIXME
69     break;
70     case T_DATA:
71     rb_raise( rb_eRuntimeError, "Object");
72     break;
73   }
74   std::cout << TYPE($1) << std::endl;
75   rb_raise( rb_eRuntimeError, "Conversion of Ruby type not supported");
76   $result = YCPValue();
77 }
78
79 %typemap(out) YCPValue {
80   // TODO
81   // YT_BYTEBLOCK YT_PATH YT_SYMBOL YT_LIST YT_TERM YT_MAP YT_CODE YT_RETURN YT_BREAK YT_ENTRY YT_ERROR  YT_REFERENCE YT_EXTERNA
82   if($1->isVoid())
83   {
84     $result = Qnil;
85   }
86   else if($1->isBoolean())
87   {
88     $result = $1->asBoolean()->value() ? Qtrue : Qfalse;
89   }
90   else if($1->isString())
91   {
92     $result = rb_str_new2($1->asString()->value().c_str());
93   }
94   else if($1->isInteger())
95   {
96     $result = INT2NUM( $1->asInteger()->value() );
97   }
98   else if( $1->isMap() )
99   {
100     VALUE rbhash;
101     rbhash = rb_hash_new();
102     YCPMap map = $1->asMap();
103     printf("map size %d\n", map.size());
104     
105     for ( YCPMapIterator it = map.begin(); it != map.end(); ++it )
106     {
107       YCPValue *key = new YCPValue(it.key());
108       YCPValue *value = new YCPValue(it.value());
109       VALUE rkey = SWIG_NewPointerObj(key, SWIGTYPE_p_YCPValue, 1);
110       VALUE rvalue = SWIG_NewPointerObj(value, SWIGTYPE_p_YCPValue, 1);
111       rb_hash_aset(rbhash, rkey, rvalue );
112     }
113     $result = rbhash;
114   }
115   else if($1->isList())
116   {
117     VALUE rblist;
118     rblist = rb_ary_new();
119     YCPList list = $1->asList();
120     printf("list size %d\n",list.size()); 
121     for (int i=0; i < list.size(); i++)
122     {
123       YCPValue *value = new YCPValue(list.value(i));
124        VALUE rvalue = SWIG_NewPointerObj(value, SWIGTYPE_p_YCPValue, 1);
125       rb_ary_push( rblist, rvalue);
126     }
127     $result = rblist;
128   }
129   rb_raise( rb_eRuntimeError, "Conversion of YCP type %s not supported", $1->toString().c_str() );
130   $result = Qnil;
131 }
132
133 //%include "y2util/RepDef.h"
134 %include "y2/Y2ComponentBroker.h"
135 %include "y2/Y2Namespace.h"
136 %include "y2/Y2Component.h"
137 %include "y2/Y2Function.h"
138
139 %ignore SymbolEntryPtr::_nameHash;
140 %ignore SymbolEntry::emptyUstring;
141     
142 class SymbolEntry
143 {
144 public:
145     //static UstringHash* _nameHash;
146     //static Ustring emptyUstring;
147 public:
148     typedef enum {
149         c_unspec = 0,           //  0 unspecified local symbol (sets m_global = false)
150         c_global,               //  1 unspecified global symbol (translates to c_unspec, sets m_global = true)
151         c_module,               //  2 a module identifier
152         c_variable,             //  3 a variable
153         c_reference,            //  4 a reference to a variable
154         c_function,             //  5 a defined function
155         c_builtin,              //  6 a builtin function
156         c_typedef,              //  7 a type
157         c_const,                //  8 a constant (a read-only c_variable)
158         c_namespace,            //  9 a namespace identifier
159         c_self,                 // 10 the current namespace (namespace prefix used in namespace definition)
160         c_predefined,           // 11 a predefined namespace identifier
161         c_filename              // 12 a filename (used in conjunction with TableEntry to store definition locations)
162     } category_t;
163 public:
164     // create symbol beloging to namespace (at position)
165     SymbolEntry (const Y2Namespace* name_space, unsigned int position, const char *name, category_t cat, constTypePtr type);
166
167     virtual ~SymbolEntry ();
168
169     // symbols link to the defining namespace
170     const Y2Namespace *nameSpace () const;
171     void setNamespace (const Y2Namespace *name_space);
172     virtual bool onlyDeclared () const { return false; }
173
174     unsigned int position () const;
175     void setPosition (unsigned int position);
176
177     bool isGlobal () const;
178     void setGlobal (bool global);
179
180     bool isModule () const { return m_category == c_module; }
181     bool isVariable () const { return m_category == c_variable; }
182     bool isReference () const { return m_category == c_reference; }
183     bool isFunction () const { return m_category == c_function; }
184     bool isBuiltin () const { return m_category == c_builtin; }
185     bool isNamespace () const { return m_category == c_namespace; }
186     bool isSelf () const { return m_category == c_self; }
187     bool isFilename () const { return m_category == c_filename; }
188     bool isPredefined () const { return m_category == c_predefined; }
189
190     bool likeNamespace () const { return isModule() || isNamespace() || isSelf(); }
191
192     const char *name () const;
193     category_t category () const;
194     void setCategory (category_t cat);
195     constTypePtr type () const;
196     string catString () const;
197     void setType (constTypePtr type);
198     YCPValue setValue (YCPValue value);
199     YCPValue value () const;
200     
201     void push ();
202     void pop ();
203
204     virtual string toString (bool with_type = true) const;
205 };
206
207 template < typename T >
208 class Ptr {
209   public:
210   T *operator->();
211 };
212 %template (SymbolEntryPtr) Ptr<SymbolEntry>;
213
214 %include "ycp/SymbolTable.h"
215
216 %include "ycp/Import.h"
217 %include "ycp/Type.h"
218 %include "ycp/TypePtr.h"