1
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
3
// Author: Mike Kestner <mkestner@novell.com>
5
// Copyright (c) 2001-2003 Mike Kestner
6
// Copyright (c) 2004-2005 Novell, Inc.
8
// This program is free software; you can redistribute it and/or
9
// modify it under the terms of version 2 of the GNU General Public
10
// License as published by the Free Software Foundation.
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// General Public License for more details.
17
// You should have received a copy of the GNU General Public
18
// License along with this program; if not, write to the
19
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
// Boston, MA 02111-1307, USA.
23
namespace GtkSharp.Generation {
26
using System.Collections;
30
public class Ctor : MethodBase {
32
private bool preferred;
34
private bool needs_chaining = false;
36
public Ctor (XmlElement elem, ClassBase implementor) : base (elem, implementor)
38
if (elem.HasAttribute ("preferred"))
40
if (implementor is ObjectGen)
41
needs_chaining = true;
42
name = implementor.Name;
45
public bool Preferred {
46
get { return preferred; }
47
set { preferred = value; }
50
public string StaticName {
55
string[] toks = CName.Substring(CName.IndexOf("new")).Split ('_');
56
string result = String.Empty;
58
foreach (string tok in toks)
59
result += tok.Substring(0,1).ToUpper() + tok.Substring(1);
64
void GenerateImport (StreamWriter sw)
66
sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
67
sw.WriteLine("\t\tstatic extern " + Safety + "IntPtr " + CName + "(" + Parameters.ImportSignature + ");");
71
void GenerateStatic (GenerationInfo gen_info)
73
StreamWriter sw = gen_info.Writer;
74
sw.WriteLine("\t\t" + Protection + " static " + Safety + Modifiers + name + " " + StaticName + "(" + Signature + ")");
75
sw.WriteLine("\t\t{");
77
Body.Initialize(gen_info, false, false, "");
79
sw.Write("\t\t\t" + name + " result = ");
80
if (container_type is StructBase)
81
sw.Write ("{0}.New (", name);
83
sw.Write ("new {0} (", name);
84
sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));");
86
Body.HandleException (sw, "");
87
sw.WriteLine ("\t\t\treturn result;");
90
public void Generate (GenerationInfo gen_info)
95
StreamWriter sw = gen_info.Writer;
96
gen_info.CurrentMember = CName;
101
GenerateStatic (gen_info);
103
sw.WriteLine("\t\t{0} {1}{2} ({3}) {4}", Protection, Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : "");
104
sw.WriteLine("\t\t{");
106
if (needs_chaining) {
107
sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
109
if (Parameters.Count == 0) {
110
sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);");
111
sw.WriteLine ("\t\t\t\treturn;");
113
ArrayList names = new ArrayList ();
114
ArrayList values = new ArrayList ();
115
for (int i = 0; i < Parameters.Count; i++) {
116
Parameter p = Parameters[i];
117
if (container_type.GetPropertyRecursively (p.StudlyName) != null) {
120
} else if (p.PropertyName != String.Empty) {
121
names.Add (p.PropertyName);
126
if (names.Count == Parameters.Count) {
127
sw.WriteLine ("\t\t\t\tArrayList vals = new ArrayList();");
128
sw.WriteLine ("\t\t\t\tArrayList names = new ArrayList();");
129
for (int i = 0; i < names.Count; i++) {
130
Parameter p = Parameters [i];
131
string indent = "\t\t\t\t";
132
if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) {
133
sw.WriteLine (indent + "if (" + p.Name + " != null) {");
136
sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");");
137
sw.WriteLine (indent + "vals.Add (new GLib.Value (" + values[i] + "));");
139
if (p.Generatable is ClassBase && !(p.Generatable is StructBase))
140
sw.WriteLine ("\t\t\t\t}");
143
sw.WriteLine ("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));");
144
sw.WriteLine ("\t\t\t\treturn;");
146
sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");");
149
sw.WriteLine ("\t\t\t}");
152
Body.Initialize(gen_info, false, false, "");
153
sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString (false));
154
Body.Finish (sw, "");
155
Body.HandleException (sw, "");
158
sw.WriteLine("\t\t}");
161
Statistics.CtorCount++;