skiasharp/binding/HarfBuzzSharp/NativeObject.cs
Max Katz 9cbf9c80b7
Function pointers and LibraryImport (#2917)
* Update generator to emit function pointers

* Regenerate interop files

* Enable USE_LIBRARY_IMPORT in HarfBuzzSharp

* Enable USE_LIBRARY_IMPORT on SkiaSharp

* Set DisableRuntimeMarshalling on HarfBuzzSharp

* Replace remaining DllImports with LibraryImport on SkiaSharp

* Set DisableRuntimeMarshalling on SkiaSharp as well

* Fix missed proxy definition

* Regenerate skia api with a correct submodule version

* Collections literals are not supported on the CI .NET SDK

* An attempt to fix Tizen build

* Forgot about partial

* Set UnmanagedType.LPStr on evas_gl_proc_address_get instead

* Set USE_LIBRARY_IMPORT on remaining projects too

* Update generator tool to generate DelegateProxy as well

* Regenerate HarfBuzz and SkiaSharp with new DelegateProxy source gen

* Regenerate other projects as well

* Add `protected internal` to test classes too, since this project has InternalsVisibleTo configured

* Disable DelegateTypesAreValid and DelegateTypesHaveAttributes tests on .NET 7+ build, see comments

* Reduce warnings noise

* Update binding/SkiaSharp/GRGlInterface.cs

Co-authored-by: Filip Navara <filip.navara@gmail.com>

* Add missing USE_LIBRARY_IMPORT defines

* Update binding/SkiaSharp/GRGlInterface.cs

* Also needs USE_LIBRARY_IMPORT

---------

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Filip Navara <filip.navara@gmail.com>
2024-08-21 23:16:05 +08:00

78 lines
1.4 KiB
C#

#nullable disable
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace HarfBuzzSharp
{
public class NativeObject : IDisposable
{
private bool isDisposed;
private readonly bool zero;
internal NativeObject (IntPtr handle)
{
Handle = handle;
zero = true;
}
internal NativeObject (IntPtr handle, bool zero)
{
Handle = handle;
this.zero = zero;
}
~NativeObject ()
{
Dispose (false);
}
public virtual IntPtr Handle { get; protected set; }
// Dispose method - always called
protected virtual void Dispose (bool disposing)
{
if (isDisposed) {
return;
}
isDisposed = true;
if (!disposing) {
return;
}
DisposeHandler ();
if (zero) {
Handle = IntPtr.Zero;
}
}
// Intended to be overridden - always safe to use
// since it will never be called unless applicable
protected virtual void DisposeHandler ()
{
}
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
internal static IEnumerable<string> PtrToStringArray (IntPtr intPtr)
{
if (intPtr != IntPtr.Zero) {
var ptr = Marshal.ReadIntPtr (intPtr);
while (ptr != IntPtr.Zero) {
var element = Marshal.PtrToStringAnsi (ptr);
yield return element;
intPtr = new IntPtr (intPtr.ToInt64 () + IntPtr.Size);
ptr = Marshal.ReadIntPtr (intPtr);
}
}
}
}
}