2002-05-24 01:43:25 +02:00
|
|
|
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
|
|
|
|
//
|
2005-01-24 19:25:02 +01:00
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
2002-05-24 01:43:25 +02:00
|
|
|
//
|
2004-06-25 18:35:15 +02:00
|
|
|
// Copyright (c) 2001-2003 Mike Kestner
|
2005-01-24 19:25:02 +01:00
|
|
|
// Copyright (c) 2004-2005 Novell, Inc.
|
2004-06-25 18:35:15 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2002-05-24 01:43:25 +02:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
public class Ctor : MethodBase {
|
2002-05-24 01:43:25 +02:00
|
|
|
|
2002-06-25 00:04:10 +02:00
|
|
|
private bool preferred;
|
2005-01-24 19:25:02 +01:00
|
|
|
private string name;
|
2004-05-07 15:42:59 +02:00
|
|
|
private bool needs_chaining = false;
|
2002-05-24 01:43:25 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
public Ctor (XmlElement elem, ClassBase implementor) : base (elem, implementor)
|
|
|
|
{
|
|
|
|
if (elem.HasAttribute ("preferred"))
|
|
|
|
preferred = true;
|
|
|
|
if (implementor is ObjectGen)
|
|
|
|
needs_chaining = true;
|
|
|
|
name = implementor.Name;
|
|
|
|
}
|
|
|
|
|
2002-06-25 00:04:10 +02:00
|
|
|
public bool Preferred {
|
|
|
|
get { return preferred; }
|
|
|
|
set { preferred = value; }
|
|
|
|
}
|
2002-07-31 01:02:12 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
public string StaticName {
|
|
|
|
get {
|
|
|
|
if (!IsStatic)
|
|
|
|
return String.Empty;
|
2003-04-06 11:21:15 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
string[] toks = CName.Substring(CName.IndexOf("new")).Split ('_');
|
|
|
|
string result = String.Empty;
|
2002-05-24 01:43:25 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
foreach (string tok in toks)
|
|
|
|
result += tok.Substring(0,1).ToUpper() + tok.Substring(1);
|
|
|
|
return result;
|
2002-05-24 01:43:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-31 20:29:13 +02:00
|
|
|
void GenerateImport (StreamWriter sw)
|
2002-06-25 00:04:10 +02:00
|
|
|
{
|
2005-01-24 19:25:02 +01:00
|
|
|
sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
|
2007-08-01 19:30:47 +02:00
|
|
|
sw.WriteLine("\t\tstatic extern " + Safety + "IntPtr " + CName + "(" + Parameters.ImportSignature + ");");
|
2005-01-24 19:25:02 +01:00
|
|
|
sw.WriteLine();
|
2002-06-25 00:04:10 +02:00
|
|
|
}
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 22:31:23 +02:00
|
|
|
|
2007-07-31 20:29:13 +02:00
|
|
|
void GenerateStatic (GenerationInfo gen_info)
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 22:31:23 +02:00
|
|
|
{
|
2005-01-24 19:25:02 +01:00
|
|
|
StreamWriter sw = gen_info.Writer;
|
2008-12-02 16:03:02 +01:00
|
|
|
sw.WriteLine("\t\t" + Protection + " static " + Safety + Modifiers + name + " " + StaticName + "(" + Signature + ")");
|
2005-01-24 19:25:02 +01:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
|
|
|
|
Body.Initialize(gen_info, false, false, "");
|
|
|
|
|
2005-08-04 16:41:11 +02:00
|
|
|
sw.Write("\t\t\t" + name + " result = ");
|
2005-01-24 19:25:02 +01:00
|
|
|
if (container_type is StructBase)
|
|
|
|
sw.Write ("{0}.New (", name);
|
|
|
|
else
|
|
|
|
sw.Write ("new {0} (", name);
|
|
|
|
sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));");
|
2005-08-04 16:41:11 +02:00
|
|
|
Body.Finish (sw, "");
|
|
|
|
Body.HandleException (sw, "");
|
|
|
|
sw.WriteLine ("\t\t\treturn result;");
|
2002-07-13 Rachel Hestilow <hestilow@ximian.com>
* parser/Gnome.metadata, Gtk.metadata: More conflict
fixes.
* parser/build.pl: Fully qualify all lib names. (Gtk+ packages
are now LFS-compliant in Debian...)
* parser/gapi2xml.pl: Fix for whitespace in fields, defines,
and docs.
* generator/BoxedGen.cs: Remove extraneous CallByName definition,
add "override" keyword to FromNative.
(Generate): Generate methods after fields.
* generator/ClassBase.cs: Change CallByName, FromNative to virtual.
(.ctor): Ignore "hidden" nodes. Set container on signal.
(GenSignals, GenMethods): Add "implementor" argument for interface
use.
(Get(Method|Signal|Property)Recursively): Rework to correctly
recurse interfaces.
(Implements): Added.
* generator/Ctor.cs (Initialize): Move clash initialization completely
out of Generate, so we can check for collisions.
* generator/Method.cs (GenerateDeclCommon): Check for duplicates,
for "new" keyword.
(Generate): Add "implementor" argument.
* generator/ObjectGen.cs (Generate): Initialize ctor clashes on
this and all parents, before generating.
(Ctors, InitializeCtors): Added.
* generator/Signal.cs: Store the container_type, check for
collisions.
* generator/StructGen.cs: Add "override" keyword to overriden methods.
* gtk/FileSelection.custom (ActionArea): Add "new" keyword.
svn path=/trunk/gtk-sharp/; revision=5782
2002-07-13 22:31:23 +02:00
|
|
|
}
|
2005-01-24 19:25:02 +01:00
|
|
|
|
2003-10-05 02:20:17 +02:00
|
|
|
public void Generate (GenerationInfo gen_info)
|
2002-05-24 01:43:25 +02:00
|
|
|
{
|
2007-07-31 20:29:13 +02:00
|
|
|
if (!Validate ())
|
|
|
|
return;
|
|
|
|
|
2003-10-05 02:20:17 +02:00
|
|
|
StreamWriter sw = gen_info.Writer;
|
2005-04-04 18:27:08 +02:00
|
|
|
gen_info.CurrentMember = CName;
|
2002-05-24 01:43:25 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
GenerateImport (sw);
|
2002-05-24 01:43:25 +02:00
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
if (IsStatic)
|
|
|
|
GenerateStatic (gen_info);
|
|
|
|
else {
|
2008-12-02 16:03:02 +01:00
|
|
|
sw.WriteLine("\t\t{0} {1}{2} ({3}) {4}", Protection, Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : "");
|
2002-05-24 01:43:25 +02:00
|
|
|
sw.WriteLine("\t\t{");
|
2002-07-31 01:02:12 +02:00
|
|
|
|
2004-05-07 15:42:59 +02:00
|
|
|
if (needs_chaining) {
|
|
|
|
sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
|
|
|
|
|
2005-01-26 20:17:07 +01:00
|
|
|
if (Parameters.Count == 0) {
|
2004-05-07 15:42:59 +02:00
|
|
|
sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
} else {
|
|
|
|
ArrayList names = new ArrayList ();
|
|
|
|
ArrayList values = new ArrayList ();
|
2005-01-24 19:25:02 +01:00
|
|
|
for (int i = 0; i < Parameters.Count; i++) {
|
|
|
|
Parameter p = Parameters[i];
|
2004-05-07 15:42:59 +02:00
|
|
|
if (container_type.GetPropertyRecursively (p.StudlyName) != null) {
|
|
|
|
names.Add (p.Name);
|
|
|
|
values.Add (p.Name);
|
|
|
|
} else if (p.PropertyName != String.Empty) {
|
|
|
|
names.Add (p.PropertyName);
|
|
|
|
values.Add (p.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
if (names.Count == Parameters.Count) {
|
2004-05-07 15:42:59 +02:00
|
|
|
sw.WriteLine ("\t\t\t\tArrayList vals = new ArrayList();");
|
|
|
|
sw.WriteLine ("\t\t\t\tArrayList names = new ArrayList();");
|
|
|
|
for (int i = 0; i < names.Count; i++) {
|
2005-01-24 19:25:02 +01:00
|
|
|
Parameter p = Parameters [i];
|
2004-05-07 15:42:59 +02:00
|
|
|
string indent = "\t\t\t\t";
|
2005-03-03 23:40:32 +01:00
|
|
|
if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) {
|
2004-05-07 15:42:59 +02:00
|
|
|
sw.WriteLine (indent + "if (" + p.Name + " != null) {");
|
|
|
|
indent += "\t";
|
|
|
|
}
|
|
|
|
sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");");
|
2005-07-22 20:36:50 +02:00
|
|
|
sw.WriteLine (indent + "vals.Add (new GLib.Value (" + values[i] + "));");
|
2004-05-07 15:42:59 +02:00
|
|
|
|
2005-03-03 23:40:32 +01:00
|
|
|
if (p.Generatable is ClassBase && !(p.Generatable is StructBase))
|
2004-05-07 15:42:59 +02:00
|
|
|
sw.WriteLine ("\t\t\t\t}");
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
} else
|
|
|
|
sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");");
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
}
|
|
|
|
|
2005-01-24 19:25:02 +01:00
|
|
|
Body.Initialize(gen_info, false, false, "");
|
|
|
|
sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString (false));
|
2005-03-08 22:28:08 +01:00
|
|
|
Body.Finish (sw, "");
|
2005-01-24 19:25:02 +01:00
|
|
|
Body.HandleException (sw, "");
|
2002-05-24 01:43:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
|
|
|
|
Statistics.CtorCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|