EnumSerializer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. return Helpers.GetTypeCode(type);
  55. }
  56. public Type ExpectedType { get { return enumType; } }
  57. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  58. bool IProtoSerializer.ReturnsValue { get { return true; } }
  59. #if !FEAT_IKVM
  60. private int EnumToWire(object value)
  61. {
  62. if (value is int)
  63. {
  64. return (int)value;
  65. }
  66. unchecked
  67. {
  68. switch (GetTypeCode())
  69. { // unbox then convert to int
  70. case ProtoTypeCode.Byte: return (int)(byte)value;
  71. case ProtoTypeCode.SByte: return (int)(sbyte)value;
  72. case ProtoTypeCode.Int16: return (int)(short)value;
  73. case ProtoTypeCode.Int32: return (int)value;
  74. case ProtoTypeCode.Int64: return (int)(long)value;
  75. case ProtoTypeCode.UInt16: return (int)(ushort)value;
  76. case ProtoTypeCode.UInt32: return (int)(uint)value;
  77. case ProtoTypeCode.UInt64: return (int)(ulong)value;
  78. default: throw new InvalidOperationException();
  79. }
  80. }
  81. }
  82. private object WireToEnum(int value)
  83. {
  84. unchecked
  85. {
  86. switch (GetTypeCode())
  87. { // convert from int then box
  88. case ProtoTypeCode.Byte: return Enum.ToObject(enumType, (byte)value);
  89. case ProtoTypeCode.SByte: return Enum.ToObject(enumType, (sbyte)value);
  90. case ProtoTypeCode.Int16: return Enum.ToObject(enumType, (short)value);
  91. case ProtoTypeCode.Int32: return Enum.ToObject(enumType, value);
  92. case ProtoTypeCode.Int64: return Enum.ToObject(enumType, (long)value);
  93. case ProtoTypeCode.UInt16: return Enum.ToObject(enumType, (ushort)value);
  94. case ProtoTypeCode.UInt32: return Enum.ToObject(enumType, (uint)value);
  95. case ProtoTypeCode.UInt64: return Enum.ToObject(enumType, (ulong)value);
  96. default: throw new InvalidOperationException();
  97. }
  98. }
  99. }
  100. public object Read(object value, ProtoReader source)
  101. {
  102. Helpers.DebugAssert(value == null); // since replaces
  103. int wireValue = source.ReadInt32();
  104. if(map == null) {
  105. return WireToEnum(wireValue);
  106. }
  107. for(int i = 0 ; i < map.Length ; i++) {
  108. if(map[i].WireValue == wireValue) {
  109. return map[i].TypedValue;
  110. }
  111. }
  112. source.ThrowEnumException(ExpectedType, wireValue);
  113. return null; // to make compiler happy
  114. }
  115. public void Write(object value, ProtoWriter dest)
  116. {
  117. if (map == null)
  118. {
  119. ProtoWriter.WriteInt32(EnumToWire(value), dest);
  120. }
  121. else
  122. {
  123. for (int i = 0; i < map.Length; i++)
  124. {
  125. if (object.Equals(map[i].TypedValue, value))
  126. {
  127. ProtoWriter.WriteInt32(map[i].WireValue, dest);
  128. return;
  129. }
  130. }
  131. ProtoWriter.ThrowEnumException(dest, value);
  132. }
  133. }
  134. #endif
  135. #if FEAT_COMPILER
  136. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  137. {
  138. ProtoTypeCode typeCode = GetTypeCode();
  139. if (map == null)
  140. {
  141. ctx.LoadValue(valueFrom);
  142. ctx.ConvertToInt32(typeCode, false);
  143. ctx.EmitBasicWrite("WriteInt32", null);
  144. }
  145. else
  146. {
  147. using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
  148. {
  149. Compiler.CodeLabel @continue = ctx.DefineLabel();
  150. for (int i = 0; i < map.Length; i++)
  151. {
  152. Compiler.CodeLabel tryNextValue = ctx.DefineLabel(), processThisValue = ctx.DefineLabel();
  153. ctx.LoadValue(loc);
  154. WriteEnumValue(ctx, typeCode, map[i].RawValue);
  155. ctx.BranchIfEqual(processThisValue, true);
  156. ctx.Branch(tryNextValue, true);
  157. ctx.MarkLabel(processThisValue);
  158. ctx.LoadValue(map[i].WireValue);
  159. ctx.EmitBasicWrite("WriteInt32", null);
  160. ctx.Branch(@continue, false);
  161. ctx.MarkLabel(tryNextValue);
  162. }
  163. ctx.LoadReaderWriter();
  164. ctx.LoadValue(loc);
  165. ctx.CastToObject(ExpectedType);
  166. ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("ThrowEnumException"));
  167. ctx.MarkLabel(@continue);
  168. }
  169. }
  170. }
  171. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  172. {
  173. ProtoTypeCode typeCode = GetTypeCode();
  174. if (map == null)
  175. {
  176. ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
  177. ctx.ConvertFromInt32(typeCode, false);
  178. }
  179. else
  180. {
  181. int[] wireValues = new int[map.Length];
  182. object[] values = new object[map.Length];
  183. for (int i = 0; i < map.Length; i++)
  184. {
  185. wireValues[i] = map[i].WireValue;
  186. values[i] = map[i].RawValue;
  187. }
  188. using (Compiler.Local result = new Compiler.Local(ctx, ExpectedType))
  189. using (Compiler.Local wireValue = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
  190. {
  191. ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
  192. ctx.StoreValue(wireValue);
  193. Compiler.CodeLabel @continue = ctx.DefineLabel();
  194. foreach (BasicList.Group group in BasicList.GetContiguousGroups(wireValues, values))
  195. {
  196. Compiler.CodeLabel tryNextGroup = ctx.DefineLabel();
  197. int groupItemCount = group.Items.Count;
  198. if (groupItemCount == 1)
  199. {
  200. // discreet group; use an equality test
  201. ctx.LoadValue(wireValue);
  202. ctx.LoadValue(group.First);
  203. Compiler.CodeLabel processThisValue = ctx.DefineLabel();
  204. ctx.BranchIfEqual(processThisValue, true);
  205. ctx.Branch(tryNextGroup, false);
  206. WriteEnumValue(ctx, typeCode, processThisValue, @continue, group.Items[0], @result);
  207. }
  208. else
  209. {
  210. // implement as a jump-table-based switch
  211. ctx.LoadValue(wireValue);
  212. ctx.LoadValue(group.First);
  213. ctx.Subtract(); // jump-tables are zero-based
  214. Compiler.CodeLabel[] jmp = new Compiler.CodeLabel[groupItemCount];
  215. for (int i = 0; i < groupItemCount; i++) {
  216. jmp[i] = ctx.DefineLabel();
  217. }
  218. ctx.Switch(jmp);
  219. // write the default...
  220. ctx.Branch(tryNextGroup, false);
  221. for (int i = 0; i < groupItemCount; i++)
  222. {
  223. WriteEnumValue(ctx, typeCode, jmp[i], @continue, group.Items[i], @result);
  224. }
  225. }
  226. ctx.MarkLabel(tryNextGroup);
  227. }
  228. // throw source.CreateEnumException(ExpectedType, wireValue);
  229. ctx.LoadReaderWriter();
  230. ctx.LoadValue(ExpectedType);
  231. ctx.LoadValue(wireValue);
  232. ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException"));
  233. ctx.MarkLabel(@continue);
  234. ctx.LoadValue(result);
  235. }
  236. }
  237. }
  238. private static void WriteEnumValue(Compiler.CompilerContext ctx, ProtoTypeCode typeCode, object value)
  239. {
  240. switch (typeCode)
  241. {
  242. case ProtoTypeCode.Byte: ctx.LoadValue((int)(byte)value); break;
  243. case ProtoTypeCode.SByte: ctx.LoadValue((int)(sbyte)value); break;
  244. case ProtoTypeCode.Int16: ctx.LoadValue((int)(short)value); break;
  245. case ProtoTypeCode.Int32: ctx.LoadValue((int)(int)value); break;
  246. case ProtoTypeCode.Int64: ctx.LoadValue((long)(long)value); break;
  247. case ProtoTypeCode.UInt16: ctx.LoadValue((int)(ushort)value); break;
  248. case ProtoTypeCode.UInt32: ctx.LoadValue((int)(uint)value); break;
  249. case ProtoTypeCode.UInt64: ctx.LoadValue((long)(ulong)value); break;
  250. default: throw new InvalidOperationException();
  251. }
  252. }
  253. private static void WriteEnumValue(Compiler.CompilerContext ctx, ProtoTypeCode typeCode, Compiler.CodeLabel handler, Compiler.CodeLabel @continue, object value, Compiler.Local local)
  254. {
  255. ctx.MarkLabel(handler);
  256. WriteEnumValue(ctx, typeCode, value);
  257. ctx.StoreValue(local);
  258. ctx.Branch(@continue, false); // "continue"
  259. }
  260. #endif
  261. }
  262. }
  263. #endif