From 6cb03440c1a3fb52c23d5d681e44f772d687333c Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Mon, 1 Apr 2013 23:48:22 +0100 Subject: [PATCH] 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. --- generator/GenBase.cs | 2 +- generator/InterfaceGen.cs | 63 ++++++++++++++++++++++++++++----------- generator/Property.cs | 5 +++- generator/Signal.cs | 18 ++++++----- generator/SymbolTable.cs | 7 ++++- 5 files changed, 68 insertions(+), 27 deletions(-) diff --git a/generator/GenBase.cs b/generator/GenBase.cs index 73f3a4314..d7fce7f10 100644 --- a/generator/GenBase.cs +++ b/generator/GenBase.cs @@ -70,7 +70,7 @@ namespace GtkSharp.Generation { public abstract string MarshalType { get; } - public string Name { + public virtual string Name { get { return elem.GetAttribute ("name"); } diff --git a/generator/InterfaceGen.cs b/generator/InterfaceGen.cs index 738da5408..6d1185223 100644 --- a/generator/InterfaceGen.cs +++ b/generator/InterfaceGen.cs @@ -1,9 +1,12 @@ // GtkSharp.Generation.InterfaceGen.cs - The Interface Generatable. // -// Author: Mike Kestner +// Authors: +// Mike Kestner +// Andres G. Aragoneses // // Copyright (c) 2001-2003 Mike Kestner // Copyright (c) 2004, 2007 Novell, Inc. +// Copyright (c) 2013 Andres G. Aragoneses // // This program is free software; you can redistribute it and/or // 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) { - 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) { - return QualifiedName + "Adapter.GetObject (" + var + ", " + (owned ? "true" : "false") + ")"; + return QualifiedAdapterName + ".GetObject (" + var + ", " + (owned ? "true" : "false") + ")"; } public override bool ValidateForSubclass () @@ -90,9 +119,9 @@ namespace GtkSharp.Generation { { sw.WriteLine ("\t\tstatic {0} iface;", class_struct_name); sw.WriteLine (); - sw.WriteLine ("\t\tstatic " + Name + "Adapter ()"); + sw.WriteLine ("\t\tstatic " + AdapterName + " ()"); 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) { if (vm.Validate (new LogWriter (QualifiedName))) sw.WriteLine ("\t\t\tiface.{0} = new {0}NativeDelegate ({0}_cb);", vm.Name); @@ -135,12 +164,12 @@ namespace GtkSharp.Generation { sw.WriteLine (); if (!IsConsumeOnly) { - sw.WriteLine ("\t\tpublic " + Name + "Adapter ()"); + sw.WriteLine ("\t\tpublic " + AdapterName + " ()"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tInitHandler = new GLib.GInterfaceInitHandler (Initialize);"); sw.WriteLine ("\t\t}"); 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\tif (implementor == null)"); sw.WriteLine ("\t\t\t\tthrow new ArgumentNullException (\"implementor\");"); @@ -151,7 +180,7 @@ namespace GtkSharp.Generation { 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\tif (!_gtype.IsInstance (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\t\treturn null;"); if (!IsConsumeOnly) { - sw.WriteLine ("\t\t\telse if (obj is " + Name + "Implementor)"); - sw.WriteLine ("\t\t\t\treturn new {0}Adapter (obj as {0}Implementor);", Name); + sw.WriteLine ("\t\t\telse if (obj is " + ImplementorName + ")"); + 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\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\t\treturn obj as {0};", Name); sw.WriteLine ("\t\t}"); @@ -218,9 +247,9 @@ namespace GtkSharp.Generation { 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\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}"); sw.WriteLine (); @@ -228,7 +257,7 @@ namespace GtkSharp.Generation { 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 (); @@ -236,7 +265,7 @@ namespace GtkSharp.Generation { sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); 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 (); if (!IsConsumeOnly) { @@ -279,9 +308,9 @@ namespace GtkSharp.Generation { StreamWriter sw = gen_info.Writer; sw.WriteLine (); - sw.WriteLine ("\t[GLib.GInterface (typeof (" + Name + "Adapter))]"); + sw.WriteLine ("\t[GLib.GInterface (typeof (" + AdapterName + "))]"); 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 (); var vm_table = new Dictionary (); foreach (InterfaceVM vm in interface_vms) { diff --git a/generator/Property.cs b/generator/Property.cs index 4555b1d40..7e0e09549 100644 --- a/generator/Property.cs +++ b/generator/Property.cs @@ -144,8 +144,11 @@ namespace GtkSharp.Generation { if (table.IsOpaque (CType) || table.IsBoxed (CType)) { sw.WriteLine(indent + "\t" + CSType + " ret = (" + CSType + ") val;"); } else if (table.IsInterface (CType)) { + var igen = table.GetInterfaceGen (CType); + // 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 { sw.Write(indent + "\t" + CSType + " ret = "); sw.Write ("(" + CSType + ") "); diff --git a/generator/Signal.cs b/generator/Signal.cs index 5107f29e8..9c5f5d528 100644 --- a/generator/Signal.cs +++ b/generator/Signal.cs @@ -268,18 +268,22 @@ namespace GtkSharp.Generation { sw.WriteLine ("\t\tpublic " + parms[i].CSType + " " + parms[i].StudlyName + "{"); if (parms[i].PassAs != "out") { sw.WriteLine ("\t\t\tget {"); - 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); - else + if (SymbolTable.Table.IsInterface (parms [i].CType)) { + var igen = SymbolTable.Table.GetInterfaceGen (parms [i].CType); + 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}"); } if (parms[i].PassAs != "") { sw.WriteLine ("\t\t\tset {"); - 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); - else - sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms[i].CSType + ")value;"); + if (SymbolTable.Table.IsInterface (parms [i].CType)) { + var igen = SymbolTable.Table.GetInterfaceGen (parms [i].CType); + sw.WriteLine ("\t\t\t\tArgs [{0}] = value is {1} ? (value as {1}).Implementor : value;", i, igen.AdapterName); + } else { + sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms [i].CSType + ")value;"); + } sw.WriteLine ("\t\t\t}"); } sw.WriteLine ("\t\t}"); diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs index dca21ebec..7effc54a0 100644 --- a/generator/SymbolTable.cs +++ b/generator/SymbolTable.cs @@ -311,7 +311,12 @@ namespace GtkSharp.Generation { { return this[c_type] as ClassBase; } - + + public InterfaceGen GetInterfaceGen (string c_type) + { + return this[c_type] as InterfaceGen; + } + public bool IsObject(string c_type) { if (this[c_type] is ObjectGen)