XmlRpcValueBasic.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. namespace XmlRpcHandler
  17. {
  18. public class XmlRpcValueBasic : IXmlRpcValue
  19. {
  20. public XmlRpcValueBasic()
  21. : base()
  22. { }
  23. public XmlRpcValueBasic(string data)
  24. : base(data)
  25. { this.type = XmlRpcBasicValueType.String; }
  26. public XmlRpcValueBasic(int data)
  27. : base(data)
  28. { this.type = XmlRpcBasicValueType.Int; }
  29. public XmlRpcValueBasic(double data)
  30. : base(data)
  31. { this.type = XmlRpcBasicValueType.Double; }
  32. public XmlRpcValueBasic(DateTime data)
  33. : base(data)
  34. { this.type = XmlRpcBasicValueType.dateTime_iso8601; }
  35. public XmlRpcValueBasic(bool data)
  36. : base(data)
  37. { this.type = XmlRpcBasicValueType.Boolean; }
  38. public XmlRpcValueBasic(long data)
  39. : base(data)
  40. { this.type = XmlRpcBasicValueType.base64; }
  41. public XmlRpcValueBasic(object data, XmlRpcBasicValueType type)
  42. : base(data)
  43. { this.type = type; }
  44. private XmlRpcBasicValueType type = XmlRpcBasicValueType.String;
  45. /// <summary>
  46. /// Get or set the type of this basic value
  47. /// </summary>
  48. public XmlRpcBasicValueType ValueType { get { return type; } set { type = value; } }
  49. /*Oprators. help a lot.*/
  50. public static implicit operator string(XmlRpcValueBasic f)
  51. {
  52. if (f.type == XmlRpcBasicValueType.String)
  53. return f.Data.ToString();
  54. else
  55. throw new Exception("Unable to convert, this value is not string type.");
  56. }
  57. public static implicit operator int(XmlRpcValueBasic f)
  58. {
  59. if (f.type == XmlRpcBasicValueType.String)
  60. return (int)f.Data;
  61. else
  62. throw new Exception("Unable to convert, this value is not int type.");
  63. }
  64. public static implicit operator double(XmlRpcValueBasic f)
  65. {
  66. if (f.type == XmlRpcBasicValueType.String)
  67. return (double)f.Data;
  68. else
  69. throw new Exception("Unable to convert, this value is not double type.");
  70. }
  71. public static implicit operator bool(XmlRpcValueBasic f)
  72. {
  73. if (f.type == XmlRpcBasicValueType.String)
  74. return (bool)f.Data;
  75. else
  76. throw new Exception("Unable to convert, this value is not bool type.");
  77. }
  78. public static implicit operator long(XmlRpcValueBasic f)
  79. {
  80. if (f.type == XmlRpcBasicValueType.String)
  81. return (long)f.Data;
  82. else
  83. throw new Exception("Unable to convert, this value is not long type.");
  84. }
  85. public static implicit operator DateTime(XmlRpcValueBasic f)
  86. {
  87. if (f.type == XmlRpcBasicValueType.String)
  88. return (DateTime)f.Data;
  89. else
  90. throw new Exception("Unable to convert, this value is not DateTime type.");
  91. }
  92. }
  93. }