Lib.hx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 cpp;
  23. class Lib {
  24. /**
  25. Load and return a Cpp primitive from a DLL library.
  26. **/
  27. public static function load( lib : String, prim : String, nargs : Int ) : Dynamic {
  28. #if iphone
  29. return loadLazy(lib,prim,nargs);
  30. #else
  31. return untyped __global__.__loadprim(lib,prim,nargs);
  32. #end
  33. }
  34. /**
  35. Load and return a Cpp primitive from a DLL library.
  36. **/
  37. @:extern public static inline function getProcAddress( lib : String, prim : String ) : Dynamic {
  38. return untyped __global__.__hxcpp_cast_get_proc_address(lib,prim);
  39. }
  40. /**
  41. Tries to load, and always returns a valid function, but the function may throw
  42. if called.
  43. **/
  44. public static function loadLazy(lib,prim,nargs) : Dynamic {
  45. try {
  46. return untyped __global__.__loadprim(lib,prim,nargs);
  47. } catch( e : Dynamic ) {
  48. switch(nargs) {
  49. case 0 : return function() { throw e; };
  50. case 2 : return function(_1,_2) { throw e; };
  51. case 3 : return function(_1,_2,_3) { throw e; };
  52. case 4 : return function(_1,_2,_3,_4) { throw e; };
  53. case 5 : return function(_1,_2,_3,_4,_5) { throw e; };
  54. default : return function(_1) { throw e; };
  55. }
  56. }
  57. return null;
  58. }
  59. public static function rethrow(inExp:Dynamic) { throw inExp; }
  60. public static function stringReference(inExp:Dynamic) { throw inExp; }
  61. /**
  62. Print the specified value on the default output.
  63. **/
  64. public static function print( v : Dynamic ) : Void {
  65. untyped __global__.__hxcpp_print(v);
  66. }
  67. /**
  68. This function is used to make porting from neko to cpp easy.
  69. It does not need to do anything because the c-code can work with any Dynamic
  70. **/
  71. public static function haxeToNeko( v : Dynamic ) : Dynamic {
  72. return v;
  73. }
  74. /**
  75. This function is used to make porting from neko to cpp easy.
  76. It does not need to do anything because the c-code can work with any Dynamic
  77. **/
  78. public static function nekoToHaxe( v : Dynamic ) : Dynamic {
  79. return v;
  80. }
  81. /**
  82. Print the specified value on the default output followed by a newline character.
  83. **/
  84. public static function println( v : Dynamic ) : Void {
  85. untyped __global__.__hxcpp_println(v);
  86. }
  87. }