File: Ctor.cs

package info (click to toggle)
gtk-sharp2 2.12.40-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,632 kB
  • sloc: xml: 351,292; cs: 26,444; sh: 4,228; ansic: 2,915; makefile: 1,288; perl: 1,179
file content (189 lines) | stat: -rw-r--r-- 6,166 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
//
// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2004-2005 Novell, Inc.
//
// 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.Linq;
	using System.Xml;

	public class Ctor : MethodBase  {

		private bool preferred;
		private string name;
		private bool needs_chaining = false;

		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;
		}

		public bool Preferred {
			get { return preferred; }
			set { preferred = value; }
		}

		public string StaticName {
			get {
				if (!IsStatic)
					return String.Empty;

				string[] toks = CName.Substring(CName.IndexOf("new")).Split ('_');
				string result = String.Empty;

				foreach (string tok in toks)
					result += tok.Substring(0,1).ToUpper() + tok.Substring(1);
				return result;
			}
		}

		void GenerateImport (StreamWriter sw)
		{
			sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\", CallingConvention = CallingConvention.Cdecl)]");
			sw.WriteLine("\t\tstatic extern " + Safety + "IntPtr " + CName + "(" + Parameters.ImportSignature + ");");
			sw.WriteLine();
		}

		void GenerateStatic (GenerationInfo gen_info)
		{
			StreamWriter sw = gen_info.Writer;
			sw.WriteLine("\t\t" + Protection + " static " + Safety + Modifiers +  name + " " + StaticName + "(" + Signature + ")");
			sw.WriteLine("\t\t{");

			Body.Initialize(gen_info, false, false, ""); 

			sw.Write("\t\t\t" + name + " result = ");
			if (container_type is StructBase)
				sw.Write ("{0}.New (", name);
			else
				sw.Write ("new {0} (", name);
			sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));");
			Body.Finish (sw, ""); 
			Body.HandleException (sw, ""); 
			sw.WriteLine ("\t\t\treturn result;");
		}

		static bool IsNullable (IGeneratable gen)
		{
			return gen is ClassBase && !(gen is StructBase);
		}

		public void Generate (GenerationInfo gen_info)
		{
			if (!Validate ())
				return;

			StreamWriter sw = gen_info.Writer;
			gen_info.CurrentMember = CName;

			GenerateImport (sw);
			
			if (IsStatic)
				GenerateStatic (gen_info);
			else {
				sw.WriteLine("\t\t{0} {1}{2} ({3}) {4}", Protection, Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : "");
				sw.WriteLine("\t\t{");

				if (needs_chaining) {
					sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
					
					if (Parameters.Count == 0) {
						sw.WriteLine ("\t\t\t\tCreateNativeObject (new IntPtr [0], new GLib.Value[0], 0);");
						sw.WriteLine ("\t\t\t\treturn;");
					} else {
						ArrayList names = new ArrayList ();
						ArrayList values = new ArrayList ();
						for (int i = 0; i < Parameters.Count; i++) {
							Parameter p = Parameters[i];
							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);
							}
						}

						if (names.Count == Parameters.Count) {
							bool genFixedDimension = true;
							foreach (Parameter par in Parameters) {
								if (IsNullable (par.Generatable)) {
									genFixedDimension = false;
									break;
								}
							}
							sw.WriteLine ("\t\t\t\tvar vals = new GLib.Value[{0}];", Parameters.Count);
							sw.WriteLine ("\t\t\t\tvar names = new IntPtr[{0}];", names.Count);
							if (!genFixedDimension)
								sw.WriteLine ("\t\t\t\tvar param_count = 0;");
							for (int i = 0; i < names.Count; i++) {
								Parameter p = Parameters [i];
								string indent = "\t\t\t\t";
								if (IsNullable (p.Generatable)) {
									sw.WriteLine (indent + "if (" + p.Name + " != null) {");
									indent += "\t";
								}
								if (genFixedDimension) {
									sw.WriteLine (indent + "names[" + i + "] = GLib.Marshaller.StringToPtrGStrdup (\"" + names [i] + "\");");
									sw.WriteLine (indent + "vals[" + i + "] = new GLib.Value (" + values [i] + ");");
								} else {
									sw.WriteLine (indent + "names[param_count] = GLib.Marshaller.StringToPtrGStrdup (\"" + names [i] + "\");");
									sw.WriteLine (indent + "vals[param_count++] = new GLib.Value (" + values [i] + ");");
								}

								if (IsNullable (p.Generatable))
									sw.WriteLine ("\t\t\t\t}");
							}

							if (genFixedDimension)
								sw.WriteLine ("\t\t\t\tCreateNativeObject (names, vals, {0});", names.Count);
							else
								sw.WriteLine ("\t\t\t\tCreateNativeObject (names, vals, param_count);");
							
							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}");
				}
	
				Body.Initialize(gen_info, false, false, ""); 
				sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString (false));
				Body.Finish (sw, "");
				Body.HandleException (sw, "");
			}
			
			sw.WriteLine("\t\t}");
			sw.WriteLine();
			
			Statistics.CtorCount++;
		}
	}
}