// 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 { /// /// Root class for all Cpp interop object. /// public class CppObject : DisposeBase, ICallbackable { /// /// The native pointer /// protected internal unsafe void* _nativePointer; /// /// Gets or sets a custom user tag object to associate with this instance.. /// /// The tag object. public object Tag { get; set; } /// /// Default constructor. /// /// Pointer to Cpp Object public CppObject(IntPtr pointer) { NativePointer = pointer; } /// /// Initializes a new instance of the class. /// protected CppObject() { } /// /// Get a pointer to the underlying Cpp Object /// 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); } } } } /// /// Performs an explicit conversion from to . /// /// The CPP object. /// /// The result of the conversion. /// public static explicit operator IntPtr(CppObject cppObject) { return cppObject == null ? IntPtr.Zero : cppObject.NativePointer; } /// /// Initializes this instance with a pointer from a temporary object and set the pointer of the temporary /// object to IntPtr.Zero. /// /// The instance to get the NativePointer. protected void FromTemp(CppObject temp) { NativePointer = temp.NativePointer; temp.NativePointer = IntPtr.Zero; } /// /// Initializes this instance with a pointer from a temporary object and set the pointer of the temporary /// object to IntPtr.Zero. /// /// The instance to get the NativePointer. protected void FromTemp(IntPtr temp) { NativePointer = temp; } /// /// Method called when is going to be update. /// protected virtual void NativePointerUpdating() { } /// /// Method called when the is updated. /// protected virtual void NativePointerUpdated(IntPtr oldNativePointer) { } protected override void Dispose(bool disposing) { } /// /// Instantiate a ComObject from a native pointer. /// /// The ComObject class that will be returned /// The native pointer to a com object. /// An instance of T binded to the native pointer public static T FromPointer(IntPtr comObjectPtr) where T : CppObject { return (comObjectPtr == IntPtr.Zero) ? null : (T) Activator.CreateInstance(typeof (T), comObjectPtr); } internal static T FromPointerUnsafe(IntPtr comObjectPtr) { return (comObjectPtr == IntPtr.Zero) ? (T)(object)null : (T)Activator.CreateInstance(typeof(T), comObjectPtr); } /// /// Return the unmanaged C++ pointer from a instance. /// /// The type of the callback. /// The callback. /// A pointer to the unmanaged C++ object of the callback public static IntPtr ToCallbackPtr(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)); } /// /// Implements but it cannot not be set. /// This is only used to support for interop with unmanaged callback. /// 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."); } } } }