Skip to content

Commit

Permalink
Reader: add ReadSignature overloads.
Browse files Browse the repository at this point in the history
Rename ReadSignatureAsSignature to ReadSignature.
Add ReadSignature overload that accepts expected as ROS<byte>.
  • Loading branch information
tmds committed Nov 11, 2024
1 parent f749a0c commit 752f757
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/Tmds.DBus.Protocol/Reader.Basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public unsafe double ReadDouble()
return value;
}

public Signature ReadSignature()
=> new Signature(ReadSignatureAsSpan());

public ReadOnlySpan<byte> ReadSignatureAsSpan()
{
int length = ReadByte();
Expand All @@ -87,6 +90,15 @@ public void ReadSignature(string expected)
}
}

public void ReadSignature(ReadOnlySpan<byte> expected)
{
ReadOnlySpan<byte> signature = ReadSignatureAsSpan();
if (!signature.SequenceEqual(expected))
{
ThrowHelper.ThrowUnexpectedSignature(signature, Encoding.UTF8.GetString(expected));
}
}

public ReadOnlySpan<byte> ReadObjectPathAsSpan() => ReadSpan();

public ObjectPath ReadObjectPath() => new ObjectPath(ReadString());
Expand All @@ -97,8 +109,6 @@ public void ReadSignature(string expected)

public string ReadString() => Encoding.UTF8.GetString(ReadSpan());

public Signature ReadSignatureAsSignature() => new Signature(ReadSignatureAsSpan());

public string ReadSignatureAsString() => Encoding.UTF8.GetString(ReadSignatureAsSpan());

private ReadOnlySpan<byte> ReadSpan()
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.ReadT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ref partial struct Reader
}
else if (typeof(T) == typeof(Signature))
{
return (T)(object)ReadSignatureAsSignature();
return (T)(object)ReadSignature();
}
else if (typeof(T).IsAssignableTo(typeof(SafeHandle)))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private VariantValue ReadTypeAsVariantValue(DBusType type, ReadOnlySpan<byte> in
case DBusType.ObjectPath:
return new VariantValue(ReadObjectPath(), nesting);
case DBusType.Signature:
return new VariantValue(ReadSignatureAsSignature(), nesting);
return new VariantValue(ReadSignature(), nesting);
case DBusType.UnixFd:
int idx = (int)ReadUInt32();
return new VariantValue(_handles, idx, nesting);
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Tool/ProtocolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ private void AppendInterface(string name, XElement interfaceXml)
{
AppendLine($"case \"{property.Name}\":");
_indentation++;
AppendLine($"reader.ReadSignature(\"{property.Signature}\");");
AppendLine($"reader.ReadSignature(\"{property.Signature}\"u8);");
AppendLine($"props.{property.NameUpper} = {CallReadArgumentType(property.Signature)};");
AppendLine($"changedList?.Add(\"{property.NameUpper}\");");
AppendLine("break;");
Expand Down Expand Up @@ -757,7 +757,7 @@ private void AppendReadMessageMethod(string name, bool variant, Argument[] args)
AppendLine("var reader = message.GetBodyReader();");
if (variant)
{
AppendLine($"reader.ReadSignature(\"{signature}\");");
AppendLine($"reader.ReadSignature(\"{signature}\"u8);");
}
if (args.Length == 1)
{
Expand Down

0 comments on commit 752f757

Please sign in to comment.