Skip to content

Instantly share code, notes, and snippets.

@tritao
Created April 25, 2019 13:21
Show Gist options
  • Save tritao/58eb1d5d00655165c0a09c870d591e8e to your computer and use it in GitHub Desktop.
Save tritao/58eb1d5d00655165c0a09c870d591e8e to your computer and use it in GitHub Desktop.
// DEBUG: struct DLL_API MyFlags
// DEBUG: {
// DEBUG: MyFlags() {}
// DEBUG: MyFlags(const MyFlags& other) : mBits(other.mBits) {}
// DEBUG: uint16_t mBits;
// DEBUG: }
public unsafe partial class MyFlags : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 2)]
public partial struct __Internal
{
[FieldOffset(0)]
internal ushort mBits;
[SuppressUnmanagedCodeSecurity]
[DllImport("Common.Native.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="??0MyFlags@@QEAA@XZ")]
internal static extern global::System.IntPtr ctor(global::System.IntPtr __instance);
[SuppressUnmanagedCodeSecurity]
[DllImport("Common.Native.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="??0MyFlags@@QEAA@AEBU0@@Z")]
internal static extern global::System.IntPtr cctor(global::System.IntPtr __instance, global::System.IntPtr other);
}
public global::System.IntPtr __Instance { get; protected set; }
protected int __PointerAdjustment;
internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::CommonTest.MyFlags> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, global::CommonTest.MyFlags>();
protected internal void*[] __OriginalVTables;
protected bool __ownsNativeInstance;
internal static global::CommonTest.MyFlags __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
{
return new global::CommonTest.MyFlags(native.ToPointer(), skipVTables);
}
internal static global::CommonTest.MyFlags __CreateInstance(global::CommonTest.MyFlags.__Internal native, bool skipVTables = false)
{
return new global::CommonTest.MyFlags(native, skipVTables);
}
private static void* __CopyValue(global::CommonTest.MyFlags.__Internal native)
{
var ret = Marshal.AllocHGlobal(sizeof(global::CommonTest.MyFlags.__Internal));
global::CommonTest.MyFlags.__Internal.cctor(ret, new global::System.IntPtr(&native));
return ret.ToPointer();
}
private MyFlags(global::CommonTest.MyFlags.__Internal native, bool skipVTables = false)
: this(__CopyValue(native), skipVTables)
{
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
}
protected MyFlags(void* native, bool skipVTables = false)
{
if (native == null)
return;
__Instance = new global::System.IntPtr(native);
}
// DEBUG: MyFlags() {}
public MyFlags()
{
__Instance = Marshal.AllocHGlobal(sizeof(global::CommonTest.MyFlags.__Internal));
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
__Internal.ctor((__Instance + __PointerAdjustment));
}
// DEBUG: MyFlags(const MyFlags& other) : mBits(other.mBits) {}
public MyFlags(global::CommonTest.MyFlags other)
{
__Instance = Marshal.AllocHGlobal(sizeof(global::CommonTest.MyFlags.__Internal));
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
if (ReferenceEquals(other, null))
throw new global::System.ArgumentNullException("other", "Cannot be null because it is a C++ reference (&).");
var __arg0 = other.__Instance;
__Internal.cctor((__Instance + __PointerAdjustment), __arg0);
}
public void Dispose()
{
Dispose(disposing: true);
}
public virtual void Dispose(bool disposing)
{
if (__Instance == IntPtr.Zero)
return;
global::CommonTest.MyFlags __dummy;
NativeToManagedMap.TryRemove(__Instance, out __dummy);
if (__ownsNativeInstance)
Marshal.FreeHGlobal(__Instance);
__Instance = IntPtr.Zero;
}
public ushort MBits
{
get
{
return ((global::CommonTest.MyFlags.__Internal*) __Instance)->mBits;
}
set
{
((global::CommonTest.MyFlags.__Internal*)__Instance)->mBits = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment