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

« back to all changes in this revision

Viewing changes to lib/gio-sharp/generator/OpaqueGen.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.OpaqueGen.cs - The Opaque Generatable.
2
 
//
3
 
// Author: Mike Kestner <mkestner@speakeasy.net>
4
 
//
5
 
// Copyright (c) 2001-2003 Mike Kestner
6
 
//
7
 
// This program is free software; you can redistribute it and/or
8
 
// modify it under the terms of version 2 of the GNU General Public
9
 
// License as published by the Free Software Foundation.
10
 
//
11
 
// This program is distributed in the hope that it will be useful,
12
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
// General Public License for more details.
15
 
//
16
 
// You should have received a copy of the GNU General Public
17
 
// License along with this program; if not, write to the
18
 
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 
// Boston, MA 02111-1307, USA.
20
 
 
21
 
 
22
 
namespace GtkSharp.Generation {
23
 
 
24
 
        using System;
25
 
        using System.Collections;
26
 
        using System.IO;
27
 
        using System.Xml;
28
 
 
29
 
        public class OpaqueGen : HandleBase {
30
 
 
31
 
                public OpaqueGen (XmlElement ns, XmlElement elem) : base (ns, elem) {}
32
 
        
33
 
                public override string FromNative(string var, bool owned)
34
 
                {
35
 
                        return var + " == IntPtr.Zero ? null : (" + QualifiedName + ") GLib.Opaque.GetOpaque (" + var + ", typeof (" + QualifiedName + "), " + (owned ? "true" : "false") + ")";
36
 
                }
37
 
 
38
 
                private bool DisableRawCtor {
39
 
                        get {
40
 
                                return Elem.HasAttribute ("disable_raw_ctor");
41
 
                        }
42
 
                }
43
 
 
44
 
                public override void Generate (GenerationInfo gen_info)
45
 
                {
46
 
                        gen_info.CurrentType = Name;
47
 
 
48
 
                        StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name);
49
 
 
50
 
                        sw.WriteLine ("namespace " + NS + " {");
51
 
                        sw.WriteLine ();
52
 
                        sw.WriteLine ("\tusing System;");
53
 
                        sw.WriteLine ("\tusing System.Collections;");
54
 
                        sw.WriteLine ("\tusing System.Runtime.InteropServices;");
55
 
                        sw.WriteLine ();
56
 
 
57
 
                        sw.WriteLine ("#region Autogenerated code");
58
 
 
59
 
                        SymbolTable table = SymbolTable.Table;
60
 
 
61
 
                        Method ref_, unref, dispose;
62
 
                        GetSpecialMethods (out ref_, out unref, out dispose);
63
 
 
64
 
                        if (IsDeprecated)
65
 
                                sw.WriteLine ("\t[Obsolete]");
66
 
                        sw.Write ("\t{0} class " + Name, IsInternal ? "internal" : "public");
67
 
                        string cs_parent = table.GetCSType(Elem.GetAttribute("parent"));
68
 
                        if (cs_parent != "")
69
 
                                sw.Write (" : " + cs_parent);
70
 
                        else
71
 
                                sw.Write (" : GLib.Opaque");
72
 
                        sw.WriteLine (" {");
73
 
                        sw.WriteLine ();
74
 
            
75
 
                        GenFields (gen_info);
76
 
                        GenMethods (gen_info, null, null);
77
 
                        GenCtors (gen_info);
78
 
 
79
 
                        if (ref_ != null) {
80
 
                                ref_.GenerateImport (sw);
81
 
                                sw.WriteLine ("\t\tprotected override void Ref (IntPtr raw)");
82
 
                                sw.WriteLine ("\t\t{");
83
 
                                sw.WriteLine ("\t\t\tif (!Owned) {");
84
 
                                sw.WriteLine ("\t\t\t\t" + ref_.CName + " (raw);");
85
 
                                sw.WriteLine ("\t\t\t\tOwned = true;");
86
 
                                sw.WriteLine ("\t\t\t}");
87
 
                                sw.WriteLine ("\t\t}");
88
 
                                sw.WriteLine ();
89
 
 
90
 
                                if (ref_.IsDeprecated) {
91
 
                                        sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]");
92
 
                                        if (ref_.ReturnType == "void")
93
 
                                                sw.WriteLine ("\t\tpublic void Ref () {}");
94
 
                                        else
95
 
                                                sw.WriteLine ("\t\tpublic " + Name + " Ref () { return this; }");
96
 
                                        sw.WriteLine ();
97
 
                                }
98
 
                        }
99
 
 
100
 
                        bool finalizer_needed = false;
101
 
 
102
 
                        if (unref != null) {
103
 
                                unref.GenerateImport (sw);
104
 
                                sw.WriteLine ("\t\tprotected override void Unref (IntPtr raw)");
105
 
                                sw.WriteLine ("\t\t{");
106
 
                                sw.WriteLine ("\t\t\tif (Owned) {");
107
 
                                sw.WriteLine ("\t\t\t\t" + unref.CName + " (raw);");
108
 
                                sw.WriteLine ("\t\t\t\tOwned = false;");
109
 
                                sw.WriteLine ("\t\t\t}");
110
 
                                sw.WriteLine ("\t\t}");
111
 
                                sw.WriteLine ();
112
 
 
113
 
                                if (unref.IsDeprecated) {
114
 
                                        sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]");
115
 
                                        sw.WriteLine ("\t\tpublic void Unref () {}");
116
 
                                        sw.WriteLine ();
117
 
                                }       
118
 
                                finalizer_needed = true;
119
 
                        }
120
 
 
121
 
                        if (dispose != null) {
122
 
                                dispose.GenerateImport (sw);
123
 
                                sw.WriteLine ("\t\tprotected override void Free (IntPtr raw)");
124
 
                                sw.WriteLine ("\t\t{");
125
 
                                sw.WriteLine ("\t\t\t" + dispose.CName + " (raw);");
126
 
                                sw.WriteLine ("\t\t}");
127
 
                                sw.WriteLine ();
128
 
 
129
 
                                if (dispose.IsDeprecated) {
130
 
                                        sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now freed automatically\")]");
131
 
                                        sw.WriteLine ("\t\tpublic void " + dispose.Name + " () {}");
132
 
                                        sw.WriteLine ();
133
 
                                }       
134
 
                                finalizer_needed = true;
135
 
                        }
136
 
 
137
 
                        if (finalizer_needed) {
138
 
                                sw.WriteLine ("\t\tclass FinalizerInfo {");
139
 
                                sw.WriteLine ("\t\t\tIntPtr handle;");
140
 
                                sw.WriteLine ();
141
 
                                sw.WriteLine ("\t\t\tpublic FinalizerInfo (IntPtr handle)");
142
 
                                sw.WriteLine ("\t\t\t{");
143
 
                                sw.WriteLine ("\t\t\t\tthis.handle = handle;");
144
 
                                sw.WriteLine ("\t\t\t}");
145
 
                                sw.WriteLine ();
146
 
                                sw.WriteLine ("\t\t\tpublic bool Handler ()");
147
 
                                sw.WriteLine ("\t\t\t{");
148
 
                                if (dispose != null)
149
 
                                        sw.WriteLine ("\t\t\t\t{0} (handle);", dispose.CName);
150
 
                                else if (unref != null)
151
 
                                        sw.WriteLine ("\t\t\t\t{0} (handle);", unref.CName);
152
 
                                sw.WriteLine ("\t\t\t\treturn false;");
153
 
                                sw.WriteLine ("\t\t\t}");
154
 
                                sw.WriteLine ("\t\t}");
155
 
                                sw.WriteLine ();
156
 
                                sw.WriteLine ("\t\t~{0} ()", Name);
157
 
                                sw.WriteLine ("\t\t{");
158
 
                                sw.WriteLine ("\t\t\tif (!Owned)");
159
 
                                sw.WriteLine ("\t\t\t\treturn;");
160
 
                                sw.WriteLine ("\t\t\tFinalizerInfo info = new FinalizerInfo (Handle);");
161
 
                                sw.WriteLine ("\t\t\tGLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));");
162
 
                                sw.WriteLine ("\t\t}");
163
 
                                sw.WriteLine ();
164
 
                        }
165
 
 
166
 
#if false
167
 
                        Method copy = Methods ["Copy"] as Method;
168
 
                        if (copy != null && copy.Parameters.Count == 0) {
169
 
                                sw.WriteLine ("\t\tprotected override GLib.Opaque Copy (IntPtr raw)");
170
 
                                sw.WriteLine ("\t\t{");
171
 
                                sw.WriteLine ("\t\t\tGLib.Opaque result = new " + QualifiedName + " (" + copy.CName + " (raw));");
172
 
                                sw.WriteLine ("\t\t\tresult.Owned = true;");
173
 
                                sw.WriteLine ("\t\t\treturn result;");
174
 
                                sw.WriteLine ("\t\t}");
175
 
                                sw.WriteLine ();
176
 
                        }
177
 
#endif
178
 
                        sw.WriteLine ("#endregion");
179
 
                        
180
 
                        AppendCustom(sw, gen_info.CustomDir);
181
 
 
182
 
                        sw.WriteLine ("\t}");
183
 
                        sw.WriteLine ("}");
184
 
 
185
 
                        sw.Close ();
186
 
                        gen_info.Writer = null;
187
 
                        Statistics.OpaqueCount++;
188
 
                }
189
 
 
190
 
                void GetSpecialMethods (out Method ref_, out Method unref, out Method dispose)
191
 
                {
192
 
                        ref_ = CheckSpecialMethod (GetMethod ("Ref"));
193
 
                        unref = CheckSpecialMethod (GetMethod ("Unref"));
194
 
 
195
 
                        dispose = GetMethod ("Free");
196
 
                        if (dispose == null) {
197
 
                                dispose = GetMethod ("Destroy");
198
 
                                if (dispose == null)
199
 
                                        dispose = GetMethod ("Dispose");
200
 
                        }
201
 
                        dispose = CheckSpecialMethod (dispose);
202
 
                }
203
 
 
204
 
                Method CheckSpecialMethod (Method method)
205
 
                {
206
 
                        if (method == null)
207
 
                                return null;
208
 
                        if (method.ReturnType != "void" &&
209
 
                            method.ReturnType != QualifiedName)
210
 
                                return null;
211
 
                        if (method.Signature.ToString () != "")
212
 
                                return null;
213
 
 
214
 
                        methods.Remove (method.Name);
215
 
                        return method;
216
 
                }
217
 
 
218
 
                protected override void GenCtors (GenerationInfo gen_info)
219
 
                {
220
 
                        if (!DisableRawCtor) {
221
 
                                gen_info.Writer.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}");
222
 
                                gen_info.Writer.WriteLine();
223
 
                        }
224
 
 
225
 
                        base.GenCtors (gen_info);
226
 
                }
227
 
 
228
 
        }
229
 
}
230