Null.hx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. //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. @:keep @:struct @:nativeGen @:native("haxe.lang.Null") private class Nullable<T>
  40. {
  41. @:readOnly public var value:T;
  42. @:readOnly public var hasValue:Bool;
  43. @:functionCode('
  44. if ( !(v is System.ValueType) && System.Object.ReferenceEquals(v, default(T)))
  45. {
  46. hasValue = false;
  47. }
  48. this.@value = v;
  49. this.hasValue = hasValue;
  50. ')
  51. public function new(v:T, hasValue:Bool)
  52. {
  53. this.value = v;
  54. this.hasValue = hasValue;
  55. }
  56. @:functionCode('
  57. if (obj == null)
  58. {
  59. return new haxe.lang.Null<D>(default(D), false);
  60. } else if (typeof(D).Equals(typeof(double))) {
  61. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toDouble(obj), true);
  62. } else if (typeof(D).Equals(typeof(int))) {
  63. return new haxe.lang.Null<D>((D) (object) haxe.lang.Runtime.toInt(obj), true);
  64. } else {
  65. return new haxe.lang.Null<D>((D) obj, true);
  66. }
  67. ')
  68. public static function ofDynamic<D>(obj:Dynamic):Nullable<D>
  69. {
  70. return null;
  71. }
  72. @:functionCode('
  73. if (this.hasValue)
  74. return value;
  75. return null;
  76. ')
  77. public function toDynamic():Dynamic
  78. {
  79. return null;
  80. }
  81. }