2009-05-20 15:29:36 +00:00
|
|
|
// GtkSharp.Generation.MiniObjectGen.cs - The Mini Object Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@ximian.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2001-2003 Mike Kestner
|
|
|
|
// Copyright (c) 2003-2004 Novell, Inc.
|
|
|
|
// Copyright (c) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the GNU General Public
|
|
|
|
// License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class MiniObjectGen : ObjectBase {
|
|
|
|
|
|
|
|
private ArrayList custom_attrs = new ArrayList();
|
|
|
|
private ArrayList strings = new ArrayList();
|
|
|
|
private static Hashtable dirs = new Hashtable ();
|
|
|
|
|
|
|
|
public MiniObjectGen (XmlElement ns, XmlElement elem) : base (ns, elem, false)
|
|
|
|
{
|
|
|
|
foreach (XmlNode node in elem.ChildNodes) {
|
|
|
|
if (!(node is XmlElement)) continue;
|
|
|
|
XmlElement member = (XmlElement) node;
|
|
|
|
if (member.HasAttribute ("hidden") && member.GetAttribute ("hidden") == "1") continue;
|
|
|
|
|
|
|
|
switch (node.Name) {
|
|
|
|
case "custom-attribute":
|
|
|
|
custom_attrs.Add (member.InnerXml);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "static-string":
|
|
|
|
strings.Add (node);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (!IsNodeNameHandled (node.Name))
|
|
|
|
Console.WriteLine ("Unexpected node " + node.Name + " in " + CName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Validate ()
|
|
|
|
{
|
|
|
|
ArrayList invalids = new ArrayList ();
|
|
|
|
|
|
|
|
return base.Validate ();
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool DisableVoidCtor {
|
|
|
|
get {
|
|
|
|
return Elem.HasAttribute ("disable_void_ctor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool DisableGTypeCtor {
|
|
|
|
get {
|
|
|
|
return Elem.HasAttribute ("disable_gtype_ctor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DirectoryInfo {
|
|
|
|
public string assembly_name;
|
|
|
|
public Hashtable objects;
|
|
|
|
|
|
|
|
public DirectoryInfo (string assembly_name) {
|
|
|
|
this.assembly_name = assembly_name;
|
|
|
|
objects = new Hashtable ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static DirectoryInfo GetDirectoryInfo (string dir, string assembly_name)
|
|
|
|
{
|
|
|
|
DirectoryInfo result;
|
|
|
|
|
|
|
|
if (dirs.ContainsKey (dir)) {
|
|
|
|
result = dirs [dir] as DirectoryInfo;
|
|
|
|
if (result.assembly_name != assembly_name) {
|
|
|
|
Console.WriteLine ("Can't put multiple assemblies in one directory.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = new DirectoryInfo (assembly_name);
|
|
|
|
dirs.Add (dir, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Generate (GenerationInfo gen_info)
|
|
|
|
{
|
|
|
|
gen_info.CurrentType = Name;
|
|
|
|
|
|
|
|
string asm_name = gen_info.AssemblyName.Length == 0 ? NS.ToLower () + "-sharp" : gen_info.AssemblyName;
|
|
|
|
DirectoryInfo di = GetDirectoryInfo (gen_info.Dir, asm_name);
|
|
|
|
|
|
|
|
StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name);
|
|
|
|
|
|
|
|
sw.WriteLine ("namespace " + NS + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ("\tusing System.Collections;");
|
|
|
|
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
|
|
|
sw.WriteLine ("#region Autogenerated code");
|
|
|
|
if (IsDeprecated)
|
|
|
|
sw.WriteLine ("\t[Obsolete]");
|
|
|
|
foreach (string attr in custom_attrs)
|
|
|
|
sw.WriteLine ("\t" + attr);
|
|
|
|
sw.Write ("\t{0} {1}class " + Name, IsInternal ? "internal" : "public", IsAbstract ? "abstract " : "");
|
|
|
|
string cs_parent = table.GetCSType(Elem.GetAttribute("parent"));
|
|
|
|
if (cs_parent != "") {
|
|
|
|
di.objects.Add (CName, QualifiedName);
|
|
|
|
sw.Write (" : " + cs_parent);
|
|
|
|
}
|
|
|
|
foreach (string iface in managed_interfaces) {
|
|
|
|
if (Parent != null && Parent.Implements (iface))
|
|
|
|
continue;
|
|
|
|
sw.Write (", " + iface);
|
|
|
|
}
|
|
|
|
sw.WriteLine (" {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
GenCtors (gen_info);
|
|
|
|
GenFields (gen_info);
|
|
|
|
|
|
|
|
GenClassMembers (gen_info, cs_parent);
|
|
|
|
GenMethods (gen_info, null, null);
|
|
|
|
|
|
|
|
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}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cs_parent != String.Empty && GetExpected (CName) != QualifiedName) {
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tstatic " + Name + " ()");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tGtkSharp." + Studlify (asm_name) + ".ObjectManager.Initialize ();");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("#endregion");
|
|
|
|
AppendCustom (sw, gen_info.CustomDir);
|
|
|
|
|
|
|
|
sw.WriteLine ("\t}");
|
|
|
|
sw.WriteLine ("}");
|
|
|
|
|
|
|
|
sw.Close ();
|
|
|
|
gen_info.Writer = null;
|
|
|
|
Statistics.ObjectCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void GenCtors (GenerationInfo gen_info)
|
|
|
|
{
|
|
|
|
if (!Elem.HasAttribute("parent"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!DisableGTypeCtor) {
|
|
|
|
gen_info.Writer.WriteLine("\t\t[Obsolete]");
|
|
|
|
gen_info.Writer.WriteLine("\t\tprotected " + Name + "(GLib.GType gtype) : base(gtype) {}");
|
|
|
|
}
|
|
|
|
gen_info.Writer.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}");
|
|
|
|
if (ctors.Count == 0 && !DisableVoidCtor) {
|
|
|
|
gen_info.Writer.WriteLine();
|
|
|
|
gen_info.Writer.WriteLine("\t\tprotected " + Name + "() : base(IntPtr.Zero)");
|
|
|
|
gen_info.Writer.WriteLine("\t\t{");
|
|
|
|
gen_info.Writer.WriteLine("\t\t\tCreateNativeObject ();");
|
|
|
|
gen_info.Writer.WriteLine("\t\t}");
|
|
|
|
}
|
|
|
|
gen_info.Writer.WriteLine();
|
|
|
|
|
|
|
|
base.GenCtors (gen_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenClassMembers (GenerationInfo gen_info, string cs_parent)
|
|
|
|
{
|
|
|
|
GenVirtualMethods (gen_info, null);
|
|
|
|
|
|
|
|
if (class_struct_name == null || !CanGenerateClassStruct) return;
|
|
|
|
StreamWriter sw = gen_info.Writer;
|
|
|
|
GenerateClassStruct (gen_info);
|
|
|
|
if (cs_parent == "")
|
|
|
|
sw.WriteLine ("\t\tstatic uint class_offset = 0;");
|
|
|
|
else
|
|
|
|
sw.WriteLine ("\t\tstatic uint class_offset = ((GLib.GType) typeof ({0})).ClassSize;", cs_parent);
|
|
|
|
sw.WriteLine ("\t\tstatic Hashtable class_structs;");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tstatic {0} GetClassStruct (GLib.GType gtype, bool use_cache)", class_struct_name);
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tif (class_structs == null)");
|
|
|
|
sw.WriteLine ("\t\t\t\tclass_structs = new Hashtable ();");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\t\tif (use_cache && class_structs.Contains (gtype))");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn ({0}) class_structs [gtype];", class_struct_name);
|
|
|
|
sw.WriteLine ("\t\t\telse {");
|
|
|
|
sw.WriteLine ("\t\t\t\tIntPtr class_ptr = new IntPtr (gtype.ClassPtr.ToInt64 () + class_offset);");
|
|
|
|
sw.WriteLine ("\t\t\t\t{0} class_struct = ({0}) Marshal.PtrToStructure (class_ptr, typeof ({0}));", class_struct_name);
|
|
|
|
sw.WriteLine ("\t\t\t\tif (use_cache)");
|
|
|
|
sw.WriteLine ("\t\t\t\t\tclass_structs.Add (gtype, class_struct);");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn class_struct;");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tstatic void OverrideClassStruct (GLib.GType gtype, {0} class_struct)", class_struct_name);
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tIntPtr class_ptr = new IntPtr (gtype.ClassPtr.ToInt64 () + class_offset);");
|
|
|
|
sw.WriteLine ("\t\t\tMarshal.StructureToPtr (class_struct, class_ptr, false);");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep this in sync with the one in glib/GType.cs */
|
|
|
|
private static string GetExpected (string cname)
|
|
|
|
{
|
|
|
|
for (int i = 1; i < cname.Length; i++) {
|
|
|
|
if (Char.IsUpper (cname[i])) {
|
|
|
|
if (i == 1 && cname[0] == 'G')
|
|
|
|
return "GLib." + cname.Substring (1);
|
|
|
|
else
|
|
|
|
return cname.Substring (0, i) + "." + cname.Substring (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException ("cname doesn't follow the NamespaceType capitalization style: " + cname);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool NeedsMap (Hashtable objs, string assembly_name)
|
|
|
|
{
|
|
|
|
foreach (string key in objs.Keys)
|
|
|
|
if (GetExpected (key) != ((string) objs[key]))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string Studlify (string name)
|
|
|
|
{
|
|
|
|
string result = "";
|
|
|
|
|
|
|
|
string[] subs = name.Split ('-');
|
|
|
|
foreach (string sub in subs)
|
|
|
|
result += Char.ToUpper (sub[0]) + sub.Substring (1);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void GenerateMappers ()
|
|
|
|
{
|
|
|
|
foreach (string dir in dirs.Keys) {
|
|
|
|
|
|
|
|
DirectoryInfo di = dirs[dir] as DirectoryInfo;
|
|
|
|
|
|
|
|
if (!NeedsMap (di.objects, di.assembly_name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
GenerationInfo gen_info = new GenerationInfo (dir, di.assembly_name);
|
|
|
|
|
|
|
|
GenerateMapper (di, gen_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void GenerateMapper (DirectoryInfo dir_info, GenerationInfo gen_info)
|
|
|
|
{
|
|
|
|
StreamWriter sw = gen_info.OpenStream ("ObjectManager");
|
|
|
|
|
|
|
|
sw.WriteLine ("namespace GtkSharp." + Studlify (dir_info.assembly_name) + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tpublic class ObjectManager {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tstatic bool initialized = false;");
|
|
|
|
sw.WriteLine ("\t\t// Call this method from the appropriate module init function.");
|
|
|
|
sw.WriteLine ("\t\tpublic static void Initialize ()");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tif (initialized)");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
sw.WriteLine ("");
|
|
|
|
sw.WriteLine ("\t\t\tinitialized = true;");
|
|
|
|
|
|
|
|
foreach (string key in dir_info.objects.Keys) {
|
|
|
|
if (GetExpected(key) != ((string) dir_info.objects[key]))
|
|
|
|
sw.WriteLine ("\t\t\tGLib.GType.Register ({0}.GType, typeof ({0}));", dir_info.objects [key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ("\t}");
|
|
|
|
sw.WriteLine ("}");
|
|
|
|
sw.Close ();
|
|
|
|
}
|
|
|
|
|
2009-06-12 14:37:08 +00:00
|
|
|
public string CallByName (string name, bool owned)
|
|
|
|
{
|
|
|
|
return name + " == null ? IntPtr.Zero : " + name + (owned ? ".OwnedHandle" : ".Handle");
|
|
|
|
}
|
|
|
|
|
2009-05-20 15:29:36 +00:00
|
|
|
public override string FromNative (string var, bool owned)
|
|
|
|
{
|
|
|
|
return "Gst.MiniObject.GetObject(" + var + (owned ? ", true" : "") + ") as " + QualifiedName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|