Skip to content

Commit

Permalink
DIM implement protogeneric
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 21, 2024
1 parent fa076e0 commit d48ab65
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/ntcore/ProtobufTopic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NetworkTables;

public sealed class ProtobufTopic<T> : Topic, IEquatable<ProtobufTopic<T>?>, IEqualityOperators<ProtobufTopic<T>?, ProtobufTopic<T>?, bool> where T : IProtobufSerializable<T>
{
public IGenericProtobuf<T> Proto { get; } = T.ProtoGeneric;
public IGenericProtobuf<T> Proto { get; } = T.Proto;

private ProtobufTopic(Topic topic) : base(topic.Instance, topic.Handle)
{
Expand Down
3 changes: 1 addition & 2 deletions src/wpimath/Geometry/Pose2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public partial class Pose2dJsonContext : JsonSerializerContext
{
}

public readonly struct Pose2d : IStructSerializable<Pose2d>, IProtobufSerializable<Pose2d>,
public readonly struct Pose2d : IStructSerializable<Pose2d>, IProtobufSerializable<Pose2d, ProtobufPose2d>,
IMultiplyOperators<Pose2d, double, Pose2d>,
IAdditionOperators<Pose2d, Transform2d, Pose2d>,
ISubtractionOperators<Pose2d, Pose2d, Transform2d>,
Expand All @@ -69,7 +69,6 @@ public partial class Pose2dJsonContext : JsonSerializerContext
{
public static IStruct<Pose2d> Struct { get; } = new Pose2dStruct();
public static IProtobuf<Pose2d, ProtobufPose2d> Proto { get; } = new Pose2dProto();
static IGenericProtobuf<Pose2d> IProtobufSerializable<Pose2d>.ProtoGeneric => Proto;

[JsonInclude]
[JsonPropertyName("translation")]
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public partial class QuaternionJsonContext : JsonSerializerContext
{
public static IStruct<Quaternion> Struct { get; } = new QuaternionStruct();
public static IProtobuf<Quaternion, ProtobufQuaternion> Proto { get; } = new QuaternionProto();
static IGenericProtobuf<Quaternion> IProtobufSerializable<Quaternion>.ProtoGeneric => Proto;

[JsonInclude]
public double W { get; init; }
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Rotation2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public partial class Rotation2dJsonContext : JsonSerializerContext
{
public static IStruct<Rotation2d> Struct { get; } = new Rotation2dStruct();
public static IProtobuf<Rotation2d, ProtobufRotation2d> Proto { get; } = new Rotation2dProto();
static IGenericProtobuf<Rotation2d> IProtobufSerializable<Rotation2d>.ProtoGeneric => Proto;

public Rotation2d(Angle angle)
{
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Rotation3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public partial class Rotation3dJsonContext : JsonSerializerContext
{
public static IStruct<Rotation3d> Struct { get; } = new Rotation3dStruct();
public static IProtobuf<Rotation3d, ProtobufRotation3d> Proto { get; } = new Rotation3dProto();
static IGenericProtobuf<Rotation3d> IProtobufSerializable<Rotation3d>.ProtoGeneric => Proto;

[JsonInclude]
[JsonPropertyName("quaternion")]
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Transform2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public partial class Transform2dJsonContext : JsonSerializerContext
{
public static IStruct<Transform2d> Struct { get; } = new Transform2dStruct();
public static IProtobuf<Transform2d, ProtobufTransform2d> Proto { get; } = new Transform2dProto();
static IGenericProtobuf<Transform2d> IProtobufSerializable<Transform2d>.ProtoGeneric => Proto;

[JsonInclude]
[JsonPropertyName("translation")]
Expand Down
3 changes: 1 addition & 2 deletions src/wpimath/Geometry/Translation2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public partial class Translation2dJsonContext : JsonSerializerContext
{
}

public readonly struct Translation2d : IStructSerializable<Translation2d>, IProtobufSerializable<Translation2d>,
public readonly struct Translation2d : IStructSerializable<Translation2d>, IProtobufSerializable<Translation2d, ProtobufTranslation2d>,
IAdditionOperators<Translation2d, Translation2d, Translation2d>,
ISubtractionOperators<Translation2d, Translation2d, Translation2d>,
IUnaryNegationOperators<Translation2d, Translation2d>,
Expand All @@ -71,7 +71,6 @@ public partial class Translation2dJsonContext : JsonSerializerContext
{
public static IStruct<Translation2d> Struct { get; } = new Translation2dStruct();
public static IProtobuf<Translation2d, ProtobufTranslation2d> Proto { get; } = new Translation2dProto();
static IGenericProtobuf<Translation2d> IProtobufSerializable<Translation2d>.ProtoGeneric => Proto;

[JsonIgnore]
public Length X { get; }
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Twist2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public partial class Twist2dJsonContext : JsonSerializerContext
{
public static IStruct<Twist2d> Struct { get; } = new Twist2dStruct();
public static IProtobuf<Twist2d, ProtobufTwist2d> Proto { get; } = new Twist2dProto();
static IGenericProtobuf<Twist2d> IProtobufSerializable<Twist2d>.ProtoGeneric => Proto;

[JsonIgnore]
public Length Dx { get; init; }
Expand Down
1 change: 0 additions & 1 deletion src/wpimath/Geometry/Twist3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public partial class Twist3dJsonContext : JsonSerializerContext
{
public static IStruct<Twist3d> Struct { get; } = new Twist3dStruct();
public static IProtobuf<Twist3d, ProtobufTwist3d> Proto { get; } = new Twist3dProto();
static IGenericProtobuf<Twist3d> IProtobufSerializable<Twist3d>.ProtoGeneric => Proto;

[JsonIgnore]
public Length Dx { get; init; }
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/IWPISerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ namespace WPIUtil;
/// <summary>
/// Marker interface to indicate a class is serializable using WPI serialization methods.
/// </summary>
public interface IWPISerializable<T> { }
public interface IWPISerializable<T> where T : IWPISerializable<T> { }
4 changes: 2 additions & 2 deletions src/wpiutil/Logging/ProtobufLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public sealed class ProtobufLogEntry<T> : DataLogEntry where T : IProtobufSerial
private readonly ProtobufBuffer<T> m_storage = new();
private readonly object m_lockObject = new();

public ProtobufLogEntry(DataLog log, string name, string metadata = "", long timestamp = 0) : base(log, name, T.ProtoGeneric.TypeString, metadata, timestamp)
public ProtobufLogEntry(DataLog log, string name, string metadata = "", long timestamp = 0) : base(log, name, T.Proto.TypeString, metadata, timestamp)
{
log.AddSchema(T.ProtoGeneric, timestamp);
log.AddSchema(T.Proto, timestamp);
}

public void Append(T value, long timestamp = 0)
Expand Down
11 changes: 8 additions & 3 deletions src/wpiutil/Serialization/Protobuf/IProtobufSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
namespace WPIUtil.Serialization.Protobuf;

public interface IProtobufSerializable<T> : IWPISerializable<T>
where T : IProtobufSerializable<T>
{
public static abstract IGenericProtobuf<T> ProtoGeneric { get; }
public static abstract IGenericProtobuf<T> Proto { get; }
}

public interface IProtobufSerializable<T, TProto> : IProtobufSerializable<T> where TProto : IMessage<TProto>
public interface IProtobufSerializable<T, TProto> : IProtobufSerializable<T>
where T : IProtobufSerializable<T, TProto>
where TProto : IMessage<TProto>
{
public static abstract IProtobuf<T, TProto> Proto { get; }
public static new abstract IProtobuf<T, TProto> Proto { get; }

static IGenericProtobuf<T> IProtobufSerializable<T>.Proto => T.Proto;
}
2 changes: 1 addition & 1 deletion src/wpiutil/Serialization/Protobuf/ProtobufBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct ProtobufBuffer<T> where T : IProtobufSerializable<T>
{
public ProtobufBuffer()
{
Proto = T.ProtoGeneric;
Proto = T.Proto;
m_msg = Proto.GenericCreateMessage();
}

Expand Down
1 change: 1 addition & 0 deletions src/wpiutil/Serialization/Struct/StructSerializable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace WPIUtil.Serialization.Struct;

public interface IStructSerializable<T> : IWPISerializable<T>
where T : IStructSerializable<T>
{
public static abstract IStruct<T> Struct { get; }
}
6 changes: 3 additions & 3 deletions test/wpimath.test/GenericHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static T StructRoundTrip<T>(T start) where T : IStructSerializable<T>

public static T ProtoRoundTrip<T>(T start) where T : IProtobufSerializable<T>
{
var proto = T.ProtoGeneric.GenericCreateMessage();
T.ProtoGeneric.GenericPack(proto, start);
return T.ProtoGeneric.GenericUnpack(proto);
var proto = T.Proto.GenericCreateMessage();
T.Proto.GenericPack(proto, start);
return T.Proto.GenericUnpack(proto);
}

public static T ProtoTypedRoundTrip<T, U>(T start) where T : IProtobufSerializable<T, U> where U : IMessage<U>
Expand Down

0 comments on commit d48ab65

Please sign in to comment.