IexMacros.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IEXMACROS_H
  6. #define INCLUDED_IEXMACROS_H
  7. //--------------------------------------------------------------------
  8. //
  9. // Macros which make throwing exceptions more convenient
  10. //
  11. //--------------------------------------------------------------------
  12. #include <sstream>
  13. //----------------------------------------------------------------------------
  14. // A macro to throw exceptions whose text is assembled using stringstreams.
  15. //
  16. // Example:
  17. //
  18. // THROW (InputExc, "Syntax error in line " << line ", " << file << ".");
  19. //
  20. //----------------------------------------------------------------------------
  21. #include "IexExport.h"
  22. #include "IexForward.h"
  23. IEX_EXPORT void iex_debugTrap();
  24. #define THROW(type, text) \
  25. do \
  26. { \
  27. iex_debugTrap(); \
  28. std::stringstream _iex_throw_s; \
  29. _iex_throw_s << text; \
  30. throw type (_iex_throw_s); \
  31. } \
  32. while (0)
  33. //----------------------------------------------------------------------------
  34. // Macros to add to or to replace the text of an exception.
  35. // The new text is assembled using stringstreams.
  36. //
  37. // Examples:
  38. //
  39. // Append to end of an exception's text:
  40. //
  41. // catch (BaseExc &e)
  42. // {
  43. // APPEND_EXC (e, " Directory " << name << " does not exist.");
  44. // throw;
  45. // }
  46. //
  47. // Replace an exception's text:
  48. //
  49. // catch (BaseExc &e)
  50. // {
  51. // REPLACE_EXC (e, "Directory " << name << " does not exist. " << e);
  52. // throw;
  53. // }
  54. //----------------------------------------------------------------------------
  55. #define APPEND_EXC(exc, text) \
  56. do \
  57. { \
  58. std::stringstream _iex_append_s; \
  59. _iex_append_s << text; \
  60. exc.append (_iex_append_s); \
  61. } \
  62. while (0)
  63. #define REPLACE_EXC(exc, text) \
  64. do \
  65. { \
  66. std::stringstream _iex_replace_s; \
  67. _iex_replace_s << text; \
  68. exc.assign (_iex_replace_s); \
  69. } \
  70. while (0)
  71. //-------------------------------------------------------------
  72. // A macro to throw ErrnoExc exceptions whose text is assembled
  73. // using stringstreams:
  74. //
  75. // Example:
  76. //
  77. // THROW_ERRNO ("Cannot open file " << name << " (%T).");
  78. //
  79. //-------------------------------------------------------------
  80. #define THROW_ERRNO(text) \
  81. do \
  82. { \
  83. std::stringstream _iex_throw_errno_s; \
  84. _iex_throw_errno_s << text; \
  85. ::IEX_NAMESPACE::throwErrnoExc (_iex_throw_errno_s.str()); \
  86. } \
  87. while (0)
  88. //-------------------------------------------------------------
  89. // A macro to throw exceptions if an assertion is false.
  90. //
  91. // Example:
  92. //
  93. // ASSERT (ptr != 0, NullExc, "Null pointer" );
  94. //
  95. //-------------------------------------------------------------
  96. #define ASSERT(assertion, type, text) \
  97. do \
  98. { \
  99. if( bool(assertion) == false ) \
  100. { \
  101. THROW( type, text ); \
  102. } \
  103. } \
  104. while (0)
  105. //-------------------------------------------------------------
  106. // A macro to throw an IEX_NAMESPACE::LogicExc if an assertion is false,
  107. // with the text composed from the source code file, line number,
  108. // and assertion argument text.
  109. //
  110. // Example:
  111. //
  112. // LOGIC_ASSERT (i < n);
  113. //
  114. //-------------------------------------------------------------
  115. #define LOGIC_ASSERT(assertion) \
  116. ASSERT(assertion, \
  117. IEX_NAMESPACE::LogicExc, \
  118. __FILE__ << "(" << __LINE__ << "): logical assertion failed: " << #assertion )
  119. #endif