Pointer.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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;
  23. import cs.StdTypes.Int64;
  24. /**
  25. This type represents pointer types for C# function parameters. It should only
  26. be used inside an unsafe context (not checked by the Haxe compiler)
  27. C# code:
  28. int[] src;
  29. fixed (int* pSrc = src)
  30. {
  31. ...
  32. }
  33. Haxe code:
  34. var src:NativeArray<Int>;
  35. cs.Lib.fixed({
  36. var pSrc:cs.Pointer<Int> = cs.Lib.pointerOfArray(src);
  37. ...
  38. });
  39. **/
  40. #if !unsafe
  41. #error "You need to define 'unsafe' to be able to use unsafe code in hxcs"
  42. #else
  43. @:runtimeValue @:coreType abstract Pointer<T> from Int64 from PointerAccess<T> to PointerAccess<T> {
  44. @:op(A + B) public static function addIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  45. @:op(A + B) public static function addp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  46. @:op(A * B) public static function mulIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  47. @:op(A * B) public static function mulp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  48. @:op(A % B) public static function modIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  49. @:op(A % B) public static function modp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  50. @:op(A - B) public static function subIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  51. @:op(A - B) public static function subp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  52. @:op(A / B) public static function divIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  53. @:op(A / B) public static function divp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  54. @:op(A | B) public static function orIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  55. @:op(A | B) public static function orp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  56. @:op(A ^ B) public static function xorIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  57. @:op(A ^ B) public static function xorp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  58. @:op(A & B) public static function andIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  59. @:op(A & B) public static function andp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  60. @:op(A << B) public static function shlIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  61. @:op(A << B) public static function shlp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  62. @:op(A >> B) public static function shrIp<T>(lhs:Pointer<T>, rhs:Int):Pointer<T>;
  63. @:op(A >> B) public static function shrp<T>(lhs:Pointer<T>, rhs:Int64):Pointer<T>;
  64. @:op(A > B) public static function gtp<T>(lhs:Pointer<T>, rhs:Pointer<T>):Bool;
  65. @:op(A >= B) public static function gtep<T>(lhs:Pointer<T>, rhs:Pointer<T>):Bool;
  66. @:op(A < B) public static function ltp<T>(lhs:Pointer<T>, rhs:Pointer<T>):Bool;
  67. @:op(A <= B) public static function ltep<T>(lhs:Pointer<T>, rhs:Pointer<T>):Bool;
  68. @:op(~A) public static function bnegp<T>(t:Pointer<T>):Pointer<T>;
  69. @:op(A++) public static function prepp<T>(t:Pointer<T>):Pointer<T>;
  70. @:op(A--) public static function prenn<T>(t:Pointer<T>):Pointer<T>;
  71. @:op(++A) public static function postpp<T>(t:Pointer<T>):Pointer<T>;
  72. @:op(--A) public static function postnn<T>(t:Pointer<T>):Pointer<T>;
  73. /**
  74. Returns a `cs.PointerAccess` type, which in turn allows the underlying Pointer's
  75. fields to be accessed.
  76. **/
  77. // @:analyzer(no_simplification)
  78. public var acc(get, never):PointerAccess<T>;
  79. // @:analyzer(no_simplification)
  80. extern inline private function get_acc():PointerAccess<T>
  81. return (cast this : PointerAccess<T>);
  82. // backwards compatibility
  83. inline public function add(i:Int):Pointer<T> {
  84. return this + i;
  85. }
  86. @:arrayAccess public static function getIp<T>(p:Pointer<T>, at:Int):T;
  87. @:arrayAccess public static function setIp<T>(p:Pointer<T>, at:Int, val:T):T;
  88. @:arrayAccess public static function getp<T>(p:Pointer<T>, at:Int64):T;
  89. @:arrayAccess public static function setp<T>(p:Pointer<T>, at:Int64, val:T):T;
  90. }
  91. @:forward abstract PointerAccess<T>(T) {}
  92. #end