2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.CallbackGen.cs - The Callback Generatable.
|
|
|
|
//
|
2007-03-15 21:26:38 +00:00
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
2002-11-10 10:03:51 +00:00
|
|
|
//
|
2004-06-25 16:35:15 +00:00
|
|
|
// Copyright (c) 2002-2003 Mike Kestner
|
2007-03-15 21:26:38 +00:00
|
|
|
// Copyright (c) 2007 Novell, Inc.
|
2004-06-25 16:35:15 +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.
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
2005-07-02 15:23:27 +00:00
|
|
|
public class CallbackGen : GenBase, IAccessor {
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
private Parameters parms;
|
2003-12-03 23:08:14 +00:00
|
|
|
private Signature sig = null;
|
|
|
|
private ImportSignature isig = null;
|
2004-12-27 21:32:08 +00:00
|
|
|
private ReturnValue retval;
|
2005-08-22 16:51:38 +00:00
|
|
|
private bool valid = true;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
public CallbackGen (XmlElement ns, XmlElement elem) : base (ns, elem)
|
|
|
|
{
|
2004-12-27 21:32:08 +00:00
|
|
|
retval = new ReturnValue (elem ["return-type"]);
|
2005-01-26 19:17:07 +00:00
|
|
|
parms = new Parameters (elem ["parameters"]);
|
|
|
|
parms.HideData = true;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 20:34:45 +00:00
|
|
|
public override bool Validate ()
|
|
|
|
{
|
|
|
|
if (!retval.Validate ()) {
|
|
|
|
Console.WriteLine ("rettype: " + retval.CType + " in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
2005-08-22 16:51:38 +00:00
|
|
|
valid = false;
|
2005-08-05 20:34:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parms.Validate ()) {
|
|
|
|
Console.WriteLine (" in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
2005-08-22 16:51:38 +00:00
|
|
|
valid = false;
|
2005-08-05 20:34:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-22 16:51:38 +00:00
|
|
|
valid = true;
|
2005-08-05 20:34:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override string MarshalType {
|
|
|
|
get {
|
2005-08-22 16:51:38 +00:00
|
|
|
if (valid)
|
|
|
|
return NS + "Sharp." + Name + "Native";
|
|
|
|
else
|
|
|
|
return "";
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override string CallByName (string var_name)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return var_name + ".NativeDelegate";
|
|
|
|
}
|
|
|
|
|
2005-05-04 11:47:25 +00:00
|
|
|
public override string FromNative (string var)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2005-05-04 11:47:25 +00:00
|
|
|
return NS + "Sharp." + Name + "Wrapper.GetManagedDelegate (" + var + ")";
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2005-07-02 15:23:27 +00:00
|
|
|
public void WriteAccessors (StreamWriter sw, string indent, string var)
|
|
|
|
{
|
|
|
|
sw.WriteLine (indent + "get {");
|
|
|
|
sw.WriteLine (indent + "\treturn " + FromNative (var) + ";");
|
|
|
|
sw.WriteLine (indent + "}");
|
|
|
|
}
|
|
|
|
|
2005-07-02 14:55:08 +00:00
|
|
|
public string GenWrapper (GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
string wrapper = Name + "Native";
|
2005-07-02 14:55:08 +00:00
|
|
|
string qualname = MarshalType;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2005-07-02 14:55:08 +00:00
|
|
|
isig = new ImportSignature (parms);
|
2003-12-03 23:08:14 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (qualname);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2005-07-02 14:55:08 +00:00
|
|
|
sw.WriteLine ("namespace " + NS + "Sharp {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
2007-01-26 20:46:09 +00:00
|
|
|
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#region Autogenerated code");
|
2005-03-25 18:13:00 +00:00
|
|
|
sw.WriteLine ("\t[GLib.CDeclCallback]");
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + isig + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\tinternal class " + Name + "Wrapper {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + retval.MarshalType + " NativeCallback (" + isig + ")");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2007-03-15 21:26:38 +00:00
|
|
|
sw.WriteLine ("\t\t\ttry {");
|
2003-04-06 09:21:15 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
bool need_sep = false;
|
2007-03-15 21:26:38 +00:00
|
|
|
bool throws_error = false;
|
2002-11-10 10:03:51 +00:00
|
|
|
string call_str = "";
|
2003-10-13 21:53:40 +00:00
|
|
|
string cleanup_str = "";
|
2005-01-26 19:17:07 +00:00
|
|
|
for (int i = 0, idx = 0; i < parms.Count; i++)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2004-12-27 21:32:08 +00:00
|
|
|
Parameter p = parms [i];
|
2003-02-24 03:13:08 +00:00
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
if (p.CType == "GError**") {
|
2007-03-15 21:26:38 +00:00
|
|
|
sw.WriteLine ("\t\t\t\t" + p.Name + " = IntPtr.Zero;");
|
|
|
|
throws_error = true;
|
2004-05-11 20:56:24 +00:00
|
|
|
continue;
|
* generator/Parameters.cs (IsHidden): method to check if a
parameter should be hidden in the managed sig (eg, because it's
user_data, or it's the length of the preceding array/string, etc).
(VisibleCount): the number of parameters that will actually be
exposed in the managed signature.
(IsAccessor): test VisibleCount, not Count
(AccessorReturnType, AccessorName): deal with the fact that the
accessor parameter might not be the first one.
* generator/CallbackGen.cs:
* generator/Signature.cs: use Parameters.IsHidden.
* generator/Method.cs (Initialize): set is_set based on
VisibleCount, not Count.
(Validate): call base.Validate() before Initialize() so that
VisibleCount will be correct in Initialize.
* generator/MethodBody.cs (GetCallString, CallArrayLength,
Initialize): update to deal with accessors with multiple args.
* gtk/Clipboard.custom (SetText): implement as an Obsolete variant
of the Text property
* gtk/IconTheme.custom (SearchPath, SetSearchPath): obsolete
SetSearchPath, implement a setter on SearchPath instead.
* gtk/ListStore.custom (SetColumnTypes):
* gtk/TreeStore.custom (SetColumnTypes): implement as an Obsolete
variant of the ColumnTypes property.
* glade/XML.custom (CustomHandler): implement as a property
(SetCustomHandler): Mark this obsolete
* glade/Global.custom (SetCustomHandler): deprecate in favor of
XML.CustomHandler.
* gnomedb/Editor.custom (SetText): implement as an Obsolete
variant of the Text property
svn path=/trunk/gtk-sharp/; revision=43898
2005-05-02 20:10:03 +00:00
|
|
|
} else if (parms.IsHidden (p))
|
|
|
|
continue;
|
2004-05-11 20:56:24 +00:00
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
IGeneratable gen = p.Generatable;
|
|
|
|
|
2007-03-15 21:26:38 +00:00
|
|
|
sw.Write("\t\t\t\t" + p.CSType + " _arg" + idx);
|
2004-12-27 21:32:08 +00:00
|
|
|
if (p.PassAs == "out") {
|
2003-10-13 21:53:40 +00:00
|
|
|
sw.WriteLine(";");
|
2007-03-15 21:26:38 +00:00
|
|
|
cleanup_str += "\t\t\t\t" + p.Name + " = " + gen.CallByName ("_arg" + idx) + ";\n";
|
2003-10-13 21:53:40 +00:00
|
|
|
} else
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine(" = " + gen.FromNative (p.Name) + ";");
|
2003-10-13 21:53:40 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
if (need_sep)
|
|
|
|
call_str += ", ";
|
|
|
|
else
|
|
|
|
need_sep = true;
|
2004-12-27 21:32:08 +00:00
|
|
|
call_str += String.Format ("{0} _arg{1}", p.PassAs, idx);
|
2002-11-10 10:03:51 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2007-03-15 21:26:38 +00:00
|
|
|
bool has_out_params = cleanup_str.Length > 0;
|
|
|
|
cleanup_str += "\t\t\t\tif (release_on_call)\n\t\t\t\t\tgch.Free ();\n";
|
2007-01-26 20:46:09 +00:00
|
|
|
|
2007-03-15 21:26:38 +00:00
|
|
|
sw.Write ("\t\t\t\t");
|
2005-03-12 18:54:53 +00:00
|
|
|
string invoke = "managed (" + call_str + ")";
|
2004-12-27 21:32:08 +00:00
|
|
|
if (retval.MarshalType != "void") {
|
2007-01-26 20:46:09 +00:00
|
|
|
sw.Write (retval.MarshalType + " ret = ");
|
2007-03-15 21:26:38 +00:00
|
|
|
cleanup_str += "\t\t\t\treturn ret;\n";
|
2003-11-03 01:36:55 +00:00
|
|
|
|
2004-12-27 21:32:08 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
ClassBase ret_wrapper = table.GetClassGen (retval.CType);
|
2005-08-09 20:10:45 +00:00
|
|
|
if (ret_wrapper != null && ret_wrapper is HandleBase)
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine ("(({0}) {1}).Handle;", retval.CSType, invoke);
|
|
|
|
else if (table.IsStruct (retval.CType) || table.IsBoxed (retval.CType)) {
|
2003-11-03 01:36:55 +00:00
|
|
|
// Shoot. I have no idea what to do here.
|
2004-12-27 21:32:08 +00:00
|
|
|
Console.WriteLine ("Struct return type {0} in callback {1}", retval.CType, CName);
|
2003-11-03 01:36:55 +00:00
|
|
|
sw.WriteLine ("IntPtr.Zero;");
|
2004-12-27 21:32:08 +00:00
|
|
|
} else if (table.IsEnum (retval.CType))
|
2003-11-03 01:36:55 +00:00
|
|
|
sw.WriteLine ("(int) {0};", invoke);
|
|
|
|
else
|
2005-12-21 00:18:52 +00:00
|
|
|
sw.WriteLine ("({0}) ({1});", retval.MarshalType, table.ToNativeReturn (retval.CType, invoke));
|
2003-10-13 21:53:40 +00:00
|
|
|
} else
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine (invoke + ";");
|
2003-10-13 21:53:40 +00:00
|
|
|
|
2007-03-15 21:26:38 +00:00
|
|
|
sw.Write (cleanup_str);
|
|
|
|
bool fatal = (retval.MarshalType != "void" && retval.MarshalType != "bool") || has_out_params;
|
|
|
|
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;");
|
|
|
|
} else if (retval.MarshalType == "bool") {
|
|
|
|
if (throws_error)
|
|
|
|
sw.WriteLine ("\t\t\t\terror = IntPtr.Zero;");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn false;");
|
|
|
|
}
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
2007-01-26 20:46:09 +00:00
|
|
|
sw.WriteLine ("\t\tbool release_on_call = false;");
|
|
|
|
sw.WriteLine ("\t\tGCHandle gch;");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic void PersistUntilCalled ()");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\trelease_on_call = true;");
|
|
|
|
sw.WriteLine ("\t\t\tgch = GCHandle.Alloc (this);");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tinternal " + wrapper + " NativeDelegate;");
|
|
|
|
sw.WriteLine ("\t\t" + NS + "." + Name + " managed;");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + Name + "Wrapper (" + NS + "." + Name + " managed)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\t\t\tthis.managed = managed;");
|
2005-03-24 22:47:50 +00:00
|
|
|
sw.WriteLine ("\t\t\tif (managed != null)");
|
|
|
|
sw.WriteLine ("\t\t\t\tNativeDelegate = new " + wrapper + " (NativeCallback);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
2005-05-04 11:47:25 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tpublic static " + NS + "." + Name + " GetManagedDelegate (" + wrapper + " native)");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tif (native == null)");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn null;");
|
|
|
|
sw.WriteLine ("\t\t\t" + Name + "Wrapper wrapper = (" + Name + "Wrapper) native.Target;");
|
|
|
|
sw.WriteLine ("\t\t\tif (wrapper == null)");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn null;");
|
|
|
|
sw.WriteLine ("\t\t\treturn wrapper.managed;");
|
|
|
|
sw.WriteLine ("\t\t}");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t}");
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#endregion");
|
2003-10-05 00:37:24 +00:00
|
|
|
sw.WriteLine ("}");
|
|
|
|
sw.Close ();
|
2005-07-02 14:55:08 +00:00
|
|
|
return NS + "Sharp." + Name + "Wrapper";
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2004-12-26 19:33:34 +00:00
|
|
|
public override void Generate (GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
2005-04-04 16:27:08 +00:00
|
|
|
gen_info.CurrentType = Name;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
sig = new Signature (parms);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ("namespace " + NS + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ();
|
2004-12-27 21:32:08 +00:00
|
|
|
sw.WriteLine ("\tpublic delegate " + retval.CSType + " " + Name + "(" + sig.ToString() + ");");
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("}");
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.Close ();
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2005-07-02 14:55:08 +00:00
|
|
|
GenWrapper (gen_info);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
Statistics.CBCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|