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
|
// GtkSharp.Generation.VirtualMethod.cs - The VirtualMethod Generatable.
//
// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2003-2004 Novell, Inc.
// Copyright (c) 2009 Christian Hoff
//
// 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.Xml;
public abstract class VirtualMethod : MethodBase {
protected ReturnValue retval;
protected ManagedCallString call;
protected string modifiers = "";
public VirtualMethod (XmlElement elem, ObjectBase container_type) : base (elem, container_type)
{
if (container_type.ParserVersion == 1) {
// The old pre 2.14 parser didn't drop the 1st parameter in all <signal> and <virtual_method> elements
parms = new Parameters (elem ["parameters"], true);
}
retval = new ReturnValue (elem ["return-type"]);
}
protected abstract string CallString {
get;
}
VMSignature signature;
protected new VMSignature Signature {
get {
if (signature == null)
signature = new VMSignature (parms);
return signature;
}
}
/* Creates a callback method which invokes the corresponding virtual method
* @implementor is the class that implements the virtual method(e.g. the class that derives from an interface) or NULL if containing and declaring type are equal
*/
public void GenerateCallback (StreamWriter sw, ClassBase implementor)
{
if (!Validate ())
return;
string native_signature = "";
if (!IsStatic) {
native_signature += "IntPtr inst";
if (parms.Count > 0)
native_signature += ", ";
}
if (parms.Count > 0)
native_signature += parms.ImportSignature;
sw.WriteLine ("\t\t[GLib.CDeclCallback]");
sw.WriteLine ("\t\tdelegate {0} {1}NativeDelegate ({2});", retval.ToNativeType, this.Name, native_signature);
sw.WriteLine ();
sw.WriteLine ("\t\tstatic {0} {1}_cb ({2})", retval.ToNativeType, this.Name, native_signature);
sw.WriteLine ("\t\t{");
string unconditional = call.Unconditional ("\t\t\t");
if (unconditional.Length > 0)
sw.WriteLine (unconditional);
sw.WriteLine ("\t\t\ttry {");
if (!this.IsStatic) {
string type;
if (implementor != null)
type = implementor.QualifiedName;
else if (this.container_type is InterfaceGen)
type = this.container_type.Name + "Implementor"; // We are in an interface/adaptor, invoke the method in the implementor class
else
type = this.container_type.Name;
sw.WriteLine ("\t\t\t\t{0} __obj = GLib.Object.GetObject (inst, false) as {0};", type);
}
sw.Write (call.Setup ("\t\t\t\t"));
sw.Write ("\t\t\t\t");
if (!retval.IsVoid)
sw.Write (retval.CSType + " __result = ");
if (!this.IsStatic)
sw.Write ("__obj.");
sw.WriteLine (this.CallString + ";");
sw.Write (call.Finish ("\t\t\t\t"));
if (!retval.IsVoid)
sw.WriteLine ("\t\t\t\treturn " + retval.ToNative ("__result") + ";");
bool fatal = parms.HasOutParam || !retval.IsVoid;
sw.WriteLine ("\t\t\t} catch (Exception e) {");
sw.WriteLine ("\t\t\t\tGLib.ExceptionManager.RaiseUnhandledException (e, " + (fatal ? "true" : "false") + ");");
if (fatal) {
sw.WriteLine ("\t\t\t\t// NOTREACHED: above call does not return.");
sw.WriteLine ("\t\t\t\tthrow e;");
}
sw.WriteLine ("\t\t\t}");
sw.WriteLine ("\t\t}");
sw.WriteLine ();
}
public bool IsValid {
get {
return Validate ();
}
}
enum ValidState {
Unvalidated,
Invalid,
Valid
}
ValidState vstate = ValidState.Unvalidated;
public override bool Validate ()
{
if (vstate != ValidState.Unvalidated)
return vstate == ValidState.Valid;
vstate = ValidState.Valid;
if (!parms.Validate () || !retval.Validate ()) {
vstate = ValidState.Invalid;
}
if (vstate == ValidState.Invalid) {
Console.WriteLine ("(in virtual method " + container_type.QualifiedName + "." + Name + ")");
return false;
} else {
// The call string has to be created *after* the params have been validated since the Parameters class contains no elements before validation
call = new ManagedCallString (parms);
return true;
}
}
}
}
|