GtkSharp/generator/MethodBody.cs

181 lines
5.5 KiB
C#
Raw Normal View History

// GtkSharp.Generation.MethodBody.cs - The MethodBody Generation Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// Copyright (c) 2001-2003 Mike Kestner
// Copyright (c) 2003-2004 Novell, Inc.
//
// 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;
public class MethodBody {
Parameters parameters;
public MethodBody (Parameters parms)
{
parameters = parms;
}
private string CastFromInt (string type)
{
return type != "int" ? "(" + type + ") " : "";
}
public string GetCallString (bool is_set)
{
if (parameters.Count == 0)
return String.Empty;
string[] result = new string [parameters.Count];
for (int i = 0; i < parameters.Count; i++) {
Parameter p = parameters [i];
IGeneratable igen = p.Generatable;
bool is_prop = is_set && i == 0;
if (i > 0 && parameters [i - 1].IsString && p.IsLength) {
2005-05-02 20:10:03 +00:00
string string_name = (i == 1 && is_set) ? "value" : parameters [i - 1].Name;
result[i] = igen.CallByName (CastFromInt (p.CSType) + "System.Text.Encoding.UTF8.GetByteCount (" + string_name + ")");
continue;
}
if (is_prop)
p.CallName = "value";
else
p.CallName = p.Name;
string call_parm = p.CallString;
if (p.IsUserData && parameters.IsHidden (p) && !parameters.HideData &&
* parser/gapi2xml.pl (addParamsElem): change the handling of anonymous function pointer types in method signatures. Before, we added a <callback> child to the <parameters> node, but the generator just ignored it. Now we add the callback (with a made-up name) to the toplevel node, and add an ordinary <param> node referencing it to the <parameters> node. Also, if the last param of the callback is a gpointer, rename it from "arg#" to "data" so it will be treated correctly (as the user data passed from the calling method). [Fixes #66241] * art/art-api.raw: * gdk/gdk-api-2.4.raw: * gdk/gdk-api-2.6.raw: Regen * generator/Parameters.cs (IsHidden): loosen the definition of hideable user_data; it doesn't have to occur at the end of the parameter list, as long as there's a callback arg before it. * generator/MethodBody.cs (GetCallString): Use Parameters.IsHidden to decide whether or not to squash user_data params, rather than trying to duplicate its logic. As a side effect, this also causes a handful of methods that take non-hidden IntPtr arguments to start actually passing those arguments to C rather than always passing IntPtr.Zero. * generator/Method.cs (Equals, GetHashCode): Remove unnecessary and possibly erroneous hashing overrides. * gtk/Gtk.metadata: Hide Gtk.Container.ForeachFull, since it's useless and wasn't in gtk# 1.0 * gtk/Menu.custom (Popup): * gtk/TextIter.custom (ForwardFindChar, BackwardFindChar): * gnome/App.custom (CreateMenusInterp, InsertMenusInterp, CreateToolbarInterp): * gnome/Client.custom (RequestInteractionInterp): * gnome/Popup.custom (MenuDoPopupModal, MenuDoPopup): Add [Obsolete] compat overloads for methods that have now lost a useless IntPtr. svn path=/trunk/gtk-sharp/; revision=47566
2005-07-22 19:10:04 +00:00
(i == 0 || parameters [i - 1].Scope != "notified")) {
call_parm = "IntPtr.Zero";
}
result [i] += call_parm;
}
return String.Join (", ", result);
}
public void Initialize (GenerationInfo gen_info)
{
Initialize (gen_info, false, false, String.Empty);
}
public void Initialize (GenerationInfo gen_info, bool is_get, bool is_set, string indent)
{
if (parameters.Count == 0)
return;
StreamWriter sw = gen_info.Writer;
for (int i = 0; i < parameters.Count; i++) {
Parameter p = parameters [i];
IGeneratable gen = p.Generatable;
string name = p.Name;
if (is_set)
name = "value";
p.CallName = name;
foreach (string prep in p.Prepare)
sw.WriteLine (indent + "\t\t\t" + prep);
if (gen is CallbackGen) {
CallbackGen cbgen = gen as CallbackGen;
string wrapper = cbgen.GenWrapper(gen_info);
switch (p.Scope) {
* generator/Parameters.cs (Parameters.Validate): If the parameters end with "callback, gpointer, GDestroyNotify", then mark the callback as having "notified" Scope. (Parameters.IsHidden): Hide user_data and GDestroyNotify after a callback. (Parameter.Scope): make this settable (Parameter.IsDestroyNotify): new test * generator/MethodBody.cs (Initialize): Handle "notified" callback scope (using a GCHandle and GLib.DestroyHelper.NotifyHandler) * generator/CallbackGen.cs (GenWrapper): Add a static "GetManagedDelegate" method to the wrapper type, to translate a native delegate back to its corresponding managed delegate. (FromNative): use GetManagedDelegate. * generator/ReturnValue.cs (Validate): We handle callback return values now * generator/SymbolTable.cs: marshal GDestroyNotify as GLib.DestroyNotify * glib/DestroyNotify.cs: Moved from gtk * gtk/Gtk.metadata: globally change GtkDestroyNotify to GDestroyNotify, but then change back the ones that are exposed in the API. Un-hide lots of methods we can correctly autogenerate now. * gtk/DestroyHelper.cs: moved to glib * gtk/*.custom: remove methods that are autogenerated now, add Obsolete wrappers where needed, replace Gtk.DestroyHelper usage with GLib.DestroyHelper. * gdk/Gdk.metadata: * gnome/Gnome.metadata: Turn Gdk.Drawable.SetData and Gnome.IconList.SetIconDataFull's GDestroyNotify args into gpointers so the generated API stays the same as it used to be. * rsvg/Handle.custom: implement deprecated SetSizeCallback * sample/GtkDemo/DemoIconView.cs (CreateSort): update for API changes svn path=/trunk/gtk-sharp/; revision=44020
2005-05-04 11:47:25 +00:00
case "notified":
sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper;", wrapper, name);
sw.WriteLine (indent + "\t\t\tIntPtr {0};", parameters [i + 1].Name);
sw.WriteLine (indent + "\t\t\t{0} {1};", parameters [i + 2].CSType, parameters [i + 2].Name);
sw.WriteLine (indent + "\t\t\tif ({0} == null) {{", name);
sw.WriteLine (indent + "\t\t\t\t{0}_wrapper = null;", name);
sw.WriteLine (indent + "\t\t\t\t{0} = IntPtr.Zero;", parameters [i + 1].Name);
sw.WriteLine (indent + "\t\t\t\t{0} = null;", parameters [i + 2].Name);
sw.WriteLine (indent + "\t\t\t} else {");
sw.WriteLine (indent + "\t\t\t\t{0}_wrapper = new {1} ({0});", name, wrapper);
sw.WriteLine (indent + "\t\t\t\t{0} = (IntPtr) GCHandle.Alloc ({1}_wrapper);", parameters [i + 1].Name, name);
sw.WriteLine (indent + "\t\t\t\t{0} = GLib.DestroyHelper.NotifyHandler;", parameters [i + 2].Name, parameters [i + 2].CSType);
* generator/Parameters.cs (Parameters.Validate): If the parameters end with "callback, gpointer, GDestroyNotify", then mark the callback as having "notified" Scope. (Parameters.IsHidden): Hide user_data and GDestroyNotify after a callback. (Parameter.Scope): make this settable (Parameter.IsDestroyNotify): new test * generator/MethodBody.cs (Initialize): Handle "notified" callback scope (using a GCHandle and GLib.DestroyHelper.NotifyHandler) * generator/CallbackGen.cs (GenWrapper): Add a static "GetManagedDelegate" method to the wrapper type, to translate a native delegate back to its corresponding managed delegate. (FromNative): use GetManagedDelegate. * generator/ReturnValue.cs (Validate): We handle callback return values now * generator/SymbolTable.cs: marshal GDestroyNotify as GLib.DestroyNotify * glib/DestroyNotify.cs: Moved from gtk * gtk/Gtk.metadata: globally change GtkDestroyNotify to GDestroyNotify, but then change back the ones that are exposed in the API. Un-hide lots of methods we can correctly autogenerate now. * gtk/DestroyHelper.cs: moved to glib * gtk/*.custom: remove methods that are autogenerated now, add Obsolete wrappers where needed, replace Gtk.DestroyHelper usage with GLib.DestroyHelper. * gdk/Gdk.metadata: * gnome/Gnome.metadata: Turn Gdk.Drawable.SetData and Gnome.IconList.SetIconDataFull's GDestroyNotify args into gpointers so the generated API stays the same as it used to be. * rsvg/Handle.custom: implement deprecated SetSizeCallback * sample/GtkDemo/DemoIconView.cs (CreateSort): update for API changes svn path=/trunk/gtk-sharp/; revision=44020
2005-05-04 11:47:25 +00:00
sw.WriteLine (indent + "\t\t\t}");
break;
case "async":
sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name);
sw.WriteLine (indent + "\t\t\t{0}_wrapper.PersistUntilCalled ();", name);
break;
case "call":
default:
if (p.Scope == String.Empty)
Console.WriteLine ("Defaulting " + gen.Name + " param to 'call' scope in method " + gen_info.CurrentMember);
2005-05-02 20:10:03 +00:00
sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name);
break;
}
}
}
if (ThrowsException)
sw.WriteLine (indent + "\t\t\tIntPtr error = IntPtr.Zero;");
}
public void InitAccessor (StreamWriter sw, Signature sig, string indent)
{
sw.WriteLine (indent + "\t\t\t" + sig.AccessorType + " " + sig.AccessorName + ";");
}
public void Finish (StreamWriter sw, string indent)
{
foreach (Parameter p in parameters)
foreach (string s in p.Finish)
sw.WriteLine(indent + "\t\t\t" + s);
}
public void FinishAccessor (StreamWriter sw, Signature sig, string indent)
{
sw.WriteLine (indent + "\t\t\treturn " + sig.AccessorName + ";");
}
public void HandleException (StreamWriter sw, string indent)
{
if (!ThrowsException)
return;
sw.WriteLine (indent + "\t\t\tif (error != IntPtr.Zero) throw new GLib.GException (error);");
}
public bool ThrowsException {
get {
int idx = parameters.Count - 1;
while (idx >= 0) {
if (parameters [idx].IsUserData)
idx--;
else if (parameters [idx].CType == "GError**")
return true;
else
break;
}
return false;
}
}
}
}