GtkSharp/generator/ObjectGen.cs
Mike Kestner b6114fef1e 2003-05-18 Mike Kestner <mkestner@speakeasy.net>
* generator/CallbackGen.cs : use non-static symtab, kill doc comments
	* generator/ClassBase.cs : use non-static symtab
	* generator/CodeGenerator.cs : use non-static symtab
	* generator/EnumGen.cs : kill doc comments, don't gen using System here
	* generator/GenBase.cs : gen using System here for all types
	* generator/InterfaceGen.cs : don't gen using System here.
	* generator/Method.cs : use non-static symtab
	* generator/ObjectGen.cs : kill doc comments, use non-static symtab
	* generator/OpaqueGen.cs : don't gen using System here.
	* generator/Parameters.cs : use non static symtab.
	* generator/Parser.cs : use non static symtab. add SimpleGen's and
	ManualGen's
	* generator/Property.cs : use non static symtab
	* generator/SignalHandler.cs : use non static symtab
	* generator/StructBase.cs : use non static symtab
	* generator/SymbolTable.cs : major refactoring. now uses SimpleGen and
	ManualGen IGeneratables to simplify the method and prop code.  Is now
	instance based with a static prop to get the singleton instance, so that
	a this indexer can be provided to access the IGeneratables nicely. Gearing
	up to remove even more code from here by accessing IGeneratables directly.

svn path=/trunk/gtk-sharp/; revision=14687
2003-05-19 02:45:17 +00:00

234 lines
6.2 KiB
C#

// GtkSharp.Generation.ObjectGen.cs - The Object Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001-2002 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
public class ObjectGen : ClassBase, IGeneratable {
private ArrayList strings = new ArrayList();
private static Hashtable namespaces = new Hashtable ();
public ObjectGen (XmlElement ns, XmlElement elem) : base (ns, elem)
{
Hashtable objs;
if (namespaces.ContainsKey (NS))
objs = (Hashtable) namespaces[NS];
else {
objs = new Hashtable();
namespaces.Add (NS, objs);
}
objs.Add (CName, QualifiedName + "," + NS.ToLower() + "-sharp");
foreach (XmlNode node in elem.ChildNodes) {
if (!(node is XmlElement)) continue;
XmlElement member = (XmlElement) node;
switch (node.Name) {
case "field":
case "callback":
Statistics.IgnoreCount++;
break;
case "static-string":
strings.Add (node);
break;
default:
if (!IsNodeNameHandled (node.Name))
Console.WriteLine ("Unexpected node " + node.Name + " in " + CName);
break;
}
}
}
public void Generate ()
{
if (!DoGenerate)
return;
StreamWriter sw = CreateWriter ();
sw.WriteLine ("\tusing System.Collections;");
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
sw.WriteLine ();
SymbolTable table = SymbolTable.Table;
sw.WriteLine ("#region Autogenerated code");
sw.Write ("\tpublic class " + Name);
string cs_parent = table.GetCSType(Elem.GetAttribute("parent"));
if (cs_parent != "")
sw.Write (" : " + cs_parent);
if (interfaces != null) {
foreach (string iface in interfaces) {
if (Parent != null && Parent.Implements (iface))
continue;
sw.Write (", " + table.GetCSType (iface));
}
}
sw.WriteLine (" {");
sw.WriteLine ();
GenCtors (sw);
GenProperties (sw);
bool has_sigs = (sigs != null);
if (!has_sigs) {
foreach (string iface in interfaces) {
ClassBase igen = table.GetClassGen (iface);
if (igen.Signals != null) {
has_sigs = true;
break;
}
}
}
if (has_sigs && Elem.HasAttribute("parent"))
{
sw.WriteLine("\t\tprivate Hashtable Signals = new Hashtable();");
GenSignals (sw, null);
}
GenMethods (sw, null, null, true);
if (interfaces != null) {
Hashtable all_methods = new Hashtable ();
Hashtable collisions = new Hashtable ();
foreach (string iface in interfaces) {
ClassBase igen = table.GetClassGen (iface);
foreach (Method m in igen.Methods.Values) {
if (all_methods.Contains (m.Name))
collisions[m.Name] = true;
else
all_methods[m.Name] = true;
}
}
foreach (string iface in interfaces) {
if (Parent != null && Parent.Implements (iface))
continue;
ClassBase igen = table.GetClassGen (iface);
igen.GenMethods (sw, collisions, this, false);
igen.GenSignals (sw, this);
}
}
foreach (XmlElement str in strings) {
sw.Write ("\t\tpublic static string " + str.GetAttribute ("name"));
sw.WriteLine (" {\n\t\t\t get { return \"" + str.GetAttribute ("value") + "\"; }\n\t\t}");
}
sw.WriteLine ("#endregion");
AppendCustom(sw);
sw.WriteLine ("\t}");
CloseWriter (sw);
Statistics.ObjectCount++;
}
protected override void GenCtors (StreamWriter sw)
{
if (!Elem.HasAttribute("parent"))
return;
sw.WriteLine("\t\t~" + Name + "()");
sw.WriteLine("\t\t{");
sw.WriteLine("\t\t\tDispose();");
sw.WriteLine("\t\t}");
sw.WriteLine();
sw.WriteLine("\t\tprotected " + Name + "(GLib.Type gtype) : base(gtype) {}");
sw.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}");
sw.WriteLine();
base.GenCtors (sw);
}
/* Keep this in sync with the one in glib/ObjectManager.cs */
static string GetExpected (string cname)
{
StringBuilder expected = new StringBuilder ();
string ns = "";
bool needs_dot = true;
for (int i = 0; i < cname.Length; i++)
{
if (needs_dot && i > 0 && Char.IsUpper (cname[i])) {
ns = expected.ToString ().ToLower ();
expected.Append ('.');
needs_dot = false;
}
expected.Append (cname[i]);
}
expected.AppendFormat (",{0}-sharp", ns);
return expected.ToString ();
}
public static void GenerateMapper ()
{
foreach (string ns in namespaces.Keys) {
Hashtable objs = (Hashtable) namespaces[ns];
bool needs_map = false;
foreach (string key in objs.Keys) {
string expected = GetExpected (key);
if (expected != ((string) objs[key])) {
needs_map = true;
}
}
if (!needs_map)
continue;
char sep = Path.DirectorySeparatorChar;
string dir = ".." + sep + ns.ToLower () + sep + "generated";
if (!Directory.Exists(dir)) {
Console.WriteLine ("creating " + dir);
Directory.CreateDirectory(dir);
}
String filename = dir + sep + "ObjectManager.cs";
FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter (stream);
sw.WriteLine ("// Generated File. Do not modify.");
sw.WriteLine ("// <c> 2001-2002 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace GtkSharp {");
sw.WriteLine ();
sw.WriteLine ("\tnamespace " + ns + " {");
sw.WriteLine ();
sw.WriteLine ("\tpublic class ObjectManager {");
sw.WriteLine ();
sw.WriteLine ("\t\t// Call this method from the appropriate module init function.");
sw.WriteLine ("\t\tpublic static void Initialize ()");
sw.WriteLine ("\t\t{");
foreach (string key in objs.Keys) {
string expected = GetExpected (key);
if (expected != ((string) objs[key])) {
sw.WriteLine ("\t\t\tGtkSharp.ObjectManager.RegisterType(\"" + key + "\", \"" + objs[key] + "\");");
}
}
sw.WriteLine ("\t\t}");
sw.WriteLine ("\t}");
sw.WriteLine ("}");
sw.WriteLine ("}");
sw.Flush ();
sw.Close ();
}
}
}
}