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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// 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.Runtime.InteropServices;
using GLib;
internal class ManagedValue {
GCHandle gch;
object instance;
int ref_count = 1;
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 ();
}
}
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
delegate IntPtr CopyFunc (IntPtr gch);
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
delegate void FreeFunc (IntPtr gch);
static CopyFunc copy;
static FreeFunc free;
static GType boxed_type = GType.Invalid;
[DllImport (Global.GObjectNativeDll, CallingConvention = CallingConvention.Cdecl)]
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 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;
}
static IntPtr Copy (IntPtr ptr)
{
try {
if (ptr == IntPtr.Zero)
return ptr;
ManagedValue val = FromHandle (ptr);
val.Ref ();
return ptr;
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
return IntPtr.Zero;
}
static void Free (IntPtr ptr)
{
try {
if (ptr == IntPtr.Zero)
return;
ManagedValue val = FromHandle (ptr);
val.Unref ();
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
}
public static IntPtr WrapObject (object obj)
{
if (obj == null)
return IntPtr.Zero;
return new ManagedValue (obj).Handle;
}
public static object ObjectForWrapper (IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return null;
ManagedValue val = FromHandle (ptr);
return val == null ? null : val.Instance;
}
public static void ReleaseWrapper (IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return;
ManagedValue val = FromHandle (ptr);
val.Unref ();
}
}
}
|