Skip to content

Commit

Permalink
feat: implement IntPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 23, 2020
1 parent 657655e commit 2eea5b2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
38 changes: 38 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Framework/Packet/IntPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace Mediapipe {
public class IntPacket : Packet<int> {
public IntPacket() : base() {}

public IntPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {}

public IntPacket(int value) : base() {
UnsafeNativeMethods.mp__MakeIntPacket__i(value, out var ptr).Assert();
this.ptr = ptr;
}

public IntPacket(int value, Timestamp timestamp) : base() {
UnsafeNativeMethods.mp__MakeIntPacket_At__i_Rtimestamp(value, timestamp.mpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
this.ptr = ptr;
}

public override int Get() {
UnsafeNativeMethods.mp_Packet__GetInt(mpPtr, out var value).Assert();

GC.KeepAlive(this);
return value;
}

public override StatusOr<int> Consume() {
throw new NotSupportedException();
}

public override Status ValidateAsType() {
UnsafeNativeMethods.mp_Packet__ValidateAsInt(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Framework/Packet/IntPacket.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ internal static partial class UnsafeNativeMethods {
public static extern MpReturnCode mp_Packet__ValidateAsFloat(IntPtr packet, out IntPtr status);
#endregion

#region Int
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeIntPacket__i(int value, out IntPtr packet);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeIntPacket_At__i_Rtimestamp(int value, IntPtr timestamp, out IntPtr packet);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetInt(IntPtr packet, out int value);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsInt(IntPtr packet, out IntPtr status);
#endregion

#region String
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeStringPacket__PKc(string value, out IntPtr packet);
Expand Down
35 changes: 32 additions & 3 deletions C/mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ MpReturnCode mp_Packet__DebugTypeName(mediapipe::Packet* packet, const char** st
} CATCH_EXCEPTION
}

/** BoolPacket */
// BoolPacket
MpReturnCode mp__MakeBoolPacket__b(bool value, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<bool>(value) };
Expand Down Expand Up @@ -85,7 +85,7 @@ MpReturnCode mp_Packet__ValidateAsBool(mediapipe::Packet* packet, mediapipe::Sta
} CATCH_EXCEPTION
}

/** FloatPacket */
// FloatPacket
MpReturnCode mp__MakeFloatPacket__f(float value, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<float>(value) };
Expand Down Expand Up @@ -114,7 +114,36 @@ MpReturnCode mp_Packet__ValidateAsFloat(mediapipe::Packet* packet, mediapipe::St
} CATCH_EXCEPTION
}

/** StringPacket */
// IntPacket
MpReturnCode mp__MakeIntPacket__i(int value, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<int>(value) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp__MakeIntPacket_At__i_Rtimestamp(int value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<int>(value).At(*timestamp) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__GetInt(mediapipe::Packet* packet, int* value_out) {
TRY_ALL {
*value_out = packet->Get<int>();
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}

MpReturnCode mp_Packet__ValidateAsInt(mediapipe::Packet* packet, mediapipe::Status** status_out) {
TRY {
*status_out = new mediapipe::Status { packet->ValidateAsType<int>() };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

// StringPacket
MpReturnCode mp__MakeStringPacket__PKc(const char* str, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<std::string>(std::string(str)) };
Expand Down
6 changes: 6 additions & 0 deletions C/mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ MP_CAPI(MpReturnCode) mp__MakeFloatPacket_At__f_Rtimestamp(float value, mediapip
MP_CAPI(MpReturnCode) mp_Packet__GetFloat(mediapipe::Packet* packet, float* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsFloat(mediapipe::Packet* packet, mediapipe::Status** status_out);

// Int
MP_CAPI(MpReturnCode) mp__MakeIntPacket__i(int value, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeIntPacket_At__i_Rtimestamp(int value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetInt(mediapipe::Packet* packet, int* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsInt(mediapipe::Packet* packet, mediapipe::Status** status_out);

// String
MP_CAPI(MpReturnCode) mp__MakeStringPacket__PKc(const char* str, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeStringPacket_At__PKc_Rtimestamp(const char* str,
Expand Down

0 comments on commit 2eea5b2

Please sign in to comment.