Native.hx 3.7 KB

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