From 95a7a8652af19f25b857aaf5eb9c443fc14d88e8 Mon Sep 17 00:00:00 2001 From: Kuba Ober Date: Wed, 18 Sep 2019 13:23:02 -0400 Subject: [PATCH] Implement TypeId attribute. --- Capnp.Net.Runtime/TypeIdAttribute.cs | 27 +++++++++++++++++++++ capnpc-csharp/Generator/CommonSnippetGen.cs | 14 +++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Capnp.Net.Runtime/TypeIdAttribute.cs diff --git a/Capnp.Net.Runtime/TypeIdAttribute.cs b/Capnp.Net.Runtime/TypeIdAttribute.cs new file mode 100644 index 0000000..098dd03 --- /dev/null +++ b/Capnp.Net.Runtime/TypeIdAttribute.cs @@ -0,0 +1,27 @@ +using System; + +namespace Capnp +{ + /// + /// Annotates an enum, class or interface with its schema type identifier. + /// + [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Class |AttributeTargets.Interface, AllowMultiple = false, Inherited = false)] + public class TypeIdAttribute : Attribute + { + /// + /// Constructs this attribute. + /// + /// The 64-bit type identifier from the schema file. + /// is zero. + public TypeIdAttribute(ulong typeId) + { + if (typeId == 0) throw new ArgumentOutOfRangeException(nameof(typeId), "The value cannot be zero."); + Id = typeId; + } + + /// + /// The schema type identifier. + /// + public ulong Id { get; } + } +} diff --git a/capnpc-csharp/Generator/CommonSnippetGen.cs b/capnpc-csharp/Generator/CommonSnippetGen.cs index 56d974e..40a4fa9 100644 --- a/capnpc-csharp/Generator/CommonSnippetGen.cs +++ b/capnpc-csharp/Generator/CommonSnippetGen.cs @@ -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( + AttributeArgument(HexLiteral(id))))); + + public static SyntaxList MakeTypeIdAttributeLists(ulong id) => + SingletonList( + AttributeList( + SingletonSeparatedList( + CommonSnippetGen.MakeTypeIdAttribute(id)))); } }