generator: generate all interfaces with the "I" prefix (.NET convention)

To be able to do this, Name property of GenBase class is now virtual, so
that it can be overriden in InterfaceGen with the new name.

Adapters, however, are still classes and then need special care as such
(cannot use the 'Name' property anymore), but this improves a bit the
readability of some parts of the code as the *Implementor and *Adapter
suffixes are now concentrated in just two new properties of GenBase and
not repeated all over the place.
This commit is contained in:
Andres G. Aragoneses 2013-04-01 23:48:22 +01:00 committed by Bertrand Lorentz
parent 214532f684
commit 6cb03440c1
5 changed files with 68 additions and 27 deletions

View File

@ -70,7 +70,7 @@ namespace GtkSharp.Generation {
public abstract string MarshalType { get; } public abstract string MarshalType { get; }
public string Name { public virtual string Name {
get { get {
return elem.GetAttribute ("name"); return elem.GetAttribute ("name");
} }

View File

@ -1,9 +1,12 @@
// GtkSharp.Generation.InterfaceGen.cs - The Interface Generatable. // GtkSharp.Generation.InterfaceGen.cs - The Interface Generatable.
// //
// Author: Mike Kestner <mkestner@speakeasy.net> // Authors:
// Mike Kestner <mkestner@speakeasy.net>
// Andres G. Aragoneses <knocte@gmail.com>
// //
// Copyright (c) 2001-2003 Mike Kestner // Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2004, 2007 Novell, Inc. // Copyright (c) 2004, 2007 Novell, Inc.
// Copyright (c) 2013 Andres G. Aragoneses
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the GNU General Public // modify it under the terms of version 2 of the GNU General Public
@ -58,14 +61,40 @@ namespace GtkSharp.Generation {
} }
} }
public string AdapterName {
get {
return base.Name + "Adapter";
}
}
public string QualifiedAdapterName {
get {
return NS + "." + AdapterName;
}
}
public string ImplementorName {
get {
return Name + "Implementor";
}
}
public override string Name {
get {
return "I" + base.Name;
}
}
public override string CallByName (string var, bool owned) public override string CallByName (string var, bool owned)
{ {
return String.Format ("{0} == null ? IntPtr.Zero : (({0} is GLib.Object) ? ({0} as GLib.Object).{1} : ({0} as {2}Adapter).{1})", var, owned ? "OwnedHandle" : "Handle", QualifiedName); return String.Format (
"{0} == null ? IntPtr.Zero : (({0} is GLib.Object) ? ({0} as GLib.Object).{1} : ({0} as {2}).{1})",
var, owned ? "OwnedHandle" : "Handle", QualifiedAdapterName);
} }
public override string FromNative (string var, bool owned) public override string FromNative (string var, bool owned)
{ {
return QualifiedName + "Adapter.GetObject (" + var + ", " + (owned ? "true" : "false") + ")"; return QualifiedAdapterName + ".GetObject (" + var + ", " + (owned ? "true" : "false") + ")";
} }
public override bool ValidateForSubclass () public override bool ValidateForSubclass ()
@ -90,9 +119,9 @@ namespace GtkSharp.Generation {
{ {
sw.WriteLine ("\t\tstatic {0} iface;", class_struct_name); sw.WriteLine ("\t\tstatic {0} iface;", class_struct_name);
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t\tstatic " + Name + "Adapter ()"); sw.WriteLine ("\t\tstatic " + AdapterName + " ()");
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tGLib.GType.Register (_gtype, typeof({0}Adapter));", Name); sw.WriteLine ("\t\t\tGLib.GType.Register (_gtype, typeof ({0}));", AdapterName);
foreach (InterfaceVM vm in interface_vms) { foreach (InterfaceVM vm in interface_vms) {
if (vm.Validate (new LogWriter (QualifiedName))) if (vm.Validate (new LogWriter (QualifiedName)))
sw.WriteLine ("\t\t\tiface.{0} = new {0}NativeDelegate ({0}_cb);", vm.Name); sw.WriteLine ("\t\t\tiface.{0} = new {0}NativeDelegate ({0}_cb);", vm.Name);
@ -135,12 +164,12 @@ namespace GtkSharp.Generation {
sw.WriteLine (); sw.WriteLine ();
if (!IsConsumeOnly) { if (!IsConsumeOnly) {
sw.WriteLine ("\t\tpublic " + Name + "Adapter ()"); sw.WriteLine ("\t\tpublic " + AdapterName + " ()");
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tInitHandler = new GLib.GInterfaceInitHandler (Initialize);"); sw.WriteLine ("\t\t\tInitHandler = new GLib.GInterfaceInitHandler (Initialize);");
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t\tpublic {0}Adapter ({0}Implementor implementor)", Name); sw.WriteLine ("\t\tpublic {0} ({1} implementor)", AdapterName, ImplementorName);
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tif (implementor == null)"); sw.WriteLine ("\t\t\tif (implementor == null)");
sw.WriteLine ("\t\t\t\tthrow new ArgumentNullException (\"implementor\");"); sw.WriteLine ("\t\t\t\tthrow new ArgumentNullException (\"implementor\");");
@ -151,7 +180,7 @@ namespace GtkSharp.Generation {
sw.WriteLine (); sw.WriteLine ();
} }
sw.WriteLine ("\t\tpublic " + Name + "Adapter (IntPtr handle)"); sw.WriteLine ("\t\tpublic " + AdapterName + " (IntPtr handle)");
sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t{");
sw.WriteLine ("\t\t\tif (!_gtype.IsInstance (handle))"); sw.WriteLine ("\t\t\tif (!_gtype.IsInstance (handle))");
sw.WriteLine ("\t\t\t\tthrow new ArgumentException (\"The gobject doesn't implement the GInterface of this adapter\", \"handle\");"); sw.WriteLine ("\t\t\t\tthrow new ArgumentException (\"The gobject doesn't implement the GInterface of this adapter\", \"handle\");");
@ -205,11 +234,11 @@ namespace GtkSharp.Generation {
sw.WriteLine ("\t\t\tif (obj == null)"); sw.WriteLine ("\t\t\tif (obj == null)");
sw.WriteLine ("\t\t\t\treturn null;"); sw.WriteLine ("\t\t\t\treturn null;");
if (!IsConsumeOnly) { if (!IsConsumeOnly) {
sw.WriteLine ("\t\t\telse if (obj is " + Name + "Implementor)"); sw.WriteLine ("\t\t\telse if (obj is " + ImplementorName + ")");
sw.WriteLine ("\t\t\t\treturn new {0}Adapter (obj as {0}Implementor);", Name); sw.WriteLine ("\t\t\t\treturn new {0} (obj as {1});", AdapterName, ImplementorName);
} }
sw.WriteLine ("\t\t\telse if (obj as " + Name + " == null)"); sw.WriteLine ("\t\t\telse if (obj as " + Name + " == null)");
sw.WriteLine ("\t\t\t\treturn new {0}Adapter (obj.Handle);", Name); sw.WriteLine ("\t\t\t\treturn new {0} (obj.Handle);", AdapterName);
sw.WriteLine ("\t\t\telse"); sw.WriteLine ("\t\t\telse");
sw.WriteLine ("\t\t\t\treturn obj as {0};", Name); sw.WriteLine ("\t\t\t\treturn obj as {0};", Name);
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");
@ -218,9 +247,9 @@ namespace GtkSharp.Generation {
void GenerateImplementorProp (StreamWriter sw) void GenerateImplementorProp (StreamWriter sw)
{ {
sw.WriteLine ("\t\tpublic " + Name + "Implementor Implementor {"); sw.WriteLine ("\t\tpublic " + ImplementorName + " Implementor {");
sw.WriteLine ("\t\t\tget {"); sw.WriteLine ("\t\t\tget {");
sw.WriteLine ("\t\t\t\treturn implementor as {0}Implementor;", Name); sw.WriteLine ("\t\t\t\treturn implementor as {0};", ImplementorName);
sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t\t}");
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");
sw.WriteLine (); sw.WriteLine ();
@ -228,7 +257,7 @@ namespace GtkSharp.Generation {
void GenerateAdapter (GenerationInfo gen_info) void GenerateAdapter (GenerationInfo gen_info)
{ {
StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name + "Adapter"); StreamWriter sw = gen_info.Writer = gen_info.OpenStream (AdapterName);
sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine ("namespace " + NS + " {");
sw.WriteLine (); sw.WriteLine ();
@ -236,7 +265,7 @@ namespace GtkSharp.Generation {
sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;");
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("#region Autogenerated code"); sw.WriteLine ("#region Autogenerated code");
sw.WriteLine ("\tpublic partial class " + Name + "Adapter : GLib.GInterfaceAdapter, " + QualifiedName + " {"); sw.WriteLine ("\tpublic partial class " + AdapterName + " : GLib.GInterfaceAdapter, " + QualifiedName + " {");
sw.WriteLine (); sw.WriteLine ();
if (!IsConsumeOnly) { if (!IsConsumeOnly) {
@ -279,9 +308,9 @@ namespace GtkSharp.Generation {
StreamWriter sw = gen_info.Writer; StreamWriter sw = gen_info.Writer;
sw.WriteLine (); sw.WriteLine ();
sw.WriteLine ("\t[GLib.GInterface (typeof (" + Name + "Adapter))]"); sw.WriteLine ("\t[GLib.GInterface (typeof (" + AdapterName + "))]");
string access = IsInternal ? "internal" : "public"; string access = IsInternal ? "internal" : "public";
sw.WriteLine ("\t" + access + " partial interface " + Name + "Implementor : GLib.IWrapper {"); sw.WriteLine ("\t" + access + " partial interface " + ImplementorName + " : GLib.IWrapper {");
sw.WriteLine (); sw.WriteLine ();
var vm_table = new Dictionary<string, InterfaceVM> (); var vm_table = new Dictionary<string, InterfaceVM> ();
foreach (InterfaceVM vm in interface_vms) { foreach (InterfaceVM vm in interface_vms) {

View File

@ -144,8 +144,11 @@ namespace GtkSharp.Generation {
if (table.IsOpaque (CType) || table.IsBoxed (CType)) { if (table.IsOpaque (CType) || table.IsBoxed (CType)) {
sw.WriteLine(indent + "\t" + CSType + " ret = (" + CSType + ") val;"); sw.WriteLine(indent + "\t" + CSType + " ret = (" + CSType + ") val;");
} else if (table.IsInterface (CType)) { } else if (table.IsInterface (CType)) {
var igen = table.GetInterfaceGen (CType);
// Do we have to dispose the GLib.Object from the GLib.Value? // Do we have to dispose the GLib.Object from the GLib.Value?
sw.WriteLine (indent + "\t{0} ret = {0}Adapter.GetObject ((GLib.Object) val);", CSType); sw.WriteLine (indent + "\t{0} ret = {1}.GetObject ((GLib.Object) val);",
igen.QualifiedName, igen.QualifiedAdapterName);
} else { } else {
sw.Write(indent + "\t" + CSType + " ret = "); sw.Write(indent + "\t" + CSType + " ret = ");
sw.Write ("(" + CSType + ") "); sw.Write ("(" + CSType + ") ");

View File

@ -268,18 +268,22 @@ namespace GtkSharp.Generation {
sw.WriteLine ("\t\tpublic " + parms[i].CSType + " " + parms[i].StudlyName + "{"); sw.WriteLine ("\t\tpublic " + parms[i].CSType + " " + parms[i].StudlyName + "{");
if (parms[i].PassAs != "out") { if (parms[i].PassAs != "out") {
sw.WriteLine ("\t\t\tget {"); sw.WriteLine ("\t\t\tget {");
if (SymbolTable.Table.IsInterface (parms [i].CType)) if (SymbolTable.Table.IsInterface (parms [i].CType)) {
sw.WriteLine ("\t\t\t\treturn {0}Adapter.GetObject (Args [{1}] as GLib.Object);", parms [i].CSType, i); var igen = SymbolTable.Table.GetInterfaceGen (parms [i].CType);
else sw.WriteLine ("\t\t\t\treturn {0}.GetObject (Args [{1}] as GLib.Object);", igen.QualifiedAdapterName, i);
} else {
sw.WriteLine ("\t\t\t\treturn ({0}) Args [{1}];", parms [i].CSType, i); sw.WriteLine ("\t\t\t\treturn ({0}) Args [{1}];", parms [i].CSType, i);
}
sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t\t}");
} }
if (parms[i].PassAs != "") { if (parms[i].PassAs != "") {
sw.WriteLine ("\t\t\tset {"); sw.WriteLine ("\t\t\tset {");
if (SymbolTable.Table.IsInterface (parms [i].CType)) if (SymbolTable.Table.IsInterface (parms [i].CType)) {
sw.WriteLine ("\t\t\t\tArgs [{0}] = value is {1}Adapter ? (value as {1}Adapter).Implementor : value;", i, parms [i].CSType); var igen = SymbolTable.Table.GetInterfaceGen (parms [i].CType);
else sw.WriteLine ("\t\t\t\tArgs [{0}] = value is {1} ? (value as {1}).Implementor : value;", i, igen.AdapterName);
sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms[i].CSType + ")value;"); } else {
sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms [i].CSType + ")value;");
}
sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t\t}");
} }
sw.WriteLine ("\t\t}"); sw.WriteLine ("\t\t}");

View File

@ -311,7 +311,12 @@ namespace GtkSharp.Generation {
{ {
return this[c_type] as ClassBase; return this[c_type] as ClassBase;
} }
public InterfaceGen GetInterfaceGen (string c_type)
{
return this[c_type] as InterfaceGen;
}
public bool IsObject(string c_type) public bool IsObject(string c_type)
{ {
if (this[c_type] is ObjectGen) if (this[c_type] is ObjectGen)