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