-
-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement non-generic Packet for boolean
- Loading branch information
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
|
||
namespace Mediapipe | ||
{ | ||
public class Packet : MpResourceHandle | ||
{ | ||
private Packet(IntPtr ptr, bool isOwner) : base(ptr, isOwner) { } | ||
|
||
protected override void DeleteMpPtr() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__delete(ptr); | ||
} | ||
|
||
public long TimestampMicroseconds() | ||
{ | ||
var value = SafeNativeMethods.mp_Packet__TimestampMicroseconds(mpPtr); | ||
GC.KeepAlive(this); | ||
|
||
return value; | ||
} | ||
|
||
internal static Packet CreateEmpty() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__(out var ptr).Assert(); | ||
|
||
return new Packet(ptr, true); | ||
} | ||
|
||
/// <summary> | ||
/// Low-level API to reference the packet that <paramref name="ptr" /> points to. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method is to be used when you want to reference the packet whose lifetime is managed by native code. | ||
/// </remarks> | ||
/// <param name="ptr"> | ||
/// A pointer to a native Packet instance. | ||
/// </param> | ||
public static Packet CreateForReference(IntPtr ptr) => new Packet(ptr, false); | ||
|
||
public static Packet CreateBool(bool value) | ||
{ | ||
UnsafeNativeMethods.mp__MakeBoolPacket__b(value, out var ptr).Assert(); | ||
|
||
return new Packet(ptr, true); | ||
} | ||
|
||
public static Packet CreateBoolAt(bool value, long timestampMicrosec) | ||
{ | ||
UnsafeNativeMethods.mp__MakeBoolPacket_At__b_ll(value, timestampMicrosec, out var ptr).Assert(); | ||
|
||
return new Packet(ptr, true); | ||
} | ||
|
||
/// <summary> | ||
/// Get the content of the <see cref="Packet"/> as a boolean. | ||
/// </summary> | ||
/// <remarks> | ||
/// On some platforms (e.g. Windows), it will abort the process when <see cref="MediaPipeException"/> should be thrown. | ||
/// </remarks> | ||
/// <exception cref="MediaPipeException"> | ||
/// If the <see cref="Packet"/> doesn't contain bool data. | ||
/// </exception> | ||
public bool GetBool() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__GetBool(mpPtr, out var value).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return value; | ||
} | ||
|
||
/// <summary> | ||
/// Validate if the content of the <see cref="Packet"/> is a boolean. | ||
/// </summary> | ||
/// <exception cref="BadStatusException"> | ||
/// If the <see cref="Packet"/> doesn't contain bool data. | ||
/// </exception> | ||
public void ValidateAsBool() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__ValidateAsBool(mpPtr, out var statusPtr).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
AssertStatusOk(statusPtr); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/PacketTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using NUnit.Framework; | ||
|
||
namespace Mediapipe.Tests | ||
{ | ||
public class PacketTest | ||
{ | ||
#region Bool | ||
[TestCase(true)] | ||
[TestCase(false)] | ||
public void CreateBool_ShouldReturnNewBoolPacket(bool value) | ||
{ | ||
using var packet = Packet.CreateBool(value); | ||
|
||
Assert.DoesNotThrow(packet.ValidateAsBool); | ||
Assert.AreEqual(value, packet.GetBool()); | ||
|
||
using var unsetTimestamp = Timestamp.Unset(); | ||
Assert.AreEqual(unsetTimestamp.Microseconds(), packet.TimestampMicroseconds()); | ||
} | ||
|
||
[TestCase(true)] | ||
[TestCase(false)] | ||
public void CreateBoolAt_ShouldReturnNewBoolPacket(bool value) | ||
{ | ||
var timestamp = 1; | ||
using var packet = Packet.CreateBoolAt(value, timestamp); | ||
|
||
Assert.DoesNotThrow(packet.ValidateAsBool); | ||
Assert.AreEqual(value, packet.GetBool()); | ||
Assert.AreEqual(timestamp, packet.TimestampMicroseconds()); | ||
} | ||
#endregion | ||
|
||
#region #Validate | ||
[Test] | ||
public void ValidateAsBool_ShouldThrow_When_ValueIsNotSet() | ||
{ | ||
using var packet = Packet.CreateEmpty(); | ||
_ = Assert.Throws<BadStatusException>(packet.ValidateAsBool); | ||
} | ||
#endregion | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/PacketTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters