forked from loongly/PureScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStore.wrapper.cs
More file actions
84 lines (70 loc) · 1.8 KB
/
Copy pathObjectStore.wrapper.cs
File metadata and controls
84 lines (70 loc) · 1.8 KB
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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
public interface IWObject
{
IntPtr Handle { get; }
}
public class WObject : IWObject
{
private IntPtr _handle;
public IntPtr Handle { get { return _handle; } }
public void SetHandle(IntPtr handle)
{
_handle = handle;
}
protected virtual Type GetWType() { return typeof(WObject); }
}
public static class WObjectExtend
{
public static IntPtr __GetHandle(this WObject obj)
{
if (object.ReferenceEquals(obj , null) )
return IntPtr.Zero;
return obj.Handle;
}
}
public static class ObjectStore
{
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object GetObject(IntPtr ptr);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IntPtr StoreObject(object obj, IntPtr ptr);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object OnException(Exception e);
/*
public static int Store(object obj)
{
}
*/
public static IntPtr Store(WObject obj,IntPtr handle)
{
if (obj == null)
return IntPtr.Zero;
return StoreObject(obj,handle);
}
public static T Get<T>(IntPtr handle)
where T : WObject
{
if (handle == IntPtr.Zero)
return null;
var obj = GetObject(handle);
return obj as T;
}
public static void RefMember(WObject obj, ref GCHandle handle,object member)
{
if(member == null)
{
if (handle.IsAllocated)
handle.Free();
}
else
{
if (!handle.IsAllocated)
handle = GCHandle.Alloc(obj);
}
}
}