File: FieldBase.cs

package info (click to toggle)
clutter-sharp 1.0.0~alpha3~git20090817.r1.349dba6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,104 kB
  • ctags: 2,193
  • sloc: xml: 23,456; cs: 9,946; sh: 3,393; perl: 1,213; makefile: 270; awk: 50; sed: 13
file content (270 lines) | stat: -rw-r--r-- 9,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// GtkSharp.Generation.FieldBase.cs - base class for struct and object
// fields
//
// Copyright (c) 2004 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


namespace GtkSharp.Generation {

	using System;
	using System.Collections;
	using System.IO;
	using System.Xml;

	public abstract class FieldBase : PropertyBase {
		public FieldBase (XmlElement elem, ClassBase container_type) : base (elem, container_type) {}

		public virtual bool Validate ()
		{
			if (!Ignored && !Hidden && CSType == "") {
				Console.Write("Field {0} has unknown Type {1} ", Name, CType);
				Statistics.ThrottledCount++;
				return false;
			}

			return true;
		}

		protected virtual bool Readable {
			get {
				return elem.GetAttribute ("readable") != "false";
			}
		}

		protected virtual bool Writable {
			get {
				return elem.GetAttribute ("writeable") != "false";
			}
		}

		protected abstract string DefaultAccess { get; }

		protected string Access {
			get {
				return elem.HasAttribute ("access") ? elem.GetAttribute ("access") : DefaultAccess;
			}
		}

		public bool IsArray {
			get {
				return elem.HasAttribute("array_len") || elem.HasAttribute("array");
			}
		}

		public bool IsBitfield {
			get {
				return elem.HasAttribute("bits");
			}
		}

		public bool Ignored {
			get {
				if (container_type.GetProperty (Name) != null)
					return true;
				if (IsArray)
					return true;
				if (Access == "private" && (Getter == null) && (Setter == null))
					return true;
				return false;
			}
		}

		string getterName, setterName;
		string getOffsetName, offsetName;

		void CheckGlue ()
		{
			getterName = setterName = getOffsetName = null;
			if (Access != "public")
				return;

			string prefix = (container_type.NS + "Sharp_" + container_type.NS + "_" + container_type.Name).Replace(".", "__").ToLower ();

			if (IsBitfield) {
				if (Readable && Getter == null)
					getterName = prefix + "_get_" + CName;
				if (Writable && Setter == null)
					setterName = prefix + "_set_" + CName;
			} else {
				if ((Readable && Getter == null) || (Writable && Setter == null)) {
					offsetName = CName + "_offset";
					getOffsetName = prefix + "_get_" + offsetName;
				}
			}
		}

		protected override void GenerateImports (GenerationInfo gen_info, string indent)
		{
			StreamWriter sw = gen_info.Writer;
			SymbolTable table = SymbolTable.Table;

			if (getterName != null) {
				sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
				sw.WriteLine (indent + "extern static {0} {1} ({2} raw);",
					      table.GetMarshalReturnType (CType), getterName,
					      container_type.MarshalType);
			}

			if (setterName != null) {
				sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
				sw.WriteLine (indent + "extern static void {0} ({1} raw, {2} value);",
					      setterName, container_type.MarshalType, table.GetMarshalType (CType));
			}

			if (getOffsetName != null) {
				sw.WriteLine (indent + "[DllImport (\"{0}\")]", gen_info.GluelibName);
				sw.WriteLine (indent + "extern static uint {0} ();", getOffsetName);
				sw.WriteLine ();
				sw.WriteLine (indent + "static uint " + offsetName + " = " + getOffsetName + " ();");
			}

			base.GenerateImports (gen_info, indent);
		}

		public virtual void Generate (GenerationInfo gen_info, string indent)
		{
			if (Ignored || Hidden)
				return;

			CheckGlue ();
			if ((getterName != null || setterName != null || getOffsetName != null) &&
			    gen_info.GlueWriter == null) {
				Console.WriteLine ("No glue-filename specified, can't create glue for {0}.{1}",
						   container_type.Name, Name);
				return;
			}

			GenerateImports (gen_info, indent);

			SymbolTable table = SymbolTable.Table;
			StreamWriter sw = gen_info.Writer;
			string modifiers = elem.HasAttribute ("new_flag") ? "new " : "";
			bool is_struct = table.IsStruct (CType) || table.IsBoxed (CType);

			sw.WriteLine (indent + "public " + modifiers + CSType + " " + Name + " {");

			if (Getter != null) {
				sw.Write (indent + "\tget ");
				Getter.GenerateBody (gen_info, container_type, "\t");
				sw.WriteLine ("");
			} else if (getterName != null) {
				sw.WriteLine (indent + "\tget {");
				container_type.Prepare (sw, indent + "\t\t");
				sw.WriteLine (indent + "\t\t" + CSType + " result = " + table.FromNativeReturn (ctype, getterName + " (" + container_type.CallByName () + ")") + ";");
				container_type.Finish (sw, indent + "\t\t");
				sw.WriteLine (indent + "\t\treturn result;");
				sw.WriteLine (indent + "\t}");
			} else if (Readable && offsetName != null) {
				sw.WriteLine (indent + "\tget {");
				sw.WriteLine (indent + "\t\tunsafe {");
				if (is_struct) {
					sw.WriteLine (indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
					sw.WriteLine (indent + "\t\t\treturn *raw_ptr;");
				} else {
					sw.WriteLine (indent + "\t\t\t" + table.GetMarshalReturnType (CType) + "* raw_ptr = (" + table.GetMarshalReturnType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
					sw.WriteLine (indent + "\t\t\treturn " + table.FromNativeReturn (ctype, "(*raw_ptr)") + ";");
				}
				sw.WriteLine (indent + "\t\t}");
				sw.WriteLine (indent + "\t}");
			}

			if (Setter != null) {
				sw.Write (indent + "\tset ");
				Setter.GenerateBody (gen_info, container_type, "\t");
				sw.WriteLine ("");
			} else if (setterName != null) {
				sw.WriteLine (indent + "\tset {");
				container_type.Prepare (sw, indent + "\t\t");
				sw.WriteLine (indent + "\t\t" + setterName + " (" + container_type.CallByName () + ", " + table.CallByName (ctype, "value") + ");");
				container_type.Finish (sw, indent + "\t\t");
				sw.WriteLine (indent + "\t}");
			} else if (Writable && offsetName != null) {
				sw.WriteLine (indent + "\tset {");
				sw.WriteLine (indent + "\t\tunsafe {");
				if (is_struct) {
					sw.WriteLine (indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
					sw.WriteLine (indent + "\t\t\t*raw_ptr = value;");
				} else {
					sw.WriteLine (indent + "\t\t\t" + table.GetMarshalReturnType (CType) + "* raw_ptr = (" + table.GetMarshalReturnType (CType) + "*)(((byte*)" + container_type.CallByName () + ") + " + offsetName + ");");
					sw.WriteLine (indent + "\t\t\t*raw_ptr = " + table.ToNativeReturn (ctype, "value") + ";");
				}
				sw.WriteLine (indent + "\t\t}");
				sw.WriteLine (indent + "\t}");
			}

			sw.WriteLine (indent + "}");
			sw.WriteLine ("");

			if (getterName != null || setterName != null || getOffsetName != null)
				GenerateGlue (gen_info);
		}

		protected void GenerateGlue (GenerationInfo gen_info)
		{
			StreamWriter sw = gen_info.GlueWriter;
			SymbolTable table = SymbolTable.Table;

			string FieldCType = CType.Replace ("-", " ");
			bool byref = table[CType] is ByRefGen || table[CType] is StructGen;
			string GlueCType = byref ? FieldCType + " *" : FieldCType;
			string ContainerCType = container_type.CName;
			string ContainerCName = container_type.Name.ToLower ();

			if (getterName != null) {
				sw.WriteLine ("{0} {1} ({2} *{3});",
					      GlueCType, getterName, ContainerCType, ContainerCName);
			}
			if (setterName != null) {
				sw.WriteLine ("void {0} ({1} *{2}, {3} value);",
					      setterName, ContainerCType, ContainerCName, GlueCType);
			}
			if (getOffsetName != null)
				sw.WriteLine ("guint {0} (void);", getOffsetName);
			sw.WriteLine ("");

			if (getterName != null) {
				sw.WriteLine (GlueCType);
				sw.WriteLine ("{0} ({1} *{2})", getterName, ContainerCType, ContainerCName);
				sw.WriteLine ("{");
				sw.WriteLine ("\treturn ({0}){1}{2}->{3};", GlueCType,
					      byref ? "&" : "", ContainerCName, CName);
				sw.WriteLine ("}");
				sw.WriteLine ("");
			}
			if (setterName != null) {
				sw.WriteLine ("void");
				sw.WriteLine ("{0} ({1} *{2}, {3} value)",
					      setterName, ContainerCType, ContainerCName, GlueCType);
				sw.WriteLine ("{");
				sw.WriteLine ("\t{0}->{1} = ({2}){3}value;", ContainerCName, CName,
					      FieldCType, byref ? "*" : "");
				sw.WriteLine ("}");
				sw.WriteLine ("");
			}
			if (getOffsetName != null) {
				sw.WriteLine ("guint");
				sw.WriteLine ("{0} (void)", getOffsetName);
				sw.WriteLine ("{");
				sw.WriteLine ("\treturn (guint)G_STRUCT_OFFSET ({0}, {1});",
					      ContainerCType, CName);
				sw.WriteLine ("}");
				sw.WriteLine ("");
			}
		}
	}
}