1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
using System;
using System.Runtime.InteropServices;
using GLib;
internal static class GTypeExtensions
{
public static IntPtr ValFromInstancePtr (IntPtr handle)
{
if (handle == IntPtr.Zero)
return IntPtr.Zero;
// First field of instance is a GTypeClass*.
IntPtr klass = Marshal.ReadIntPtr (handle);
// First field of GTypeClass is a GType.
return Marshal.ReadIntPtr (klass);
}
public static bool IsInstance (GType type, IntPtr raw)
{
return Is (ValFromInstancePtr (raw), type);
}
public static bool Is (IntPtr type, GType is_a_type)
{
return g_type_is_a (type, is_a_type.Val);
}
public static IntPtr GetClassPtr (GType type)
{
IntPtr klass = g_type_class_peek (type.Val);
if (klass == IntPtr.Zero)
klass = g_type_class_ref (type.Val);
return klass;
}
public static GType GetThresholdType (GType type)
{
GLib.GType curr_type = type;
while (curr_type.ToString ().StartsWith ("__gtksharp_")) {
curr_type = GetBaseType (curr_type);
}
return curr_type;
}
public static uint GetClassSize (GType type)
{
GTypeQuery query;
g_type_query (type.Val, out query);
return query.class_size;
}
public static GType GetBaseType (GType type)
{
IntPtr parent = g_type_parent (type.Val);
if (parent == IntPtr.Zero)
return GType.None;
else
return new GType (parent);
}
struct GTypeQuery {
public IntPtr type;
public IntPtr type_name;
public uint class_size;
public uint instance_size;
}
[DllImport ("libgobject-2.0-0.dll")]
static extern bool g_type_is_a (IntPtr type, IntPtr is_a_type);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_class_peek (IntPtr gtype);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_class_ref (IntPtr gtype);
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_type_parent (IntPtr type);
[DllImport("libgobject-2.0-0.dll")]
static extern void g_type_query (IntPtr type, out GTypeQuery query);
}
|