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;
|
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
|
|
|
}
|
|
|
|
|
2007-09-21 16:37:04 +00:00
|
|
|
public override string DefaultValue {
|
|
|
|
get { return "null"; }
|
|
|
|
}
|
|
|
|
|
2005-08-05 20:34:45 +00:00
|
|
|
public override bool Validate ()
|
|
|
|
{
|
2011-02-20 18:11:08 +00:00
|
|
|
valid = true;
|
|
|
|
LogWriter log = new LogWriter ();
|
|
|
|
log.Type = QualifiedName;
|
|
|
|
if (!retval.Validate (log) || !parms.Validate (log)) {
|
2005-08-05 20:34:45 +00:00
|
|
|
Statistics.ThrottledCount++;
|
2005-08-22 16:51:38 +00:00
|
|
|
valid = false;
|
2005-08-05 20:34:45 +00:00
|
|
|
}
|
|
|
|
|
2011-04-28 23:38:07 +00:00
|
|
|
if (!String.IsNullOrEmpty (retval.CountParameterName))
|
|
|
|
retval.CountParameter = parms.GetCountParameter (retval.CountParameterName);
|
2013-06-05 17:26:33 +00:00
|
|
|
if (retval.CountParameterIndex >= 0)
|
|
|
|
retval.CountParameter = parms[retval.CountParameterIndex];
|
2011-04-28 23:38:07 +00:00
|
|
|
|
2011-02-20 18:11:08 +00:00
|
|
|
return valid;
|
2005-08-05 20:34:45 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 20:34:24 +00:00
|
|
|
public string InvokerName {
|
|
|
|
get {
|
|
|
|
if (!valid)
|
|
|
|
return String.Empty;
|
|
|
|
return NS + "Sharp." + Name + "Invoker";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 15:05:58 +00:00
|
|
|
public string WrapperName {
|
|
|
|
get {
|
|
|
|
if (!valid)
|
|
|
|
return String.Empty;
|
|
|
|
return NS + "Sharp." + Name + "Wrapper";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 + "}");
|
|
|
|
}
|
|
|
|
|
2007-09-11 20:34:24 +00:00
|
|
|
string CastFromInt (string type)
|
|
|
|
{
|
|
|
|
return type != "int" ? "(" + type + ") " : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
string InvokeString {
|
|
|
|
get {
|
|
|
|
if (parms.Count == 0)
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
string[] result = new string [parms.Count];
|
|
|
|
for (int i = 0; i < parms.Count; i++) {
|
|
|
|
Parameter p = parms [i];
|
|
|
|
IGeneratable igen = p.Generatable;
|
|
|
|
|
|
|
|
if (i > 0 && parms [i - 1].IsString && p.IsLength) {
|
|
|
|
string string_name = parms [i - 1].Name;
|
|
|
|
result[i] = igen.CallByName (CastFromInt (p.CSType) + "System.Text.Encoding.UTF8.GetByteCount (" + string_name + ")");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.CallName = p.Name;
|
|
|
|
result [i] = p.CallString;
|
|
|
|
if (p.IsUserData)
|
2008-03-22 01:30:42 +00:00
|
|
|
result [i] = "__data";
|
2007-09-11 20:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return String.Join (", ", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodBody body;
|
|
|
|
|
|
|
|
void GenInvoker (GenerationInfo gen_info, StreamWriter sw)
|
|
|
|
{
|
2007-09-14 17:35:14 +00:00
|
|
|
if (sig == null)
|
|
|
|
sig = new Signature (parms);
|
|
|
|
|
2007-09-11 20:34:24 +00:00
|
|
|
sw.WriteLine ("\tinternal class " + Name + "Invoker {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\t" + Name + "Native native_cb;");
|
2008-03-22 01:30:42 +00:00
|
|
|
sw.WriteLine ("\t\tIntPtr __data;");
|
|
|
|
sw.WriteLine ("\t\tGLib.DestroyNotify __notify;");
|
2007-09-11 20:34:24 +00:00
|
|
|
sw.WriteLine ();
|
2008-03-22 01:30:42 +00:00
|
|
|
sw.WriteLine ("\t\t~" + Name + "Invoker ()");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tif (__notify == null)");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn;");
|
|
|
|
sw.WriteLine ("\t\t\t__notify (__data);");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tinternal " + Name + "Invoker (" + Name + "Native native_cb) : this (native_cb, IntPtr.Zero, null) {}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tinternal " + Name + "Invoker (" + Name + "Native native_cb, IntPtr data) : this (native_cb, data, null) {}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tinternal " + Name + "Invoker (" + Name + "Native native_cb, IntPtr data, GLib.DestroyNotify notify)");
|
2007-09-11 20:34:24 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
sw.WriteLine ("\t\t\tthis.native_cb = native_cb;");
|
2008-03-22 01:30:42 +00:00
|
|
|
sw.WriteLine ("\t\t\t__data = data;");
|
|
|
|
sw.WriteLine ("\t\t\t__notify = notify;");
|
2007-09-11 20:34:24 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\tinternal " + QualifiedName + " Handler {");
|
|
|
|
sw.WriteLine ("\t\t\tget {");
|
|
|
|
sw.WriteLine ("\t\t\t\treturn new " + QualifiedName + "(InvokeNative);");
|
|
|
|
sw.WriteLine ("\t\t\t}");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\t\t" + retval.CSType + " InvokeNative (" + sig + ")");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
body.Initialize (gen_info);
|
|
|
|
string call = "native_cb (" + InvokeString + ")";
|
|
|
|
if (retval.IsVoid)
|
|
|
|
sw.WriteLine ("\t\t\t" + call + ";");
|
|
|
|
else
|
2011-01-23 01:43:14 +00:00
|
|
|
sw.WriteLine ("\t\t\t" + retval.CSType + " __result = " + retval.FromNative (call) + ";");
|
2007-09-11 20:34:24 +00:00
|
|
|
body.Finish (sw, String.Empty);
|
|
|
|
if (!retval.IsVoid)
|
2011-01-23 01:43:14 +00:00
|
|
|
sw.WriteLine ("\t\t\treturn __result;");
|
2007-09-11 20:34:24 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ("\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-08-13 14:29:06 +00:00
|
|
|
if (!Validate ())
|
|
|
|
return String.Empty;
|
|
|
|
|
2007-09-11 20:34:24 +00:00
|
|
|
body = new MethodBody (parms);
|
|
|
|
|
|
|
|
StreamWriter save_sw = gen_info.Writer;
|
2014-02-24 01:04:03 +00:00
|
|
|
StreamWriter sw = gen_info.Writer = gen_info.OpenStream (qualname, NS);
|
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");
|
2009-09-03 19:50:53 +00:00
|
|
|
sw.WriteLine ("\t[UnmanagedFunctionPointer (CallingConvention.Cdecl)]");
|
2007-08-13 14:29:06 +00:00
|
|
|
sw.WriteLine ("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + parms.ImportSignature + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2007-09-11 20:34:24 +00:00
|
|
|
GenInvoker (gen_info, sw);
|
2005-03-12 18:54:53 +00:00
|
|
|
sw.WriteLine ("\tinternal class " + Name + "Wrapper {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2009-04-13 17:44:48 +00:00
|
|
|
ManagedCallString call = new ManagedCallString (parms);
|
2007-08-13 14:29:06 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + retval.MarshalType + " NativeCallback (" + parms.ImportSignature + ")");
|
2002-11-10 10:03:51 +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-03-15 21:26:38 +00:00
|
|
|
sw.WriteLine ("\t\t\ttry {");
|
2008-07-08 21:12:22 +00:00
|
|
|
string call_setup = call.Setup ("\t\t\t\t");
|
|
|
|
if (call_setup.Length > 0)
|
|
|
|
sw.WriteLine (call_setup);
|
|
|
|
if (retval.CSType == "void")
|
|
|
|
sw.WriteLine ("\t\t\t\tmanaged ({0});", call);
|
|
|
|
else
|
|
|
|
sw.WriteLine ("\t\t\t\t{0} __ret = managed ({1});", retval.CSType, call);
|
|
|
|
string finish = call.Finish ("\t\t\t\t");
|
|
|
|
if (finish.Length > 0)
|
|
|
|
sw.WriteLine (finish);
|
|
|
|
sw.WriteLine ("\t\t\t\tif (release_on_call)\n\t\t\t\t\tgch.Free ();");
|
2011-04-28 23:38:07 +00:00
|
|
|
Parameter cnt = retval.CountParameter;
|
|
|
|
if (cnt != null)
|
|
|
|
sw.WriteLine ("\t\t\t\t{0} = {1}{2};", cnt.Name, cnt.CSType == "int" ? String.Empty : "(" + cnt.MarshalType + ")(" + cnt.CSType + ")", "__ret.Length");
|
2008-07-08 21:12:22 +00:00
|
|
|
if (retval.CSType != "void")
|
|
|
|
sw.WriteLine ("\t\t\t\treturn {0};", retval.ToNative ("__ret"));
|
|
|
|
|
|
|
|
/* If the function expects one or more "out" parameters(error parameters are excluded) or has a return value different from void and bool, exceptions
|
|
|
|
* thrown in the managed function have to be considered fatal meaning that an exception is to be thrown and the function call cannot not return
|
|
|
|
*/
|
|
|
|
bool fatal = (retval.MarshalType != "void" && retval.MarshalType != "bool") || call.HasOutParam;
|
2007-03-15 21:26:38 +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;");
|
|
|
|
} else if (retval.MarshalType == "bool") {
|
|
|
|
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 ();
|
2007-09-11 20:34:24 +00:00
|
|
|
gen_info.Writer = save_sw;
|
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
|
|
|
{
|
2011-02-20 18:11:08 +00:00
|
|
|
gen_info.CurrentType = QualifiedName;
|
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
|
|
|
|
2014-02-24 01:04:03 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (Name, NS);
|
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 ();
|
2008-09-30 17:44:24 +00:00
|
|
|
sw.WriteLine ("\t{0} delegate " + retval.CSType + " " + Name + "(" + sig.ToString() + ");", IsInternal ? "internal" : "public");
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|