Implement TypeId attribute.

This commit is contained in:
Kuba Ober 2019-09-18 13:23:02 -04:00
parent 056a5101ff
commit 95a7a8652a
2 changed files with 41 additions and 0 deletions

View 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; }
}
}

View File

@ -108,5 +108,19 @@ namespace CapnpC.Generator
new[]{
Token(SyntaxKind.PublicKeyword),
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))));
}
}