t1 / TFDContents / Assets / Standard Assets / NativeObjectCache.cs @ 9
이력 | 보기 | 이력해설 | 다운로드 (4.52 KB)
| 1 | 3 | KTH | #if !(UNITY_WSA_10_0 && NETFX_CORE) |
|---|---|---|---|
| 2 | using System; |
||
| 3 | using System.Collections.Generic; |
||
| 4 | using System.Runtime.InteropServices; |
||
| 5 | using System.Linq; |
||
| 6 | |||
| 7 | namespace Helper |
||
| 8 | {
|
||
| 9 | public static class NativeObjectCache |
||
| 10 | {
|
||
| 11 | private static object _lock = new object(); |
||
| 12 | private static Dictionary<Type, Dictionary<IntPtr, WeakReference>> _objectCache = new Dictionary<Type, Dictionary<IntPtr, WeakReference>>(); |
||
| 13 | public static void AddObject<T>(IntPtr nativePtr, T obj) where T : class |
||
| 14 | {
|
||
| 15 | lock (_lock) |
||
| 16 | {
|
||
| 17 | Dictionary<IntPtr, WeakReference> objCache = null; |
||
| 18 | |||
| 19 | if (!_objectCache.TryGetValue(typeof(T), out objCache) || objCache == null) |
||
| 20 | {
|
||
| 21 | objCache = new Dictionary<IntPtr, WeakReference>(); |
||
| 22 | _objectCache[typeof(T)] = objCache; |
||
| 23 | } |
||
| 24 | |||
| 25 | objCache[nativePtr] = new WeakReference(obj); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public static void Flush() |
||
| 30 | {
|
||
| 31 | lock(_lock) |
||
| 32 | {
|
||
| 33 | foreach (var byType in _objectCache.ToArray()) |
||
| 34 | {
|
||
| 35 | foreach(var kvp in byType.Value.ToArray()) |
||
| 36 | {
|
||
| 37 | IDisposable disp = kvp.Value.Target as IDisposable; |
||
| 38 | if(disp != null) |
||
| 39 | {
|
||
| 40 | disp.Dispose(); |
||
| 41 | } |
||
| 42 | |||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public static void RemoveObject<T>(IntPtr nativePtr) |
||
| 49 | {
|
||
| 50 | lock (_lock) |
||
| 51 | {
|
||
| 52 | Dictionary<IntPtr, WeakReference> objCache = null; |
||
| 53 | |||
| 54 | if (!_objectCache.TryGetValue(typeof(T), out objCache) || objCache == null) |
||
| 55 | {
|
||
| 56 | objCache = new Dictionary<IntPtr, WeakReference>(); |
||
| 57 | _objectCache[typeof(T)] = objCache; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (objCache.ContainsKey(nativePtr)) |
||
| 61 | {
|
||
| 62 | objCache.Remove(nativePtr); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | public static T GetObject<T>(IntPtr nativePtr) where T : class |
||
| 68 | {
|
||
| 69 | lock (_lock) |
||
| 70 | {
|
||
| 71 | Dictionary<IntPtr, WeakReference> objCache = null; |
||
| 72 | |||
| 73 | if (!_objectCache.TryGetValue(typeof(T), out objCache) || objCache == null) |
||
| 74 | {
|
||
| 75 | objCache = new Dictionary<IntPtr, WeakReference>(); |
||
| 76 | _objectCache[typeof(T)] = objCache; |
||
| 77 | } |
||
| 78 | |||
| 79 | WeakReference reference = null; |
||
| 80 | if (objCache.TryGetValue(nativePtr, out reference)) |
||
| 81 | {
|
||
| 82 | if (reference != null) |
||
| 83 | {
|
||
| 84 | T obj = reference.Target as T; |
||
| 85 | if (obj != null) |
||
| 86 | {
|
||
| 87 | return (T)obj; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | return null; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | public static T CreateOrGetObject<T>(IntPtr nativePtr, Func<System.IntPtr,T> create) where T : class |
||
| 97 | {
|
||
| 98 | T outputValue = null; |
||
| 99 | |||
| 100 | lock (_lock) |
||
| 101 | {
|
||
| 102 | Dictionary<IntPtr, WeakReference> objCache = null; |
||
| 103 | |||
| 104 | if (!_objectCache.TryGetValue(typeof(T), out objCache) || objCache == null) |
||
| 105 | {
|
||
| 106 | objCache = new Dictionary<IntPtr, WeakReference>(); |
||
| 107 | _objectCache[typeof(T)] = objCache; |
||
| 108 | } |
||
| 109 | |||
| 110 | WeakReference reference = null; |
||
| 111 | if (objCache.TryGetValue(nativePtr, out reference)) |
||
| 112 | {
|
||
| 113 | if ((reference != null) && reference.IsAlive) |
||
| 114 | {
|
||
| 115 | outputValue = reference.Target as T; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | if (outputValue == null) |
||
| 120 | {
|
||
| 121 | if (create != null) |
||
| 122 | {
|
||
| 123 | outputValue = create(nativePtr); |
||
| 124 | objCache[nativePtr] = new WeakReference(outputValue); |
||
| 125 | } |
||
| 126 | else if(typeof(T) == typeof(System.Object)) |
||
| 127 | {
|
||
| 128 | //T is an object, so lets just pass back our IntPtr, which is an object. |
||
| 129 | outputValue = (T)(System.Object)nativePtr; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return outputValue; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | #endif |