ModelicaUtilities.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ModelicaUtilities.h - External utility functions header
  2. Copyright (C) 2010-2016, Modelica Association and DLR
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /* Utility functions which can be called by external Modelica functions.
  23. These functions are defined in section 12.8.6 of the
  24. Modelica Specification 3.0 and section 12.9.6 of the
  25. Modelica Specification 3.1 and 3.2.
  26. A generic C-implementation of these functions cannot be given,
  27. because it is tool dependent how strings are output in a
  28. window of the respective simulation tool. Therefore, only
  29. this header file is shipped with the Modelica Standard Library.
  30. */
  31. #ifndef MODELICA_UTILITIES_H
  32. #define MODELICA_UTILITIES_H
  33. #include <stddef.h>
  34. #include <stdarg.h>
  35. #if defined(__cplusplus)
  36. extern "C" {
  37. #endif
  38. /*
  39. Some of the functions never return to the caller. In order to compile
  40. external Modelica C-code in most compilers, noreturn attributes need to
  41. be present to avoid warnings or errors.
  42. The following macros handle noreturn attributes according to the latest
  43. C11/C++11 standard with fallback to GNU or MSVC extensions if using an
  44. older compiler.
  45. */
  46. #if __STDC_VERSION__ >= 201112L
  47. #define MODELICA_NORETURN _Noreturn
  48. #define MODELICA_NORETURNATTR
  49. #elif __cplusplus >= 201103L
  50. #define MODELICA_NORETURN [[noreturn]]
  51. #define MODELICA_NORETURNATTR
  52. #elif defined(__GNUC__)
  53. #define MODELICA_NORETURN
  54. #define MODELICA_NORETURNATTR __attribute__((noreturn))
  55. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  56. #define MODELICA_NORETURN __declspec(noreturn)
  57. #define MODELICA_NORETURNATTR
  58. #else
  59. #define MODELICA_NORETURN
  60. #define MODELICA_NORETURNATTR
  61. #endif
  62. DYMOLA_STATIC void ModelicaMessage(const char *string);
  63. /*
  64. Output the message string (no format control).
  65. */
  66. DYMOLA_STATIC void ModelicaFormatMessage(const char *string, ...);
  67. /*
  68. Output the message under the same format control as the C-function printf.
  69. */
  70. DYMOLA_STATIC void ModelicaVFormatMessage(const char *string, va_list args);
  71. /*
  72. Output the message under the same format control as the C-function vprintf.
  73. */
  74. DYMOLA_STATIC void ModelicaError(const char *string);
  75. /*
  76. Output the error message string (no format control). This function
  77. never returns to the calling function, but handles the error
  78. similarly to an assert in the Modelica code.
  79. */
  80. DYMOLA_STATIC void ModelicaFormatError(const char *string, ...);
  81. /*
  82. Output the error message under the same format control as the C-function
  83. printf. This function never returns to the calling function,
  84. but handles the error similarly to an assert in the Modelica code.
  85. */
  86. DYMOLA_STATIC void ModelicaVFormatError(const char *string, va_list args);
  87. /*
  88. Output the error message under the same format control as the C-function
  89. vprintf. This function never returns to the calling function,
  90. but handles the error similarly to an assert in the Modelica code.
  91. */
  92. DYMOLA_STATIC char* ModelicaAllocateString(size_t len);
  93. /*
  94. Allocate memory for a Modelica string which is used as return
  95. argument of an external Modelica function. Note, that the storage
  96. for string arrays (= pointer to string array) is still provided by the
  97. calling program, as for any other array. If an error occurs, this
  98. function does not return, but calls "ModelicaError".
  99. */
  100. DYMOLA_STATIC char* ModelicaAllocateStringWithErrorReturn(size_t len);
  101. /*
  102. Same as ModelicaAllocateString, except that in case of error, the
  103. function returns 0. This allows the external function to close files
  104. and free other open resources in case of error. After cleaning up
  105. resources use ModelicaError or ModelicaFormatError to signal
  106. the error.
  107. */
  108. #if defined(__cplusplus)
  109. }
  110. #endif
  111. #endif