Skip to content

Commit

Permalink
Slightly redo Protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 14, 2024
1 parent de1b7e5 commit ee72d1a
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/ntcore/NetworkTableInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public void AddSchema(IStructBase proto)
AddSchemaImpl(proto, []);
}

public void AddSchema(IProtobufBase proto)
public void AddSchema(IProtobuf proto)
{
proto.ForEachDescriptor(HasSchema, (typeString, schema) => AddSchema(typeString, "proto:FileDescriptorProto", schema));
}
Expand Down
4 changes: 2 additions & 2 deletions src/ntcore/ProtobufTopic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NetworkTables;

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

private ProtobufTopic(Topic topic) : base(topic.Instance, topic.Handle)
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public override bool Equals(object? obj)
public bool Equals(ProtobufTopic<T>? other)
{
return other is not null &&
EqualityComparer<IProtobuf<T>>.Default.Equals(Proto, other.Proto);
EqualityComparer<IGenericProtobuf<T>>.Default.Equals(Proto, other.Proto);
}

public override int GetHashCode()
Expand Down
2 changes: 1 addition & 1 deletion src/wpimath/Geometry/Pose2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public partial class Pose2dJsonContext : JsonSerializerContext
{
public static IStruct<Pose2d> Struct { get; } = new Pose2dStruct();
public static IProtobuf<Pose2d, ProtobufPose2d> Proto { get; } = new Pose2dProto();
static IProtobuf<Pose2d> IProtobufSerializable<Pose2d>.Proto { get; } = Proto.UntypedProto;
static IGenericProtobuf<Pose2d> IProtobufSerializable<Pose2d>.ProtoGeneric => Proto;

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

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

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

[JsonInclude]
[JsonPropertyName("quaternion")]
Expand Down
2 changes: 1 addition & 1 deletion src/wpimath/Geometry/Translation2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public partial class Translation2dJsonContext : JsonSerializerContext
{
public static IStruct<Translation2d> Struct { get; } = new Translation2dStruct();
public static IProtobuf<Translation2d, ProtobufTranslation2d> Proto { get; } = new Translation2dProto();
static IProtobuf<Translation2d> IProtobufSerializable<Translation2d>.Proto => Proto.UntypedProto;
static IGenericProtobuf<Translation2d> IProtobufSerializable<Translation2d>.ProtoGeneric => Proto;

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

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

[JsonIgnore]
public Length Dx { get; init; }
Expand Down
2 changes: 1 addition & 1 deletion src/wpiutil/Logging/DataLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void AddSchema(IStructBase value, long timestamp = 0)
AddSchemaImpl(value, timestamp == 0 ? (long)TimestampNative.Now() : timestamp, []);
}

public void AddSchema(IProtobufBase proto, long timestamp = 0)
public void AddSchema(IProtobuf proto, long timestamp = 0)
{
long actualTimestamp = timestamp == 0 ? (long)TimestampNative.Now() : timestamp;
proto.ForEachDescriptor(HasSchema, (typeString, schema) =>
Expand Down
4 changes: 2 additions & 2 deletions src/wpiutil/Logging/ProtobufLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public sealed class ProtobufLogEntry<T> : DataLogEntry where T : IProtobufSerial
private readonly ProtobufBuffer<T> m_storage = new();
private readonly object m_lockObject = new();

private ProtobufLogEntry(DataLog log, string name, IProtobufBase proto, string metadata = "", long timestamp = 0) : base(log, name, proto.TypeString, metadata, timestamp)
private ProtobufLogEntry(DataLog log, string name, IProtobuf proto, string metadata = "", long timestamp = 0) : base(log, name, proto.TypeString, metadata, timestamp)
{
log.AddSchema(proto, timestamp);
}

public ProtobufLogEntry<T> Create(DataLog log, string name, string metadata = "", long timestamp = 0)
{
return new ProtobufLogEntry<T>(log, name, T.Proto, metadata, timestamp);
return new ProtobufLogEntry<T>(log, name, T.ProtoGeneric, metadata, timestamp);
}

public void Append(T value, long timestamp = 0)
Expand Down
41 changes: 22 additions & 19 deletions src/wpiutil/Serialization/Protobuf/IProtobuf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace WPIUtil.Serialization.Protobuf;

public interface IProtobufBase
public interface IProtobuf
{
string TypeString => $"proto:{Descriptor.FullName}";
MessageDescriptor Descriptor { get; }
IProtobufBase[] Nested => [];
IProtobuf[] Nested => [];

void ForEachDescriptor(Func<string, bool> exists, Action<string, byte[]> fn)
{
Expand All @@ -30,35 +30,38 @@ private static void ForEachDescriptorImpl(FileDescriptor desc, Func<string, bool
}
}

public interface IProtobuf<T> : IProtobufBase
public interface IGenericProtobuf<T> : IProtobuf
{
IMessage CreateMessage();
IMessage GenericCreateMessage();

T Unpack(IMessage msg);
T GenericUnpack(IMessage msg);

void Pack(IMessage msg, T value);
void GenericPack(IMessage msg, T value);

void UnpackInto(ref T value, IMessage msg);
void GenericUnpackInto(ref T value, IMessage msg);
}

public interface IProtobuf<T, MessageType> : IProtobufBase where MessageType : IMessage<MessageType>
public interface IProtobuf<T, MessageType> : IGenericProtobuf<T> where MessageType : IMessage<MessageType>
{
internal class ProtobufWrapper(IProtobuf<T, MessageType> proto) : IProtobuf<T>
IMessage IGenericProtobuf<T>.GenericCreateMessage()
{
private readonly IProtobuf<T, MessageType> m_proto = proto;

public MessageDescriptor Descriptor => m_proto.Descriptor;

public IMessage CreateMessage() => m_proto.CreateMessage();

public void Pack(IMessage msg, T value) => m_proto.Pack((MessageType)msg, value);
return CreateMessage();
}

public T Unpack(IMessage msg) => m_proto.Unpack((MessageType)msg);
T IGenericProtobuf<T>.GenericUnpack(IMessage msg)
{
return Unpack((MessageType)msg);
}

public void UnpackInto(ref T value, IMessage msg) => m_proto.UnpackInto(ref value, (MessageType)msg);
void IGenericProtobuf<T>.GenericPack(IMessage msg, T value)
{
Pack((MessageType)msg, value);
}

public IProtobuf<T> UntypedProto => new ProtobufWrapper(this);
void IGenericProtobuf<T>.GenericUnpackInto(ref T value, IMessage msg)
{
UnpackInto(ref value, (MessageType)msg);
}

MessageType CreateMessage();

Expand Down
4 changes: 2 additions & 2 deletions src/wpiutil/Serialization/Protobuf/IProtobufSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace WPIUtil.Serialization.Protobuf;

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

public interface IProtobufSerializable<T, TProto> : IProtobufSerializable<T> where TProto : IMessage<TProto>
{
public static new abstract IProtobuf<T, TProto> Proto { get; }
public static abstract IProtobuf<T, TProto> Proto { get; }
}
12 changes: 6 additions & 6 deletions src/wpiutil/Serialization/Protobuf/ProtobufBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ public struct ProtobufBuffer<T> where T : IProtobufSerializable<T>
{
public ProtobufBuffer()
{
Proto = T.Proto;
m_msg = Proto.CreateMessage();
Proto = T.ProtoGeneric;
m_msg = Proto.GenericCreateMessage();
}

public IProtobuf<T> Proto { get; }
public IGenericProtobuf<T> Proto { get; }

public readonly string TypeString => Proto.TypeString;

public ReadOnlySpan<byte> Write(T value)
{
Proto.Pack(m_msg, value);
Proto.GenericPack(m_msg, value);
int size = m_msg.CalculateSize();
if (size > m_buf.Length)
{
Expand All @@ -30,13 +30,13 @@ public ReadOnlySpan<byte> Write(T value)
public readonly T Read(ReadOnlySpan<byte> buffer)
{
m_msg.MergeFrom(buffer);
return Proto.Unpack(m_msg);
return Proto.GenericUnpack(m_msg);
}

public readonly void ReadInto(ref T output, ReadOnlySpan<byte> buffer)
{
m_msg.MergeFrom(buffer);
Proto.UnpackInto(ref output, m_msg);
Proto.GenericUnpackInto(ref output, m_msg);
}

private readonly IMessage m_msg;
Expand Down
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.Proto.CreateMessage();
T.Proto.Pack(proto, start);
return T.Proto.Unpack(proto);
var proto = T.ProtoGeneric.GenericCreateMessage();
T.ProtoGeneric.GenericPack(proto, start);
return T.ProtoGeneric.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 ee72d1a

Please sign in to comment.