cpp_throw.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_WAVE_CPP_THROW_HPP_INCLUDED)
  9. #define BOOST_WAVE_CPP_THROW_HPP_INCLUDED
  10. #include <string>
  11. #include <boost/throw_exception.hpp>
  12. #ifdef BOOST_NO_STRINGSTREAM
  13. #include <strstream>
  14. #else
  15. #include <sstream>
  16. #endif
  17. namespace boost { namespace wave { namespace util
  18. {
  19. #ifdef BOOST_NO_STRINGSTREAM
  20. template <typename Exception, typename S1, typename Pos>
  21. void throw_(typename Exception::error_code code, S1 msg, Pos const& pos)
  22. {
  23. std::strstream stream;
  24. stream << Exception::severity_text(code) << ": "
  25. << Exception::error_text(code);
  26. if (msg[0] != 0)
  27. stream << ": " << msg;
  28. stream << std::ends;
  29. std::string throwmsg = stream.str(); stream.freeze(false);
  30. boost::throw_exception(Exception(throwmsg.c_str(), code,
  31. pos.get_line(), pos.get_column(), pos.get_file().c_str()));
  32. }
  33. template <typename Exception, typename Context, typename S1, typename Pos>
  34. void throw_(Context& ctx, typename Exception::error_code code,
  35. S1 msg, Pos const& pos)
  36. {
  37. std::strstream stream;
  38. stream << Exception::severity_text(code) << ": "
  39. << Exception::error_text(code);
  40. if (msg[0] != 0)
  41. stream << ": " << msg;
  42. stream << std::ends;
  43. std::string throwmsg = stream.str(); stream.freeze(false);
  44. ctx.get_hooks().throw_exception(ctx.derived(),
  45. Exception(throwmsg.c_str(), code, pos.get_line(), pos.get_column(),
  46. pos.get_file().c_str()));
  47. }
  48. template <typename Exception, typename S1, typename Pos, typename S2>
  49. void throw_(typename Exception::error_code code, S1 msg, Pos const& pos,
  50. S2 name)
  51. {
  52. std::strstream stream;
  53. stream << Exception::severity_text(code) << ": "
  54. << Exception::error_text(code);
  55. if (msg[0] != 0)
  56. stream << ": " << msg;
  57. stream << std::ends;
  58. std::string throwmsg = stream.str(); stream.freeze(false);
  59. boost::throw_exception(Exception(throwmsg.c_str(), code,
  60. pos.get_line(), pos.get_column(), pos.get_file().c_str(), name));
  61. }
  62. template <typename Exception, typename Context, typename S1, typename Pos,
  63. typename S2>
  64. void throw_(Context& ctx, typename Exception::error_code code,
  65. S1 msg, Pos const& pos, S2 name)
  66. {
  67. std::strstream stream;
  68. stream << Exception::severity_text(code) << ": "
  69. << Exception::error_text(code);
  70. if (msg[0] != 0)
  71. stream << ": " << msg;
  72. stream << std::ends;
  73. std::string throwmsg = stream.str(); stream.freeze(false);
  74. ctx.get_hooks().throw_exception(ctx.derived(),
  75. Exception(throwmsg.c_str(), code, pos.get_line(), pos.get_column(),
  76. pos.get_file().c_str(), name));
  77. }
  78. #else
  79. template <typename Exception, typename S1, typename Pos>
  80. void throw_(typename Exception::error_code code, S1 msg, Pos const& pos)
  81. {
  82. std::stringstream stream;
  83. stream << Exception::severity_text(code) << ": "
  84. << Exception::error_text(code);
  85. if (msg[0] != 0)
  86. stream << ": " << msg;
  87. stream << std::ends;
  88. std::string throwmsg = stream.str();
  89. boost::throw_exception(Exception(throwmsg.c_str(), code,
  90. pos.get_line(), pos.get_column(), pos.get_file().c_str()));
  91. }
  92. template <typename Exception, typename Context, typename S1, typename Pos>
  93. void throw_(Context& ctx, typename Exception::error_code code,
  94. S1 msg, Pos const& pos)
  95. {
  96. std::stringstream stream;
  97. stream << Exception::severity_text(code) << ": "
  98. << Exception::error_text(code);
  99. if (msg[0] != 0)
  100. stream << ": " << msg;
  101. stream << std::ends;
  102. std::string throwmsg = stream.str();
  103. ctx.get_hooks().throw_exception(ctx.derived(),
  104. Exception(throwmsg.c_str(), code, pos.get_line(), pos.get_column(),
  105. pos.get_file().c_str()));
  106. }
  107. template <typename Exception, typename S1, typename Pos, typename S2>
  108. void throw_(typename Exception::error_code code, S1 msg, Pos const& pos,
  109. S2 name)
  110. {
  111. std::stringstream stream;
  112. stream << Exception::severity_text(code) << ": "
  113. << Exception::error_text(code);
  114. if (msg[0] != 0)
  115. stream << ": " << msg;
  116. stream << std::ends;
  117. std::string throwmsg = stream.str();
  118. boost::throw_exception(Exception(throwmsg.c_str(), code,
  119. pos.get_line(), pos.get_column(), pos.get_file().c_str(), name));
  120. }
  121. template <typename Exception, typename Context, typename S1, typename Pos1,
  122. typename S2>
  123. void throw_(Context& ctx, typename Exception::error_code code,
  124. S1 msg, Pos1 const& pos, S2 name)
  125. {
  126. std::stringstream stream;
  127. stream << Exception::severity_text(code) << ": "
  128. << Exception::error_text(code);
  129. if (msg[0] != 0)
  130. stream << ": " << msg;
  131. stream << std::ends;
  132. std::string throwmsg = stream.str();
  133. ctx.get_hooks().throw_exception(ctx.derived(),
  134. Exception(throwmsg.c_str(), code, pos.get_line(), pos.get_column(),
  135. pos.get_file().c_str(), name));
  136. }
  137. #endif
  138. }}}
  139. ///////////////////////////////////////////////////////////////////////////////
  140. // helper macro for throwing exceptions
  141. #if !defined(BOOST_WAVE_THROW)
  142. #define BOOST_WAVE_THROW(cls, code, msg, act_pos) \
  143. boost::wave::util::throw_<cls>(cls::code, msg, act_pos) \
  144. /**/
  145. #define BOOST_WAVE_THROW_CTX(ctx, cls, code, msg, act_pos) \
  146. boost::wave::util::throw_<cls>(ctx, cls::code, msg, act_pos) \
  147. /**/
  148. #endif // BOOST_WAVE_THROW
  149. ///////////////////////////////////////////////////////////////////////////////
  150. // helper macro for throwing exceptions with additional parameter
  151. #if !defined(BOOST_WAVE_THROW_NAME_CTX)
  152. #define BOOST_WAVE_THROW_NAME_CTX(ctx, cls, code, msg, act_pos, name) \
  153. boost::wave::util::throw_<cls>(cls::code, msg, act_pos, name) \
  154. /**/
  155. #endif // BOOST_WAVE_THROW_NAME_CTX
  156. ///////////////////////////////////////////////////////////////////////////////
  157. // helper macro for throwing exceptions with additional parameter
  158. #if !defined(BOOST_WAVE_THROW_VAR_CTX)
  159. #define BOOST_WAVE_THROW_VAR_CTX(ctx, cls, code, msg, act_pos) \
  160. boost::wave::util::throw_<cls>(ctx, code, msg, act_pos) \
  161. /**/
  162. #endif // BOOST_WAVE_THROW_VAR_CTX
  163. #endif // !defined(BOOST_WAVE_CPP_THROW_HPP_INCLUDED)