PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.Pinch/Interlace.Pinch.Compiler/Test.cs

https://bitbucket.org/VahidN/interlace
C# | 900 lines | 680 code | 208 blank | 12 comment | 56 complexity | a2f795ec1443958d58ef1b18b9953f67 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using Interlace.Pinch.Implementation;
  4. namespace Interlace.Pinch.Test
  5. {
  6. public enum FixQuality
  7. {
  8. None = 3,
  9. Bad = 1,
  10. Good = 2,
  11. Excellent = 4
  12. }
  13. public class MessageFactory : IPinchableFactory
  14. {
  15. static MessageFactory _instance = new MessageFactory();
  16. public object Create(IPinchDecodingContext context)
  17. {
  18. return new Message(context);
  19. }
  20. public static IPinchableFactory Instance
  21. {
  22. get
  23. {
  24. return _instance;
  25. }
  26. }
  27. }
  28. public partial class Message : IPinchable, INotifyPropertyChanged
  29. {
  30. Content _content;
  31. static PinchFieldProperties _contentProperties = new PinchFieldProperties(1, 1, null);
  32. public Message()
  33. {
  34. }
  35. public Message(IPinchDecodingContext context)
  36. {
  37. }
  38. public Content Content
  39. {
  40. get { return _content; }
  41. set
  42. {
  43. _content = value;
  44. FirePropertyChanged("Content");
  45. }
  46. }
  47. int IPinchable.ProtocolVersion
  48. {
  49. get
  50. {
  51. return 2;
  52. }
  53. }
  54. protected virtual void OnAdditionalFutureFields(IPinchDecoder decoder)
  55. {
  56. }
  57. void IPinchable.Encode(IPinchEncoder encoder)
  58. {
  59. encoder.OpenSequence(1);
  60. // Encode fields for version 1:
  61. encoder.EncodeRequiredStructure(_content, _contentProperties);
  62. encoder.CloseSequence();
  63. }
  64. void IPinchable.Decode(IPinchDecoder decoder)
  65. {
  66. int remainingFields = decoder.OpenSequence();
  67. // Decode members for version 1:
  68. if (remainingFields >= 1)
  69. {
  70. _content = (Content)decoder.DecodeRequiredStructure(ContentFactory.Instance, _contentProperties);
  71. remainingFields -= 1;
  72. }
  73. else
  74. {
  75. if (remainingFields != 0) throw new PinchInvalidCodingException();
  76. }
  77. if (remainingFields > 0)
  78. {
  79. OnAdditionalFutureFields(decoder);
  80. decoder.SkipFields(remainingFields);
  81. }
  82. decoder.CloseSequence();
  83. }
  84. #region INotifyPropertyChanged Members
  85. public event PropertyChangedEventHandler PropertyChanged;
  86. protected void FirePropertyChanged(string propertyName)
  87. {
  88. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  89. }
  90. #endregion
  91. }
  92. public class ContentFactory : IPinchableFactory
  93. {
  94. static ContentFactory _instance = new ContentFactory();
  95. public object Create(IPinchDecodingContext context)
  96. {
  97. return new Content(context);
  98. }
  99. public static IPinchableFactory Instance
  100. {
  101. get
  102. {
  103. return _instance;
  104. }
  105. }
  106. }
  107. public enum ContentKind
  108. {
  109. None = 0,
  110. Fixes = 2,
  111. Exception = 1,
  112. }
  113. public partial class Content : IPinchable, INotifyPropertyChanged
  114. {
  115. object _value;
  116. ContentKind _valueKind;
  117. static PinchFieldProperties _fixesProperties = new PinchFieldProperties(2, 1, null);
  118. static PinchFieldProperties _exceptionProperties = new PinchFieldProperties(1, 1, null);
  119. public Content(Fixes value)
  120. {
  121. _value = value;
  122. _valueKind = ContentKind.Fixes;
  123. }
  124. public static implicit operator Content(Fixes value)
  125. {
  126. return new Content(value);
  127. }
  128. public Content(Exception value)
  129. {
  130. _value = value;
  131. _valueKind = ContentKind.Exception;
  132. }
  133. public static implicit operator Content(Exception value)
  134. {
  135. return new Content(value);
  136. }
  137. public Content()
  138. {
  139. _value = null;
  140. _valueKind = ContentKind.None;
  141. }
  142. public Content(IPinchDecodingContext context)
  143. {
  144. }
  145. public object Value
  146. {
  147. get { return _value; }
  148. }
  149. public ContentKind ValueKind
  150. {
  151. get { return _valueKind; }
  152. }
  153. public Fixes Fixes
  154. {
  155. get { return _valueKind == ContentKind.Fixes ? (Fixes)_value : null; }
  156. set
  157. {
  158. ContentKind existingKind = _valueKind;
  159. _value = value;
  160. _valueKind = ContentKind.Fixes;
  161. if (existingKind != _valueKind) FirePropertyChanged(existingKind);
  162. FirePropertyChanged(_valueKind);
  163. }
  164. }
  165. public Exception Exception
  166. {
  167. get { return _valueKind == ContentKind.Exception ? (Exception)_value : null; }
  168. set
  169. {
  170. ContentKind existingKind = _valueKind;
  171. _value = value;
  172. _valueKind = ContentKind.Exception;
  173. if (existingKind != _valueKind) FirePropertyChanged(existingKind);
  174. FirePropertyChanged(_valueKind);
  175. }
  176. }
  177. int IPinchable.ProtocolVersion
  178. {
  179. get
  180. {
  181. return 2;
  182. }
  183. }
  184. public void FirePropertyChanged(ContentKind kind)
  185. {
  186. switch (kind)
  187. {
  188. case ContentKind.None:
  189. break;
  190. case ContentKind.Fixes:
  191. FirePropertyChanged("Fixes");
  192. break;
  193. case ContentKind.Exception:
  194. FirePropertyChanged("Exception");
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. void IPinchable.Encode(IPinchEncoder encoder)
  201. {
  202. encoder.EncodeChoiceMarker((int)_valueKind);
  203. switch (_valueKind)
  204. {
  205. case ContentKind.None:
  206. throw new PinchNullRequiredFieldException();
  207. case ContentKind.Fixes:
  208. encoder.EncodeRequiredStructure((IPinchable)_value, _fixesProperties);
  209. break;
  210. case ContentKind.Exception:
  211. encoder.EncodeRequiredStructure((IPinchable)_value, _exceptionProperties);
  212. break;
  213. }
  214. }
  215. void IPinchable.Decode(IPinchDecoder decoder)
  216. {
  217. _valueKind = (ContentKind)decoder.DecodeChoiceMarker();
  218. switch (_valueKind)
  219. {
  220. case ContentKind.Fixes:
  221. _value = decoder.DecodeRequiredStructure(FixesFactory.Instance, _fixesProperties);
  222. break;
  223. case ContentKind.Exception:
  224. _value = decoder.DecodeRequiredStructure(ExceptionFactory.Instance, _exceptionProperties);
  225. break;
  226. default:
  227. throw new PinchInvalidCodingException();
  228. }
  229. }
  230. #region INotifyPropertyChanged Members
  231. public event PropertyChangedEventHandler PropertyChanged;
  232. protected void FirePropertyChanged(string propertyName)
  233. {
  234. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  235. }
  236. #endregion
  237. }
  238. public class ExceptionFactory : IPinchableFactory
  239. {
  240. static ExceptionFactory _instance = new ExceptionFactory();
  241. public object Create(IPinchDecodingContext context)
  242. {
  243. return new Exception(context);
  244. }
  245. public static IPinchableFactory Instance
  246. {
  247. get
  248. {
  249. return _instance;
  250. }
  251. }
  252. }
  253. public partial class Exception : IPinchable, INotifyPropertyChanged
  254. {
  255. string _message;
  256. static PinchFieldProperties _messageProperties = new PinchFieldProperties(1, 1, null);
  257. public Exception()
  258. {
  259. }
  260. public Exception(IPinchDecodingContext context)
  261. {
  262. }
  263. public string Message
  264. {
  265. get { return _message; }
  266. set
  267. {
  268. _message = value;
  269. FirePropertyChanged("Message");
  270. }
  271. }
  272. int IPinchable.ProtocolVersion
  273. {
  274. get
  275. {
  276. return 2;
  277. }
  278. }
  279. protected virtual void OnAdditionalFutureFields(IPinchDecoder decoder)
  280. {
  281. }
  282. void IPinchable.Encode(IPinchEncoder encoder)
  283. {
  284. encoder.OpenSequence(1);
  285. // Encode fields for version 1:
  286. encoder.EncodeRequiredString(_message, _messageProperties);
  287. encoder.CloseSequence();
  288. }
  289. void IPinchable.Decode(IPinchDecoder decoder)
  290. {
  291. int remainingFields = decoder.OpenSequence();
  292. // Decode members for version 1:
  293. if (remainingFields >= 1)
  294. {
  295. _message = (string)decoder.DecodeRequiredString(_messageProperties);
  296. remainingFields -= 1;
  297. }
  298. else
  299. {
  300. if (remainingFields != 0) throw new PinchInvalidCodingException();
  301. }
  302. if (remainingFields > 0)
  303. {
  304. OnAdditionalFutureFields(decoder);
  305. decoder.SkipFields(remainingFields);
  306. }
  307. decoder.CloseSequence();
  308. }
  309. #region INotifyPropertyChanged Members
  310. public event PropertyChangedEventHandler PropertyChanged;
  311. protected void FirePropertyChanged(string propertyName)
  312. {
  313. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  314. }
  315. #endregion
  316. }
  317. public class FixesFactory : IPinchableFactory
  318. {
  319. static FixesFactory _instance = new FixesFactory();
  320. public object Create(IPinchDecodingContext context)
  321. {
  322. return new Fixes(context);
  323. }
  324. public static IPinchableFactory Instance
  325. {
  326. get
  327. {
  328. return _instance;
  329. }
  330. }
  331. }
  332. public partial class Fixes : IPinchable, INotifyPropertyChanged
  333. {
  334. List<Fix> _fix;
  335. static PinchFieldProperties _fixProperties = new PinchFieldProperties(1, 1, null);
  336. public Fixes()
  337. {
  338. _fix = new List<Fix>();
  339. }
  340. public Fixes(IPinchDecodingContext context)
  341. {
  342. }
  343. public List<Fix> Fix
  344. {
  345. get { return _fix; }
  346. set
  347. {
  348. _fix = value;
  349. FirePropertyChanged("Fix");
  350. }
  351. }
  352. int IPinchable.ProtocolVersion
  353. {
  354. get
  355. {
  356. return 2;
  357. }
  358. }
  359. protected virtual void OnAdditionalFutureFields(IPinchDecoder decoder)
  360. {
  361. }
  362. void IPinchable.Encode(IPinchEncoder encoder)
  363. {
  364. encoder.OpenSequence(1);
  365. // Encode fields for version 1:
  366. encoder.OpenSequence(_fix.Count);
  367. foreach (Fix value in _fix)
  368. {
  369. encoder.EncodeRequiredStructure(value, _fixProperties);
  370. }
  371. encoder.CloseSequence();
  372. encoder.CloseSequence();
  373. }
  374. void IPinchable.Decode(IPinchDecoder decoder)
  375. {
  376. int remainingFields = decoder.OpenSequence();
  377. // Decode members for version 1:
  378. if (remainingFields >= 1)
  379. {
  380. int fixCount = decoder.OpenSequence();
  381. _fix = new List<Fix>();
  382. for (int i = 0; i < fixCount; i++)
  383. {
  384. _fix.Add((Fix)decoder.DecodeRequiredStructure(FixFactory.Instance, _fixProperties));
  385. }
  386. decoder.CloseSequence();
  387. remainingFields -= 1;
  388. }
  389. else
  390. {
  391. if (remainingFields != 0) throw new PinchInvalidCodingException();
  392. }
  393. if (remainingFields > 0)
  394. {
  395. OnAdditionalFutureFields(decoder);
  396. decoder.SkipFields(remainingFields);
  397. }
  398. decoder.CloseSequence();
  399. }
  400. #region INotifyPropertyChanged Members
  401. public event PropertyChangedEventHandler PropertyChanged;
  402. protected void FirePropertyChanged(string propertyName)
  403. {
  404. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  405. }
  406. #endregion
  407. }
  408. public class TimestampSurrogateFactory : IPinchableFactory
  409. {
  410. static TimestampSurrogateFactory _instance = new TimestampSurrogateFactory();
  411. public object Create(IPinchDecodingContext context)
  412. {
  413. return new TimestampSurrogate(context);
  414. }
  415. public static IPinchableFactory Instance
  416. {
  417. get
  418. {
  419. return _instance;
  420. }
  421. }
  422. }
  423. public partial class TimestampSurrogate : IPinchable, INotifyPropertyChanged
  424. {
  425. long _ticks;
  426. static PinchFieldProperties _ticksProperties = new PinchFieldProperties(1, 1, null);
  427. public TimestampSurrogate()
  428. {
  429. }
  430. public TimestampSurrogate(IPinchDecodingContext context)
  431. {
  432. }
  433. public long Ticks
  434. {
  435. get { return _ticks; }
  436. set
  437. {
  438. _ticks = value;
  439. FirePropertyChanged("Ticks");
  440. }
  441. }
  442. int IPinchable.ProtocolVersion
  443. {
  444. get
  445. {
  446. return 2;
  447. }
  448. }
  449. protected virtual void OnAdditionalFutureFields(IPinchDecoder decoder)
  450. {
  451. }
  452. void IPinchable.Encode(IPinchEncoder encoder)
  453. {
  454. encoder.OpenSequence(1);
  455. // Encode fields for version 1:
  456. encoder.EncodeRequiredInt64(_ticks, _ticksProperties);
  457. encoder.CloseSequence();
  458. }
  459. void IPinchable.Decode(IPinchDecoder decoder)
  460. {
  461. int remainingFields = decoder.OpenSequence();
  462. // Decode members for version 1:
  463. if (remainingFields >= 1)
  464. {
  465. _ticks = (long)decoder.DecodeRequiredInt64(_ticksProperties);
  466. remainingFields -= 1;
  467. }
  468. else
  469. {
  470. if (remainingFields != 0) throw new PinchInvalidCodingException();
  471. }
  472. if (remainingFields > 0)
  473. {
  474. OnAdditionalFutureFields(decoder);
  475. decoder.SkipFields(remainingFields);
  476. }
  477. decoder.CloseSequence();
  478. }
  479. #region INotifyPropertyChanged Members
  480. public event PropertyChangedEventHandler PropertyChanged;
  481. protected void FirePropertyChanged(string propertyName)
  482. {
  483. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  484. }
  485. #endregion
  486. #region Surrogate Methods
  487. public static TimestampSurrogate ValueToSurrogateOptional(System.DateTime? value)
  488. {
  489. if (value != null)
  490. {
  491. TimestampSurrogate surrogate = TimestampSurrogate.ValueToSurrogate((System.DateTime)value);
  492. if (surrogate == null) throw new PinchNullRequiredFieldException();
  493. return surrogate;
  494. }
  495. else
  496. {
  497. return null;
  498. }
  499. }
  500. public static TimestampSurrogate ValueToSurrogateRequired(System.DateTime value)
  501. {
  502. TimestampSurrogate surrogate = TimestampSurrogate.ValueToSurrogate(value);
  503. if (surrogate == null) throw new PinchNullRequiredFieldException();
  504. return surrogate;
  505. }
  506. public static System.DateTime? SurrogateToValueOptional(TimestampSurrogate surrogate)
  507. {
  508. if (surrogate != null)
  509. {
  510. return TimestampSurrogate.SurrogateToValue(surrogate);
  511. }
  512. else
  513. {
  514. return null;
  515. }
  516. }
  517. public static System.DateTime SurrogateToValueRequired(TimestampSurrogate surrogate)
  518. {
  519. if (surrogate != null)
  520. {
  521. return TimestampSurrogate.SurrogateToValue(surrogate);
  522. }
  523. else
  524. {
  525. throw new PinchNullRequiredFieldException();
  526. }
  527. }
  528. #endregion
  529. }
  530. public class FixFactory : IPinchableFactory
  531. {
  532. static FixFactory _instance = new FixFactory();
  533. public object Create(IPinchDecodingContext context)
  534. {
  535. return new Fix(context);
  536. }
  537. public static IPinchableFactory Instance
  538. {
  539. get
  540. {
  541. return _instance;
  542. }
  543. }
  544. }
  545. public partial class Fix : IPinchable, INotifyPropertyChanged
  546. {
  547. System.DateTime _when;
  548. System.DateTime? _received;
  549. List<System.DateTime?> _receivedTimes;
  550. float _latitude;
  551. float _longitude;
  552. static PinchFieldProperties _whenProperties = new PinchFieldProperties(4, 1, null);
  553. static PinchFieldProperties _receivedProperties = new PinchFieldProperties(2, 1, null);
  554. static PinchFieldProperties _receivedTimesProperties = new PinchFieldProperties(3, 1, null);
  555. static PinchFieldProperties _latitudeProperties = new PinchFieldProperties(7, 2, null);
  556. static PinchFieldProperties _longitudeProperties = new PinchFieldProperties(8, 2, null);
  557. static PinchFieldProperties _xProperties = new PinchFieldProperties(5, 1, 2);
  558. static PinchFieldProperties _yProperties = new PinchFieldProperties(6, 1, 2);
  559. static PinchFieldProperties _hDOPProperties = new PinchFieldProperties(1, 1, 2);
  560. public Fix()
  561. {
  562. _receivedTimes = new List<System.DateTime?>();
  563. }
  564. public Fix(IPinchDecodingContext context)
  565. {
  566. }
  567. public System.DateTime When
  568. {
  569. get { return _when; }
  570. set
  571. {
  572. _when = value;
  573. FirePropertyChanged("When");
  574. }
  575. }
  576. public System.DateTime? Received
  577. {
  578. get { return _received; }
  579. set
  580. {
  581. _received = value;
  582. FirePropertyChanged("Received");
  583. }
  584. }
  585. public List<System.DateTime?> ReceivedTimes
  586. {
  587. get { return _receivedTimes; }
  588. set
  589. {
  590. _receivedTimes = value;
  591. FirePropertyChanged("ReceivedTimes");
  592. }
  593. }
  594. public float Latitude
  595. {
  596. get { return _latitude; }
  597. set
  598. {
  599. _latitude = value;
  600. FirePropertyChanged("Latitude");
  601. }
  602. }
  603. public float Longitude
  604. {
  605. get { return _longitude; }
  606. set
  607. {
  608. _longitude = value;
  609. FirePropertyChanged("Longitude");
  610. }
  611. }
  612. int IPinchable.ProtocolVersion
  613. {
  614. get
  615. {
  616. return 2;
  617. }
  618. }
  619. protected virtual void OnAdditionalFutureFields(IPinchDecoder decoder)
  620. {
  621. }
  622. void IPinchable.Encode(IPinchEncoder encoder)
  623. {
  624. encoder.OpenSequence(8);
  625. // Encode fields for version 1:
  626. encoder.EncodeRemoved();
  627. encoder.EncodeOptionalStructure(TimestampSurrogate.ValueToSurrogateOptional(_received), _receivedProperties);
  628. encoder.OpenSequence(_receivedTimes.Count);
  629. foreach (System.DateTime? value in _receivedTimes)
  630. {
  631. encoder.EncodeOptionalStructure(TimestampSurrogate.ValueToSurrogateOptional(value), _receivedTimesProperties);
  632. }
  633. encoder.CloseSequence();
  634. encoder.EncodeRequiredStructure(TimestampSurrogate.ValueToSurrogateRequired(_when), _whenProperties);
  635. encoder.EncodeRemoved();
  636. encoder.EncodeRemoved();
  637. // Encode fields for version 2:
  638. encoder.EncodeRequiredFloat32(_latitude, _latitudeProperties);
  639. encoder.EncodeRequiredFloat32(_longitude, _longitudeProperties);
  640. encoder.CloseSequence();
  641. }
  642. void IPinchable.Decode(IPinchDecoder decoder)
  643. {
  644. int remainingFields = decoder.OpenSequence();
  645. // Decode members for version 1:
  646. if (remainingFields >= 6)
  647. {
  648. decoder.SkipRemoved();
  649. _received = TimestampSurrogate.SurrogateToValueOptional((TimestampSurrogate)decoder.DecodeOptionalStructure(TimestampSurrogateFactory.Instance, _receivedProperties));
  650. int receivedTimesCount = decoder.OpenSequence();
  651. _receivedTimes = new List<System.DateTime?>();
  652. for (int i = 0; i < receivedTimesCount; i++)
  653. {
  654. _receivedTimes.Add(TimestampSurrogate.SurrogateToValueOptional((TimestampSurrogate)decoder.DecodeOptionalStructure(TimestampSurrogateFactory.Instance, _receivedTimesProperties)));
  655. }
  656. decoder.CloseSequence();
  657. _when = TimestampSurrogate.SurrogateToValueRequired((TimestampSurrogate)decoder.DecodeRequiredStructure(TimestampSurrogateFactory.Instance, _whenProperties));
  658. decoder.SkipRemoved();
  659. decoder.SkipRemoved();
  660. remainingFields -= 6;
  661. }
  662. else
  663. {
  664. if (remainingFields != 0) throw new PinchInvalidCodingException();
  665. }
  666. // Decode members for version 2:
  667. if (remainingFields >= 2)
  668. {
  669. _latitude = (float)decoder.DecodeRequiredFloat32(_latitudeProperties);
  670. _longitude = (float)decoder.DecodeRequiredFloat32(_longitudeProperties);
  671. remainingFields -= 2;
  672. }
  673. else
  674. {
  675. if (remainingFields != 0) throw new PinchInvalidCodingException();
  676. }
  677. if (remainingFields > 0)
  678. {
  679. OnAdditionalFutureFields(decoder);
  680. decoder.SkipFields(remainingFields);
  681. }
  682. decoder.CloseSequence();
  683. }
  684. #region INotifyPropertyChanged Members
  685. public event PropertyChangedEventHandler PropertyChanged;
  686. protected void FirePropertyChanged(string propertyName)
  687. {
  688. if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  689. }
  690. #endregion
  691. }
  692. }