EnumSerializer.cs 12 KB

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