XmlRpcValueBasic.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* This file is part of OpenSubtitles Handler
  2. A library that handle OpenSubtitles.org XML-RPC methods.
  3. Copyright © Ala Ibrahim Hadid 2013
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace XmlRpcHandler
  18. {
  19. public class XmlRpcValueBasic : IXmlRpcValue
  20. {
  21. public XmlRpcValueBasic()
  22. : base()
  23. { }
  24. public XmlRpcValueBasic(string data)
  25. : base(data)
  26. { this.type = XmlRpcBasicValueType.String; }
  27. public XmlRpcValueBasic(int data)
  28. : base(data)
  29. { this.type = XmlRpcBasicValueType.Int; }
  30. public XmlRpcValueBasic(double data)
  31. : base(data)
  32. { this.type = XmlRpcBasicValueType.Double; }
  33. public XmlRpcValueBasic(DateTime data)
  34. : base(data)
  35. { this.type = XmlRpcBasicValueType.dateTime_iso8601; }
  36. public XmlRpcValueBasic(bool data)
  37. : base(data)
  38. { this.type = XmlRpcBasicValueType.Boolean; }
  39. public XmlRpcValueBasic(long data)
  40. : base(data)
  41. { this.type = XmlRpcBasicValueType.base64; }
  42. public XmlRpcValueBasic(object data, XmlRpcBasicValueType type)
  43. : base(data)
  44. { this.type = type; }
  45. private XmlRpcBasicValueType type = XmlRpcBasicValueType.String;
  46. /// <summary>
  47. /// Get or set the type of this basic value
  48. /// </summary>
  49. public XmlRpcBasicValueType ValueType { get { return type; } set { type = value; } }
  50. /*Oprators. help a lot.*/
  51. public static implicit operator string(XmlRpcValueBasic f)
  52. {
  53. if (f.type == XmlRpcBasicValueType.String)
  54. return f.Data.ToString();
  55. else
  56. throw new Exception("Unable to convert, this value is not string type.");
  57. }
  58. public static implicit operator int(XmlRpcValueBasic f)
  59. {
  60. if (f.type == XmlRpcBasicValueType.String)
  61. return (int)f.Data;
  62. else
  63. throw new Exception("Unable to convert, this value is not int type.");
  64. }
  65. public static implicit operator double(XmlRpcValueBasic f)
  66. {
  67. if (f.type == XmlRpcBasicValueType.String)
  68. return (double)f.Data;
  69. else
  70. throw new Exception("Unable to convert, this value is not double type.");
  71. }
  72. public static implicit operator bool(XmlRpcValueBasic f)
  73. {
  74. if (f.type == XmlRpcBasicValueType.String)
  75. return (bool)f.Data;
  76. else
  77. throw new Exception("Unable to convert, this value is not bool type.");
  78. }
  79. public static implicit operator long(XmlRpcValueBasic f)
  80. {
  81. if (f.type == XmlRpcBasicValueType.String)
  82. return (long)f.Data;
  83. else
  84. throw new Exception("Unable to convert, this value is not long type.");
  85. }
  86. public static implicit operator DateTime(XmlRpcValueBasic f)
  87. {
  88. if (f.type == XmlRpcBasicValueType.String)
  89. return (DateTime)f.Data;
  90. else
  91. throw new Exception("Unable to convert, this value is not DateTime type.");
  92. }
  93. }
  94. }