Null.hx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cs.internal;
  23. @:classCode('
  24. //This function is here to be used with Reflection, when the haxe.lang.Null type is known
  25. public static haxe.lang.Null<T> _ofDynamic(object obj)
  26. {
  27. if (obj == null)
  28. {
  29. return new haxe.lang.Null<T>(default(T), false);
  30. } else if (typeof(T).Equals(typeof(double))) {
  31. return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toDouble(obj), true);
  32. } else if (typeof(T).Equals(typeof(int))) {
  33. return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toInt(obj), true);
  34. } else {
  35. return new haxe.lang.Null<T>((T) obj, true);
  36. }
  37. }
  38. ')
  39. #if core_api_serialize
  40. @:meta(System.Serializable)
  41. #end
  42. @:keep @:struct @:nativeGen @:native("haxe.lang.Null") private class Nullable<T>
  43. {
  44. @:readOnly public var value(default,never):T;
  45. @:readOnly public var hasValue(default,never):Bool;
  46. public function new(v:T, hasValue:Bool)
  47. {
  48. if (hasValue && cs.system.Object.ReferenceEquals(v, null))
  49. {
  50. hasValue = false;
  51. }
  52. untyped this.value = v;
  53. untyped this.hasValue = hasValue;
  54. }
  55. @:functionCode('
  56. if (obj == null)
  57. {
  58. return new haxe.lang.Null<D>(default(D), false);
  59. } else if (typeof(D).Equals(typeof(double))) {
  60. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
  61. } else if (typeof(D).Equals(typeof(int))) {
  62. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
  63. } else {
  64. return new haxe.lang.Null<D>((D) obj, true);
  65. }
  66. ')
  67. public static function ofDynamic<D>(obj:Dynamic):Nullable<D>
  68. {
  69. return null;
  70. }
  71. public function toDynamic():Dynamic
  72. {
  73. if (this.hasValue)
  74. return value;
  75. return null;
  76. }
  77. }