1
// GtkSharp.Generation.StructBase.cs - The Structure/Boxed Base Class.
3
// Author: Mike Kestner <mkestner@speakeasy.net>
5
// Copyright (c) 2001-2003 Mike Kestner
7
// This program is free software; you can redistribute it and/or
8
// modify it under the terms of version 2 of the GNU General Public
9
// License as published by the Free Software Foundation.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
// General Public License for more details.
16
// You should have received a copy of the GNU General Public
17
// License along with this program; if not, write to the
18
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
// Boston, MA 02111-1307, USA.
22
namespace GtkSharp.Generation {
25
using System.Collections;
27
using System.Text.RegularExpressions;
30
public abstract class StructBase : ClassBase, IManualMarshaler {
32
new ArrayList fields = new ArrayList ();
33
bool need_read_native = false;
35
protected StructBase (XmlElement ns, XmlElement elem) : base (ns, elem)
37
foreach (XmlNode node in elem.ChildNodes) {
39
if (!(node is XmlElement)) continue;
40
XmlElement member = (XmlElement) node;
44
fields.Add (new StructField (member, this));
48
Statistics.IgnoreCount++;
52
if (!IsNodeNameHandled (node.Name))
53
Console.WriteLine ("Unexpected node " + node.Name + " in " + CName);
59
public override string DefaultValue {
61
return QualifiedName + ".Zero";
65
public override string MarshalType {
71
public override string AssignToName {
72
get { throw new NotImplementedException (); }
75
public override string CallByName ()
77
return "this_as_native";
80
public override string CallByName (string var)
82
return var + "_as_native";
85
public override string FromNative (string var)
88
return var + " == IntPtr.Zero ? " + QualifiedName + ".Zero : (" + QualifiedName + ") System.Runtime.InteropServices.Marshal.PtrToStructure (" + var + ", typeof (" + QualifiedName + "))";
90
return QualifiedName + ".New (" + var + ")";
93
public string AllocNative (string var)
95
return "GLib.Marshaller.StructureToPtrAlloc (" + var + ")";
98
public string ReleaseNative (string var)
100
return "Marshal.FreeHGlobal (" +var + ")";
103
private bool DisableNew {
105
return Elem.HasAttribute ("disable_new");
109
protected new void GenFields (GenerationInfo gen_info)
112
bool need_field = true;
114
foreach (StructField field in fields) {
115
if (field.IsBitfield) {
117
StreamWriter sw = gen_info.Writer;
119
sw.WriteLine ("\t\tprivate uint _bitfield{0};\n", bitfields++);
124
field.Generate (gen_info, "\t\t");
128
public override bool Validate ()
130
foreach (StructField field in fields) {
131
if (!field.Validate ()) {
132
Console.WriteLine ("in Struct " + QualifiedName);
133
if (!field.IsPointer)
138
return base.Validate ();
141
public override void Generate (GenerationInfo gen_info)
143
bool need_close = false;
144
if (gen_info.Writer == null) {
145
gen_info.Writer = gen_info.OpenStream (Name);
149
StreamWriter sw = gen_info.Writer;
151
sw.WriteLine ("namespace " + NS + " {");
153
sw.WriteLine ("\tusing System;");
154
sw.WriteLine ("\tusing System.Collections;");
155
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
158
sw.WriteLine ("#region Autogenerated code");
160
sw.WriteLine ("\t[Obsolete]");
161
sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]");
162
string access = IsInternal ? "internal" : "public";
163
sw.WriteLine ("\t" + access + " struct " + Name + " {");
166
need_read_native = false;
167
GenFields (gen_info);
170
GenMethods (gen_info, null, this);
171
if (need_read_native)
177
sw.WriteLine ("#endregion");
178
AppendCustom(sw, gen_info.CustomDir);
180
sw.WriteLine ("\t}");
183
gen_info.Writer = null;
186
protected override void GenCtors (GenerationInfo gen_info)
188
StreamWriter sw = gen_info.Writer;
190
sw.WriteLine ("\t\tpublic static {0} Zero = new {0} ();", QualifiedName);
193
sw.WriteLine ("\t\tpublic static " + QualifiedName + " New(IntPtr raw) {");
194
sw.WriteLine ("\t\t\tif (raw == IntPtr.Zero)");
195
sw.WriteLine ("\t\t\t\treturn {0}.Zero;", QualifiedName);
196
sw.WriteLine ("\t\t\treturn ({0}) Marshal.PtrToStructure (raw, typeof ({0}));", QualifiedName);
197
sw.WriteLine ("\t\t}");
201
foreach (Ctor ctor in Ctors)
202
ctor.IsStatic = true;
204
base.GenCtors (gen_info);
207
void GenReadNative (StreamWriter sw)
209
sw.WriteLine ("\t\tstatic void ReadNative (IntPtr native, ref {0} target)", QualifiedName);
210
sw.WriteLine ("\t\t{");
211
sw.WriteLine ("\t\t\ttarget = New (native);");
212
sw.WriteLine ("\t\t}");
216
public override void Prepare (StreamWriter sw, string indent)
218
sw.WriteLine (indent + "IntPtr this_as_native = System.Runtime.InteropServices.Marshal.AllocHGlobal (System.Runtime.InteropServices.Marshal.SizeOf (this));");
219
sw.WriteLine (indent + "System.Runtime.InteropServices.Marshal.StructureToPtr (this, this_as_native, false);");
222
public override void Finish (StreamWriter sw, string indent)
224
need_read_native = true;
225
sw.WriteLine (indent + "ReadNative (this_as_native, ref this);");
226
sw.WriteLine (indent + "System.Runtime.InteropServices.Marshal.FreeHGlobal (this_as_native);");