TupleSerializer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. switch(members[i].MemberType)
  157. {
  158. case MemberTypes.Field:
  159. ctx.LoadValue((FieldInfo)members[i]);
  160. break;
  161. case MemberTypes.Property:
  162. ctx.LoadValue((PropertyInfo)members[i]);
  163. break;
  164. }
  165. ctx.WriteNullCheckedTail(type, tails[i], null);
  166. }
  167. }
  168. }
  169. void IProtoTypeSerializer.EmitCreateInstance(Compiler.CompilerContext ctx) { throw new NotSupportedException(); }
  170. public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
  171. {
  172. using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
  173. {
  174. Compiler.Local[] locals = new Compiler.Local[members.Length];
  175. try
  176. {
  177. for (int i = 0; i < locals.Length; i++)
  178. {
  179. Type type = GetMemberType(i);
  180. bool store = true;
  181. locals[i] = new Compiler.Local(ctx, type);
  182. if (!ExpectedType.IsValueType)
  183. {
  184. // value-types always read the old value
  185. if (type.IsValueType)
  186. {
  187. switch (Helpers.GetTypeCode(type))
  188. {
  189. case ProtoTypeCode.Boolean:
  190. case ProtoTypeCode.Byte:
  191. case ProtoTypeCode.Int16:
  192. case ProtoTypeCode.Int32:
  193. case ProtoTypeCode.SByte:
  194. case ProtoTypeCode.UInt16:
  195. case ProtoTypeCode.UInt32:
  196. ctx.LoadValue(0);
  197. break;
  198. case ProtoTypeCode.Int64:
  199. case ProtoTypeCode.UInt64:
  200. ctx.LoadValue(0L);
  201. break;
  202. case ProtoTypeCode.Single:
  203. ctx.LoadValue(0.0F);
  204. break;
  205. case ProtoTypeCode.Double:
  206. ctx.LoadValue(0.0D);
  207. break;
  208. case ProtoTypeCode.Decimal:
  209. ctx.LoadValue(0M);
  210. break;
  211. case ProtoTypeCode.Guid:
  212. ctx.LoadValue(Guid.Empty);
  213. break;
  214. default:
  215. ctx.LoadAddress(locals[i], type);
  216. ctx.EmitCtor(type);
  217. store = false;
  218. break;
  219. }
  220. }
  221. else
  222. {
  223. ctx.LoadNullRef();
  224. }
  225. if (store)
  226. {
  227. ctx.StoreValue(locals[i]);
  228. }
  229. }
  230. }
  231. Compiler.CodeLabel skipOld = ExpectedType.IsValueType
  232. ? new Compiler.CodeLabel()
  233. : ctx.DefineLabel();
  234. if (!ExpectedType.IsValueType)
  235. {
  236. ctx.LoadAddress(objValue, ExpectedType);
  237. ctx.BranchIfFalse(skipOld, false);
  238. }
  239. for (int i = 0; i < members.Length; i++)
  240. {
  241. ctx.LoadAddress(objValue, ExpectedType);
  242. switch (members[i].MemberType)
  243. {
  244. case MemberTypes.Field:
  245. ctx.LoadValue((FieldInfo) members[i]);
  246. break;
  247. case MemberTypes.Property:
  248. ctx.LoadValue((PropertyInfo) members[i]);
  249. break;
  250. }
  251. ctx.StoreValue(locals[i]);
  252. }
  253. if (!ExpectedType.IsValueType) ctx.MarkLabel(skipOld);
  254. using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof (int))))
  255. {
  256. Compiler.CodeLabel @continue = ctx.DefineLabel(),
  257. processField = ctx.DefineLabel(),
  258. notRecognised = ctx.DefineLabel();
  259. ctx.Branch(@continue, false);
  260. Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length];
  261. for (int i = 0; i < members.Length; i++)
  262. {
  263. handlers[i] = ctx.DefineLabel();
  264. }
  265. ctx.MarkLabel(processField);
  266. ctx.LoadValue(fieldNumber);
  267. ctx.LoadValue(1);
  268. ctx.Subtract(); // jump-table is zero-based
  269. ctx.Switch(handlers);
  270. // and the default:
  271. ctx.Branch(notRecognised, false);
  272. for (int i = 0; i < handlers.Length; i++)
  273. {
  274. ctx.MarkLabel(handlers[i]);
  275. IProtoSerializer tail = tails[i];
  276. Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null;
  277. ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded);
  278. if (tail.ReturnsValue)
  279. {
  280. if (locals[i].Type.IsValueType)
  281. {
  282. ctx.StoreValue(locals[i]);
  283. }
  284. else
  285. {
  286. Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel();
  287. ctx.CopyValue();
  288. ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign"
  289. ctx.DiscardValue();
  290. ctx.Branch(allDone, true);
  291. ctx.MarkLabel(hasValue);
  292. ctx.StoreValue(locals[i]);
  293. ctx.MarkLabel(allDone);
  294. }
  295. }
  296. ctx.Branch(@continue, false);
  297. }
  298. ctx.MarkLabel(notRecognised);
  299. ctx.LoadReaderWriter();
  300. ctx.EmitCall(ctx.MapType(typeof (ProtoReader)).GetMethod("SkipField"));
  301. ctx.MarkLabel(@continue);
  302. ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof (int)));
  303. ctx.CopyValue();
  304. ctx.StoreValue(fieldNumber);
  305. ctx.LoadValue(0);
  306. ctx.BranchIfGreater(processField, false);
  307. }
  308. for (int i = 0; i < locals.Length; i++)
  309. {
  310. ctx.LoadValue(locals[i]);
  311. }
  312. ctx.EmitCtor(ctor);
  313. ctx.StoreValue(objValue);
  314. }
  315. finally
  316. {
  317. for (int i = 0; i < locals.Length; i++)
  318. {
  319. if (locals[i] != null)
  320. locals[i].Dispose(); // release for re-use
  321. }
  322. }
  323. }
  324. }
  325. #endif
  326. }
  327. }
  328. #endif