Native.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 cpp;
  23. @:include("stdlib.h")
  24. extern class Native
  25. {
  26. @:native("malloc")
  27. public static function nativeMalloc(bytes:Int) : cpp.Star<cpp.Void>;
  28. @:native("calloc")
  29. public static function nativeCalloc(bytes:Int) : cpp.Star<cpp.Void>;
  30. @:native("realloc")
  31. public static function nativeRealloc(inPtr:cpp.Star<cpp.Void>,bytes:Int) : cpp.RawPointer<cpp.Void>;
  32. @:native("free")
  33. public static function nativeFree(ptr:cpp.Star<cpp.Void>) : Void;
  34. @:native("memcpy")
  35. public static function nativeMemcpy(dest:cpp.Star<cpp.Void>, src:cpp.Star<cpp.Void>, bytes:Int) : Void;
  36. @:native("hx::ClassSizeOf") @:templatedCall
  37. public static function sizeof<T>(t:T) : Int;
  38. #if !cppia
  39. @:native("hx::Dereference")
  40. public static function star<T>(ptr:cpp.Star<T>) : cpp.Reference<T>;
  41. @:generic
  42. public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
  43. {
  44. var ref: cpp.Reference<T> = star(ptr);
  45. ref = value;
  46. }
  47. @:generic
  48. public static inline function get<T>(ptr:cpp.Star<T>) : T
  49. {
  50. var ref: cpp.Reference<T> = star(ptr);
  51. return ref;
  52. }
  53. @:generic
  54. public static inline function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void
  55. nativeMemcpy(cast dest,cast src, bytes);
  56. @:generic
  57. public static inline function malloc<T>(bytes:Int) : cpp.Star<T>
  58. return cast nativeMalloc(bytes);
  59. @:generic
  60. public static inline function calloc<T>(bytes:Int) : cpp.Star<T>
  61. return cast nativeCalloc(bytes);
  62. @:generic
  63. public static inline function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T>
  64. return cast nativeRealloc(cast ioPtr, bytes);
  65. @:generic
  66. public static inline function free<T>(ptr:cpp.Star<T>) : Void
  67. {
  68. if (ptr!=null)
  69. nativeFree(cast ptr);
  70. }
  71. @:native("hx::StarOf")
  72. public static function addressOf<T>(inVariable:Reference<T>) : Star<T>;
  73. #else
  74. public static inline function addressOf<T>(inVariable:Reference<T>) : Star<T>
  75. {
  76. throw "Native.addressOf not available in cppia";
  77. }
  78. public static inline function star<T>(ptr:cpp.Star<T>) : cpp.Reference<T>
  79. {
  80. throw "Native.star not available in cppia";
  81. }
  82. public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
  83. {
  84. throw "Native.set not available in cppia";
  85. }
  86. public static inline function get<T>(ptr:cpp.Star<T>) : T
  87. {
  88. throw "Native.get not available in cppia";
  89. var d:Dynamic = null;
  90. return d;
  91. }
  92. public static function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void;
  93. public static function malloc<T>(bytes:Int) : cpp.Star<T>;
  94. public static function calloc<T>(bytes:Int) : cpp.Star<T>;
  95. public static function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T>;
  96. public static function free<T>(ptr:cpp.Star<T>) : Void;
  97. #end
  98. }