GtkSharp/glib/ManagedValue.cs
Mike Kestner ac4cc59206 2005-03-23 Mike Kestner <mkestner@novell.com>
* gapi-cdecl-insert : a little perl script to insert modopts
	* Makefile.am : dist the new script.
	for cdecl callback delegates on win32.
	* glade/makefile.win32 : use gapi-cdecl-insert
	* glade/XML.custom : add [GLib.CDeclCallback] to RawXMLConnectFunc.
	* glib/makefile.win32 : use gapi-cdecl-insert
	* glib/CDeclCallbackAttribute.cs : new attr to tag delegates with
	that will be invoked from native code. We have to mangle the il
	with a modopt otherwise they are stdcall'd.
	* glib/ManagedValue.cs : add [GLib.CDeclCallback] to Copy/Free.
	switch to using GCHandles instead of the current IntPtr hack.

svn path=/trunk/gtk-sharp/; revision=42168
2005-03-23 20:35:15 +00:00

84 lines
2.3 KiB
C#

// GLib.ManagedValue.cs : Managed types boxer
//
// Author: Rachel Hestilow <hestilow@ximian.com>
//
// Copyright (c) 2002 Rachel Hestilow
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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 GLib {
using System;
using System.Collections;
using System.Runtime.InteropServices;
using GLib;
internal class ManagedValue {
[CDeclCallback]
delegate IntPtr CopyFunc (IntPtr gch);
[CDeclCallback]
delegate void FreeFunc (IntPtr gch);
static CopyFunc copy;
static FreeFunc free;
static GType boxed_type = GType.Invalid;
[DllImport("libgobject-2.0-0.dll")]
static extern IntPtr g_boxed_type_register_static (IntPtr typename, CopyFunc copy_func, FreeFunc free_func);
public static GType GType {
get {
if (boxed_type == GType.Invalid) {
copy = new CopyFunc (Copy);
free = new FreeFunc (Free);
IntPtr name = Marshaller.StringToPtrGStrdup ("GtkSharpValue");
boxed_type = new GLib.GType (g_boxed_type_register_static (name, copy, free));
Marshaller.Free (name);
}
return boxed_type;
}
}
static IntPtr Copy (IntPtr ptr)
{
Console.WriteLine ("Copying ManagedGValue: " + ptr);
GCHandle gch = (GCHandle) ptr;
return (IntPtr) GCHandle.Alloc (gch.Target);
}
static void Free (IntPtr ptr)
{
Console.WriteLine ("Freeing ManagedGValue: " + ptr);
GCHandle gch = (GCHandle) ptr;
gch.Free ();
}
public static IntPtr WrapObject (object obj)
{
Console.WriteLine ("Wrapping Object in ManagedGValue: " + obj);
return (IntPtr) GCHandle.Alloc (obj);
}
public static object ObjectForWrapper (IntPtr ptr)
{
Console.WriteLine ("Getting object of ManagedGValue: " + ptr);
return ((GCHandle)ptr).Target;
}
}
}