This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 638
/
CppObject.cs
190 lines (171 loc) · 6.97 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
// 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;
namespace SharpDX
{
/// <summary>
/// Root class for all Cpp interop object.
/// </summary>
public class CppObject : DisposeBase, ICallbackable
{
/// <summary>
/// The native pointer
/// </summary>
protected internal unsafe void* _nativePointer;
/// <summary>
/// Gets or sets a custom user tag object to associate with this instance..
/// </summary>
/// <value>The tag object.</value>
public object Tag { get; set; }
/// <summary>
/// Default constructor.
/// </summary>
/// <param name = "pointer">Pointer to Cpp Object</param>
public CppObject(IntPtr pointer)
{
NativePointer = pointer;
}
/// <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
{
get
{
unsafe
{
return (IntPtr) _nativePointer;
}
}
set
{
unsafe
{
var newNativePointer = (void*) value;
if (_nativePointer != newNativePointer)
{
NativePointerUpdating();
var oldNativePointer = _nativePointer;
_nativePointer = newNativePointer;
NativePointerUpdated((IntPtr)oldNativePointer);
}
}
}
}
/// <summary>
/// Performs an explicit conversion from <see cref="SharpDX.CppObject"/> to <see cref="System.IntPtr"/>.
/// </summary>
/// <param name="cppObject">The CPP object.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static explicit operator IntPtr(CppObject cppObject)
{
return cppObject == null ? IntPtr.Zero : cppObject.NativePointer;
}
/// <summary>
/// Initializes this instance with a pointer from a temporary object and set the pointer of the temporary
/// object to IntPtr.Zero.
/// </summary>
/// <param name="temp">The instance to get the NativePointer.</param>
protected void FromTemp(CppObject temp)
{
NativePointer = temp.NativePointer;
temp.NativePointer = IntPtr.Zero;
}
/// <summary>
/// Initializes this instance with a pointer from a temporary object and set the pointer of the temporary
/// object to IntPtr.Zero.
/// </summary>
/// <param name="temp">The instance to get the NativePointer.</param>
protected void FromTemp(IntPtr temp)
{
NativePointer = temp;
}
/// <summary>
/// Method called when <see cref="NativePointer"/> is going to be update.
/// </summary>
protected virtual void NativePointerUpdating()
{
}
/// <summary>
/// Method called when the <see cref="NativePointer"/> is updated.
/// </summary>
protected virtual void NativePointerUpdated(IntPtr oldNativePointer)
{
}
protected override void Dispose(bool disposing)
{
}
/// <summary>
/// Instantiate a ComObject from a native pointer.
/// </summary>
/// <typeparam name="T">The ComObject class that will be returned</typeparam>
/// <param name="comObjectPtr">The native pointer to a com object.</param>
/// <returns>An instance of T binded to the native pointer</returns>
public static T FromPointer<T>(IntPtr comObjectPtr) where T : CppObject
{
return (comObjectPtr == IntPtr.Zero) ? null : (T) Activator.CreateInstance(typeof (T), comObjectPtr);
}
internal static T FromPointerUnsafe<T>(IntPtr comObjectPtr)
{
return (comObjectPtr == IntPtr.Zero) ? (T)(object)null : (T)Activator.CreateInstance(typeof(T), comObjectPtr);
}
/// <summary>
/// Return the unmanaged C++ pointer from a <see cref="ICallbackable"/> instance.
/// </summary>
/// <typeparam name="TCallback">The type of the callback.</typeparam>
/// <param name="callback">The callback.</param>
/// <returns>A pointer to the unmanaged C++ object of the callback</returns>
public static IntPtr ToCallbackPtr<TCallback>(ICallbackable callback)
where TCallback : ICallbackable
{
// If callback is null, then return a null pointer
if (callback == null)
return IntPtr.Zero;
// If callback is CppObject
if (callback is CppObject)
return ((CppObject)callback).NativePointer;
// Setup the shadow container in order to support multiple inheritance
var shadowContainer = callback.Shadow as ShadowContainer;
if (shadowContainer == null)
{
shadowContainer = new ShadowContainer();
shadowContainer.Initialize(callback);
}
return shadowContainer.Find(typeof(TCallback));
}
/// <summary>
/// Implements <see cref="ICallbackable"/> but it cannot not be set.
/// This is only used to support for interop with unmanaged callback.
/// </summary>
IDisposable ICallbackable.Shadow
{
get { throw new InvalidOperationException("Invalid access to Callback. This is used internally."); }
set { throw new InvalidOperationException("Invalid access to Callback. This is used internally."); }
}
}
}