EnumSerializer.cs 12 KB

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