ProtoMemberAttribute.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. #if FEAT_IKVM
  3. using Type = IKVM.Reflection.Type;
  4. using IKVM.Reflection;
  5. #else
  6. using System.Reflection;
  7. #endif
  8. namespace ProtoBuf
  9. {
  10. /// <summary>
  11. /// Declares a member to be used in protocol-buffer serialization, using
  12. /// the given Tag. A DataFormat may be used to optimise the serialization
  13. /// format (for instance, using zigzag encoding for negative numbers, or
  14. /// fixed-length encoding for large values.
  15. /// </summary>
  16. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field,
  17. AllowMultiple = false, Inherited = true)]
  18. public class ProtoMemberAttribute : Attribute
  19. , IComparable
  20. #if !NO_GENERICS
  21. , IComparable<ProtoMemberAttribute>
  22. #endif
  23. {
  24. /// <summary>
  25. /// Compare with another ProtoMemberAttribute for sorting purposes
  26. /// </summary>
  27. public int CompareTo(object other) { return CompareTo(other as ProtoMemberAttribute); }
  28. /// <summary>
  29. /// Compare with another ProtoMemberAttribute for sorting purposes
  30. /// </summary>
  31. public int CompareTo(ProtoMemberAttribute other)
  32. {
  33. if (other == null) return -1;
  34. if ((object)this == (object)other) return 0;
  35. int result = this.tag.CompareTo(other.tag);
  36. if (result == 0) result = string.CompareOrdinal(this.name, other.name);
  37. return result;
  38. }
  39. /// <summary>
  40. /// Creates a new ProtoMemberAttribute instance.
  41. /// </summary>
  42. /// <param name="tag">Specifies the unique tag used to identify this member within the type.</param>
  43. public ProtoMemberAttribute(int tag) : this(tag, false)
  44. { }
  45. internal ProtoMemberAttribute(int tag, bool forced)
  46. {
  47. if (tag <= 0 && !forced) throw new ArgumentOutOfRangeException("tag");
  48. this.tag = tag;
  49. }
  50. #if !NO_RUNTIME
  51. internal MemberInfo Member;
  52. internal bool TagIsPinned;
  53. #endif
  54. /// <summary>
  55. /// Gets or sets the original name defined in the .proto; not used
  56. /// during serialization.
  57. /// </summary>
  58. public string Name { get { return name; } set { name = value; } }
  59. private string name;
  60. /// <summary>
  61. /// Gets or sets the data-format to be used when encoding this value.
  62. /// </summary>
  63. public DataFormat DataFormat { get { return dataFormat; } set { dataFormat = value; } }
  64. private DataFormat dataFormat;
  65. /// <summary>
  66. /// Gets the unique tag used to identify this member within the type.
  67. /// </summary>
  68. public int Tag { get { return tag; } }
  69. private int tag;
  70. internal void Rebase(int tag) { this.tag = tag; }
  71. /// <summary>
  72. /// Gets or sets a value indicating whether this member is mandatory.
  73. /// </summary>
  74. public bool IsRequired {
  75. get { return (options & MemberSerializationOptions.Required) == MemberSerializationOptions.Required; }
  76. set {
  77. if (value) options |= MemberSerializationOptions.Required;
  78. else options &= ~MemberSerializationOptions.Required;
  79. }
  80. }
  81. /// <summary>
  82. /// Gets a value indicating whether this member is packed.
  83. /// This option only applies to list/array data of primitive types (int, double, etc).
  84. /// </summary>
  85. public bool IsPacked
  86. {
  87. get { return (options & MemberSerializationOptions.Packed) == MemberSerializationOptions.Packed;}
  88. set {
  89. if (value) options |= MemberSerializationOptions.Packed;
  90. else options &= ~MemberSerializationOptions.Packed;
  91. }
  92. }
  93. /// <summary>
  94. /// Indicates whether this field should *repace* existing values (the default is false, meaning *append*).
  95. /// This option only applies to list/array data.
  96. /// </summary>
  97. public bool OverwriteList
  98. {
  99. get { return (options & MemberSerializationOptions.OverwriteList) == MemberSerializationOptions.OverwriteList; }
  100. set
  101. {
  102. if (value) options |= MemberSerializationOptions.OverwriteList;
  103. else options &= ~MemberSerializationOptions.OverwriteList;
  104. }
  105. }
  106. /// <summary>
  107. /// Enables full object-tracking/full-graph support.
  108. /// </summary>
  109. public bool AsReference
  110. {
  111. get { return (options & MemberSerializationOptions.AsReference) == MemberSerializationOptions.AsReference; }
  112. set
  113. {
  114. if (value) options |= MemberSerializationOptions.AsReference;
  115. else options &= ~MemberSerializationOptions.AsReference;
  116. options |= MemberSerializationOptions.AsReferenceHasValue;
  117. }
  118. }
  119. internal bool AsReferenceHasValue
  120. {
  121. get { return (options & MemberSerializationOptions.AsReferenceHasValue) == MemberSerializationOptions.AsReferenceHasValue; }
  122. set {
  123. if (value) options |= MemberSerializationOptions.AsReferenceHasValue;
  124. else options &= ~MemberSerializationOptions.AsReferenceHasValue;
  125. }
  126. }
  127. /// <summary>
  128. /// Embeds the type information into the stream, allowing usage with types not known in advance.
  129. /// </summary>
  130. public bool DynamicType
  131. {
  132. get { return (options & MemberSerializationOptions.DynamicType) == MemberSerializationOptions.DynamicType; }
  133. set
  134. {
  135. if (value) options |= MemberSerializationOptions.DynamicType;
  136. else options &= ~MemberSerializationOptions.DynamicType;
  137. }
  138. }
  139. /// <summary>
  140. /// Gets or sets a value indicating whether this member is packed (lists/arrays).
  141. /// </summary>
  142. public MemberSerializationOptions Options { get { return options; } set { options = value; } }
  143. private MemberSerializationOptions options;
  144. }
  145. /// <summary>
  146. /// Additional (optional) settings that control serialization of members
  147. /// </summary>
  148. [Flags]
  149. public enum MemberSerializationOptions
  150. {
  151. /// <summary>
  152. /// Default; no additional options
  153. /// </summary>
  154. None = 0,
  155. /// <summary>
  156. /// Indicates that repeated elements should use packed (length-prefixed) encoding
  157. /// </summary>
  158. Packed = 1,
  159. /// <summary>
  160. /// Indicates that the given item is required
  161. /// </summary>
  162. Required = 2,
  163. /// <summary>
  164. /// Enables full object-tracking/full-graph support
  165. /// </summary>
  166. AsReference = 4,
  167. /// <summary>
  168. /// Embeds the type information into the stream, allowing usage with types not known in advance
  169. /// </summary>
  170. DynamicType = 8,
  171. /// <summary>
  172. /// Indicates whether this field should *repace* existing values (the default is false, meaning *append*).
  173. /// This option only applies to list/array data.
  174. /// </summary>
  175. OverwriteList = 16,
  176. /// <summary>
  177. /// Determines whether the types AsReferenceDefault value is used, or whether this member's AsReference should be used
  178. /// </summary>
  179. AsReferenceHasValue = 32
  180. }
  181. /// <summary>
  182. /// Declares a member to be used in protocol-buffer serialization, using
  183. /// the given Tag and MemberName. This allows ProtoMemberAttribute usage
  184. /// even for partial classes where the individual members are not
  185. /// under direct control.
  186. /// A DataFormat may be used to optimise the serialization
  187. /// format (for instance, using zigzag encoding for negative numbers, or
  188. /// fixed-length encoding for large values.
  189. /// </summary>
  190. [AttributeUsage(AttributeTargets.Class,
  191. AllowMultiple = true, Inherited = false)]
  192. public sealed class ProtoPartialMemberAttribute : ProtoMemberAttribute
  193. {
  194. /// <summary>
  195. /// Creates a new ProtoMemberAttribute instance.
  196. /// </summary>
  197. /// <param name="tag">Specifies the unique tag used to identify this member within the type.</param>
  198. /// <param name="memberName">Specifies the member to be serialized.</param>
  199. public ProtoPartialMemberAttribute(int tag, string memberName)
  200. : base(tag)
  201. {
  202. if (Helpers.IsNullOrEmpty(memberName)) throw new ArgumentNullException("memberName");
  203. this.memberName = memberName;
  204. }
  205. /// <summary>
  206. /// The name of the member to be serialized.
  207. /// </summary>
  208. public string MemberName { get { return memberName; } }
  209. private readonly string memberName;
  210. }
  211. }