2004-11-18 22:47:05 +00:00
|
|
|
// GtkSharp.Generation.VirtualMethod.cs - The VirtualMethod Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2003-2004 Novell, Inc.
|
2009-04-13 17:44:48 +00:00
|
|
|
// Copyright (c) 2009 Christian Hoff
|
2004-11-18 22:47:05 +00:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
public abstract class VirtualMethod : MethodBase {
|
|
|
|
protected ReturnValue retval;
|
|
|
|
protected ManagedCallString call;
|
2004-11-18 22:47:05 +00:00
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
protected string modifiers = "";
|
2004-11-18 22:47:05 +00:00
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
public VirtualMethod (XmlElement elem, ObjectBase container_type) : base (elem, container_type)
|
2004-11-18 22:47:05 +00:00
|
|
|
{
|
2009-04-13 17:44:48 +00:00
|
|
|
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);
|
|
|
|
}
|
2004-11-18 22:47:05 +00:00
|
|
|
retval = new ReturnValue (elem ["return-type"]);
|
|
|
|
}
|
2009-04-13 17:44:48 +00:00
|
|
|
|
|
|
|
public string MarshalReturnType {
|
2007-09-11 20:34:24 +00:00
|
|
|
get {
|
2009-04-13 17:44:48 +00:00
|
|
|
return SymbolTable.Table.GetToNativeReturnType (elem["return-type"].GetAttribute("type"));
|
2007-09-11 20:34:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
protected abstract string CallString {
|
|
|
|
get;
|
2007-09-11 20:34:24 +00:00
|
|
|
}
|
2009-04-13 17:44:48 +00:00
|
|
|
|
|
|
|
VMSignature signature;
|
|
|
|
protected VMSignature Signature {
|
2004-11-18 22:47:05 +00:00
|
|
|
get {
|
2009-04-13 17:44:48 +00:00
|
|
|
if (signature == null)
|
|
|
|
signature = new VMSignature (parms);
|
|
|
|
|
|
|
|
return signature;
|
2004-11-18 22:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
/* 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)
|
2004-11-18 22:47:05 +00:00
|
|
|
{
|
2007-09-11 20:34:24 +00:00
|
|
|
if (!Validate ())
|
|
|
|
return;
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
string native_signature = "";
|
|
|
|
if (!IsStatic) {
|
|
|
|
native_signature += "IntPtr inst";
|
|
|
|
if (parms.Count > 0)
|
|
|
|
native_signature += ", ";
|
|
|
|
}
|
|
|
|
if (parms.Count > 0)
|
|
|
|
native_signature += parms.ImportSignature;
|
2007-09-19 18:15:55 +00:00
|
|
|
|
|
|
|
sw.WriteLine ("\t\t[GLib.CDeclCallback]");
|
2009-04-13 17:44:48 +00:00
|
|
|
sw.WriteLine ("\t\tdelegate {0} {1}NativeDelegate ({2});", MarshalReturnType, this.Name, native_signature);
|
2007-09-19 18:15:55 +00:00
|
|
|
sw.WriteLine ();
|
2009-04-13 17:44:48 +00:00
|
|
|
sw.WriteLine ("\t\tstatic {0} {1}_cb ({2})", MarshalReturnType, this.Name, native_signature);
|
2004-11-18 22:47:05 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2008-07-08 21:12:22 +00:00
|
|
|
string unconditional = call.Unconditional ("\t\t\t");
|
|
|
|
if (unconditional.Length > 0)
|
|
|
|
sw.WriteLine (unconditional);
|
2007-10-02 15:57:45 +00:00
|
|
|
sw.WriteLine ("\t\t\ttry {");
|
2009-04-13 17:44:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:57:45 +00:00
|
|
|
sw.Write (call.Setup ("\t\t\t\t"));
|
2009-04-13 17:44:48 +00:00
|
|
|
sw.Write ("\t\t\t\t");
|
|
|
|
if (!retval.IsVoid)
|
|
|
|
sw.Write (retval.CSType + " __result = ");
|
|
|
|
if (!this.IsStatic)
|
|
|
|
sw.Write ("__obj.");
|
|
|
|
sw.WriteLine (this.CallString + ";");
|
2007-10-02 15:57:45 +00:00
|
|
|
sw.Write (call.Finish ("\t\t\t\t"));
|
2007-09-11 20:34:24 +00:00
|
|
|
if (!retval.IsVoid)
|
2008-04-04 16:10:08 +00:00
|
|
|
sw.WriteLine ("\t\t\t\treturn " + retval.ToNative ("__result") + ";");
|
2007-10-02 15:57:45 +00:00
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
bool fatal = parms.HasOutParam || !retval.IsVoid;
|
2007-10-02 15:57:45 +00:00
|
|
|
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}");
|
2004-11-18 22:47:05 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
2009-04-13 17:44:48 +00:00
|
|
|
sw.WriteLine ();
|
2004-11-18 22:47:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
public bool IsValid {
|
|
|
|
get {
|
|
|
|
return Validate ();
|
|
|
|
}
|
2007-10-02 15:57:45 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 20:34:24 +00:00
|
|
|
enum ValidState {
|
|
|
|
Unvalidated,
|
|
|
|
Invalid,
|
|
|
|
Valid
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidState vstate = ValidState.Unvalidated;
|
|
|
|
|
2007-10-02 15:57:45 +00:00
|
|
|
public override bool Validate ()
|
2004-11-18 22:47:05 +00:00
|
|
|
{
|
2009-04-13 17:44:48 +00:00
|
|
|
if (vstate != ValidState.Unvalidated)
|
|
|
|
return vstate == ValidState.Valid;
|
|
|
|
|
|
|
|
vstate = ValidState.Valid;
|
2004-11-18 22:47:05 +00:00
|
|
|
if (!parms.Validate () || !retval.Validate ()) {
|
2007-09-11 20:34:24 +00:00
|
|
|
vstate = ValidState.Invalid;
|
2004-11-18 22:47:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-13 17:44:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-11-18 22:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|