IexMacros.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2018, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IEXMACROS_H
  35. #define INCLUDED_IEXMACROS_H
  36. //--------------------------------------------------------------------
  37. //
  38. // Macros which make throwing exceptions more convenient
  39. //
  40. //--------------------------------------------------------------------
  41. #include <sstream>
  42. //----------------------------------------------------------------------------
  43. // A macro to throw exceptions whose text is assembled using stringstreams.
  44. //
  45. // Example:
  46. //
  47. // THROW (InputExc, "Syntax error in line " << line ", " << file << ".");
  48. //
  49. //----------------------------------------------------------------------------
  50. #include "IexExport.h"
  51. #include "IexForward.h"
  52. IEX_EXPORT void iex_debugTrap();
  53. #define THROW(type, text) \
  54. do \
  55. { \
  56. iex_debugTrap(); \
  57. std::stringstream _iex_throw_s; \
  58. _iex_throw_s << text; \
  59. throw type (_iex_throw_s); \
  60. } \
  61. while (0)
  62. //----------------------------------------------------------------------------
  63. // Macros to add to or to replace the text of an exception.
  64. // The new text is assembled using stringstreams.
  65. //
  66. // Examples:
  67. //
  68. // Append to end of an exception's text:
  69. //
  70. // catch (BaseExc &e)
  71. // {
  72. // APPEND_EXC (e, " Directory " << name << " does not exist.");
  73. // throw;
  74. // }
  75. //
  76. // Replace an exception's text:
  77. //
  78. // catch (BaseExc &e)
  79. // {
  80. // REPLACE_EXC (e, "Directory " << name << " does not exist. " << e);
  81. // throw;
  82. // }
  83. //----------------------------------------------------------------------------
  84. #define APPEND_EXC(exc, text) \
  85. do \
  86. { \
  87. std::stringstream _iex_append_s; \
  88. _iex_append_s << text; \
  89. exc.append (_iex_append_s); \
  90. } \
  91. while (0)
  92. #define REPLACE_EXC(exc, text) \
  93. do \
  94. { \
  95. std::stringstream _iex_replace_s; \
  96. _iex_replace_s << text; \
  97. exc.assign (_iex_replace_s); \
  98. } \
  99. while (0)
  100. //-------------------------------------------------------------
  101. // A macro to throw ErrnoExc exceptions whose text is assembled
  102. // using stringstreams:
  103. //
  104. // Example:
  105. //
  106. // THROW_ERRNO ("Cannot open file " << name << " (%T).");
  107. //
  108. //-------------------------------------------------------------
  109. #define THROW_ERRNO(text) \
  110. do \
  111. { \
  112. std::stringstream _iex_throw_errno_s; \
  113. _iex_throw_errno_s << text; \
  114. ::IEX_NAMESPACE::throwErrnoExc (_iex_throw_errno_s.str()); \
  115. } \
  116. while (0)
  117. //-------------------------------------------------------------
  118. // A macro to throw exceptions if an assertion is false.
  119. //
  120. // Example:
  121. //
  122. // ASSERT (ptr != 0, NullExc, "Null pointer" );
  123. //
  124. //-------------------------------------------------------------
  125. #define ASSERT(assertion, type, text) \
  126. do \
  127. { \
  128. if( bool(assertion) == false ) \
  129. { \
  130. THROW( type, text ); \
  131. } \
  132. } \
  133. while (0)
  134. //-------------------------------------------------------------
  135. // A macro to throw an IEX_NAMESPACE::LogicExc if an assertion is false,
  136. // with the text composed from the source code file, line number,
  137. // and assertion argument text.
  138. //
  139. // Example:
  140. //
  141. // LOGIC_ASSERT (i < n);
  142. //
  143. //-------------------------------------------------------------
  144. #define LOGIC_ASSERT(assertion) \
  145. ASSERT(assertion, \
  146. IEX_NAMESPACE::LogicExc, \
  147. __FILE__ << "(" << __LINE__ << "): logical assertion failed: " << #assertion )
  148. #endif