~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to lib/gio-sharp/generator/FieldBase.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GtkSharp.Generation.FieldBase.cs - base class for struct and object
 
2
// fields
 
3
//
 
4
// Copyright (c) 2004 Novell, Inc.
 
5
//
 
6
// This program is free software; you can redistribute it and/or
 
7
// modify it under the terms of version 2 of the GNU General Public
 
8
// License as published by the Free Software Foundation.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
// General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public
 
16
// License along with this program; if not, write to the
 
17
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
// Boston, MA 02111-1307, USA.
 
19
 
 
20
 
 
21
namespace GtkSharp.Generation {
 
22
 
 
23
        using System;
 
24
        using System.Collections;
 
25
        using System.IO;
 
26
        using System.Xml;
 
27
 
 
28
        public abstract class FieldBase : PropertyBase {
 
29
                public FieldBase (XmlElement elem, ClassBase container_type) : base (elem, container_type) {}
 
30
 
 
31
                public bool Validate ()
 
32
                {
 
33
                        if (!Ignored && !Hidden && CSType == "") {
 
34
                                Console.Write("Field {0} has unknown Type {1} ", Name, CType);
 
35
                                Statistics.ThrottledCount++;
 
36
                                return false;
 
37
                        }
 
38
 
 
39
                        return true;
 
40
                }
 
41
 
 
42
                protected virtual bool Readable {
 
43
                        get {
 
44
                                return elem.GetAttribute ("readable") != "false";
 
45
                        }
 
46
                }
 
47
 
 
48
                protected virtual bool Writable {
 
49
                        get {
 
50
                                return elem.GetAttribute ("writeable") != "false";
 
51
                        }
 
52
                }
 
53
 
 
54
                protected abstract string DefaultAccess { get; }
 
55
 
 
56
                protected string Access {
 
57
                        get {
 
58
                                return elem.HasAttribute ("access") ? elem.GetAttribute ("access") : DefaultAccess;
 
59
                        }
 
60
                }
 
61
 
 
62
                public bool IsArray {
 
63
                        get {
 
64
                                return elem.HasAttribute("array_len") || elem.HasAttribute("array");
 
65
                        }
 
66
                }
 
67
 
 
68
                public bool IsBitfield {
 
69
                        get {
 
70
                                return elem.HasAttribute("bits");
 
71
                        }
 
72
                }
 
73
 
 
74
                public bool Ignored {
 
75
                        get {
 
76
                                if (container_type.GetProperty (Name) != null)
 
77
                                        return true;
 
78
                                if (IsArray)
 
79
                                        return true;
 
80
                                if (Access == "private" && (Getter == null) && (Setter == null))
 
81
                                        return true;
 
82
                                return false;
 
83
                        }
 
84
                }
 
85
 
 
86
                string getterName, setterName;
 
87
                string getOffsetName, offsetName;
 
88
 
 
89
                void CheckGlue ()
 
90
                {
 
91
                        getterName = setterName = getOffsetName = null;
 
92
                        if (Access != "public")
 
93
                                return;
 
94
 
 
95
                        string prefix = (container_type.NS + "Sharp_" + container_type.NS + "_" + container_type.Name).ToLower ();
 
96
 
 
97
                        if (IsBitfield) {
 
98
                                if (Readable && Getter == null)
 
99
                                        getterName = prefix + "_get_" + CName;
 
100
                                if (Writable && Setter == null)
 
101
                                        setterName = prefix + "_set_" + CName;
 
102
                        } else {
 
103
                                if ((Readable && Getter == null) || (Writable && Setter == null)) {
 
104
                                        offsetName = CName + "_offset";
 
105
                                        getOffsetName = prefix + "_get_" + offsetName;
 
106
                                }
 
107
                        }
 
108
                }
 
109
 
 
110
                protected override void GenerateImports (GenerationInfo gen_info, string indent)
 
111
                {
 
112
                        StreamWriter sw = gen_info.Writer;
 
113
                        SymbolTable table = SymbolTable.Table;
 
114
 
 
115
                        if (getterName != null) {
 
116
                                sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
 
117
                                sw.WriteLine (indent + "extern static {0} {1} ({2} raw);",
 
118
                                              table.GetMarshalReturnType (CType), getterName,
 
119
                                              container_type.MarshalType);
 
120
                        }
 
121
 
 
122
                        if (setterName != null) {
 
123
                                sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
 
124
                                sw.WriteLine (indent + "extern static void {0} ({1} raw, {2} value);",
 
125
                                              setterName, container_type.MarshalType, table.GetMarshalType (CType));
 
126
                        }
 
127
 
 
128
                        if (getOffsetName != null) {
 
129
                                sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
 
130
                                sw.WriteLine (indent + "extern static uint {0} ();", getOffsetName);
 
131
                                sw.WriteLine ();
 
132
                                sw.WriteLine (indent + "static uint " + offsetName + " = " + getOffsetName + " ();");
 
133
                        }
 
134
 
 
135
                        base.GenerateImports (gen_info, indent);
 
136
                }
 
137
 
 
138
                public virtual void Generate (GenerationInfo gen_info, string indent)
 
139
                {
 
140
                        if (Ignored || Hidden)
 
141
                                return;
 
142
 
 
143
                        CheckGlue ();
 
144
                        if ((getterName != null || setterName != null || getOffsetName != null) &&
 
145
                            gen_info.GlueWriter == null) {
 
146
                                Console.WriteLine ("No glue-filename specified, can't create glue for {0}.{1}",
 
147
                                                   container_type.Name, Name);
 
148
                                return;
 
149
                        }
 
150
 
 
151
                        GenerateImports (gen_info, indent);
 
152
 
 
153
                        SymbolTable table = SymbolTable.Table;
 
154
                        StreamWriter sw = gen_info.Writer;
 
155
                        string modifiers = elem.HasAttribute ("new_flag") ? "new " : "";
 
156
                        bool is_struct = table.IsStruct (CType) || table.IsBoxed (CType);
 
157
 
 
158
                        sw.WriteLine (indent + "public " + modifiers + CSType + " " + Name + " {");
 
159
 
 
160
                        if (Getter != null) {
 
161
                                sw.Write (indent + "\tget ");
 
162
                                Getter.GenerateBody (gen_info, container_type, "\t");
 
163
                                sw.WriteLine ("");
 
164
                        } else if (getterName != null) {
 
165
                                sw.WriteLine (indent + "\tget {");
 
166
                                container_type.Prepare (sw, indent + "\t\t");
 
167
                                sw.WriteLine (indent + "\t\t" + CSType + " result = " + table.FromNativeReturn (ctype, getterName + " (" + container_type.CallByName () + ")") + ";");
 
168
                                container_type.Finish (sw, indent + "\t\t");
 
169
                                sw.WriteLine (indent + "\t\treturn result;");
 
170
                                sw.WriteLine (indent + "\t}");
 
171
                        } else if (Readable && offsetName != null) {
 
172
                                sw.WriteLine (indent + "\tget {");
 
173
                                sw.WriteLine (indent + "\t\tunsafe {");
 
174
                                if (is_struct) {
 
175
                                        sw.WriteLine (indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
 
176
                                        sw.WriteLine (indent + "\t\t\treturn *raw_ptr;");
 
177
                                } else {
 
178
                                        sw.WriteLine (indent + "\t\t\t" + table.GetMarshalReturnType (CType) + "* raw_ptr = (" + table.GetMarshalReturnType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
 
179
                                        sw.WriteLine (indent + "\t\t\treturn " + table.FromNativeReturn (ctype, "(*raw_ptr)") + ";");
 
180
                                }
 
181
                                sw.WriteLine (indent + "\t\t}");
 
182
                                sw.WriteLine (indent + "\t}");
 
183
                        }
 
184
 
 
185
                        if (Setter != null) {
 
186
                                sw.Write (indent + "\tset ");
 
187
                                Setter.GenerateBody (gen_info, container_type, "\t");
 
188
                                sw.WriteLine ("");
 
189
                        } else if (setterName != null) {
 
190
                                sw.WriteLine (indent + "\tset {");
 
191
                                container_type.Prepare (sw, indent + "\t\t");
 
192
                                sw.WriteLine (indent + "\t\t" + setterName + " (" + container_type.CallByName () + ", " + table.CallByName (ctype, "value") + ");");
 
193
                                container_type.Finish (sw, indent + "\t\t");
 
194
                                sw.WriteLine (indent + "\t}");
 
195
                        } else if (Writable && offsetName != null) {
 
196
                                sw.WriteLine (indent + "\tset {");
 
197
                                sw.WriteLine (indent + "\t\tunsafe {");
 
198
                                if (is_struct) {
 
199
                                        sw.WriteLine (indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
 
200
                                        sw.WriteLine (indent + "\t\t\t*raw_ptr = value;");
 
201
                                } else {
 
202
                                        sw.WriteLine (indent + "\t\t\t" + table.GetMarshalReturnType (CType) + "* raw_ptr = (" + table.GetMarshalReturnType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
 
203
                                        sw.WriteLine (indent + "\t\t\t*raw_ptr = " + table.ToNativeReturn (ctype, "value") + ";");
 
204
                                }
 
205
                                sw.WriteLine (indent + "\t\t}");
 
206
                                sw.WriteLine (indent + "\t}");
 
207
                        }
 
208
 
 
209
                        sw.WriteLine (indent + "}");
 
210
                        sw.WriteLine ("");
 
211
 
 
212
                        if (getterName != null || setterName != null || getOffsetName != null)
 
213
                                GenerateGlue (gen_info);
 
214
                }
 
215
 
 
216
                protected void GenerateGlue (GenerationInfo gen_info)
 
217
                {
 
218
                        StreamWriter sw = gen_info.GlueWriter;
 
219
                        SymbolTable table = SymbolTable.Table;
 
220
 
 
221
                        string FieldCType = CType.Replace ("-", " ");
 
222
                        bool byref = table[CType] is ByRefGen || table[CType] is StructGen;
 
223
                        string GlueCType = byref ? FieldCType + " *" : FieldCType;
 
224
                        string ContainerCType = container_type.CName;
 
225
                        string ContainerCName = container_type.Name.ToLower ();
 
226
 
 
227
                        if (getterName != null) {
 
228
                                sw.WriteLine ("{0} {1} ({2} *{3});",
 
229
                                              GlueCType, getterName, ContainerCType, ContainerCName);
 
230
                        }
 
231
                        if (setterName != null) {
 
232
                                sw.WriteLine ("void {0} ({1} *{2}, {3} value);",
 
233
                                              setterName, ContainerCType, ContainerCName, GlueCType);
 
234
                        }
 
235
                        if (getOffsetName != null)
 
236
                                sw.WriteLine ("guint {0} (void);", getOffsetName);
 
237
                        sw.WriteLine ("");
 
238
 
 
239
                        if (getterName != null) {
 
240
                                sw.WriteLine (GlueCType);
 
241
                                sw.WriteLine ("{0} ({1} *{2})", getterName, ContainerCType, ContainerCName);
 
242
                                sw.WriteLine ("{");
 
243
                                sw.WriteLine ("\treturn ({0}){1}{2}->{3};", GlueCType,
 
244
                                              byref ? "&" : "", ContainerCName, CName);
 
245
                                sw.WriteLine ("}");
 
246
                                sw.WriteLine ("");
 
247
                        }
 
248
                        if (setterName != null) {
 
249
                                sw.WriteLine ("void");
 
250
                                sw.WriteLine ("{0} ({1} *{2}, {3} value)",
 
251
                                              setterName, ContainerCType, ContainerCName, GlueCType);
 
252
                                sw.WriteLine ("{");
 
253
                                sw.WriteLine ("\t{0}->{1} = ({2}){3}value;", ContainerCName, CName,
 
254
                                              FieldCType, byref ? "*" : "");
 
255
                                sw.WriteLine ("}");
 
256
                                sw.WriteLine ("");
 
257
                        }
 
258
                        if (getOffsetName != null) {
 
259
                                sw.WriteLine ("guint");
 
260
                                sw.WriteLine ("{0} (void)", getOffsetName);
 
261
                                sw.WriteLine ("{");
 
262
                                sw.WriteLine ("\treturn (guint)G_STRUCT_OFFSET ({0}, {1});",
 
263
                                              ContainerCType, CName);
 
264
                                sw.WriteLine ("}");
 
265
                                sw.WriteLine ("");
 
266
                        }
 
267
                }
 
268
        }
 
269
}
 
270