mirror of
https://github.com/cosyneco/MediaPipe.NET.git
synced 2025-08-24 08:40:39 +08:00
Port the whole Mediapipe.Net.Core
This commit is contained in:
63
Mediapipe.Net/Core/Disposable.cs
Normal file
63
Mediapipe.Net/Core/Disposable.cs
Normal 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);
|
||||
}
|
||||
}
|
28
Mediapipe.Net/Core/IMpResourceHandle.cs
Normal file
28
Mediapipe.Net/Core/IMpResourceHandle.cs
Normal 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();
|
||||
}
|
12
Mediapipe.Net/Core/InternalException.cs
Normal file
12
Mediapipe.Net/Core/InternalException.cs
Normal 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) { }
|
||||
}
|
12
Mediapipe.Net/Core/MediapipeException.cs
Normal file
12
Mediapipe.Net/Core/MediapipeException.cs
Normal 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) { }
|
||||
}
|
12
Mediapipe.Net/Core/MediapipeNetException.cs
Normal file
12
Mediapipe.Net/Core/MediapipeNetException.cs
Normal 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) { }
|
||||
}
|
76
Mediapipe.Net/Core/MpResourceHandle.cs
Normal file
76
Mediapipe.Net/Core/MpResourceHandle.cs
Normal 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;
|
||||
}
|
||||
}
|
18
Mediapipe.Net/Core/SharedPtrHandle.cs
Normal file
18
Mediapipe.Net/Core/SharedPtrHandle.cs
Normal 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();
|
||||
}
|
18
Mediapipe.Net/Core/UniquePtrHandle.cs
Normal file
18
Mediapipe.Net/Core/UniquePtrHandle.cs
Normal 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();
|
||||
}
|
Reference in New Issue
Block a user