Port the whole Mediapipe.Net.Core

This commit is contained in:
Speykious
2022-01-15 04:31:40 +01:00
parent 1e13f4fd21
commit 8380b7f96c
8 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,63 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
using System.Threading;
namespace Mediapipe.Net.Core;
/// <remarks>
/// based on <see href="https://github.com/shimat/opencvsharp/blob/9a5f9828a74cfa3995562a06716e177705cde038/src/OpenCvSharp/Fundamentals/DisposableObject.cs">OpenCvSharp</see>
/// </remarks>
public abstract class Disposable : IDisposable
{
private volatile int disposeSignaled = 0;
public bool IsDisposed { get; protected set; }
protected bool IsOwner { get; private set; }
protected Disposable() : this(true) { }
protected Disposable(bool isOwner)
{
IsDisposed = false;
IsOwner = isOwner;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (Interlocked.Exchange(ref disposeSignaled, 1) != 0)
return;
IsDisposed = true;
if (disposing)
DisposeManaged();
DisposeUnmanaged();
}
~Disposable()
{
Dispose(false);
}
protected virtual void DisposeManaged() { }
protected virtual void DisposeUnmanaged() { }
public void TransferOwnership() => IsOwner = false;
public void ThrowIfDisposed()
{
if (IsDisposed)
throw new ObjectDisposedException(GetType().FullName);
}
}

View File

@ -0,0 +1,28 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public interface IMpResourceHandle : IDisposable
{
IntPtr MpPtr { get; }
/// <summary>
/// Relinquish the ownership, and release the resource it owns if necessary.
/// This method should be called only if the underlying native api moves the pointer.
/// </summary>
/// <remarks>
/// If the object itself is no longer used, call <see cref="Dispose" /> instead.
/// </remarks>
void ReleaseMpResource();
/// <summary>
/// Relinquish the ownership
/// </summary>
void TransferOwnership();
bool OwnsResource();
}

View File

@ -0,0 +1,12 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public class InternalException : Exception
{
public InternalException(string message) : base(message) { }
}

View File

@ -0,0 +1,12 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public class MediapipeException : Exception
{
public MediapipeException(string message) : base(message) { }
}

View File

@ -0,0 +1,12 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public class MediapipeNetException : Exception
{
public MediapipeNetException(string message) : base(message) { }
}

View File

@ -0,0 +1,76 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
using System.Runtime.InteropServices;
using Mediapipe.Net.Native;
using static Mediapipe.Net.Native.MpReturnCodeExtension;
namespace Mediapipe.Net.Core;
public abstract class MpResourceHandle : Disposable, IMpResourceHandle
{
protected IntPtr Ptr;
protected MpResourceHandle(bool isOwner = true) : this(IntPtr.Zero, isOwner) { }
protected MpResourceHandle(IntPtr ptr, bool isOwner = true) : base(isOwner)
{
Ptr = ptr;
}
#region IMpResourceHandle
public IntPtr MpPtr
{
get
{
ThrowIfDisposed();
return Ptr;
}
}
public void ReleaseMpResource()
{
if (OwnsResource())
DeleteMpPtr();
TransferOwnership();
}
public bool OwnsResource() => IsOwner && Ptr != IntPtr.Zero;
#endregion
protected override void DisposeUnmanaged()
{
if (OwnsResource())
DeleteMpPtr();
ReleaseMpPtr();
base.DisposeUnmanaged();
}
/// <summary>
/// Forgets the pointer address.
/// After calling this method, <see ref="OwnsResource" /> will return false.
/// </summary>
protected void ReleaseMpPtr() => Ptr = IntPtr.Zero;
/// <summary>
/// Release the memory (call `delete` or `delete[]`) whether or not it owns it.
/// </summary>
/// <remarks>In most cases, this method should not be called directly.</remarks>
protected abstract void DeleteMpPtr();
protected delegate MpReturnCode StringOutFunc(IntPtr ptr, out IntPtr strPtr);
protected string? MarshalStringFromNative(StringOutFunc func)
{
func(MpPtr, out IntPtr strPtr).Assert();
GC.KeepAlive(this);
string? str = Marshal.PtrToStringAnsi(strPtr);
UnsafeNativeMethods.delete_array__PKc(strPtr);
return str;
}
}

View File

@ -0,0 +1,18 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public abstract class SharedPtrHandle : MpResourceHandle
{
protected SharedPtrHandle(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
/// <returns>The owning pointer</returns>
public abstract IntPtr Get();
/// <summary>Release the owning pointer</summary>
public abstract void Reset();
}

View File

@ -0,0 +1,18 @@
// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
namespace Mediapipe.Net.Core;
public abstract class UniquePtrHandle : MpResourceHandle
{
protected UniquePtrHandle(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
/// <returns>The owning pointer</returns>
public abstract IntPtr Get();
/// <summary>Release the owning pointer</summary>
public abstract IntPtr Release();
}