Null.hx 3.1 KB

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