1
// GtkSharp.Generation.OpaqueGen.cs - The Opaque Generatable.
3
// Author: Mike Kestner <mkestner@speakeasy.net>
5
// Copyright (c) 2001-2003 Mike Kestner
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.
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.
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.
22
namespace GtkSharp.Generation {
25
using System.Collections;
29
public class OpaqueGen : HandleBase {
31
public OpaqueGen (XmlElement ns, XmlElement elem) : base (ns, elem) {}
33
public override string FromNative(string var, bool owned)
35
return var + " == IntPtr.Zero ? null : (" + QualifiedName + ") GLib.Opaque.GetOpaque (" + var + ", typeof (" + QualifiedName + "), " + (owned ? "true" : "false") + ")";
38
private bool DisableRawCtor {
40
return Elem.HasAttribute ("disable_raw_ctor");
44
public override void Generate (GenerationInfo gen_info)
46
gen_info.CurrentType = Name;
48
StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name);
50
sw.WriteLine ("namespace " + NS + " {");
52
sw.WriteLine ("\tusing System;");
53
sw.WriteLine ("\tusing System.Collections;");
54
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
57
sw.WriteLine ("#region Autogenerated code");
59
SymbolTable table = SymbolTable.Table;
61
Method ref_, unref, dispose;
62
GetSpecialMethods (out ref_, out unref, out dispose);
65
sw.WriteLine ("\t[Obsolete]");
66
sw.Write ("\t{0} class " + Name, IsInternal ? "internal" : "public");
67
string cs_parent = table.GetCSType(Elem.GetAttribute("parent"));
69
sw.Write (" : " + cs_parent);
71
sw.Write (" : GLib.Opaque");
76
GenMethods (gen_info, null, 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}");
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 () {}");
95
sw.WriteLine ("\t\tpublic " + Name + " Ref () { return this; }");
100
bool finalizer_needed = false;
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}");
113
if (unref.IsDeprecated) {
114
sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]");
115
sw.WriteLine ("\t\tpublic void Unref () {}");
118
finalizer_needed = true;
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}");
129
if (dispose.IsDeprecated) {
130
sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now freed automatically\")]");
131
sw.WriteLine ("\t\tpublic void " + dispose.Name + " () {}");
134
finalizer_needed = true;
137
if (finalizer_needed) {
138
sw.WriteLine ("\t\tclass FinalizerInfo {");
139
sw.WriteLine ("\t\t\tIntPtr handle;");
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}");
146
sw.WriteLine ("\t\t\tpublic bool Handler ()");
147
sw.WriteLine ("\t\t\t{");
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}");
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}");
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}");
178
sw.WriteLine ("#endregion");
180
AppendCustom(sw, gen_info.CustomDir);
182
sw.WriteLine ("\t}");
186
gen_info.Writer = null;
187
Statistics.OpaqueCount++;
190
void GetSpecialMethods (out Method ref_, out Method unref, out Method dispose)
192
ref_ = CheckSpecialMethod (GetMethod ("Ref"));
193
unref = CheckSpecialMethod (GetMethod ("Unref"));
195
dispose = GetMethod ("Free");
196
if (dispose == null) {
197
dispose = GetMethod ("Destroy");
199
dispose = GetMethod ("Dispose");
201
dispose = CheckSpecialMethod (dispose);
204
Method CheckSpecialMethod (Method method)
208
if (method.ReturnType != "void" &&
209
method.ReturnType != QualifiedName)
211
if (method.Signature.ToString () != "")
214
methods.Remove (method.Name);
218
protected override void GenCtors (GenerationInfo gen_info)
220
if (!DisableRawCtor) {
221
gen_info.Writer.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}");
222
gen_info.Writer.WriteLine();
225
base.GenCtors (gen_info);