Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@m039
Last active February 14, 2017 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m039/8d9aec6764082c16fdb55638acaac1a4 to your computer and use it in GitHub Desktop.
Save m039/8d9aec6764082c16fdb55638acaac1a4 to your computer and use it in GitHub Desktop.
Issue when using empty namespace in CppSharp
#pragma once
#include "IBar.hpp"
// Bar hosts IBar
class Bar {
public:
IBar *impl;
Bar();
void Register(IBar *impl);
int PerformGetNumber();
};
//----------------------------------------------------------------------------
// <auto-generated>
// This is autogenerated code by CppSharp.
// Do not edit this file or all your changes will be lost after re-generation.
// </auto-generated>
//----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security;
public unsafe abstract partial class IBar : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public partial struct __Internal
{
[FieldOffset(0)]
public global::System.IntPtr vptr_IBar;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN4IBarC2Ev")]
internal static extern void ctor_0(global::System.IntPtr instance);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN4IBarC2ERKS_")]
internal static extern void cctor_1(global::System.IntPtr instance, global::System.IntPtr _0);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN4IBarD2Ev")]
internal static extern void dtor_0(global::System.IntPtr instance);
}
public global::System.IntPtr __Instance { get; protected set; }
protected int __PointerAdjustment;
internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, IBar> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, IBar>();
protected void*[] __OriginalVTables;
protected bool __ownsNativeInstance;
internal static IBar __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
{
return new IBarInternal(native.ToPointer(), skipVTables);
}
internal static IBar __CreateInstance(IBar.__Internal native, bool skipVTables = false)
{
return new IBarInternal(native, skipVTables);
}
protected IBar(void* native, bool skipVTables = false)
{
if (native == null)
return;
__Instance = new global::System.IntPtr(native);
if (skipVTables)
__OriginalVTables = new void*[] { ((__Internal*) native)->vptr_IBar.ToPointer() };
else
SetupVTables(true);
}
protected IBar()
{
__Instance = Marshal.AllocHGlobal(8);
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
__Internal.ctor_0((__Instance + __PointerAdjustment));
SetupVTables(GetType().FullName == "global::IBar");
}
protected IBar(global::IBar _0)
{
__Instance = Marshal.AllocHGlobal(8);
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
if (ReferenceEquals(_0, null))
throw new global::System.ArgumentNullException("_0", "Cannot be null because it is a C++ reference (&).");
var __arg0 = _0.__Instance;
__Internal.cctor_1((__Instance + __PointerAdjustment), __arg0);
SetupVTables(GetType().FullName == "global::IBar");
}
public void Dispose()
{
Dispose(disposing: true);
}
public virtual void Dispose(bool disposing)
{
global::IBar __dummy;
NativeToManagedMap.TryRemove(__Instance, out __dummy);
((__Internal*) __Instance)->vptr_IBar = new global::System.IntPtr(__OriginalVTables[0]);
if (disposing)
{
var __slot = *(void**) ((IntPtr) __OriginalVTables[0] + 1 * 8);
if (__slot != null)
{
var ___dtor_0Delegate = (Action_IntPtr) Marshal.GetDelegateForFunctionPointer(new IntPtr(__slot), typeof(Action_IntPtr));
___dtor_0Delegate((__Instance + __PointerAdjustment));
}
else
__Internal.dtor_0((__Instance + __PointerAdjustment));
}
if (__ownsNativeInstance)
Marshal.FreeHGlobal(__Instance);
}
public abstract int Number
{
get;
}
#region Virtual table interop
// int GetNumber() = 0
private static Func_int_IntPtr _GetNumber_0DelegateInstance;
private static int _GetNumber_0DelegateHook(global::System.IntPtr instance)
{
if (!NativeToManagedMap.ContainsKey(instance))
throw new global::System.Exception("No managed instance was found");
var __target = (IBar) NativeToManagedMap[instance];
if (__target.__ownsNativeInstance)
__target.SetupVTables();
var __ret = __target.Number;
return __ret;
}
// virtual ~IBar()
private static Action_IntPtr _dtor_0DelegateInstance;
private static void _dtor_0DelegateHook(global::System.IntPtr instance)
{
if (!NativeToManagedMap.ContainsKey(instance))
throw new global::System.Exception("No managed instance was found");
var __target = (IBar) NativeToManagedMap[instance];
if (__target.__ownsNativeInstance)
__target.SetupVTables();
__target.Dispose(true);
}
private static void*[] __ManagedVTables;
private static void*[] __ManagedVTablesDtorOnly;
private static void*[] _Thunks;
private void SetupVTables(bool destructorOnly = false)
{
if (__OriginalVTables != null)
return;
var native = (__Internal*) __Instance.ToPointer();
__OriginalVTables = new void*[] { native->vptr_IBar.ToPointer() };
if (_Thunks == null)
{
_Thunks = new void*[2];
_GetNumber_0DelegateInstance += _GetNumber_0DelegateHook;
_Thunks[0] = Marshal.GetFunctionPointerForDelegate(_GetNumber_0DelegateInstance).ToPointer();
_dtor_0DelegateInstance += _dtor_0DelegateHook;
_Thunks[1] = Marshal.GetFunctionPointerForDelegate(_dtor_0DelegateInstance).ToPointer();
}
if (destructorOnly)
{
if (__ManagedVTablesDtorOnly == null)
{
__ManagedVTablesDtorOnly = new void*[1];
var vtptr = Marshal.AllocHGlobal(5 * 8);
var vfptr0 = vtptr + 2 * 8;
__ManagedVTablesDtorOnly[0] = vfptr0.ToPointer();
*(void**)(vfptr0 + -16) = *(void**)(native->vptr_IBar + -16);
*(void**)(vfptr0 + -8) = *(void**)(native->vptr_IBar + -8);
*(void**)(vfptr0 + 0) = *(void**)(native->vptr_IBar + 0);
*(void**)(vfptr0 + 8) = *(void**)(native->vptr_IBar + 8);
*(void**)(vfptr0 + 16) = _Thunks[1];
}
native->vptr_IBar = new IntPtr(__ManagedVTablesDtorOnly[0]);
}
else
{
if (__ManagedVTables == null)
{
__ManagedVTables = new void*[1];
var vtptr = Marshal.AllocHGlobal(5 * 8);
var vfptr0 = vtptr + 2 * 8;
__ManagedVTables[0] = vfptr0.ToPointer();
*(void**)(vfptr0 + -16) = *(void**)(native->vptr_IBar + -16);
*(void**)(vfptr0 + -8) = *(void**)(native->vptr_IBar + -8);
*(void**)(vfptr0 + 0) = _Thunks[0];
*(void**)(vfptr0 + 8) = *(void**)(native->vptr_IBar + 8);
*(void**)(vfptr0 + 16) = _Thunks[1];
}
native->vptr_IBar = new IntPtr(__ManagedVTables[0]);
}
}
#endregion
}
public unsafe partial class IBarInternal : global::IBar, IDisposable
{
private static void* __CopyValue(IBar.__Internal native)
{
var ret = Marshal.AllocHGlobal(8);
*(IBar.__Internal*) ret = native;
return ret.ToPointer();
}
internal IBarInternal(IBar.__Internal native, bool skipVTables = false)
: this(__CopyValue(native), skipVTables)
{
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
}
internal IBarInternal(void* native, bool skipVTables = false)
: base((void*) null)
{
__PointerAdjustment = 0;
__Instance = new global::System.IntPtr(native);
__OriginalVTables = new void*[] { ((__Internal*) native)->vptr_IBar.ToPointer() };
}
public override int Number
{
get
{
var __slot = *(void**) ((IntPtr) __OriginalVTables[0] + 0 * 8);
var ___GetNumber_0Delegate = (Func_int_IntPtr) Marshal.GetDelegateForFunctionPointer(new IntPtr(__slot), typeof(Func_int_IntPtr));
var __ret = ___GetNumber_0Delegate((__Instance + __PointerAdjustment));
return __ret;
}
}
}
public unsafe partial class Bar : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 8)]
public partial struct __Internal
{
[FieldOffset(0)]
public global::System.IntPtr impl;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN3BarC2Ev")]
internal static extern void ctor_0(global::System.IntPtr instance);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN3BarC2ERKS_")]
internal static extern void cctor_1(global::System.IntPtr instance, global::System.IntPtr _0);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN3Bar8RegisterEP4IBar")]
internal static extern void Register_0(global::System.IntPtr instance, global::System.IntPtr impl);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppFooBar", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN3Bar16PerformGetNumberEv")]
internal static extern int PerformGetNumber_0(global::System.IntPtr instance);
}
public global::System.IntPtr __Instance { get; protected set; }
protected int __PointerAdjustment;
internal static readonly global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, Bar> NativeToManagedMap = new global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, Bar>();
protected void*[] __OriginalVTables;
protected bool __ownsNativeInstance;
internal static Bar __CreateInstance(global::System.IntPtr native, bool skipVTables = false)
{
return new Bar(native.ToPointer(), skipVTables);
}
internal static Bar __CreateInstance(Bar.__Internal native, bool skipVTables = false)
{
return new Bar(native, skipVTables);
}
private static void* __CopyValue(Bar.__Internal native)
{
var ret = Marshal.AllocHGlobal(8);
*(Bar.__Internal*) ret = native;
return ret.ToPointer();
}
private Bar(Bar.__Internal native, bool skipVTables = false)
: this(__CopyValue(native), skipVTables)
{
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
}
protected Bar(void* native, bool skipVTables = false)
{
if (native == null)
return;
__Instance = new global::System.IntPtr(native);
}
public Bar()
{
__Instance = Marshal.AllocHGlobal(8);
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
__Internal.ctor_0((__Instance + __PointerAdjustment));
}
public Bar(global::Bar _0)
{
__Instance = Marshal.AllocHGlobal(8);
__ownsNativeInstance = true;
NativeToManagedMap[__Instance] = this;
*((Bar.__Internal*) __Instance) = *((Bar.__Internal*) _0.__Instance);
}
public void Dispose()
{
Dispose(disposing: true);
}
public virtual void Dispose(bool disposing)
{
global::Bar __dummy;
NativeToManagedMap.TryRemove(__Instance, out __dummy);
if (__ownsNativeInstance)
Marshal.FreeHGlobal(__Instance);
}
public void Register(global::IBar impl)
{
var __arg0 = ReferenceEquals(impl, null) ? global::System.IntPtr.Zero : impl.__Instance;
__Internal.Register_0((__Instance + __PointerAdjustment), __arg0);
}
public int PerformGetNumber()
{
var __ret = __Internal.PerformGetNumber_0((__Instance + __PointerAdjustment));
return __ret;
}
public global::IBar Impl
{
get
{
global::IBar __result0;
if (((__Internal*) __Instance)->impl == IntPtr.Zero) __result0 = null;
else if (global::IBar.NativeToManagedMap.ContainsKey(((__Internal*) __Instance)->impl))
__result0 = (global::IBar) global::IBar.NativeToManagedMap[((__Internal*) __Instance)->impl];
else global::IBar.NativeToManagedMap[((__Internal*) __Instance)->impl] = __result0 = (global::IBar) global::IBar.__CreateInstance(((__Internal*) __Instance)->impl);
return __result0;
}
set
{
((__Internal*) __Instance)->impl = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
}
namespace Delegates
{
[SuppressUnmanagedCodeSecurity, UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.Cdecl)]
internal unsafe delegate int Func_int_IntPtr(global::System.IntPtr instance);
[SuppressUnmanagedCodeSecurity, UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.Cdecl)]
internal unsafe delegate void Action_IntPtr(global::System.IntPtr instance);
}
#pragma once
class IBar {
public:
IBar(); // Note: needed for CppSharp
virtual int GetNumber() = 0;
virtual ~IBar();
};
using System;
using CppSharp;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Passes;
namespace CppSharpLibraryProject
{
class MainClass
{
class FooBarLibrary : ILibrary
{
public void Postprocess(Driver driver, ASTContext ctx)
{
}
public void Preprocess(Driver driver, ASTContext ctx)
{
}
public void Setup(Driver driver)
{
var options = driver.Options;
options.GeneratorKind = GeneratorKind.CSharp;
options.LibraryName = "CppFooBar";
var prefix = Environment.CurrentDirectory + "/../../../../CppLibraryProject/";
options.Headers.Add(prefix + "IBar.hpp");
options.Headers.Add(prefix + "Bar.hpp");
options.OutputNamespace = string.Empty;
}
public void SetupPasses(Driver driver)
{
}
}
public static void Main(string[] args)
{
ConsoleDriver.Run(new FooBarLibrary());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment