Ryujinx-GtkSharp/generator/StructBase.cs
Rachel Hestilow 967e3e9c5a 2002-07-30 Rachel Hestilow <hestilow@ximian.com>
* generator/ClassBase.cs: Change hasDefaultConstructor to protected,
	adjust now that it is an attr and not a subnode. Also add virtual
	property AssignToName (for ctors).

	* generator/Ctor.cs: Add property ForceStatic.
	(Generate): Optimize return code a bit for the static case.

	* generator/Method.cs: Assign to a "raw_ret" pointer before calling
	FromNativeReturn.

	* generator/Parameters.cs: Change "out ref" to "out", not "ref".

	* generator/Property.cs: Fix to work correctly with all object and
	struct types (mostly just some if-cases added).

	* generator/SignalHandler.cs: Remove args_type and argfields (unused).
	(Generate): Initialize struct if necessary.

	* generator/StructBase.cs: Massive reworking to support methods, ctors,
	etc.

	* generator/SymbolTable.cs: Add GdkAtom and gconstpointer simple types.

	* glib/Boxed.cs: Accept both IntPtr and object ctors. Add access for both.

	* glib/Opaque.cs: Fix copy/pasted copyright notice, remove data and event
	fields. Fix docs.

	* glib/Value.cs: Work correctly with boxed properties.

	* gnome/Modules.cs: Use new struct ctors.

	* gnome/Program.custom: Remove Get, this is being generated now.

	* parser/Gdk.metadata: Fix the drawable classes to inherit correctly.

	* parser/Metadata.pm: Change per-class attributes to actually be
	attributes.

	* parser/Gtk.metadata: Add a dummy attribute value for disabledefaultctor.

	* parser/gapi2xml.pl: Add hacks for the (broken) Drawable and Bitmap
	typedefs.

	* sample/test/TestColorSelection.cs: Display color string in hex format,
	update to use IsNull instead of == null, and size dialog to look pretty.

	* sample/Size.cs: Added.

svn path=/trunk/gtk-sharp/; revision=6264
2002-07-30 23:02:12 +00:00

222 lines
5.2 KiB
C#

// GtkSharp.Generation.StructBase.cs - The Structure/Object Base Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
public class StructBase : ClassBase {
ArrayList fields = new ArrayList ();
uint bitfields;
public StructBase (XmlElement ns, XmlElement elem) : base (ns, elem)
{
hasDefaultConstructor = false;
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;
switch (node.Name) {
case "field":
fields.Add (member);
break;
case "callback":
Statistics.IgnoreCount++;
break;
default:
if (!IsNodeNameHandled (node.Name))
Console.WriteLine ("Unexpected node " + node.Name + " in " + CName);
break;
}
}
}
public override String MarshalType {
get
{
return "ref " + QualifiedName;
}
}
public override String MarshalReturnType {
get
{
return "IntPtr";
}
}
public override String CallByName (String var_name)
{
return "ref " + var_name;
}
public override String CallByName ()
{
return "ref this";
}
public override String AssignToName {
get { return "raw"; }
}
public override String FromNative(String var)
{
return var;
}
public override String FromNativeReturn(String var)
{
return QualifiedName + ".New (" + var + ")";
}
bool IsBit (XmlElement field)
{
return (field.HasAttribute("bits") && (field.GetAttribute("bits") == "1"));
}
bool IsPointer (XmlElement field)
{
string c_type = field.GetAttribute("type");
return (c_type[c_type.Length - 1] == '*');
}
protected void GenFields (StreamWriter sw)
{
bitfields = 0;
bool need_field = true;
foreach (XmlElement field in fields) {
if (IsBit (field)) {
if (need_field)
need_field = false;
else
continue;
} else
need_field = true;
GenField (field, sw);
}
}
protected bool GetFieldInfo (XmlElement field, out string type, out string name)
{
name = "";
if (IsBit (field))
type = "uint";
else if (IsPointer (field)) {
type = "IntPtr";
name = "_";
} else {
string c_type = field.GetAttribute ("type");
type = SymbolTable.GetCSType (c_type);
if (type == "") {
Console.WriteLine ("Field has unknown Type {0}", c_type);
Statistics.ThrottledCount++;
return false;
}
}
if (field.HasAttribute("array_len"))
type += "[]";
if (IsBit (field))
name = String.Format ("_bitfield{0}", bitfields++);
else
name += MangleName (field.GetAttribute ("cname"));
return true;
}
protected bool GenField (XmlElement field, StreamWriter sw)
{
string type, name;
if (!GetFieldInfo (field, out type, out name))
return false;
sw.WriteLine ("\t\tpublic {0} {1};", type, name);
return true;
}
private String MangleName(String name)
{
if (name == "string") {
return "str1ng";
} else if (name == "event") {
return "evnt";
} else if (name == "object") {
return "objekt";
} else if (name == "in") {
return "inn";
} else {
return name;
}
}
public virtual void Generate ()
{
StreamWriter sw = CreateWriter ();
sw.WriteLine ("\tusing System;");
sw.WriteLine ("\tusing System.Collections;");
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
sw.WriteLine ();
sw.WriteLine("\t\t/// <summary> " + Name + " Struct </summary>");
sw.WriteLine("\t\t/// <remarks>");
sw.WriteLine("\t\t/// </remarks>");
sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]");
sw.WriteLine ("\tpublic struct " + Name + " {");
sw.WriteLine ();
GenFields (sw);
sw.WriteLine ();
GenCtors (sw);
GenMethods (sw, null, null, true);
AppendCustom(sw);
sw.WriteLine ("\t}");
CloseWriter (sw);
}
protected override void GenCtors (StreamWriter sw)
{
sw.WriteLine ("\t\tbool _is_null;");
sw.WriteLine ("\t\tpublic bool IsNull {");
sw.WriteLine ("\t\t\tget { return _is_null; }");
sw.WriteLine ("\t\t}");
sw.WriteLine ("\t\tpublic void _Initialize () {");
sw.WriteLine ("\t\t\t_is_null = false;");
sw.WriteLine ("\t\t}");
sw.WriteLine();
sw.WriteLine ("\t\tpublic static " + QualifiedName + " New(IntPtr raw) {");
sw.WriteLine ("\t\t\t{0} self = new {0}();", QualifiedName);
sw.WriteLine ("\t\t\tif (raw == IntPtr.Zero) {");
sw.WriteLine ("\t\t\t\tself._is_null = true;");
sw.WriteLine ("\t\t\t} else {");
sw.WriteLine ("\t\t\t\tself = ({0}) Marshal.PtrToStructure (raw, self.GetType ());", QualifiedName);
sw.WriteLine ("\t\t\t\tself._is_null = false;");
sw.WriteLine ("\t\t\t}");
sw.WriteLine ("\t\t\treturn self;");
sw.WriteLine ("\t\t}");
sw.WriteLine();
foreach (Ctor ctor in Ctors) {
ctor.ForceStatic = true;
}
base.GenCtors (sw);
}
}
}