2002-11-10 10:03:51 +00:00
|
|
|
// 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 + ")";
|
|
|
|
}
|
|
|
|
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
public override String ToNativeReturn(String var)
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
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 c_type, out string type, out string name)
|
|
|
|
{
|
|
|
|
name = "";
|
|
|
|
c_type = field.GetAttribute ("type");
|
2003-05-19 02:45:17 +00:00
|
|
|
type = SymbolTable.Table.GetCSType (c_type);
|
2002-11-10 10:03:51 +00:00
|
|
|
if (IsBit (field)) {
|
|
|
|
type = "uint";
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if ((IsPointer (field) || SymbolTable.Table.IsOpaque (c_type)) && type != "string") {
|
2002-11-10 10:03:51 +00:00
|
|
|
type = "IntPtr";
|
|
|
|
name = "_";
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if (SymbolTable.Table.IsCallback (c_type)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
type = "IntPtr";
|
|
|
|
} else {
|
|
|
|
if (type == "") {
|
|
|
|
Console.WriteLine ("Field has unknown Type {0}", c_type);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: marshalling not implemented here in mono
|
|
|
|
if (field.HasAttribute("array_len"))
|
|
|
|
type = "IntPtr";
|
|
|
|
|
|
|
|
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 c_type, type, name;
|
|
|
|
if (!GetFieldInfo (field, out c_type, out type, out name))
|
|
|
|
return false;
|
|
|
|
sw.WriteLine ("\t\tpublic {0} {1};", type, name);
|
|
|
|
|
|
|
|
if (field.HasAttribute("array_len"))
|
|
|
|
Console.WriteLine ("warning: array field {0}.{1} probably incorrectly generated", QualifiedName, name);
|
2003-05-19 02:45:17 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
string wrapped = table.GetCSType (c_type);
|
2002-11-10 10:03:51 +00:00
|
|
|
string wrapped_name = MangleName (field.GetAttribute ("cname"));
|
2003-05-19 02:45:17 +00:00
|
|
|
if (table.IsObject (c_type)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic " + wrapped + " " + wrapped_name + " {");
|
|
|
|
sw.WriteLine ("\t\t\tget { ");
|
2003-07-23 17:19:21 +00:00
|
|
|
sw.WriteLine ("\t\t\t\tbool ref_owned = false;");
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine ("\t\t\t\t" + wrapped + " ret = " + table.FromNativeReturn(c_type, name) + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t\t\treturn ret;");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine ("\t\t\tset { " + name + " = " + table.CallByName (c_type, "value") + "; }");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if (table.IsOpaque (c_type)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic " + wrapped + " " + wrapped_name + " {");
|
|
|
|
sw.WriteLine ("\t\t\tget { ");
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine ("\t\t\t\t" + wrapped + " ret = " + table.FromNativeReturn(c_type, name) + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t\t\tif (ret == null) ret = new " + wrapped + "(" + name + ");");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn ret;");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine ("\t\t\tset { " + name + " = " + table.CallByName (c_type, "value") + "; }");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if (IsPointer (field) && (table.IsStruct (c_type) || table.IsBoxed (c_type))) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic " + wrapped + " " + wrapped_name + " {");
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine ("\t\t\tget { return " + table.FromNativeReturn (c_type, name) + "; }");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String MangleName(String name)
|
|
|
|
{
|
|
|
|
if (name == "string") {
|
|
|
|
return "str1ng";
|
|
|
|
} else if (name == "event") {
|
|
|
|
return "evnt";
|
|
|
|
} else if (name == "null") {
|
|
|
|
return "is_null";
|
|
|
|
} else if (name == "object") {
|
|
|
|
return "objekt";
|
|
|
|
} else if (name == "ref") {
|
|
|
|
return "reference";
|
|
|
|
} else if (name == "params") {
|
|
|
|
return "parms";
|
|
|
|
} else if (name == "fixed") {
|
|
|
|
return "mfixed";
|
|
|
|
} else if (name == "in") {
|
|
|
|
return "inn";
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Generate ()
|
|
|
|
{
|
|
|
|
StreamWriter sw = CreateWriter ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\tusing System.Collections;");
|
|
|
|
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#region Autogenerated code");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]");
|
|
|
|
sw.WriteLine ("\tpublic struct " + Name + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
GenFields (sw);
|
|
|
|
sw.WriteLine ();
|
|
|
|
GenCtors (sw);
|
|
|
|
GenMethods (sw, null, null, true);
|
2003-01-05 23:51:37 +00:00
|
|
|
|
|
|
|
sw.WriteLine ("#endregion");
|
2002-11-10 10:03:51 +00:00
|
|
|
AppendCustom(sw);
|
|
|
|
|
|
|
|
sw.WriteLine ("\t}");
|
|
|
|
CloseWriter (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void GenCtors (StreamWriter sw)
|
|
|
|
{
|
|
|
|
sw.WriteLine ("\t\tpublic static {0} Zero = new {0} ();", QualifiedName);
|
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine ("\t\tpublic static " + QualifiedName + " New(IntPtr raw) {");
|
|
|
|
sw.WriteLine ("\t\t\tif (raw == IntPtr.Zero) {");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn {0}.Zero;", QualifiedName);
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
sw.WriteLine ("\t\t\t{0} self = new {0}();", QualifiedName);
|
|
|
|
sw.WriteLine ("\t\t\tself = ({0}) Marshal.PtrToStructure (raw, self.GetType ());", QualifiedName);
|
|
|
|
sw.WriteLine ("\t\t\treturn self;");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic static bool operator == ({0} a, {0} b)", QualifiedName);
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\treturn a.Equals (b);");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic static bool operator != ({0} a, {0} b)", QualifiedName);
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\treturn ! a.Equals (b);");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
|
|
|
|
foreach (Ctor ctor in Ctors) {
|
|
|
|
ctor.ForceStatic = true;
|
2003-04-06 09:21:15 +00:00
|
|
|
if (ctor.Params != null)
|
|
|
|
ctor.Params.Static = true;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
2003-04-06 09:21:15 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
base.GenCtors (sw);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|