TupleSerializer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 TupleSerializer : IProtoTypeSerializer
  13. {
  14. private readonly MemberInfo[] members;
  15. private readonly ConstructorInfo ctor;
  16. private IProtoSerializer[] tails;
  17. public TupleSerializer(RuntimeTypeModel model, ConstructorInfo ctor, MemberInfo[] members)
  18. {
  19. if (ctor == null) throw new ArgumentNullException("ctor");
  20. if (members == null) throw new ArgumentNullException("members");
  21. this.ctor = ctor;
  22. this.members = members;
  23. this.tails = new IProtoSerializer[members.Length];
  24. ParameterInfo[] parameters = ctor.GetParameters();
  25. for (int i = 0; i < members.Length; i++)
  26. {
  27. WireType wireType;
  28. Type finalType = parameters[i].ParameterType;
  29. Type itemType = null, defaultType = null;
  30. MetaType.ResolveListTypes(model, finalType, ref itemType, ref defaultType);
  31. Type tmp = itemType == null ? finalType : itemType;
  32. bool asReference = false;
  33. int typeIndex = model.FindOrAddAuto(tmp, false, true, false);
  34. if (typeIndex >= 0)
  35. {
  36. asReference = model[tmp].AsReferenceDefault;
  37. }
  38. IProtoSerializer tail = ValueMember.TryGetCoreSerializer(model, DataFormat.Default, tmp, out wireType, asReference, false, false, true), serializer;
  39. if (tail == null)
  40. {
  41. throw new InvalidOperationException("No serializer defined for type: " + tmp.FullName);
  42. }
  43. tail = new TagDecorator(i + 1, wireType, false, tail);
  44. if (itemType == null)
  45. {
  46. serializer = tail;
  47. }
  48. else
  49. {
  50. if (finalType.IsArray)
  51. {
  52. serializer = new ArrayDecorator(model, tail, i + 1, false, wireType, finalType, false, false);
  53. }
  54. else
  55. {
  56. serializer = ListDecorator.Create(model, finalType, defaultType, tail, i + 1, false, wireType, true, false, false);
  57. }
  58. }
  59. tails[i] = serializer;
  60. }
  61. }
  62. public bool HasCallbacks(Meta.TypeModel.CallbackType callbackType)
  63. {
  64. return false;
  65. }
  66. #if FEAT_COMPILER
  67. public void EmitCallback(Compiler.CompilerContext ctx, Compiler.Local valueFrom, Meta.TypeModel.CallbackType callbackType) { }
  68. #endif
  69. public Type ExpectedType
  70. {
  71. get { return ctor.DeclaringType; }
  72. }
  73. #if !FEAT_IKVM
  74. void IProtoTypeSerializer.Callback(object value, Meta.TypeModel.CallbackType callbackType, SerializationContext context) { }
  75. object IProtoTypeSerializer.CreateInstance(ProtoReader source) { throw new NotSupportedException(); }
  76. private object GetValue(object obj, int index)
  77. {
  78. PropertyInfo prop;
  79. FieldInfo field;
  80. if ((prop = members[index] as PropertyInfo) != null)
  81. {
  82. if (obj == null)
  83. return Helpers.IsValueType(prop.PropertyType) ? Activator.CreateInstance(prop.PropertyType) : null;
  84. return prop.GetValue(obj, null);
  85. }
  86. else if ((field = members[index] as FieldInfo) != null)
  87. {
  88. if (obj == null)
  89. return Helpers.IsValueType(field.FieldType) ? Activator.CreateInstance(field.FieldType) : null;
  90. return field.GetValue(obj);
  91. }
  92. else
  93. {
  94. throw new InvalidOperationException();
  95. }
  96. }
  97. public object Read(object value, ProtoReader source)
  98. {
  99. object[] values = new object[members.Length];
  100. bool invokeCtor = false;
  101. if (value == null)
  102. {
  103. invokeCtor = true;
  104. }
  105. for (int i = 0; i < values.Length; i++)
  106. values[i] = GetValue(value, i);
  107. int field;
  108. while ((field = source.ReadFieldHeader()) > 0)
  109. {
  110. invokeCtor = true;
  111. if (field <= tails.Length)
  112. {
  113. IProtoSerializer tail = tails[field - 1];
  114. values[field - 1] = tails[field - 1].Read(tail.RequiresOldValue ? values[field - 1] : null, source);
  115. }
  116. else
  117. {
  118. source.SkipField();
  119. }
  120. }
  121. return invokeCtor ? ctor.Invoke(values) : value;
  122. }
  123. public void Write(object value, ProtoWriter dest)
  124. {
  125. for (int i = 0; i < tails.Length; i++)
  126. {
  127. object val = GetValue(value, i);
  128. if (val != null) tails[i].Write(val, dest);
  129. }
  130. }
  131. #endif
  132. public bool RequiresOldValue
  133. {
  134. get { return true; }
  135. }
  136. public bool ReturnsValue
  137. {
  138. get { return false; }
  139. }
  140. Type GetMemberType(int index)
  141. {
  142. Type result = Helpers.GetMemberType(members[index]);
  143. if (result == null) throw new InvalidOperationException();
  144. return result;
  145. }
  146. bool IProtoTypeSerializer.CanCreateInstance() { return false; }
  147. #if FEAT_COMPILER
  148. public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  149. {
  150. using (Compiler.Local loc = ctx.GetLocalWithValue(ctor.DeclaringType, valueFrom))
  151. {
  152. for (int i = 0; i < tails.Length; i++)
  153. {
  154. Type type = GetMemberType(i);
  155. ctx.LoadAddress(loc, ExpectedType);
  156. if (members[i] is FieldInfo)
  157. {
  158. ctx.LoadValue((FieldInfo)members[i]);
  159. }
  160. else if (members[i] is PropertyInfo)
  161. {
  162. ctx.LoadValue((PropertyInfo)members[i]);
  163. }
  164. ctx.WriteNullCheckedTail(type, tails[i], null);
  165. }
  166. }
  167. }
  168. void IProtoTypeSerializer.EmitCreateInstance(Compiler.CompilerContext ctx) { throw new NotSupportedException(); }
  169. public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
  170. {
  171. using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
  172. {
  173. Compiler.Local[] locals = new Compiler.Local[members.Length];
  174. try
  175. {
  176. for (int i = 0; i < locals.Length; i++)
  177. {
  178. Type type = GetMemberType(i);
  179. bool store = true;
  180. locals[i] = new Compiler.Local(ctx, type);
  181. if (!Helpers.IsValueType(ExpectedType))
  182. {
  183. // value-types always read the old value
  184. if (Helpers.IsValueType(type))
  185. {
  186. switch (Helpers.GetTypeCode(type))
  187. {
  188. case ProtoTypeCode.Boolean:
  189. case ProtoTypeCode.Byte:
  190. case ProtoTypeCode.Int16:
  191. case ProtoTypeCode.Int32:
  192. case ProtoTypeCode.SByte:
  193. case ProtoTypeCode.UInt16:
  194. case ProtoTypeCode.UInt32:
  195. ctx.LoadValue(0);
  196. break;
  197. case ProtoTypeCode.Int64:
  198. case ProtoTypeCode.UInt64:
  199. ctx.LoadValue(0L);
  200. break;
  201. case ProtoTypeCode.Single:
  202. ctx.LoadValue(0.0F);
  203. break;
  204. case ProtoTypeCode.Double:
  205. ctx.LoadValue(0.0D);
  206. break;
  207. case ProtoTypeCode.Decimal:
  208. ctx.LoadValue(0M);
  209. break;
  210. case ProtoTypeCode.Guid:
  211. ctx.LoadValue(Guid.Empty);
  212. break;
  213. default:
  214. ctx.LoadAddress(locals[i], type);
  215. ctx.EmitCtor(type);
  216. store = false;
  217. break;
  218. }
  219. }
  220. else
  221. {
  222. ctx.LoadNullRef();
  223. }
  224. if (store)
  225. {
  226. ctx.StoreValue(locals[i]);
  227. }
  228. }
  229. }
  230. Compiler.CodeLabel skipOld = Helpers.IsValueType(ExpectedType)
  231. ? new Compiler.CodeLabel()
  232. : ctx.DefineLabel();
  233. if (!Helpers.IsValueType(ExpectedType))
  234. {
  235. ctx.LoadAddress(objValue, ExpectedType);
  236. ctx.BranchIfFalse(skipOld, false);
  237. }
  238. for (int i = 0; i < members.Length; i++)
  239. {
  240. ctx.LoadAddress(objValue, ExpectedType);
  241. if (members[i] is FieldInfo)
  242. {
  243. ctx.LoadValue((FieldInfo)members[i]);
  244. }
  245. else if (members[i] is PropertyInfo)
  246. {
  247. ctx.LoadValue((PropertyInfo)members[i]);
  248. }
  249. ctx.StoreValue(locals[i]);
  250. }
  251. if (!Helpers.IsValueType(ExpectedType)) ctx.MarkLabel(skipOld);
  252. using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
  253. {
  254. Compiler.CodeLabel @continue = ctx.DefineLabel(),
  255. processField = ctx.DefineLabel(),
  256. notRecognised = ctx.DefineLabel();
  257. ctx.Branch(@continue, false);
  258. Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length];
  259. for (int i = 0; i < members.Length; i++)
  260. {
  261. handlers[i] = ctx.DefineLabel();
  262. }
  263. ctx.MarkLabel(processField);
  264. ctx.LoadValue(fieldNumber);
  265. ctx.LoadValue(1);
  266. ctx.Subtract(); // jump-table is zero-based
  267. ctx.Switch(handlers);
  268. // and the default:
  269. ctx.Branch(notRecognised, false);
  270. for (int i = 0; i < handlers.Length; i++)
  271. {
  272. ctx.MarkLabel(handlers[i]);
  273. IProtoSerializer tail = tails[i];
  274. Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null;
  275. ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded);
  276. if (tail.ReturnsValue)
  277. {
  278. if (Helpers.IsValueType(locals[i].Type))
  279. {
  280. ctx.StoreValue(locals[i]);
  281. }
  282. else
  283. {
  284. Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel();
  285. ctx.CopyValue();
  286. ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign"
  287. ctx.DiscardValue();
  288. ctx.Branch(allDone, true);
  289. ctx.MarkLabel(hasValue);
  290. ctx.StoreValue(locals[i]);
  291. ctx.MarkLabel(allDone);
  292. }
  293. }
  294. ctx.Branch(@continue, false);
  295. }
  296. ctx.MarkLabel(notRecognised);
  297. ctx.LoadReaderWriter();
  298. ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));
  299. ctx.MarkLabel(@continue);
  300. ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
  301. ctx.CopyValue();
  302. ctx.StoreValue(fieldNumber);
  303. ctx.LoadValue(0);
  304. ctx.BranchIfGreater(processField, false);
  305. }
  306. for (int i = 0; i < locals.Length; i++)
  307. {
  308. ctx.LoadValue(locals[i]);
  309. }
  310. ctx.EmitCtor(ctor);
  311. ctx.StoreValue(objValue);
  312. }
  313. finally
  314. {
  315. for (int i = 0; i < locals.Length; i++)
  316. {
  317. if (locals[i] != null)
  318. locals[i].Dispose(); // release for re-use
  319. }
  320. }
  321. }
  322. }
  323. #endif
  324. }
  325. }
  326. #endif