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