~ubuntu-branches/debian/sid/docky/sid

« back to all changes in this revision

Viewing changes to lib/gio-sharp/generator/Ctor.cs

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2012-01-19 19:03:38 UTC
  • mfrom: (1.1.14) (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20120119190338-n44q7tmqsrkudvk7
Tags: 2.1.3-2
* Upload to unstable
* debian/watch:
  + Look for xz tarballs from now on

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// GtkSharp.Generation.Ctor.cs - The Constructor Generation Class.
2
 
//
3
 
// Author: Mike Kestner <mkestner@novell.com>
4
 
//
5
 
// Copyright (c) 2001-2003 Mike Kestner
6
 
// Copyright (c) 2004-2005 Novell, Inc.
7
 
//
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.
11
 
//
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.
16
 
//
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.
21
 
 
22
 
 
23
 
namespace GtkSharp.Generation {
24
 
 
25
 
        using System;
26
 
        using System.Collections;
27
 
        using System.IO;
28
 
        using System.Xml;
29
 
 
30
 
        public class Ctor : MethodBase  {
31
 
 
32
 
                private bool preferred;
33
 
                private string name;
34
 
                private bool needs_chaining = false;
35
 
 
36
 
                public Ctor (XmlElement elem, ClassBase implementor) : base (elem, implementor) 
37
 
                {
38
 
                        if (elem.HasAttribute ("preferred"))
39
 
                                preferred = true;
40
 
                        if (implementor is ObjectGen)
41
 
                                needs_chaining = true;
42
 
                        name = implementor.Name;
43
 
                }
44
 
 
45
 
                public bool Preferred {
46
 
                        get { return preferred; }
47
 
                        set { preferred = value; }
48
 
                }
49
 
 
50
 
                public string StaticName {
51
 
                        get {
52
 
                                if (!IsStatic)
53
 
                                        return String.Empty;
54
 
 
55
 
                                string[] toks = CName.Substring(CName.IndexOf("new")).Split ('_');
56
 
                                string result = String.Empty;
57
 
 
58
 
                                foreach (string tok in toks)
59
 
                                        result += tok.Substring(0,1).ToUpper() + tok.Substring(1);
60
 
                                return result;
61
 
                        }
62
 
                }
63
 
 
64
 
                void GenerateImport (StreamWriter sw)
65
 
                {
66
 
                        sw.WriteLine("\t\t[DllImport(\"" + LibraryName + "\")]");
67
 
                        sw.WriteLine("\t\tstatic extern " + Safety + "IntPtr " + CName + "(" + Parameters.ImportSignature + ");");
68
 
                        sw.WriteLine();
69
 
                }
70
 
 
71
 
                void GenerateStatic (GenerationInfo gen_info)
72
 
                {
73
 
                        StreamWriter sw = gen_info.Writer;
74
 
                        sw.WriteLine("\t\t" + Protection + " static " + Safety + Modifiers +  name + " " + StaticName + "(" + Signature + ")");
75
 
                        sw.WriteLine("\t\t{");
76
 
 
77
 
                        Body.Initialize(gen_info, false, false, ""); 
78
 
 
79
 
                        sw.Write("\t\t\t" + name + " result = ");
80
 
                        if (container_type is StructBase)
81
 
                                sw.Write ("{0}.New (", name);
82
 
                        else
83
 
                                sw.Write ("new {0} (", name);
84
 
                        sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));");
85
 
                        Body.Finish (sw, ""); 
86
 
                        Body.HandleException (sw, ""); 
87
 
                        sw.WriteLine ("\t\t\treturn result;");
88
 
                }
89
 
 
90
 
                public void Generate (GenerationInfo gen_info)
91
 
                {
92
 
                        if (!Validate ())
93
 
                                return;
94
 
 
95
 
                        StreamWriter sw = gen_info.Writer;
96
 
                        gen_info.CurrentMember = CName;
97
 
 
98
 
                        GenerateImport (sw);
99
 
                        
100
 
                        if (IsStatic)
101
 
                                GenerateStatic (gen_info);
102
 
                        else {
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{");
105
 
 
106
 
                                if (needs_chaining) {
107
 
                                        sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {");
108
 
                                        
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;");
112
 
                                        } else {
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) {
118
 
                                                                names.Add (p.Name);
119
 
                                                                values.Add (p.Name);
120
 
                                                        } else if (p.PropertyName != String.Empty) {
121
 
                                                                names.Add (p.PropertyName);
122
 
                                                                values.Add (p.Name);
123
 
                                                        }
124
 
                                                }
125
 
 
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) {");
134
 
                                                                        indent += "\t";
135
 
                                                                }
136
 
                                                                sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");");
137
 
                                                                sw.WriteLine (indent + "vals.Add (new GLib.Value (" + values[i] + "));");
138
 
 
139
 
                                                                if (p.Generatable is ClassBase && !(p.Generatable is StructBase))
140
 
                                                                        sw.WriteLine ("\t\t\t\t}");
141
 
                                                        }
142
 
 
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;");
145
 
                                                } else
146
 
                                                        sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");");
147
 
                                        }
148
 
                                        
149
 
                                        sw.WriteLine ("\t\t\t}");
150
 
                                }
151
 
        
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, "");
156
 
                        }
157
 
                        
158
 
                        sw.WriteLine("\t\t}");
159
 
                        sw.WriteLine();
160
 
                        
161
 
                        Statistics.CtorCount++;
162
 
                }
163
 
        }
164
 
}
165