EnumSerializer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #if !NO_RUNTIME
  2. using System;
  3. #if FEAT_IKVM
  4. using Type = IKVM.Reflection.Type;
  5. using IKVM.Reflection;
  6. #else
  7. #endif
  8. namespace ProtoBuf.Serializers
  9. {
  10. sealed class EnumSerializer : IProtoSerializer
  11. {
  12. public struct EnumPair
  13. {
  14. public readonly object RawValue; // note that this is boxing, but I'll live with it
  15. #if !FEAT_IKVM
  16. public readonly Enum TypedValue; // note that this is boxing, but I'll live with it
  17. #endif
  18. public readonly int WireValue;
  19. public EnumPair(int wireValue, object raw, Type type)
  20. {
  21. WireValue = wireValue;
  22. RawValue = raw;
  23. #if !FEAT_IKVM
  24. TypedValue = (Enum)Enum.ToObject(type, raw);
  25. #endif
  26. }
  27. }
  28. private readonly Type enumType;
  29. private readonly EnumPair[] map;
  30. public EnumSerializer(Type enumType, EnumPair[] map)
  31. {
  32. if (enumType == null) throw new ArgumentNullException("enumType");
  33. this.enumType = enumType;
  34. this.map = map;
  35. if (map != null)
  36. {
  37. for (int i = 1; i < map.Length; i++)
  38. for (int j = 0 ; j < i ; j++)
  39. {
  40. if (map[i].WireValue == map[j].WireValue && !Equals(map[i].RawValue, map[j].RawValue))
  41. {
  42. throw new ProtoException("Multiple enums with wire-value " + map[i].WireValue.ToString());
  43. }
  44. if (Equals(map[i].RawValue, map[j].RawValue) && map[i].WireValue != map[j].WireValue)
  45. {
  46. throw new ProtoException("Multiple enums with deserialized-value " + map[i].RawValue);
  47. }
  48. }
  49. }
  50. }
  51. private ProtoTypeCode GetTypeCode() {
  52. Type type = Helpers.GetUnderlyingType(enumType);
  53. if(type == null) type = enumType;
  54. if (Helpers.IsEnum(type) && type is ILRuntime.Reflection.ILRuntimeType)
  55. {
  56. Type e_type = Enum.GetUnderlyingType (type);
  57. if (e_type == typeof(long)
  58. || e_type == typeof(uint)
  59. || e_type == typeof(ulong))
  60. return ProtoTypeCode.UInt64;
  61. else
  62. return ProtoTypeCode.UInt32;
  63. }
  64. return Helpers.GetTypeCode(type);
  65. }
  66. public Type ExpectedType { get { return enumType; } }
  67. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  68. bool IProtoSerializer.ReturnsValue { get { return true; } }
  69. #if !FEAT_IKVM
  70. private int EnumToWire(object value)
  71. {
  72. if (value is int)
  73. {
  74. return (int)value;
  75. }
  76. unchecked
  77. {
  78. switch (GetTypeCode())
  79. { // unbox then convert to int
  80. case ProtoTypeCode.Byte: return (int)(byte)value;
  81. case ProtoTypeCode.SByte: return (int)(sbyte)value;
  82. case ProtoTypeCode.Int16: return (int)(short)value;
  83. case ProtoTypeCode.Int32: return (int)value;
  84. case ProtoTypeCode.Int64: return (int)(long)value;
  85. case ProtoTypeCode.UInt16: return (int)(ushort)value;
  86. case ProtoTypeCode.UInt32: return (int)(uint)value;
  87. case ProtoTypeCode.UInt64: return (int)(ulong)value;
  88. default: throw new InvalidOperationException();
  89. }
  90. }
  91. }
  92. private object WireToEnum(int value)
  93. {
  94. //ILRuntime enum就是int
  95. if (Helpers.IsEnum(enumType) && enumType is ILRuntime.Reflection.ILRuntimeType)
  96. {
  97. return value;
  98. }
  99. unchecked
  100. {
  101. switch (GetTypeCode())
  102. { // convert from int then box
  103. case ProtoTypeCode.Byte: return Enum.ToObject(enumType, (byte)value);
  104. case ProtoTypeCode.SByte: return Enum.ToObject(enumType, (sbyte)value);
  105. case ProtoTypeCode.Int16: return Enum.ToObject(enumType, (short)value);
  106. case ProtoTypeCode.Int32: return Enum.ToObject(enumType, value);
  107. case ProtoTypeCode.Int64: return Enum.ToObject(enumType, (long)value);
  108. case ProtoTypeCode.UInt16: return Enum.ToObject(enumType, (ushort)value);
  109. case ProtoTypeCode.UInt32: return Enum.ToObject(enumType, (uint)value);
  110. case ProtoTypeCode.UInt64: return Enum.ToObject(enumType, (ulong)value);
  111. default: throw new InvalidOperationException();
  112. }
  113. }
  114. }
  115. public object Read(object value, ProtoReader source)
  116. {
  117. Helpers.DebugAssert(value == null); // since replaces
  118. int wireValue = source.ReadInt32();
  119. if(map == null) {
  120. return WireToEnum(wireValue);
  121. }
  122. for(int i = 0 ; i < map.Length ; i++) {
  123. if(map[i].WireValue == wireValue) {
  124. return map[i].TypedValue;
  125. }
  126. }
  127. source.ThrowEnumException(ExpectedType, wireValue);
  128. return null; // to make compiler happy
  129. }
  130. public void Write(object value, ProtoWriter dest)
  131. {
  132. if (map == null)
  133. {
  134. ProtoWriter.WriteInt32(EnumToWire(value), dest);
  135. }
  136. else
  137. {
  138. for (int i = 0; i < map.Length; i++)
  139. {
  140. if (object.Equals(map[i].TypedValue, value))
  141. {
  142. ProtoWriter.WriteInt32(map[i].WireValue, dest);
  143. return;
  144. }
  145. }
  146. ProtoWriter.ThrowEnumException(dest, value);
  147. }
  148. }
  149. #endif
  150. #if FEAT_COMPILER
  151. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  152. {
  153. ProtoTypeCode typeCode = GetTypeCode();
  154. if (map == null)
  155. {
  156. ctx.LoadValue(valueFrom);
  157. ctx.ConvertToInt32(typeCode, false);
  158. ctx.EmitBasicWrite("WriteInt32", null);
  159. }
  160. else
  161. {
  162. using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
  163. {
  164. Compiler.CodeLabel @continue = ctx.DefineLabel();
  165. for (int i = 0; i < map.Length; i++)
  166. {
  167. Compiler.CodeLabel tryNextValue = ctx.DefineLabel(), processThisValue = ctx.DefineLabel();
  168. ctx.LoadValue(loc);
  169. WriteEnumValue(ctx, typeCode, map[i].RawValue);
  170. ctx.BranchIfEqual(processThisValue, true);
  171. ctx.Branch(tryNextValue, true);
  172. ctx.MarkLabel(processThisValue);
  173. ctx.LoadValue(map[i].WireValue);
  174. ctx.EmitBasicWrite("WriteInt32", null);
  175. ctx.Branch(@continue, false);
  176. ctx.MarkLabel(tryNextValue);
  177. }
  178. ctx.LoadReaderWriter();
  179. ctx.LoadValue(loc);
  180. ctx.CastToObject(ExpectedType);
  181. ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("ThrowEnumException"));
  182. ctx.MarkLabel(@continue);
  183. }
  184. }
  185. }
  186. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  187. {
  188. ProtoTypeCode typeCode = GetTypeCode();
  189. if (map == null)
  190. {
  191. ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
  192. ctx.ConvertFromInt32(typeCode, false);
  193. }
  194. else
  195. {
  196. int[] wireValues = new int[map.Length];
  197. object[] values = new object[map.Length];
  198. for (int i = 0; i < map.Length; i++)
  199. {
  200. wireValues[i] = map[i].WireValue;
  201. values[i] = map[i].RawValue;
  202. }
  203. using (Compiler.Local result = new Compiler.Local(ctx, ExpectedType))
  204. using (Compiler.Local wireValue = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
  205. {
  206. ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
  207. ctx.StoreValue(wireValue);
  208. Compiler.CodeLabel @continue = ctx.DefineLabel();
  209. foreach (BasicList.Group group in BasicList.GetContiguousGroups(wireValues, values))
  210. {
  211. Compiler.CodeLabel tryNextGroup = ctx.DefineLabel();
  212. int groupItemCount = group.Items.Count;
  213. if (groupItemCount == 1)
  214. {
  215. // discreet group; use an equality test
  216. ctx.LoadValue(wireValue);
  217. ctx.LoadValue(group.First);
  218. Compiler.CodeLabel processThisValue = ctx.DefineLabel();
  219. ctx.BranchIfEqual(processThisValue, true);
  220. ctx.Branch(tryNextGroup, false);
  221. WriteEnumValue(ctx, typeCode, processThisValue, @continue, group.Items[0], @result);
  222. }
  223. else
  224. {
  225. // implement as a jump-table-based switch
  226. ctx.LoadValue(wireValue);
  227. ctx.LoadValue(group.First);
  228. ctx.Subtract(); // jump-tables are zero-based
  229. Compiler.CodeLabel[] jmp = new Compiler.CodeLabel[groupItemCount];
  230. for (int i = 0; i < groupItemCount; i++) {
  231. jmp[i] = ctx.DefineLabel();
  232. }
  233. ctx.Switch(jmp);
  234. // write the default...
  235. ctx.Branch(tryNextGroup, false);
  236. for (int i = 0; i < groupItemCount; i++)
  237. {
  238. WriteEnumValue(ctx, typeCode, jmp[i], @continue, group.Items[i], @result);
  239. }
  240. }
  241. ctx.MarkLabel(tryNextGroup);
  242. }
  243. // throw source.CreateEnumException(ExpectedType, wireValue);
  244. ctx.LoadReaderWriter();
  245. ctx.LoadValue(ExpectedType);
  246. ctx.LoadValue(wireValue);
  247. ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException"));
  248. ctx.MarkLabel(@continue);
  249. ctx.LoadValue(result);
  250. }
  251. }
  252. }
  253. private static void WriteEnumValue(Compiler.CompilerContext ctx, ProtoTypeCode typeCode, object value)
  254. {
  255. switch (typeCode)
  256. {
  257. case ProtoTypeCode.Byte: ctx.LoadValue((int)(byte)value); break;
  258. case ProtoTypeCode.SByte: ctx.LoadValue((int)(sbyte)value); break;
  259. case ProtoTypeCode.Int16: ctx.LoadValue((int)(short)value); break;
  260. case ProtoTypeCode.Int32: ctx.LoadValue((int)(int)value); break;
  261. case ProtoTypeCode.Int64: ctx.LoadValue((long)(long)value); break;
  262. case ProtoTypeCode.UInt16: ctx.LoadValue((int)(ushort)value); break;
  263. case ProtoTypeCode.UInt32: ctx.LoadValue((int)(uint)value); break;
  264. case ProtoTypeCode.UInt64: ctx.LoadValue((long)(ulong)value); break;
  265. default: throw new InvalidOperationException();
  266. }
  267. }
  268. private static void WriteEnumValue(Compiler.CompilerContext ctx, ProtoTypeCode typeCode, Compiler.CodeLabel handler, Compiler.CodeLabel @continue, object value, Compiler.Local local)
  269. {
  270. ctx.MarkLabel(handler);
  271. WriteEnumValue(ctx, typeCode, value);
  272. ctx.StoreValue(local);
  273. ctx.Branch(@continue, false); // "continue"
  274. }
  275. #endif
  276. }
  277. }
  278. #endif