2003-05-19 09:18:52 +02:00
|
|
|
// GLib.ManagedValue.cs : Managed types boxer
|
|
|
|
//
|
|
|
|
// Author: Rachel Hestilow <hestilow@ximian.com>
|
|
|
|
//
|
2004-06-25 20:42:19 +02:00
|
|
|
// 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.
|
|
|
|
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2004-05-28 18:59:21 +02:00
|
|
|
namespace GLib {
|
2003-05-19 09:18:52 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Runtime.InteropServices;
|
2003-12-15 17:59:25 +01:00
|
|
|
using GLib;
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2004-05-28 18:59:21 +02:00
|
|
|
internal class ManagedValue {
|
2008-11-21 18:42:16 +01:00
|
|
|
|
|
|
|
GCHandle gch;
|
|
|
|
object instance;
|
|
|
|
int ref_count = 1;
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2008-11-21 18:42:16 +01:00
|
|
|
private ManagedValue (object instance)
|
|
|
|
{
|
|
|
|
this.instance = instance;
|
|
|
|
gch = GCHandle.Alloc (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntPtr Handle {
|
|
|
|
get { return (IntPtr) gch; }
|
|
|
|
}
|
|
|
|
|
|
|
|
object Instance {
|
|
|
|
get { return instance; }
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ref ()
|
|
|
|
{
|
|
|
|
ref_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unref ()
|
|
|
|
{
|
|
|
|
if (--ref_count == 0) {
|
|
|
|
instance = null;
|
|
|
|
gch.Free ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-02 22:18:10 +02:00
|
|
|
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
|
2005-03-23 21:35:15 +01:00
|
|
|
delegate IntPtr CopyFunc (IntPtr gch);
|
2009-07-02 22:18:10 +02:00
|
|
|
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
|
2005-03-23 21:35:15 +01:00
|
|
|
delegate void FreeFunc (IntPtr gch);
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2005-03-23 21:35:15 +01:00
|
|
|
static CopyFunc copy;
|
|
|
|
static FreeFunc free;
|
|
|
|
static GType boxed_type = GType.Invalid;
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2003-05-23 01:39:04 +02:00
|
|
|
[DllImport("libgobject-2.0-0.dll")]
|
2005-03-23 21:35:15 +01:00
|
|
|
static extern IntPtr g_boxed_type_register_static (IntPtr typename, CopyFunc copy_func, FreeFunc free_func);
|
2003-05-19 09:18:52 +02:00
|
|
|
|
2003-12-15 17:59:25 +01:00
|
|
|
public static GType GType {
|
2003-05-19 09:18:52 +02:00
|
|
|
get {
|
2003-12-15 17:59:25 +01:00
|
|
|
if (boxed_type == GType.Invalid) {
|
2003-05-19 09:18:52 +02:00
|
|
|
copy = new CopyFunc (Copy);
|
|
|
|
free = new FreeFunc (Free);
|
|
|
|
|
2005-03-08 22:28:08 +01:00
|
|
|
IntPtr name = Marshaller.StringToPtrGStrdup ("GtkSharpValue");
|
|
|
|
boxed_type = new GLib.GType (g_boxed_type_register_static (name, copy, free));
|
|
|
|
Marshaller.Free (name);
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return boxed_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-21 18:42:16 +01:00
|
|
|
static ManagedValue FromHandle (IntPtr ptr)
|
|
|
|
{
|
|
|
|
GCHandle gch = (GCHandle) ptr;
|
|
|
|
ManagedValue val = gch.Target as ManagedValue;
|
|
|
|
if (val == null)
|
|
|
|
throw new Exception ("Unexpected GCHandle received.");
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2005-03-23 21:35:15 +01:00
|
|
|
static IntPtr Copy (IntPtr ptr)
|
2003-05-19 09:18:52 +02:00
|
|
|
{
|
2007-03-08 21:28:24 +01:00
|
|
|
try {
|
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
return ptr;
|
2008-11-21 18:42:16 +01:00
|
|
|
ManagedValue val = FromHandle (ptr);
|
|
|
|
val.Ref ();
|
|
|
|
return ptr;
|
2007-03-08 21:28:24 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionManager.RaiseUnhandledException (e, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return IntPtr.Zero;
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
|
|
|
|
2005-03-23 21:35:15 +01:00
|
|
|
static void Free (IntPtr ptr)
|
2003-05-19 09:18:52 +02:00
|
|
|
{
|
2007-03-08 21:28:24 +01:00
|
|
|
try {
|
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
return;
|
2008-11-21 18:42:16 +01:00
|
|
|
ManagedValue val = FromHandle (ptr);
|
|
|
|
val.Unref ();
|
2007-03-08 21:28:24 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
ExceptionManager.RaiseUnhandledException (e, false);
|
|
|
|
}
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static IntPtr WrapObject (object obj)
|
|
|
|
{
|
2005-03-31 18:31:31 +02:00
|
|
|
if (obj == null)
|
|
|
|
return IntPtr.Zero;
|
2008-11-21 18:42:16 +01:00
|
|
|
return new ManagedValue (obj).Handle;
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static object ObjectForWrapper (IntPtr ptr)
|
|
|
|
{
|
2005-03-31 18:31:31 +02:00
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
return null;
|
2008-11-21 18:42:16 +01:00
|
|
|
ManagedValue val = FromHandle (ptr);
|
|
|
|
return val == null ? null : val.Instance;
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
2007-06-05 21:53:34 +02:00
|
|
|
|
|
|
|
public static void ReleaseWrapper (IntPtr ptr)
|
|
|
|
{
|
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
return;
|
|
|
|
|
2008-11-21 18:42:16 +01:00
|
|
|
ManagedValue val = FromHandle (ptr);
|
|
|
|
val.Unref ();
|
2007-06-05 21:53:34 +02:00
|
|
|
}
|
2003-05-19 09:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|