mirror of
https://github.com/FabInfra/capnproto-dotnetcore_Runtime.git
synced 2025-03-12 23:01:44 +01:00
Implement TypeId attribute.
This commit is contained in:
parent
056a5101ff
commit
95a7a8652a
27
Capnp.Net.Runtime/TypeIdAttribute.cs
Normal file
27
Capnp.Net.Runtime/TypeIdAttribute.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Capnp
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Annotates an enum, class or interface with its schema type identifier.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class |AttributeTargets.Interface, AllowMultiple = false, Inherited = false)]
|
||||||
|
public class TypeIdAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs this attribute.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="typeId">The 64-bit type identifier from the schema file.</param>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="typeId"/> is zero.</exception>
|
||||||
|
public TypeIdAttribute(ulong typeId)
|
||||||
|
{
|
||||||
|
if (typeId == 0) throw new ArgumentOutOfRangeException(nameof(typeId), "The value cannot be zero.");
|
||||||
|
Id = typeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The schema type identifier.
|
||||||
|
/// </summary>
|
||||||
|
public ulong Id { get; }
|
||||||
|
}
|
||||||
|
}
|
@ -108,5 +108,19 @@ namespace CapnpC.Generator
|
|||||||
new[]{
|
new[]{
|
||||||
Token(SyntaxKind.PublicKeyword),
|
Token(SyntaxKind.PublicKeyword),
|
||||||
Token(SyntaxKind.ConstKeyword)}));
|
Token(SyntaxKind.ConstKeyword)}));
|
||||||
|
|
||||||
|
public static AttributeSyntax MakeTypeIdAttribute(ulong id) =>
|
||||||
|
Attribute(
|
||||||
|
IdentifierName("TypeId"))
|
||||||
|
.WithArgumentList(
|
||||||
|
AttributeArgumentList(
|
||||||
|
SingletonSeparatedList<AttributeArgumentSyntax>(
|
||||||
|
AttributeArgument(HexLiteral(id)))));
|
||||||
|
|
||||||
|
public static SyntaxList<AttributeListSyntax> MakeTypeIdAttributeLists(ulong id) =>
|
||||||
|
SingletonList<AttributeListSyntax>(
|
||||||
|
AttributeList(
|
||||||
|
SingletonSeparatedList<AttributeSyntax>(
|
||||||
|
CommonSnippetGen.MakeTypeIdAttribute(id))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user