Null.hx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (C)2005-2017 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('//This function is here to be used with Reflection, when the haxe.lang.Null type is known
  24. public static haxe.lang.Null<T> _ofDynamic(object obj) {
  25. if (obj == null) {
  26. return new haxe.lang.Null<T>(default(T), false);
  27. } else if (typeof(T).Equals(typeof(double))) {
  28. return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toDouble(obj), true);
  29. } else if (typeof(T).Equals(typeof(int))) {
  30. return new haxe.lang.Null<T>((T) (object) haxe.lang.Runtime.toInt(obj), true);
  31. } else {
  32. return new haxe.lang.Null<T>((T) obj, true);
  33. }
  34. }
  35. public static implicit operator haxe.lang.Null<T>(T val) {
  36. return new haxe.lang.Null<T>(val, true);
  37. }
  38. public static implicit operator Null<T>(__NoValue__ noValue) {
  39. return new haxe.lang.Null<T>(default(T), false);
  40. }
  41. public sealed class __NoValue__ {
  42. private __NoValue__() {}
  43. }
  44. ')
  45. #if core_api_serialize
  46. @:meta(System.Serializable)
  47. #end
  48. @:keep @:struct @:nativeGen @:native("haxe.lang.Null") private class Nullable<T>
  49. {
  50. @:readOnly public var value(default,never):T;
  51. @:readOnly public var hasValue(default,never):Bool;
  52. public function new(v:T, hasValue:Bool)
  53. {
  54. if (hasValue && cs.system.Object.ReferenceEquals(v, null))
  55. {
  56. hasValue = false;
  57. }
  58. untyped this.value = v;
  59. untyped this.hasValue = hasValue;
  60. }
  61. @:functionCode('if (obj == null) {
  62. return new haxe.lang.Null<D>(default(D), false);
  63. } else if (typeof(D).Equals(typeof(double))) {
  64. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
  65. } else if (typeof(D).Equals(typeof(int))) {
  66. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
  67. } else {
  68. return new haxe.lang.Null<D>((D) obj, true);
  69. }')
  70. public static function ofDynamic<D>(obj:Dynamic):Nullable<D>
  71. {
  72. return null;
  73. }
  74. public function toDynamic():Dynamic
  75. {
  76. if (this.hasValue)
  77. return value;
  78. return null;
  79. }
  80. }