Native.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C)2005-2018 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> return null;
  28. @:native("calloc")
  29. public static function nativeCalloc(bytes:Int) : cpp.Star<cpp.Void> return null;
  30. @:native("realloc")
  31. public static function nativeRealloc(inPtr:cpp.Star<cpp.Void>,bytes:Int) : cpp.RawPointer<cpp.Void> return null;
  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. @:generic
  40. public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
  41. {
  42. var ref: cpp.Reference<T> = ptr;
  43. ref = value;
  44. }
  45. @:generic
  46. public static inline function get<T>(ptr:cpp.Star<T>) : T
  47. {
  48. var ref: cpp.Reference<T> = ptr;
  49. return ref;
  50. }
  51. @:generic
  52. public static inline function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void
  53. nativeMemcpy(cast dest,cast src, bytes);
  54. @:generic
  55. public static inline function malloc<T>(bytes:Int) : cpp.Star<T>
  56. return cast nativeMalloc(bytes);
  57. @:generic
  58. public static inline function calloc<T>(bytes:Int) : cpp.Star<T>
  59. return cast nativeCalloc(bytes);
  60. @:generic
  61. public static inline function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T>
  62. return cast nativeRealloc(cast ioPtr, bytes);
  63. @:generic
  64. public static inline function free<T>(ptr:cpp.Star<T>) : Void
  65. {
  66. if (ptr!=null)
  67. nativeFree(cast ptr);
  68. }
  69. #else
  70. public static inline function set<T>(ptr:cpp.Star<T>,value:T) : Void
  71. {
  72. throw "Native.set not available in cppia";
  73. }
  74. public static inline function get<T>(ptr:cpp.Star<T>) : T
  75. {
  76. throw "Native.get not available in cppia";
  77. var d:Dynamic = null;
  78. return d;
  79. }
  80. public static function memcpy<DEST,SRC>(dest:cpp.Star<DEST>, src:cpp.Star<SRC>, bytes:Int) : Void { }
  81. public static function malloc<T>(bytes:Int) : cpp.Star<T> return null;
  82. public static function calloc<T>(bytes:Int) : cpp.Star<T> return null;
  83. public static function realloc<T>(ioPtr:cpp.Star<T>, bytes:Int) : cpp.Star<T> return null;
  84. public static function free<T>(ptr:cpp.Star<T>) : Void { }
  85. #end
  86. }