java_interface.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * This file is part of OpenModelica.
  3. *
  4. * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
  5. * c/o Linköpings universitet, Department of Computer and Information Science,
  6. * SE-58183 Linköping, Sweden.
  7. *
  8. * All rights reserved.
  9. *
  10. * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
  11. * GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
  12. * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
  13. * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
  14. * ACCORDING TO RECIPIENTS CHOICE.
  15. *
  16. * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
  17. * Public License (OSMC-PL) are obtained from OSMC, either from the above
  18. * address, from the URLs: http://www.openmodelica.org or
  19. * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
  20. * distribution. GNU version 3 is obtained from:
  21. * http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
  22. * http://www.opensource.org/licenses/BSD-3-Clause.
  23. *
  24. * This program is distributed WITHOUT ANY WARRANTY; without even the implied
  25. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
  26. * EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
  27. * CONDITIONS OF OSMC-PL.
  28. *
  29. */
  30. #ifndef JAVA_INTERFACE__H
  31. #define JAVA_INTERFACE__H
  32. #include "openmodelica.h"
  33. #include "meta/meta_modelica.h"
  34. #ifdef __cplusplus
  35. /*
  36. * JNI C++ and C interfaces are different, but we use the same code for
  37. * both C++ and C. So __cplusplus has to be undefined for jni.h.
  38. */
  39. #undef __cplusplus
  40. #include "jni.h"
  41. #define __cplusplus
  42. #else
  43. #include "jni.h"
  44. #endif
  45. #define EXIT_CODE_JAVA_ERROR 17
  46. JNIEnv* getJavaEnv();
  47. /* ModelicaArray<T> -> set firstDim and dims so the Java application can
  48. * create a ModelicaArray<Mode...> nested array using the unflatten method.
  49. * This is not done automatically as it takes a lot of extra time if the Java
  50. * application actually wants a flat array like the external C programs get.
  51. * These functions destroy the old object and replace the pointer with a fresh one */
  52. void MakeJavaMultiDimArray(JNIEnv* env, jobject jarr, int numDim, jint firstDim, ...);
  53. void FlattenJavaMultiDimArray(JNIEnv* env, jobject jarr);
  54. /* Functions to create a flat array to be accessed in Java */
  55. jobject NewJavaArray(JNIEnv* env);
  56. void JavaArrayAdd(JNIEnv* env, jobject arr, jobject o);
  57. jobject JavaArrayGet(JNIEnv* env, jobject arr, jint ix);
  58. /* T[n] -> ModelicaArray<T> */
  59. jobject NewFlatJavaIntegerArray(JNIEnv* env, modelica_integer* base, int num);
  60. jobject NewFlatJavaDoubleArray(JNIEnv* env, modelica_real* base, int num);
  61. jobject NewFlatJavaStringArray(JNIEnv* env, modelica_string* base, int num);
  62. jobject NewFlatJavaBooleanArray(JNIEnv* env, modelica_boolean* base, int num);
  63. /* ModelicaArray<T> -> T[n] */
  64. void GetFlatJavaIntegerArray(JNIEnv* env, jobject arr, modelica_integer* base, int num);
  65. void GetFlatJavaDoubleArray(JNIEnv* env, jobject arr, modelica_real* base, int num);
  66. void GetFlatJavaBooleanArray(JNIEnv* env, jobject arr, modelica_boolean* base, int num);
  67. void GetFlatJavaStringArray(JNIEnv* env, jobject arr, modelica_string* base, int num);
  68. /* Pass-Record-by-Map */
  69. jobject NewJavaRecord(JNIEnv* env, const char* recordName, int ctor_index /* -1 record, >= 0 uniontype */, jobject map);
  70. jobject NewJavaMap(JNIEnv* env);
  71. jobject NewJavaInteger(JNIEnv* env, jint value);
  72. jobject NewJavaDouble(JNIEnv* env, jdouble value);
  73. jobject NewJavaBoolean(JNIEnv* env, jboolean value);
  74. jobject NewJavaString(JNIEnv* env, const char* value);
  75. jobject NewJavaTuple(JNIEnv* env, jobject arr);
  76. jobject NewJavaOption(JNIEnv* env, jobject value);
  77. jobject mmc_to_jobject(JNIEnv* env, void* mmc);
  78. void* jobject_to_mmc(JNIEnv* env, jobject o);
  79. jint GetJavaInteger(JNIEnv* env, jobject o);
  80. jdouble GetJavaDouble(JNIEnv* env, jobject o);
  81. jboolean GetJavaBoolean(JNIEnv* env, jobject o);
  82. const char* GetJavaString(JNIEnv* env, jobject o);
  83. const char* copyJstring(JNIEnv* env, jobject o);
  84. const char* jobjectToString(JNIEnv* env, jobject o);
  85. void AddObjectToJavaMap(JNIEnv* env, jobject map, const char* key, jobject value);
  86. jobject GetObjectFromJavaMap(JNIEnv* env, jobject map, const char* key);
  87. const char* __CheckForJavaException(JNIEnv* env);
  88. /* We want to check if it's a simulation, but we can't check for __cplusplus
  89. * because matchcontinue requires try-catch constructs
  90. */
  91. /* #ifdef __cplusplus */ #if 0
  92. #define CHECK_FOR_JAVA_EXCEPTION(env) do { \
  93. const char* msg = __CheckForJavaException(env); \
  94. if(msg != NULL) { \
  95. modelTermination=1; \
  96. throw TerminateSimulationException(string(msg)); \
  97. } \
  98. } while(0)
  99. #else
  100. /* ModelicaUtilities.h is not available in OpenModelica?
  101. * Assertions also can't be used in OMC stand-alone functions; only simulations
  102. * We simply print the exception and abort()
  103. */
  104. #define CHECK_FOR_JAVA_EXCEPTION(env) do { \
  105. const char* msg = __CheckForJavaException(env); \
  106. if(msg != NULL) { \
  107. fprintf(stderr, "Error: External Java Exception Thrown but can't assert in C-mode\nLocation: %s (%s:%d)\nThe exception message was:\n%s\n", __FUNCTION__, __FILE__, __LINE__, msg); \
  108. EXIT(EXIT_CODE_JAVA_ERROR); \
  109. } \
  110. } while(0)
  111. #endif
  112. #endif