-
Notifications
You must be signed in to change notification settings - Fork 56
/
CppObject.cs
211 lines (183 loc) · 6.7 KB
/
CppObject.cs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using SharpGen.Runtime.Diagnostics;
namespace SharpGen.Runtime;
/// <summary>
/// Root class for all native interop objects.
/// </summary>
public class CppObject : DisposeBase, ICallbackable, IEquatable<CppObject>
{
/// <summary>
/// The native pointer
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IntPtr _nativePointer;
private static readonly ConditionalWeakTable<CppObject, object> TagTable = new();
/// <summary>
/// Gets or sets a custom user tag object to associate with this instance.
/// </summary>
/// <value>The tag object.</value>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public object Tag
{
get => TagTable.TryGetValue(this, out var tag) ? tag : null;
set => TagTable.Add(this, value);
}
/// <summary>
/// Default constructor.
/// </summary>
/// <param name = "pointer">Pointer to C++ object</param>
public CppObject(IntPtr pointer)
{
_nativePointer = pointer;
if (ObjectTrackerReadOnlyConfiguration.IsEnabled)
ObjectTracker.Track(this, pointer);
}
/// <summary>
/// Default constructor.
/// </summary>
/// <param name = "pointer">Pointer to C++ object</param>
public unsafe CppObject(UIntPtr pointer) : this(new IntPtr(pointer.ToPointer()))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CppObject"/> class.
/// </summary>
protected CppObject()
{
}
/// <summary>
/// Get a pointer to the underlying Cpp Object
/// </summary>
public IntPtr NativePointer
{
[MethodImpl(Utilities.MethodAggressiveOptimization)]
get => _nativePointer;
set
{
var oldNativePointer = Interlocked.Exchange(ref _nativePointer, value);
if (oldNativePointer == value)
return;
if (ObjectTrackerReadOnlyConfiguration.IsEnabled)
ObjectTracker.MigrateNativePointer(this, oldNativePointer, value);
NativePointerUpdated(oldNativePointer);
}
}
public override string ToString() => Utilities.FormatPointer(NativePointer);
public static explicit operator IntPtr(CppObject cppObject) => cppObject?.NativePointer ?? IntPtr.Zero;
/// <summary>
/// Method called when the <see cref="NativePointer"/> is updated.
/// </summary>
protected virtual void NativePointerUpdated(IntPtr oldNativePointer)
{
}
protected unsafe void* this[int index]
{
[MethodImpl(Utilities.MethodAggressiveOptimization)]
#if DEBUG
get
{
Debug.Assert(!IsDisposed);
return (*(void***) _nativePointer)[index];
}
#else
get => (*(void***) _nativePointer)[index];
#endif
}
protected unsafe void* this[uint index]
{
[MethodImpl(Utilities.MethodAggressiveOptimization)]
#if DEBUG
get
{
Debug.Assert(!IsDisposed);
return (*(void***) _nativePointer)[index];
}
#else
get => (*(void***) _nativePointer)[index];
#endif
}
protected unsafe void* this[nint index]
{
[MethodImpl(Utilities.MethodAggressiveOptimization)]
#if DEBUG
get
{
Debug.Assert(!IsDisposed);
return (*(void***) _nativePointer)[index];
}
#else
get => (*(void***) _nativePointer)[index];
#endif
}
protected unsafe void* this[nuint index]
{
[MethodImpl(Utilities.MethodAggressiveOptimization)]
#if DEBUG
get
{
Debug.Assert(!IsDisposed);
return (*(void***) _nativePointer)[index];
}
#else
get => (*(void***) _nativePointer)[index];
#endif
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected override bool IsDisposed => NativePointer == IntPtr.Zero;
protected sealed override void Dispose(bool disposing)
{
var nativePointer = NativePointer;
DisposeCore(nativePointer, disposing);
if (ObjectTrackerReadOnlyConfiguration.IsEnabled)
ObjectTracker.Untrack(this, nativePointer);
// Set pointer to null (using protected members in order to avoid callbacks).
#if DEBUG
var oldNativePointer = Interlocked.Exchange(ref _nativePointer, IntPtr.Zero);
Debug.Assert(oldNativePointer == nativePointer);
#else
Interlocked.Exchange(ref _nativePointer, IntPtr.Zero);
#endif
NativePointerUpdated(nativePointer);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="nativePointer"><see cref="NativePointer"/></param>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void DisposeCore(IntPtr nativePointer, bool disposing)
{
}
public bool Equals(CppObject other)
{
if (ReferenceEquals(null, other)) return false;
return ReferenceEquals(this, other) || _nativePointer.Equals(other._nativePointer);
}
public override bool Equals(object obj) =>
ReferenceEquals(this, obj) || obj is CppObject other && _nativePointer.Equals(other._nativePointer);
public override int GetHashCode() => _nativePointer.GetHashCode();
public static bool operator ==(CppObject left, CppObject right) => Equals(left, right);
public static bool operator !=(CppObject left, CppObject right) => !Equals(left, right);
}