Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 30 additions & 46 deletions src/Tmds.DBus.Tool/ProtocolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

class ProtocolGeneratorSettings
{
public string? ServiceName { get; set; }

Check warning on line 24 in src/Tmds.DBus.Tool/ProtocolGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 24 in src/Tmds.DBus.Tool/ProtocolGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public Accessibility TypesAccessModifier = Accessibility.NotApplicable;
public string? GeneratorDescription { get; set; }

Check warning on line 26 in src/Tmds.DBus.Tool/ProtocolGenerator.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}

class ProtocolGenerator : IGenerator
Expand Down Expand Up @@ -982,8 +982,7 @@
AppendLine("MethodContext.ReplyError(\"org.freedesktop.DBus.Error.UnknownProperty\", $\"Unknown property: {Property}\");");
EndBlock();

// Handle method that reads from IReadableXxxProperties
AppendLine($"public ValueTask Handle(IReadable{name}Properties properties)");
AppendLine($"public ValueTask Handle(I{name}Properties properties)");
StartBlock();
AppendLine("switch (Property)");
StartBlock();
Expand Down Expand Up @@ -1018,8 +1017,7 @@

AppendLine("public void Dispose() => MethodContext.Dispose();");

// Handle method that reads all properties from IReadableXxxProperties
AppendLine($"public ValueTask Handle(IReadable{name}Properties properties)");
AppendLine($"public ValueTask Handle(I{name}Properties properties)");
StartBlock();
AppendLine("var writer = MethodContext.CreateReplyWriter(\"a{sv}\");");
AppendLine("var dictStart = writer.WriteDictionaryStart();");
Expand Down Expand Up @@ -1171,9 +1169,27 @@
AppendPropertyEnum(name, readableProperties);
}

if (readableProperties.Any())
if (readableProperties.Any() || writableProperties.Any())
{
AppendReadableInterface($"IReadable{name}Properties", readableProperties);
AppendLine($"interface I{name}Properties");
StartBlock();
foreach (var property in readableProperties)
{
bool isWritable = writableProperties.Any(w => w.Name == property.Name);
if (isWritable)
{
AppendLine($"{property.DotnetReadType} {property.NameUpper} {{ get; set; }}");
}
else
{
AppendLine($"{property.DotnetReadType} {property.NameUpper} {{ get; }}");
}
}
foreach (var property in writeOnlyProperties)
{
AppendLine($"{property.DotnetReadType} {property.NameUpper} {{ set; }}");
}
EndBlock();
}

if (generateHandlers)
Expand Down Expand Up @@ -1204,31 +1220,6 @@
}
EndBlock();
}

// IXxxProperties
if (readableProperties.Any() || writableProperties.Any())
{
string propertiesInterfaceName = $"I{name}Properties";
string baseInterface = readableProperties.Any() ? $" : IReadable{name}Properties" : "";
AppendLine($"interface {propertiesInterfaceName}{baseInterface}");
StartBlock();

foreach (var property in readableProperties)
{
bool isWritable = writableProperties.Any(w => w.Name == property.Name);
if (isWritable)
{
AppendLine($"new {property.DotnetReadType} {property.NameUpper} {{ get; set; }}");
}
}

foreach (var property in writeOnlyProperties)
{
AppendLine($"{property.DotnetReadType} {property.NameUpper} {{ set; }}");
}

EndBlock();
}
}

if (generateProxies)
Expand Down Expand Up @@ -1263,7 +1254,7 @@
}
EndBlock();

AppendLine($"sealed class {propertiesClassName} : {changedInterfaceName}, IReadable{name}Properties");
AppendLine($"sealed class {propertiesClassName} : {changedInterfaceName}, I{name}Properties");
StartBlock();

string flagType = readableProperties.Length <= 32 ? "uint" : "ulong";
Expand Down Expand Up @@ -1413,6 +1404,11 @@
AppendLine("return props;");
EndBlock();

foreach (var property in writeOnlyProperties)
{
AppendLine($"{property.DotnetReadType} I{name}Properties.{property.NameUpper} {{ set {{ }} }}");
}

EndBlock();
}
}
Expand All @@ -1433,17 +1429,6 @@
EndBlock();
}

private void AppendReadableInterface(string interfaceName, Argument[] readableProperties)
{
AppendLine($"interface {interfaceName}");
StartBlock();
foreach (var property in readableProperties)
{
AppendLine($"{property.DotnetReadType} {property.NameUpper} {{ get; }}");
}
EndBlock();
}

private void AppendSignalsClass(string name, XElement interfaceXml)
{
var signals = interfaceXml.Elements("signal").ToArray();
Expand Down Expand Up @@ -1491,8 +1476,7 @@
AppendLine($"writer.WriteString(\"{interfaceName}\");");
}

// Public overload: accepts IReadableXxxProperties with changed and invalidated spans
AppendLine($"public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, IReadable{name}Properties properties, ReadOnlySpan<{propertyEnumName}> changed, ReadOnlySpan<{propertyEnumName}> invalidated = default)");
AppendLine($"public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, I{name}Properties properties, ReadOnlySpan<{propertyEnumName}> changed, ReadOnlySpan<{propertyEnumName}> invalidated = default)");
StartBlock();
AppendWriteSignalHeader();

Expand Down Expand Up @@ -1539,7 +1523,7 @@
EndBlock();

// Public overload: single changed property
AppendLine($"public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, IReadable{name}Properties properties, {propertyEnumName} changed)");
AppendLine($"public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, I{name}Properties properties, {propertyEnumName} changed)");
StartBlock();
AppendLine("EmitPropertiesChanged(c, p, properties, stackalloc[] { changed }, default);");
EndBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ namespace TestNamespace
CurrentEntry = 3,
IsActive = 4
}
interface IReadableCalculatorProperties
interface ICalculatorProperties
{
double LastResult { get; }
ObjectPath CurrentPath { get; }
(string, double) CurrentEntry { get; }
ValueTuple<bool> IsActive { get; }
ValueTuple<bool> IsActive { get; set; }
}
enum WritableCalculatorProperty
{
UnknownProperty = 0,
IsActive = 4,
}
interface ICalculatorProperties : IReadableCalculatorProperties
{
new ValueTuple<bool> IsActive { get; set; }
}
interface ICalculatorHandler
{
internal static class Helper
Expand Down Expand Up @@ -243,7 +239,7 @@ namespace TestNamespace
{
MethodContext.ReplyError("org.freedesktop.DBus.Error.UnknownProperty", $"Unknown property: {Property}");
}
public ValueTask Handle(IReadableCalculatorProperties properties)
public ValueTask Handle(ICalculatorProperties properties)
{
switch (Property)
{
Expand Down Expand Up @@ -274,7 +270,7 @@ namespace TestNamespace
MethodContext = methodContext;
}
public void Dispose() => MethodContext.Dispose();
public ValueTask Handle(IReadableCalculatorProperties properties)
public ValueTask Handle(ICalculatorProperties properties)
{
var writer = MethodContext.CreateReplyWriter("a{sv}");
var dictStart = writer.WriteDictionaryStart();
Expand Down Expand Up @@ -412,7 +408,7 @@ namespace TestNamespace
writer.WriteDouble(result);
c.TrySendMessage(writer.CreateMessage());
}
public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, IReadableCalculatorProperties properties, ReadOnlySpan<CalculatorProperty> changed, ReadOnlySpan<CalculatorProperty> invalidated = default)
public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, ICalculatorProperties properties, ReadOnlySpan<CalculatorProperty> changed, ReadOnlySpan<CalculatorProperty> invalidated = default)
{
var writer = c.GetMessageWriter();
writer.WriteSignalHeader(
Expand Down Expand Up @@ -471,7 +467,7 @@ namespace TestNamespace
writer.WriteArrayEnd(arrayStart);
c.TrySendMessage(writer.CreateMessage());
}
public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, IReadableCalculatorProperties properties, CalculatorProperty changed)
public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, ICalculatorProperties properties, CalculatorProperty changed)
{
EmitPropertiesChanged(c, p, properties, stackalloc[] { changed }, default);
}
Expand Down Expand Up @@ -539,22 +535,18 @@ namespace TestNamespace
Theme = 1,
Language = 2
}
interface IReadableSettingsProperties
interface ISettingsProperties
{
string Theme { get; }
string Language { get; }
string Language { get; set; }
double Volume { set; }
}
enum WritableSettingsProperty
{
UnknownProperty = 0,
Language = 2,
Volume = 3
}
interface ISettingsProperties : IReadableSettingsProperties
{
new string Language { get; set; }
double Volume { set; }
}
interface ISettingsHandler
{
internal static class Helper
Expand Down Expand Up @@ -628,7 +620,7 @@ namespace TestNamespace
{
MethodContext.ReplyError("org.freedesktop.DBus.Error.UnknownProperty", $"Unknown property: {Property}");
}
public ValueTask Handle(IReadableSettingsProperties properties)
public ValueTask Handle(ISettingsProperties properties)
{
switch (Property)
{
Expand All @@ -653,7 +645,7 @@ namespace TestNamespace
MethodContext = methodContext;
}
public void Dispose() => MethodContext.Dispose();
public ValueTask Handle(IReadableSettingsProperties properties)
public ValueTask Handle(ISettingsProperties properties)
{
var writer = MethodContext.CreateReplyWriter("a{sv}");
var dictStart = writer.WriteDictionaryStart();
Expand Down Expand Up @@ -762,7 +754,7 @@ namespace TestNamespace
member: "Changed");
c.TrySendMessage(writer.CreateMessage());
}
public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, IReadableSettingsProperties properties, ReadOnlySpan<SettingsProperty> changed, ReadOnlySpan<SettingsProperty> invalidated = default)
public static void EmitPropertiesChanged(this DBusConnection c, ObjectPath p, ISettingsProperties properties, ReadOnlySpan<SettingsProperty> changed, ReadOnlySpan<SettingsProperty> invalidated = default)
{
var writer = c.GetMessageWriter();
writer.WriteSignalHeader(
Expand Down Expand Up @@ -805,7 +797,7 @@ namespace TestNamespace
writer.WriteArrayEnd(arrayStart);
c.TrySendMessage(writer.CreateMessage());
}
public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, IReadableSettingsProperties properties, SettingsProperty changed)
public static void EmitPropertyChanged(this DBusConnection c, ObjectPath p, ISettingsProperties properties, SettingsProperty changed)
{
EmitPropertiesChanged(c, p, properties, stackalloc[] { changed }, default);
}
Expand Down
Loading
Loading