Null.hx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (C)2005-2019 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. override public string ToString() {
  45. if (!hasValue) return "null";
  46. else return value.ToString();
  47. }
  48. ')
  49. #if core_api_serialize
  50. @:meta(System.Serializable)
  51. #end
  52. @:keep @:struct @:nativeGen @:native("haxe.lang.Null") private class Nullable<T> {
  53. @:readOnly public var value(default, never):T;
  54. @:readOnly public var hasValue(default, never):Bool;
  55. public function new(v:T, hasValue:Bool) {
  56. if (hasValue && cs.system.Object.ReferenceEquals(v, null)) {
  57. hasValue = false;
  58. }
  59. untyped this.value = v;
  60. untyped this.hasValue = hasValue;
  61. }
  62. @:functionCode('if (obj == null) {
  63. return new haxe.lang.Null<D>(default(D), false);
  64. } else if (typeof(D).Equals(typeof(double))) {
  65. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
  66. } else if (typeof(D).Equals(typeof(int))) {
  67. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
  68. } else {
  69. return new haxe.lang.Null<D>((D) obj, true);
  70. }')
  71. public static function ofDynamic<D>(obj:Dynamic):Nullable<D> {
  72. return null;
  73. }
  74. public function toDynamic():Dynamic {
  75. if (this.hasValue)
  76. return value;
  77. return null;
  78. }
  79. }