[Gtk-sharp-list] GTypeInstances other than GObject (needed for gst-sharp work I'm doing)

Peter Johanson latexer at gentoo.org
Sun Mar 5 14:20:28 EST 2006


hey folks,

So unbeknownst to me before, about a week and half ago, libgobject
actually provides a mechanism for folks to create their own base object
types other than GObject, via GTypeInstance:

<snip from gtype.h>
typedef struct _GTypeClass              GTypeClass;
typedef struct _GTypeInstance           GTypeInstance;
.
.
.
/* Basic Type Structures
 */
struct _GTypeClass
{ 
  /*< private >*/
  GType g_type;                                                            
};                                                                         
struct _GTypeInstance
{
  /*< private >*/
  GTypeClass *g_class;
};
</snip>

And lo and behold, both GObject, and gstreamer's recently discovered
"GstMiniObject" are GTypeInstances. GstMiniObject is used as a
lightweight object base for a few types in gstreamer, GstMessage,
GstBuffer, GstEvent, and GstQuery (so far).

Why should gtk-sharp-list care? Well, GAPI2 needs some changes in order
to properly generate code for classes with base GTypeInstance *other*
than GObject. Mainly, there are places where "GLib.Object.GetObject (raw)"
really needs to be "Gst.MiniObject.GetObject (raw)" as I've had to
duplicate a lot of the GLib.Object logic for object instantiation,
ref-ing, etc in Gst.MiniObject.

The attached patch is the *wrong* way to do this in GAPI2, but I needed
something to get to the 'is this actually gonna work' stage with my
gst-sharp work.

With that patch applied, my current work so far on gst-sharp (found in
a bazaar branch for now, that's not going to be the final development
home for this hopefully) found here:
http://www.peterjohanson.com/bazaar/gst-sharp/gst-sharp--dev--0.10/ will
compile, and the small sample runs with no issues. Folks without bazaar
can get a snapshot tarball here:
http://www.peterjohanson.com/files/gst-sharp-0.9.5.99.tar.gz

So, after a long and drawn out email: 

What is the best way to go about providing this functionality in GAPI2?
Is this beyond what should be offered? Some ideas from me where a
"IGTypeInstance" interface in glib-sharp that both GObject and other
GTypeInstances would implement, but one can't specify the static
"GetObject" method there, so that's not perfect.

Thoughts?

-pete

-- 
Peter Johanson
<latexer at gentoo.org>
-------------- next part --------------
Index: generator/Parser.cs
===================================================================
--- generator/Parser.cs	(revision 57530)
+++ generator/Parser.cs	(working copy)
@@ -142,13 +142,17 @@
 		{
 			string type = symbol.GetAttribute ("type");
 			string cname = symbol.GetAttribute ("cname");
+			string from_fmt = symbol.GetAttribute ("from_fmt");
 			string name = symbol.GetAttribute ("name");
 			IGeneratable result = null;
 
 			if (type == "simple")
 				result = new SimpleGen (cname, name);
 			else if (type == "manual")
-				result = new ManualGen (cname, name);
+				if (from_fmt == "")
+					result = new ManualGen (cname, name);
+				else
+					result = new ManualGen (cname, name, from_fmt);
 			else if (type == "alias")
 				result = new AliasGen (cname, name);
 			else if (type == "marshal") {
Index: generator/ObjectBase.cs
===================================================================
--- generator/ObjectBase.cs	(revision 57530)
+++ generator/ObjectBase.cs	(working copy)
@@ -27,10 +27,19 @@
 	public abstract class ObjectBase : HandleBase {
 
 		protected ObjectBase (XmlElement ns, XmlElement elem) : base (ns, elem) {}
+
+		public string BaseGTypeInstance {
+			get {
+				string base_gtype_instance = Elem.GetAttribute("base_gtype_instance");
+
+				return base_gtype_instance != "" ? base_gtype_instance : "GLib.Object";
+			}
+		}
+
 					
 		public override string FromNative (string var, bool owned)
 		{
-			return "GLib.Object.GetObject(" + var + (owned ? ", true" : "") + ") as " + QualifiedName;
+			return BaseGTypeInstance + ".GetObject(" + var + (owned ? ", true" : "") + ") as " + QualifiedName;
 		}
 	}
 }
Index: generator/Signal.cs
===================================================================
--- generator/Signal.cs	(revision 57530)
+++ generator/Signal.cs	(working copy)
@@ -224,7 +224,14 @@
 					finish += "\t\t\targ" + idx + " = " + igen.ToNativeReturn ("((" + p.CSType + ")args.Args[" + (idx - 1) + "])") + ";\n";
 			}
 			sw.WriteLine("\t\t\t{0} handler = ({0}) sig.Handler;", EventHandlerQualifiedName);
-			sw.WriteLine("\t\t\thandler (GLib.Object.GetObject (arg0), args);");
+			//sw.WriteLine("\t\t\thandler (GLib.Object.GetObject (arg0), args);");
+			ObjectBase obj_container_type = container_type as ObjectBase;
+			string base_gtype_instance =
+				obj_container_type != null ?
+				obj_container_type.BaseGTypeInstance :
+				"GLib.Object";
+
+			sw.WriteLine("\t\t\thandler (" + base_gtype_instance + ".GetObject (arg0), args);");
 			sw.WriteLine (finish);
 			if (!IsVoid) {
 				sw.WriteLine ("\t\t\tif (args.RetVal == null)");
@@ -352,7 +359,14 @@
 			sw.WriteLine ("\t\tstatic {0} {1};\n", Name + "VMDelegate", Name + "VMCallback");
 			sw.WriteLine ("\t\tstatic " + retval.ToNativeType + " " + Name.ToLower() + "_cb (" + isig.ToString () + ")");
 			sw.WriteLine ("\t\t{");
-			sw.WriteLine ("\t\t\t{0} {1}_managed = GLib.Object.GetObject ({1}, false) as {0};", implementor != null ? implementor.Name : container_type.Name, parms[0].Name);
+			//sw.WriteLine ("\t\t\t{0} {1}_managed = GLib.Object.GetObject ({1}, false) as {0};", implementor != null ? implementor.Name : container_type.Name, parms[0].Name);
+			ObjectBase obj_container_type = container_type as ObjectBase;
+			string base_gtype_instance =
+				obj_container_type != null ?
+				obj_container_type.BaseGTypeInstance :
+				"GLib.Object";
+
+			sw.WriteLine ("\t\t\t{0} {1}_managed = " + base_gtype_instance + ".GetObject ({1}, false) as {0};", implementor != null ? implementor.Name : container_type.Name, parms[0].Name);
 			sw.Write (call.Setup ("\t\t\t"));
 			sw.Write ("\t\t\t{0}", IsVoid ? "" : retval.CSType == retval.ToNativeType ? "return " : retval.CSType + " raw_ret = ");
 			sw.WriteLine ("{2}_managed.{0} ({1});", "On" + Name, call.ToString (), parms[0].Name);
Index: generator/VirtualMethod.cs
===================================================================
--- generator/VirtualMethod.cs	(revision 57530)
+++ generator/VirtualMethod.cs	(working copy)
@@ -33,10 +33,12 @@
 		ReturnValue retval;
 		Parameters parms;
 		ImportSignature isig;
+		ClassBase container_type;
 
 		public VirtualMethod (XmlElement elem, ClassBase container_type) 
 		{
 			this.elem = elem;
+			this.container_type = container_type;
 			retval = new ReturnValue (elem ["return-type"]);
 			parms = new Parameters (elem["parameters"]);
 			isig = new ImportSignature (parms);
@@ -79,9 +81,15 @@
 			string type = parms [0].CSType;
 			string name = parms [0].Name;
 			string call_string = "__obj." + Name + " (" + call + ")";
+			ObjectBase obj_container_type = container_type as ObjectBase;
+			string base_gtype_instance =
+				obj_container_type != null ?
+				obj_container_type.BaseGTypeInstance :
+				"GLib.Object";
+
 			sw.WriteLine ("\t\tstatic " + MarshalReturnType + " " + Name + "Callback (" + isig + ")");
 			sw.WriteLine ("\t\t{");
-			sw.WriteLine ("\t\t\t" + type + " __obj = GLib.Object.GetObject (" + name + ", false) as " + type + ";");
+			sw.WriteLine ("\t\t\t" + type + " __obj = " + base_gtype_instance + ".GetObject (" + name + ", false) as " + type + ";");
 			sw.Write (call.Setup ("\t\t\t"));
 			if (retval.CSType == "void")
 				sw.WriteLine ("\t\t\t" + call_string + ";");


More information about the Gtk-sharp-list mailing list