private void GenGlueVirtualMethod (GenerationInfo gen_info) { StreamWriter glue = gen_info.GlueWriter; string glue_name = String.Format ("{0}sharp_{1}_base_{2}", container_type.NS.ToLower ().Replace (".", "_"), container_type.Name.ToLower (), ClassFieldName); glue.WriteLine ("{0} {1} ({2});\n", retval.CType, glue_name, GlueSignature); glue.WriteLine ("{0}\n{1} ({2})", retval.CType, glue_name, GlueSignature); glue.WriteLine ("{"); glue.WriteLine ("\t{0}Class *klass = ({0}Class *) get_threshold_class (G_OBJECT ({1}));", container_type.CName, parms[0].Name); glue.Write ("\tif (klass->{0})\n\t\t", ClassFieldName); if (!IsVoid) glue.Write ("return "); glue.Write ("(* klass->{0}) (", ClassFieldName); for (int i = 0; i < parms.Count; i++) glue.Write (parms[i].Name + (i == parms.Count - 1 ? "" : ", ")); glue.WriteLine (");"); if (!IsVoid) glue.WriteLine ("\treturn " + DefaultGlueValue + ";"); glue.WriteLine ("}"); StreamWriter sw = gen_info.Writer; sw.WriteLine ("\t\t[DllImport (\"{0}\")]", gen_info.GluelibName); sw.WriteLine ("\t\tstatic extern {0} {1} ({2});\n", retval.MarshalType, glue_name, parms.ImportSignature); GenVMDeclaration (sw, null); sw.WriteLine ("\t\t{"); MethodBody body = new MethodBody (parms); body.Initialize (gen_info, false, false, String.Empty); sw.WriteLine ("\t\t\t{0}{1} ({2});", IsVoid ? "" : retval.MarshalType + " __ret = ", glue_name, GlueCallString); body.Finish (sw, ""); if (!IsVoid) sw.WriteLine ("\t\t\treturn {0};", retval.FromNative ("__ret")); sw.WriteLine ("\t\t}\n"); }
public string GenWrapper (GenerationInfo gen_info) { string wrapper = Name + "Native"; string qualname = MarshalType; if (!Validate ()) return String.Empty; body = new MethodBody (parms); StreamWriter save_sw = gen_info.Writer; StreamWriter sw = gen_info.Writer = gen_info.OpenStream (qualname, NS); sw.WriteLine ("namespace " + NS + "Sharp {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); sw.WriteLine ("\t[UnmanagedFunctionPointer (CallingConvention.Cdecl)]"); sw.WriteLine ("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + parms.ImportSignature + ");"); sw.WriteLine (); GenInvoker (gen_info, sw); sw.WriteLine ("\tinternal class " + Name + "Wrapper {"); sw.WriteLine (); ManagedCallString call = new ManagedCallString (parms); sw.WriteLine ("\t\tpublic " + retval.MarshalType + " NativeCallback (" + parms.ImportSignature + ")"); sw.WriteLine ("\t\t{"); string unconditional = call.Unconditional ("\t\t\t"); if (unconditional.Length > 0) sw.WriteLine (unconditional); sw.WriteLine ("\t\t\ttry {"); 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 ();"); 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"); 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; 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}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); 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 (); sw.WriteLine ("\t\tinternal " + wrapper + " NativeDelegate;"); sw.WriteLine ("\t\t" + NS + "." + Name + " managed;"); sw.WriteLine (); sw.WriteLine ("\t\tpublic " + Name + "Wrapper (" + NS + "." + Name + " managed)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tthis.managed = managed;"); sw.WriteLine ("\t\t\tif (managed != null)"); sw.WriteLine ("\t\t\t\tNativeDelegate = new " + wrapper + " (NativeCallback);"); sw.WriteLine ("\t\t}"); 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}"); sw.WriteLine ("\t}"); sw.WriteLine ("#endregion"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = save_sw; return NS + "Sharp." + Name + "Wrapper"; }
public string GenWrapper(GenerationInfo gen_info) { string wrapper = Name + "Native"; string qualname = MarshalType; if (!Validate()) { return(String.Empty); } body = new MethodBody(parms); StreamWriter save_sw = gen_info.Writer; StreamWriter sw = gen_info.Writer = gen_info.OpenStream(qualname); sw.WriteLine("namespace " + NS + "Sharp {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); sw.WriteLine("#region Autogenerated code"); sw.WriteLine("\t[GLib.CDeclCallback]"); sw.WriteLine("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + parms.ImportSignature + ");"); sw.WriteLine(); GenInvoker(gen_info, sw); sw.WriteLine("\tinternal class " + Name + "Wrapper {"); sw.WriteLine(); ManagedCallString call = new ManagedCallString(parms, false); sw.WriteLine("\t\tpublic " + retval.MarshalType + " NativeCallback (" + parms.ImportSignature + ")"); sw.WriteLine("\t\t{"); string unconditional = call.Unconditional("\t\t\t"); if (unconditional.Length > 0) { sw.WriteLine(unconditional); } sw.WriteLine("\t\t\ttry {"); 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 ();"); 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; 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}"); sw.WriteLine("\t\t}"); sw.WriteLine(); 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(); sw.WriteLine("\t\tinternal " + wrapper + " NativeDelegate;"); sw.WriteLine("\t\t" + NS + "." + Name + " managed;"); sw.WriteLine(); sw.WriteLine("\t\tpublic " + Name + "Wrapper (" + NS + "." + Name + " managed)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tthis.managed = managed;"); sw.WriteLine("\t\t\tif (managed != null)"); sw.WriteLine("\t\t\t\tNativeDelegate = new " + wrapper + " (NativeCallback);"); sw.WriteLine("\t\t}"); 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}"); sw.WriteLine("\t}"); sw.WriteLine("#endregion"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = save_sw; return(NS + "Sharp." + Name + "Wrapper"); }